Tuesday, June 10, 2014

Grails: Listing all fields/variables in a Domain/Model

In this example, I assume your domain class name is 'Research' and the full package name is 'com.package.name.Research'

Way 1:

 Research.declaredFields.each{  
      if(!it.synthetic)  
           println it.name  
 }  

Way 2:

 def names = grailsApplication.getDomainClass('com.package.name.Research').persistentProperties.collect {   
           println it.name   
      }  
   
Way 3:

 def Research domainClass = grailsApplication.getGrailsDomainClass('Research')  
 def persistentProperties = domainClass.getPersistentProperties()  
 persisentProperties.each { property ->  
      println property  
 }  
   

Thursday, June 5, 2014

Run Windows Command prompt (CMD) in Full screen mode

I got this values for running Windows CMD in full screen mode. Width=237, Height=82

Open CMD, Right click on the title bar and select Properties and set this values.

For more info, read this.

Android Tips: How to (always) show Overflow in Action bar

Here is how!

try {
        ViewConfiguration config = ViewConfiguration.get(this);
        Field menuKeyField =                        ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
        if(menuKeyField != null) {
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

Tuesday, June 3, 2014

Parsing and Formatting a Date in Groovy/Grails

If you have a string variable containing a date value such as 2014-06-03T11:03:44.779+03, then:

First -  Convert it to Date using "parse" method of SimpleDateFormat
Seond - Format the Date using the format you want such as "yyyy-MM-dd" or "yyyy/MM/dd" or any other.

Code

import java.text.SimpleDateFormat
import java.text.DateFormat

String mydate = "2014-06-03T11:03:44.779+03"

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd")

Date parsedDate = formatter.parse(mydate)

Date formattedDate = formatter.format(parsedDate)

/* In one line
    Date parsedAndFormattedDate  =  formatter.format(formatter.parse(mydate))
*/

Wednesday, April 23, 2014

Android: How to call another activity when a button is pressed and close the first activity

Here is a complete class that shows how another activity is called when a button in the first activity is pressed.
public class FirstActivity extends Activity implements View.OnClickListener {

    Button goToSecondActivityButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.first_activity);

        ((TextView) findViewById(R.id.textRecommendationMessage)).setText("This is the first activity");

        goToSecondActivityButton= (Button) findViewById(R.id.button_go_to_second_activity);
        goToSecondActivityButton.setOnClickListener(this);
    }

     @Override
     public void onClick(View view) {

         goToSecondActivity();
    }

    private void goToSecondActivity() {

        Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
        startActivity(intent);
        finish();
    }
}

Thursday, April 10, 2014

How to Reset the Root Password in MySql Database

Run the following commands separately to change/reset your 'root' user password in MySql database.

Some Info:

There is a database called 'mysql' in MySql which is the default database. It is where Users such as 'root' are stored.
In this script, it is assumed that your new password is 'MyNewPassword'. Hence, replace that with what ever you want your password to be.

UPDATE mysql.user 
SET Password=PASSWORD('MyNewPassword') 
WHERE User='root';

FLUSH PRIVILEGES;

More info can be found here.

Thursday, April 3, 2014

Android Emulator Shortcuts

These are the list of Android Emulator shortcuts for Windows.

Ctrl+F11 Switch layout orientation portrait/landscape backwards
Ctrl+F12 Switch layout orientation portrait/landscape forwards
1. Main Device Keys
Home Home Button
F2 Left Softkey / Menu / Settings button (or Page up)
Shift+f2 Right Softkey / Star button (or Page down)
Esc Back Button
F3 Call/ dial Button
F4 Hang up / end call button
F5 Search Button
2. Other Device Keys
Ctrl+F5 Volume up (or + on numeric keyboard with Num Lock off) Ctrl+F6 Volume down (or + on numeric keyboard with Num Lock off) F7 Power Button Ctrl+F3 Camera Button
Ctrl+F11 Switch layout orientation portrait/landscape backwards
Ctrl+F12 Switch layout orientation portrait/landscape forwards
F8 Toggle cell network
F9 Toggle code profiling
Alt+Enter Toggle fullscreen mode