Dec 7, 2010

Create OptionsMenu using menu.add()


package com.OptionsMenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class OptionsMenu extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, 0, 0, "Option 0");
menu.add(0, 1, 1, "Option 1");
menu.add(0, 2, 2, "Option 2");
menu.add(0, 3, 3, "Option 3");
menu.add(0, 4, 4, "Option 4");
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case (0):
break;
case (1):
break;
case (2):
break;
case (3):
break;
case (4):
break;
}
return true;
}
}

Dec 5, 2010

Create OptionsMenu in /res/menu/menu.xml

Create OptionsMenu in /res/menu/menu.xml

/workspace/OptionsMenu/res/menu/menu.xml
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/opt1"
android:title="Option 1" />
<item android:id="@+id/opt2"
android:title="Option 2" />
<item android:id="@+id/opt3"
android:title="Option 3" />
<item android:id="@+id/opt4"
android:title="Option 4" />
<item android:id="@+id/opt5"
android:title="Option 5" />
</menu>


/workspace/OptionsMenu/src/com/OptionsMenu/OptionsMenu.java
package com.OptionsMenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class OptionsMenu extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case (R.id.opt1):
break;
case (R.id.opt2):
break;
case (R.id.opt3):
break;
case (R.id.opt4):
break;
case (R.id.opt5):
break;
}
return true;
}
}


Related Post:
- Overwrite MENU key to create custom menu

Dec 2, 2010

AlertDialog.Builder - a simple way to create AlertDialog


eg.

package com.android.coding.HelloWorld;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class HelloWorld extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new AlertDialog.Builder(HelloWorld.this)
.setTitle("AlertDialog").setMessage("Hello!")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}});
}
}

Dec 1, 2010

Make Android App in Landscape/Portrait

To force Android App working in Landscape/Portrait, you can modify the AndroidManifest.xml file, to add "android:screenOrientation" in <activity>.

It can be "unspecified", "user", "behind", "landscape", "portrait", "sensor" or "nosensor".

eg.

android:label="@string/app_name"
android:screenOrientation="landscape"
>










Make Android App in Landscape

Change backbround color of activity



Changing of backbround color of activity can be achieved by assigning color value to "android:background" of the layout.

eg.

android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#e0e0ff"
>





Change backbround color of activity

android:background may be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".



Infolinks In Text Ads