Showing posts with label OptionsMenu. Show all posts
Showing posts with label OptionsMenu. Show all posts

Sunday, 23 October 2016

OptionsMenu

The Options menu (or) choices menu is the essential gathering of menu things for a movement. It's the place you ought to place activities that globally affect the application, for example, "Look", "Form email," and "Settings."

    Example :-
                   @Override
                       public boolean onCreateOptionsMenu(Menu menu) {
                       MenuInflater inflater=getMenuInflater();
                       inflater.inflate(R.menu.kiran_menu,menu);
                       return true;
                       }

activity_main.xml :-

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout  
 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" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:fitsSystemWindows="true" 
 tools:context="com.example.kiran.optiosmenus.MainActivity"
 
<android.support.design.widget.AppBarLayout 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content
 android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar 
 android:id="@+id/toolbar" 
 android:layout_width="match_parent" 
 android:layout_height="?attr/actionBarSize" 
 android:background="?attr/colorPrimary" 
 app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton 
 android:id="@+id/fab" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_gravity="bottom|end" 
 android:layout_margin="@dimen/fab_margin" 
 android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

activity_status.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.example.kiran.optiosmenus.SatusActivity">

<TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="Welcome to Status" 
 android:id="@+id/textView2" 
 android:layout_alignParentTop="true"         
 android:layout_alignParentRight="true" 
 android:layout_alignParentEnd="true" 
 android:layout_marginTop="83dp" />
</RelativeLayout>

content_main.xml :-

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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" 
 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" 
 app:layout_behavior="@string/appbar_scrolling_view_behavior" 
 tools:context="com.example.kiran.optiosmenus.MainActivity" 
 tools:showIn="@layout/activity_main">

<TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="Welcome to Options Menus" 
 android:id="@+id/textView" 
 android:layout_alignParentTop="true" 
 android:layout_centerHorizontal="true" 
 android:layout_marginTop="105dp" />
</RelativeLayout>

MainActivity.java :-

package com.example.kiran.optiosmenus;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
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);
 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);
 FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
 fab.setOnClickListener(new View.OnClickListener() {
@Override 
 public void onClick(View view) {
 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                       .setAction("Action", null).show();
 }
 });
}

@Override 
 public boolean onCreateOptionsMenu(Menu menu) {
 getMenuInflater().inflate(R.menu.menu_main, menu);
 return super.onCreateOptionsMenu(menu);
 }

@Override
 public boolean onOptionsItemSelected(MenuItem item) {
 switch (item.getItemId()) {
 case R.id.item1:
Toast.makeText(MainActivity.this, "Status is Selected"
                                                  Toast.LENGTH_SHORT).show();
 Intent in=new Intent(getApplicationContext(),SatusActivity.class);
 startActivity(in);
 break;
 case R.id.item2:
Toast.makeText(MainActivity.this, "Whatsapp Web is Selected"
                                                  Toast.LENGTH_SHORT).show();
 break;
 case R.id.item3:
Toast.makeText(MainActivity.this, "Wallpaper is Selected"
                                                   Toast.LENGTH_SHORT).show();
 break;
 case R.id.action_settings:
Toast.makeText(MainActivity.this, "Setting is Selected",
                                                    Toast.LENGTH_SHORT).show();
 break;
 }
 return super.onOptionsItemSelected(item);
 }
}

StatusActivity.java :-

package com.example.kiran.optiosmenus;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class SatusActivity extends AppCompatActivity {

@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_satus);
 }
}

Output :-

OptionsMenu
OptionsMenu fig :1





















OptionsMenu
OptionsMenu fig :2





















OptionsMenu
OptionsMenu fig :3