Start New Project as named MenuDemo as usual then click next,next and select blank Activity and click finish:
MainActivity.java
MainActivity.java
package com.broadway.menudemo; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; 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) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement
//resource file menu_main.xml ma title k cha tei click garda toast dekhaune kam garcha if (id == R.id.action_settings) { Toast.makeText(getApplicationContext(),"Settings",Toast.LENGTH_LONG).show(); return true; } if (id == R.id.action_toast) { Toast.makeText(getApplicationContext(),"toast",Toast.LENGTH_LONG).show(); return true; } return super.onOptionsItemSelected(item); } }
activity_main.xml
<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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
Add Resources file to Android->app->res->menu
Resources file named menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> <item android:id="@+id/action_settings" android:title="@string/action_settings" android:orderInCategory="100" app:showAsAction="always" android:icon="@mipmap/ic_launcher"/> <-- android:icon give the location of icon--> <item android:id="@+id/action_toast" android:title="Toast" android:orderInCategory="99" app:showAsAction="never" /><-- orderIncategory gives the order of menus--> </menu>
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.