Showing posts with label Android Popup Menu. Show all posts
Showing posts with label Android Popup Menu. Show all posts

Monday, 4 November 2013

Popup Menu Example

A PopupMenu displays a Menu in a modal popup window anchored to a View. The popup will appear below the anchor view if there is room, or above it if there is not. If the IME is visible the popup will not overlap it until it is touched. Touching outside of the popup will dismiss it. It required minimum API Level 14.

Example:





Code:

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="51dp"
        android:layout_height="52dp"
        android:layout_gravity="right"
        android:src="@drawable/menuicon" />

</LinearLayout>


menu / main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@+id/item1" android:title="Red"></item>
    <item android:id="@+id/item2" android:title="Green"></item>
    <item android:id="@+id/item3" android:title="Blue"></item>


</menu>


MainActivity.java

package in.androiddevelopmentanddiscussion.popmenu;

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

public class MainActivity extends Activity {

ImageView img;

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

img = (ImageView)findViewById(R.id.imageView1);

final PopupMenu popupMenu = new PopupMenu(this, img);
popupMenu.inflate(R.menu.main);

img.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupMenu.show();
}
});


        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.item1:
                        Toast.makeText(getApplicationContext(), "Red Clicked", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.item2:
                    Toast.makeText(getApplicationContext(), "Green Clicked", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.item3:
                    Toast.makeText(getApplicationContext(), "Blue Clicked", Toast.LENGTH_SHORT).show();
                        break;
                }
                return true;
            }
        });

}


}


Download full project from here




Check Android Apps on Google Play

https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en

 

Context Menu Example

Extension of Menu for context menus providing functionality to modify the header of the context menu. Context menus do not support item shortcuts and item icons. To show a context menu on long click, most clients will want to call registerForContextMenu (View) and override onCreateContextMenu (ContextMenu, View, ContextMenu.ContextMenuInfo).

Example:







Code:

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="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="Context Menu Example"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textStyle="bold" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="50dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="50dp" >

    </ListView>

</LinearLayout>


textstyle.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="10dp"
    android:paddingTop="20dp"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@color/blue"
    android:textSize="25sp" />

MainActivity.java


package in.androiddevelopmentanddiscussion.contextmenuexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
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 lv;
ArrayAdapter<String> ad;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.listView1);
final String fruit[] = {"Apple","Mango","Banana","Orange"};
ad = new ArrayAdapter<String>(getApplicationContext(),R.layout.textstyle,fruit);
lv.setAdapter(ad);
registerForContextMenu(lv);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
menu.setHeaderTitle("Action Perform"); 
menu.add(0, v.getId(), 0, "Save");
menu.add(0, v.getId(), 0, "Delete");
super.onCreateContextMenu(menu, v, menuInfo);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getTitle() == "Save"){
function1(item.getItemId());
}
if(item.getTitle()=="Delete"){
function2(item.getItemId());
}
return super.onContextItemSelected(item);
}
private void function2(int itemId) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_SHORT).show();
}

private void function1(int itemId) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show();
}
}


Download full project from here




Check Android Apps on Google Play

https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en

 

Saturday, 2 November 2013

Menu Example

Menus are a common user interface component in many types of applications. To provide a familiar and consistent user experience, you should use the Menu APIs to present user actions and other options in your activities.

Beginning with Android 3.0 (API level 11), Android-powered devices are no longer required to provide a dedicated Menu button. With this change, Android apps should migrate away from a dependence on the traditional 6-item menu panel and instead provide an action bar to present common user actions.

Although the design and user experience for some menu items have changed, the semantics to define a set of actions and options is still based on the Menu APIs. Here i will shows you how to how to create the three fundamental types of menus or action presentations on all versions of Android.

Type of Menus 


  1. Options Menu
  2. Context Menu
  3. Popup Menu


Options Menu:

Steps to create options menu:

First of all create a project by the name of MenuExample



Now open main.xml file from menu folder 



Now click on Add button


Select Item from popup window


Fill Title & Icon field


Now press Ctrl+s to save menu item, follow the same process for another menu item



Now we need to override "onOptionsItemSelected()" to define functionality of menu in MainActivity.java


@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId())
{
case R.id.item1:
Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
break;

case R.id.item2:
Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
break;

case R.id.item3:
Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.item4:
Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.item5:
Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.item6:
Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case R.id.item7:
Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
break;
}

return super.onOptionsItemSelected(item);





That's it ............ :)

I show you Context and Popup menu example in next post

Download full project from here



Check Android Apps on Google Play

https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en