Open top menu

In This tutorials we will implement dynamic text View design . explain below

MainActivity.java
...................................................................................................................................................................


package com.androidbeginner.testcode;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
LinearLayout ln;
ArrayList<String> ParameterList = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ln = (LinearLayout) findViewById(R.id.layouts);

final TextView tutorial = new TextView(getApplication());
tutorial.setText("AndroidBeginner");
tutorial.setTextSize(40);
tutorial.setGravity(Gravity.CENTER);
tutorial.setTextColor(Color.BLUE);
ln.addView(tutorial);

/*
* Add items in arraylist
*/

ParameterList.add("item1");
ParameterList.add("item2");
ParameterList.add("item3");
ParameterList.add("item4");
ParameterList.add("item5");
ParameterList.add("item6");
for (int i = 0; i < ParameterList.size(); i++) {

String id = ParameterList.get(i);
/*
* create a textView
*/

final TextView text = new TextView(getApplication());
text.setTag(id);
text.setText(ParameterList.get(i));
text.setTextColor(Color.RED);
text.setTextSize(20);
text.setGravity(Gravity.CENTER);
ln.addView(text);
/*
* click on text items change item text Color
*/
text.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
text.setTextColor(Color.BLACK);
}
});
}
}

}


Read more

In this tutorial Explain how to create CheckBox in android.   a checkBox very usefull widgets in android, and the CheckBox class is subclass of compoundButton class.

CheckBox methods how to call , we will explain below.


  •  public boolean isChecked()

Returns true if it is checked otherwise false.

  •  public void setChecked(boolean status)
Changes the state of the CheckBox.


activtiy_main.xml
.............................................................................................................................................................





<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="AndroidBeginner"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge" />
   <CheckBox  
        android:id="@+id/checkBox1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentLeft="true"  
        android:layout_alignParentTop="true"  
        android:text="Pizza"
        android:layout_marginTop="30dp" />  
  
    <CheckBox  
        android:id="@+id/checkBox2"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentTop="true"  
        android:layout_toRightOf="@+id/checkBox1"  
        android:text="Tea" />  
  
    <CheckBox  
        android:id="@+id/checkBox3"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentTop="true"  
        android:layout_toRightOf="@+id/checkBox2"  
        android:text="Snacks" />  
  
    <Button  
        android:id="@+id/button1"  
        android:layout_width="150dp"  
        android:layout_height="wrap_content"  
        android:layout_below="@+id/checkBox2"  
        android:layout_marginTop="32dp"  
        android:layout_toLeftOf="@+id/checkBox3"  
        android:text="Order" />  

</LinearLayout>

Main_Activity.xml
.............................................................................................................................................................




package com.androidbeginner.testcode;



import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends Activity {
 
        CheckBox pizza,Tea,sancks;  
        Button buttonOrder;  
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
            addListenerOnButtonClick();  
        }  
    public void addListenerOnButtonClick(){  
      //Getting instance of CheckBoxes and Button from the activty_main.xml file  
        pizza=(CheckBox)findViewById(R.id.checkBox1);  
        Tea=(CheckBox)findViewById(R.id.checkBox2);  
        sancks=(CheckBox)findViewById(R.id.checkBox3);  
        buttonOrder=(Button)findViewById(R.id.button1);  
      
        //Applying the Listener on the Button click  
        buttonOrder.setOnClickListener(new OnClickListener(){  
      
            @Override  
            public void onClick(View view) {  
                int totalamount=0;  
                StringBuilder result=new StringBuilder();  
                result.append("Selected Items:");  
                if(pizza.isChecked()){  
                    result.append("\nPizza 100Rs");  
                    totalamount+=100;  
                }  
                if(Tea.isChecked()){  
                    result.append("\nCoffe 50Rs");  
                    totalamount+=50;  
                }  
                if(sancks.isChecked()){  
                    result.append("\nBurger 120Rs");  
                    totalamount+=120;  
                }  
                result.append("\nTotal: "+totalamount+"Rs");  
                //Displaying the message on the toast  
                Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();  
            }  
              
        });  
    } 
  /*  public boolean isChecked();
    
    Returns true if it is checked otherwise false.
    public void setChecked(boolean status)
    Changes the state of the CheckBox. */
}

Read more

                    In this Tutorial implement the custom Toast in android

edit_text_gradient.xml
...................................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="15dp">
 <solid android:color="#45bc32"/>

    <corners
     android:bottomRightRadius="10dp"
     android:bottomLeftRadius="10dp"
     android:topLeftRadius="10dp"
     android:topRightRadius="10dp"/>

  <stroke android:width="6dip" android:color="#dce0e2" />
</shape>




custom_toast.xml
...................................................................................................................................................................


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/custom_toast"

    >
     <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="center"
            android:layout_margin="20dp"
            android:padding="7dp"
              android:background="@drawable/edit_text_gradient">
     <ImageView
        android:id="@+id/custom_toast_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/hello_world"
        android:src="@drawable/ic_launcher"/>
   
        <TextView
        android:id="@+id/custom_toast_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"      
        android:text="CUSTOM TOAST" />

     
        </LinearLayout>

</LinearLayout>



activity_main.xml
...................................................................................................................................................................

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="AndroidBeginner"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="" />

</LinearLayout>




MainActivity.xml
...................................................................................................................................................................


package com.androidbeginner.testcode;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

      Button Nextbtn;
   
     

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);    
/*
* Creating the LayoutInflater instance
*/
        LayoutInflater li = getLayoutInflater();
/*
*Getting the View object as defined in the custom_toast.xml file
*/        View layout = li.inflate(R.layout.custom_toast,
          (ViewGroup) findViewById(R.id.custom_toast));
     
/* Creating the Toast object
*
*/
        Toast toast = new Toast(getApplicationContext());
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.setView(layout);//setting the view of custom toast layout
        toast.show();
    }
}

Read more


The Switcher specialize ViewSwitcher .That implement only for children of type textView . A TextSwitcher  useful animate  labale on Screen. show example.

MainActivity.java
..............................................................................................................................................................


package com.androidbeginner.testcode;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity {
 private TextSwitcher TxtSwitcher;
      Button Nextbtn;
     
      // Array of String to Show In TextSwitcher
      String textToShow[]={"A","B","C","D","E","F","G","H","I","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
      int messageCount=textToShow.length;
      // to keep current Index of text
      int currentIndex=-1;  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     // get The references
        Nextbtn=(Button)findViewById(R.id.butn_signUp);
        TxtSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher1);
       
        // Set the ViewFactory of the TextSwitcher that will create TextView object when asked
        TxtSwitcher.setFactory(new ViewFactory() {
           
            public View makeView() {
                // TODO Auto-generated method stub
                // create new textView and set the properties like clolr, size etc
                TextView myText = new TextView(MainActivity.this);
                myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
                myText.setTextSize(36);
                myText.setTextColor(Color.BLUE);
                return myText;
            }
        });

        // Declare the in and out animations and initialize them
        Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
        Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right);
       
        // set the animation type of textSwitcher
        TxtSwitcher.setInAnimation(in);
        TxtSwitcher.setOutAnimation(out);
   
        // ClickListener for NEXT button
        // When clicked on Button TextSwitcher will switch between texts
        // The current Text will go OUT and next text will come in with specified animation
        Nextbtn.setOnClickListener(new View.OnClickListener() {
           
            public void onClick(View v) {
                // TODO Auto-generated method stub
                currentIndex++;
                // If index reaches maximum reset it
                if(currentIndex==messageCount)
                    currentIndex=0;
                TxtSwitcher.setText(textToShow[currentIndex]);
            }
        });
    }
}


acivity_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:id="@+id/text"
    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_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:padding="10dp"
        android:text="Androidbeginner"
        android:textSize="20sp" />

    <Button
        android:id="@+id/butn_signUp"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="67dp"
        android:padding="10dp"    
        android:background="@drawable/button_action"
        android:text="Next" />

    <TextSwitcher
        android:id="@+id/textSwitcher1"
        android:layout_width="400dp"
        android:layout_height="50dp"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignParentTop="true"
        android:textSize="80sp"
        android:layout_marginTop="100dp" >
    </TextSwitcher>

</RelativeLayout>

Read more


In this Tutorial we will Design best Button UI. Button shows Effcet Whene click on button ,the effect show with the help of button gradient .

res/drawable/button_normal.xml
............................................................................................................................................

<?xml version="1.0" encoding="utf-8"?>
 <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape android:shape="rectangle">
      <solid android:color="#8e918c" />
    </shape>
  </item>
    <item android:bottom="5dp" >
 
     <shape android:shape="rectangle">
      <solid android:color="#cacaca" />
    </shape>
   </item>  
 </layer-list>


res/drawable/button_action.xml
............................................................................................................................................

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
             android:state_pressed="true" >
        <shape>
        <solid
        android:color="#cacaca" />
         
            <corners
               android:radius="4dp"/>
            <padding
                android:left="1dp"
                android:top="1dp"
                android:right="1dp"
                android:bottom="1dp" />
        </shape>
    </item>
    <item android:drawable="@drawable/button_normal" android:state_activated="false">      
    </item>
</selector>


main_activity.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:id="@+id/text"
    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_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Androidbeginner"
        android:textSize="20sp" />

    <Button
        android:id="@+id/butn_signUp"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="67dp"
        android:background="@drawable/button_action"
        android:text="Registration" />


</RelativeLayout>




Read more



In this Tutorial we will design effective UI Design in android. Design is very simple show below example.


res/layout/drawable/edit_text_gradient.xm
.................................................................................................................................................................

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="15dp">


    <corners
     android:bottomRightRadius="20dp"
     android:bottomLeftRadius="20dp"
     android:topLeftRadius="20dp"
     android:topRightRadius="20dp"/>

  <stroke android:width="2dip" android:color="#5079f4" />
</shape>

res/layout/drawable/button_action.xml
..................................................................................................................................................................

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
        <solid
        android:color="#76b8f2" />
            <stroke
                android:width="1dp"
                android:color="#2f6699" />
            <corners
                android:bottomRightRadius="20dp"
                android:bottomLeftRadius="20dp"
                android:topLeftRadius="20dp"
                android:topRightRadius="20dp"/>
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#449def"
                android:endColor="#2f6699"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#ffffff" />
             <corners
                android:bottomRightRadius="20dp"
                android:bottomLeftRadius="20dp"
                android:topLeftRadius="20dp"
                android:topRightRadius="20dp"/>
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>


res/layout/activity.xml
...................................................................................................................................................


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/loginpage_icon"
    android:gravity="center">

   

        <EditText
            android:id="@+id/editText_user"
            android:layout_width="fill_parent"
            android:layout_height="50dp"          
            android:layout_margin="10dp"
            android:layout_marginTop="54dp"
            android:ems="10"
            android:maxLines="1"
            android:textStyle="italic"
            android:hint="@string/hintuesre"
            android:background="@drawable/edit_text_gradient"
            android:paddingLeft="10dp">
         
            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/editText_password"
            android:layout_width="fill_parent"
            android:layout_height="50dp"          
            android:layout_margin="10dp"        
            android:inputType="textPassword"
            android:ems="10"
            android:maxLines="1"
            android:textStyle="italic"
            android:hint="@string/hintpas"
            android:background="@drawable/edit_text_gradient"
            android:paddingLeft="10dp">
         
           </EditText>
             <CheckBox
                     android:id="@+id/checkBox1"
                     android:layout_width="fill_parent"
                     android:layout_height="wrap_content"            
                     android:layout_margin="10dp"                  
                     android:text="@string/showpass" />
     
        <Button
            android:id="@+id/button_login"
            android:layout_width="200dp"
            android:layout_height="wrap_content"                    
            android:layout_margin="10dp"
            android:text="@string/login"
            android:textColor="#ffffff"
            android:background="@drawable/button_action" />

       
   
 

</LinearLayout>

Read more



In this Tutorial we will  explain overflow menu item click on setting icon add to action bar in android.  show example.


Main_Activity.java


import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
  
PopupWindow popupWindow;
    String fiterpopUpContents[];
    private ActionBar actionBar;
    RelativeLayout layout;


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

        actionBar = getActionBar();

        layout=(RelativeLayout)findViewById(R.id.text);

        List<String> filterList = new ArrayList<String>();
        filterList.add("Red::1");
        filterList.add("Green::2");
        filterList.add("Yellow::3");
        fiterpopUpContents = new String[filterList.size()];
        filterList.toArray(fiterpopUpContents);


        // initialize pop up window
        popupWindow = popupWindow();       
        // button on click listener 
        
        actionBar.setDisplayOptions(actionBar.getDisplayOptions()
                | ActionBar.DISPLAY_SHOW_CUSTOM);
        ImageView imageView = new ImageView(actionBar.getThemedContext());
        imageView.setScaleType(ImageView.ScaleType.CENTER);
        imageView.setImageResource(R.drawable.setting);
        ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
                                                 ActionBar.LayoutParams.WRAP_CONTENT,
                                                 ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT
                                                | Gravity.CENTER_VERTICAL);

        layoutParams.rightMargin = 40;
        imageView.setLayoutParams(layoutParams);
        actionBar.setCustomView(imageView);
         
        imageView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.showAsDropDown(v, -5, 0);
}
});
    }
    
    public PopupWindow popupWindow(){ 


                         // initialize a pop up window type
    PopupWindow popupWindow = new PopupWindow(this); 
                        // the drop down list is a list view
     ListView listViewDogs = new ListView(this);                 
                       // set our adapter and pass our pop up window contents
    listViewDogs.setAdapter(dogsAdapter(fiterpopUpContents));        
                       // set the item click listener
    listViewDogs.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long arg3) {

// TODO Auto-generated method stub


if(position==0){
layout.setBackgroundColor(Color.parseColor("#ff0000"));
 

 
if(position==1){
layout.setBackgroundColor(Color.parseColor("#099b17"));

if(position==2){
layout.setBackgroundColor(Color.parseColor("#f7db09"));

}
});
    
    popupWindow.setFocusable(true);
    popupWindow.setWidth(250);
    popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);          
    popupWindow.setContentView(listViewDogs);
    return popupWindow;
    
    }



 
    private ArrayAdapter<String> dogsAdapter(String dogsArray[]) {
     
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,                                                                                                  android.R.layout.simple_list_item_1, dogsArray) {

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                                        // setting the ID and text for every items in the list
                String item = getItem(position);
                String[] itemArr = item.split("::");
                String text = itemArr[0];
                String id = itemArr[1];


                                // visual settings for the list item
                TextView listItem = new TextView(MainActivity.this); 
                listItem.setText(text);
                listItem.setTag(id);  
                listItem.setTextSize(20);
                listItem.setPadding(10, 10, 10, 10);
                listItem.setTextColor(Color.WHITE);                              
                return listItem;
            }
        };
         
        return adapter;
    }

}








Read more




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;
    }

}


Read more


 Explain How to create  Popup Menu in Android.


A Popup Menu displays a menu in a modal popup window anchored to a View. The popup will appear below the anchor view .  the popup will not overlap it until it is touched. Touching outside of the popup will dismiss it.
The menu show via main.xml class ,show below example...

My UI class  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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="47dp"
        android:text="POPUP MENU" />

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

   <item  
        android:id="@+id/one"  
        android:title="One"/>  
      
    <item  
        android:id="@+id/two"  
        android:title="Two"/>  
        
    <item  
        android:id="@+id/three"  
        android:title="Three"/>  

</menu>



My Actvity class Main_Activity.java

package com.androidbeginner.testcode;

import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
PopupMenu popup;
Button butnpoup;  
     
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        butnpoup = (Button) findViewById(R.id.button1);  
        butnpoup.setOnClickListener(this);
    }
@Override
public void onClick(View v) {
PopupMenu();
public void PopupMenu() {
PopupMenu popup = new PopupMenu(MainActivity.this, butnpoup);  
         //Inflating the Popup using xml file  
         popup.getMenuInflater().inflate(R.menu.main, popup.getMenu());  
        
         //registering popup with OnMenuItemClickListener  
         popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {  
          public boolean onMenuItemClick(MenuItem item) {  
           Toast.makeText(MainActivity.this,"You Clicked : " +                                                                                                        
item.getTitle(),Toast.LENGTH_SHORT).show();  
           return true;  
          }  
         });  

         popup.show();
          }  
           
 
}

Read more


 How to create Context Menu explain :-

The Context menu is a floating menu, the user perform long-click on an element.
The context menu Developing in Android 2.3 an higher version.the Context menu implement the best UI context view is ,ListView and GridView.  example


 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" >

   <ListView
       android:id="@+id/listView1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
       android:layout_alignParentRight="true"
       android:layout_alignParentTop="true" >
  
    </ListView>  

</RelativeLayout>


Main_Activity.java


package com.androidbeginner.testcode;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

          ListView listView1;  
    String contacts[]={"Mr.varun","Mr.Sachin","Mr.rahul"," Mr.Tarun","Mr.Yogesh"};  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        listView1=(ListView)findViewById(R.id.listView1);  
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,contacts);  
        listView1.setAdapter(adapter);  
        // Register the ListView  for Context menu  
        registerForContextMenu(listView1);  
    }  
    @Override   
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)  
    {  
            super.onCreateContextMenu(menu, v, menuInfo);  
            menu.setHeaderTitle("Select The Action");    
            menu.add(0, v.getId(), 0, "Call");//groupId, itemId, order, title   
            menu.add(0, v.getId(), 0, "SMS");   
    }   
    @Override    
    public boolean onContextItemSelected(MenuItem item){    
            if(item.getTitle()=="Call"){  
                Toast.makeText(getApplicationContext(),"calling code",Toast.LENGTH_LONG).show();  
            }    
            else if(item.getTitle()=="SMS"){  
                Toast.makeText(getApplicationContext(),"sending sms code",Toast.LENGTH_LONG).show();  
            }else{  
               return false;  
            }    
          return true;    
      }    
    }  








Read more