Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Tuesday, 17 May 2016

Optionsmenu sample

Hello friends in this blog you learn about OptionsMenu.

actvity_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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.samset.optionsmenu.MainActivity">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="16dp"
        android:autoLink="web"
        android:gravity="center"
        android:text="samsetdev.blogspot.com"
        android:textSize="20sp" />
</RelativeLayout>

MainActivity.java


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.menu_, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.option_menu_home:
                // TODO Something when menu item selected
               
return true;

            case R.id.option_menu_add:
                // TODO Something when menu item selected
               
return true;

            case R.id.option_menu_delete:
                // TODO Something when menu item selected
               
return true;
            case R.id.option_menu_update:
                // TODO Something when menu item selected
               
return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }
}



menu.xml

if you use android studio and api level 23 then you create directory by name menu and now create a resource file a name menu_.xml


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <item
            android:id="@+id/option_menu_home"
            android:orderInCategory="1"
            android:title="Home"
            app:showAsAction="never" />

        <item
            android:id="@+id/option_menu_add"
            android:orderInCategory="2"
            android:title="Add"
            app:showAsAction="never" />

        <item
            android:id="@+id/option_menu_delete"
            android:orderInCategory="3"
            android:title="Delete"
            app:showAsAction="never" />

    <item
        android:id="@+id/option_menu_update"
        android:orderInCategory="4"
        android:title="Update"
        app:showAsAction="never" />

</menu>

color.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="background">#990000</color>
    <color name="text">#fff</color>
</resources>


style.xml


<resources>

    <!-- Base application theme. -->
   
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
       
<item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <!-- To change the popup menu and app text color  -->
       
<item name="android:textColor">@color/colorPrimary</item>

        <!-- To change the background of options menu-->
       
<item name="android:itemBackground">@color/background</item>

    </style>

</resources>


Thank you

FullSource code..OptionsMenu-master

Live Sample








No comments:

Post a Comment