Open top menu




Explain ,how to add menu icon on ActionBar in android Application. The icon menu get the as menu/main.xml file. set the some icon for Example Setting button,Downloading button,and refresh in main class.


My 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: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.androidbeginner.testcode.MainActivity" >

   

    <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="37dp"
        android:text="Androidbeginner"
        android:textSize="20sp"/>

</RelativeLayout>

My menu/main.xml classs


<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="com.androidbeginner.testcode.MainActivity" >

   <item
        android:id="@+id/action_refresh"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:icon="@drawable/refres"
        android:title="Refresh"/>
    <item
        android:id="@+id/action_settings"
        android:title="Settings"
         android:orderInCategory="100"
         android:showAsAction="always"
        android:icon="@drawable/setting">
    </item>
    <item
        android:id="@+id/action_download"
        android:title="Donload"
         android:orderInCategory="100"
         android:showAsAction="always"
        android:icon="@drawable/download">
    </item>
 

</menu>


Main_Activity.java




package com.androidbeginner.testcode;

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.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;

public class MainActivity extends Activity {
PopupMenu popup;
Button butnpoup;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.main, menu);
      return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      /*// action with ID action_refresh was selected
      case R.id.action_refresh:
       Intent intent=new Intent(MainActivity.this,MainActivity.class);
       startActivity(intent);
        break;*/
      // action with ID action_settings was selected
      case R.id.action_settings:
        Toast.makeText(this, "Settings selected", Toast.LENGTH_SHORT)
            .show();
        break;
      case R.id.action_download:
          Toast.makeText(this, "Downlaoding", Toast.LENGTH_SHORT)
              .show();
          break;
      default:
        break;
      }

      return true;
    }

}


0 comments