Screenshots :
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.chantisandroid.exampleapp.MainActivity">
</RelativeLayout>
Now create menu folder under res
under menu folder create one xml file
example_menu.xml :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/item"
android:title="Android" />
<item
android:id="@+id/item2"
android:title="Java" />
<item
android:id="@+id/item3"
android:title="Oracle" />
<item
android:id="@+id/item4"
android:title="Php" />
<item
android:id="@+id/item5"
android:title="Ios" />
</menu>
MainActivity.java :
package com.chantisandroid.exampleapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.example_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item:
Toast.makeText(getApplicationContext(), "Android selected",
Toast.LENGTH_LONG).show();
break;
case R.id.item2:
Toast.makeText(getApplicationContext(), "Java selected",
Toast.LENGTH_LONG).show();
break;
case R.id.item3:
Toast.makeText(getApplicationContext(), "Oracle selected",
Toast.LENGTH_LONG).show();
break;
case R.id.item4:
Toast.makeText(getApplicationContext(), "Php selected",
Toast.LENGTH_LONG).show();
break;
case R.id.item5:
Toast.makeText(getApplicationContext(), "Ios selected",
Toast.LENGTH_LONG).show();
break;
}
return super.onOptionsItemSelected(item);
}
}
No comments:
Post a Comment