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:
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
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
No comments:
Post a Comment