We are going to create option menu when user touches option on device one menu will displayed like, Help and about Us.
DeviceOptionsMenuActivity.java (Main activity)
package pkg.DeviceOptionsMenu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
public class DeviceOptionsMenuActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_options_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about:
startActivity(new Intent(this, About.class));
return true;
case R.id.help:
startActivity(new Intent(this, Help.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
public class DeviceOptionsMenuActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_options_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about:
startActivity(new Intent(this, About.class));
return true;
case R.id.help:
startActivity(new Intent(this, Help.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
AboutUs.java
package pkg.DeviceOptionsMenu;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class About extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.about);
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class About extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.about);
}
}
Help.java
package pkg.DeviceOptionsMenu;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class Help extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.help);
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class Help extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.help);
}
}
Dont change main.xml keep it as it is..we dont need that one for our example.
Now Create about.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="280dp"
android:layout_weight="0.19" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="139dp"
android:textSize="9pt"
android:text="This Application is sponsored by"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#717C40" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:textSize="8pt"
android:text="Ronak Pandya"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#717C40" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:textSize="8pt"
android:text="You are free to use it."
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#717C40" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_centerVertical="true"
android:textSize="8pt"
android:text="Please contact us at,"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#772020" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/textView4"
android:textSize="8pt"
android:text="support@support.com"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#0F53FF" />
</RelativeLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="280dp"
android:layout_weight="0.19" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="139dp"
android:textSize="9pt"
android:text="This Application is sponsored by"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#717C40" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:textSize="8pt"
android:text="Ronak Pandya"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#717C40" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:textSize="8pt"
android:text="You are free to use it."
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#717C40" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_centerVertical="true"
android:textSize="8pt"
android:text="Please contact us at,"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#772020" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/textView4"
android:textSize="8pt"
android:text="support@support.com"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#0F53FF" />
</RelativeLayout>
</LinearLayout>
help.xml (Write whatever content you want to write....)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
Now Create a dir called menu--> in that create xml file called my_options_menu.xml in
i.e res/menu/my_options_menu.xml
my_options_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/about"
android:title="About" />
<item android:id="@+id/help"
android:title="Help" />
</menu>
<item android:id="@+id/about"
android:title="About" />
<item android:id="@+id/help"
android:title="Help" />
</menu>
Finally Add Both activities Help and about.java to AndroidMenifiest.xml file
Run it...
No comments:
Post a Comment