main.xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="@string/hello"
android:textSize="30sp"
/>
</LinearLayout>
put below strings in strings.xml layout
<string name="hello">Hello, Press Menu button!</string>
<string name="app_name">MenuDialogDemo</string>
<string name="menu_line_1">Menu Line 1</string>
<string name="menu_line_2">Click To Enter SubMenu</string>
<string name="menu_line_3">Menu Line 3</string>
<string name="menu_line_4">Menu Line 4</string>
<string name="menu_line_5">Menu Line 5</string>
<string name="menu_line_6">Menu Line 6</string>
<string name="menu_line_7">Menu Line 7</string>
<string name="menu_line_8">Menu Line 8</string>
<string name="menu_line_9">Menu Line 9</string>
<string name="menu_line_a">Menu Line A</string>
<string name="sub_menu_line_1">SubMenu Line 1</string>
<string name="sub_menu_line_2">SubMenu Line 2</string>
<string name="sub_menu_line_3">SubMenu Line 3</string>
MainActivity.java code
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;
/**
* Demo illustrating how to use MenuDialg
*/
public class MainActivity extends Activity
implements MenuDialog.MenuSelectListener
{
// main menu:
static final MenuDialog.MenuItem[] MAIN_MENU_LIST = {
new MenuDialog.MenuItem(android.R.drawable.ic_dialog_email, R.string.menu_line_1),
new MenuDialog.MenuItem(android.R.drawable.btn_plus, R.string.menu_line_2),
new MenuDialog.MenuItem(android.R.drawable.ic_menu_directions, R.string.menu_line_3),
new MenuDialog.MenuItem(android.R.drawable.ic_menu_compass, R.string.menu_line_4),
new MenuDialog.MenuItem(android.R.drawable.btn_star, 0), // only icon, no text
new MenuDialog.MenuItem(android.R.drawable.ic_dialog_info, R.string.menu_line_6),
new MenuDialog.MenuItem(android.R.drawable.ic_media_next, R.string.menu_line_7),
new MenuDialog.MenuItem(android.R.drawable.ic_input_delete, R.string.menu_line_8),
new MenuDialog.MenuItem(android.R.drawable.ic_lock_lock, R.string.menu_line_9),
new MenuDialog.MenuItem(android.R.drawable.ic_media_pause, R.string.menu_line_a)
};
// submenu:
static final MenuDialog.MenuItem[] SUBMENU_LIST = {
new MenuDialog.MenuItem(android.R.drawable.btn_minus, R.string.sub_menu_line_1),
new MenuDialog.MenuItem(android.R.drawable.btn_minus, R.string.sub_menu_line_2),
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
/** must be implemented to intercept menu key */
@Override
public boolean onKeyUp( int keyCode, KeyEvent event ) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
// intercept menu key
new MenuDialog( this, this, MAIN_MENU_LIST, R.drawable.logo1 ); // launch main menu
return true;
}
return super.onKeyDown( keyCode, event );
}
/**
* MenuDialog.MenuSelectListener implementation, gets invoked when the user selects a menu item
* to access submenu construct MenuDialog as a response
*/
@Override
public void onMenuSelect( int index, long id ) {
Toast.makeText( this, "selected item " + index + ", id=" + id, Toast.LENGTH_SHORT ).show();
if( id == R.string.menu_line_2 ) {
new MenuDialog( this, this, SUBMENU_LIST, R.drawable.android ); // launch submenu
}
}
}
create class name MenuDialog.java
import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
/**
* MenuDialog - displays the menu in a more traditional way
*/
public class MenuDialog extends Dialog {
static final int
MARGIN_X = 10,
MARGIN_Y = 0,
dummy_int = 0;
static final float
LINE_TEXT_SIZE = 30.0F,
dummy_float = 0;
Drawable mLogo;
ListView mListView;
/**
* Constructs and shows menu
* @param context - calling context
* @param menuSelectListener - callback interface
* @param menuList - list of menu items
* @param logoRes - image resource id or 0
*/
public MenuDialog( final Context context, final MenuSelectListener menuSelectListener, final MenuItem[] menuList, int logoRes ) {
super( context );
if( menuSelectListener == null || menuList == null ) {
return; // exception?
}
final Resources resources = context.getResources();
mLogo = new BitmapDrawable( BitmapFactory.decodeResource(resources, logoRes) );
requestWindowFeature( Window.FEATURE_NO_TITLE );
setContentView( createMenuLayout(context) );
BaseAdapter adapter = new BaseAdapter() {
@Override
public int getCount() {
return menuList.length;
}
@Override
/* * */
public Object getItem( int position ) {
return position;
}
/* * */
@Override
public long getItemId( int position ) {
int id = menuList[position].mTextResourceId;
if( id == 0 ) id = menuList[position].mIconResourceId;
return id;
}
/* * */
@Override
public View getView( int position, View convertView, ViewGroup parent ) {
if( convertView == null ) {
convertView = new TextView( context );
}
TextView textView = (TextView)convertView;
if( position < menuList.length ) {
if( menuList[position].mTextResourceId == 0 ) {
textView.setText( null );
} else {
textView.setText( resources.getString(menuList[position].mTextResourceId) );
}
textView.setTextSize( LINE_TEXT_SIZE );
textView.setTextColor( Color.WHITE );
if( menuList[position].mIconResourceId == 0 ) {
textView.setCompoundDrawablesWithIntrinsicBounds( 0, 0, 0, 0 );
} else {
textView.setCompoundDrawablesWithIntrinsicBounds( menuList[position].mIconResourceId, 0, 0, 0 );
}
}
return convertView;
}
};
mListView.setAdapter( adapter );
mListView.setOnItemClickListener( new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int clicked, long id ) {
menuSelectListener.onMenuSelect( clicked, id );
MenuDialog.this.dismiss();
}
});
super.show();
}
View createMenuLayout( Context context) {
LinearLayout linearLayout = new LinearLayout( context );
linearLayout.setOrientation( LinearLayout.HORIZONTAL );
linearLayout.setMinimumHeight( android.view.ViewGroup.LayoutParams.WRAP_CONTENT );
linearLayout.setMinimumWidth( android.view.ViewGroup.LayoutParams.FILL_PARENT );
if( mLogo != null ) {
ImageView imageView = new ImageView( context );
imageView.setLayoutParams( new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) );
imageView.setImageDrawable( mLogo );
linearLayout.addView( imageView );
}
mListView = new ListView( context );
mListView.setLayoutParams( new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) );
mListView.setPadding( MARGIN_X, MARGIN_Y, 0, 0 );
mListView.setBackgroundColor( Color.DKGRAY );
linearLayout.addView( mListView );
return linearLayout;
}
/**
* Menu item - used in MenuDialog constructor
* contains
* mIconResourceId or 0
* mTextResourceId or 0
* expected that at least one of these will be not 0
* the program will crash if non-zero resource id is invalid
*/
public static class MenuItem {
int mIconResourceId;
int mTextResourceId;
/**
* MenuDialog.MenuItem constructor.
* @param iconResourceId - valid icon resource id or 0
* @param textResourceId - valid text resource id or 0
* at least one of these must be non-zero
*/
public MenuItem( int iconResourceId, int textResourceId ) {
this.mIconResourceId = iconResourceId;
this.mTextResourceId = textResourceId;
}
}
/**
* Menu selection listener - used in MenuDialog constructor
*/
public static interface MenuSelectListener {
/**
* Gets invoked when a user selects a menu item
* @param index - selected index in MenuItem[]
* @param id - selected text resource id or icon resource id (if textResourceId == 0) in MenuItem[]
*/
public void onMenuSelect( int index, long id );
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="@string/hello"
android:textSize="30sp"
/>
</LinearLayout>
put below strings in strings.xml layout
<string name="hello">Hello, Press Menu button!</string>
<string name="app_name">MenuDialogDemo</string>
<string name="menu_line_1">Menu Line 1</string>
<string name="menu_line_2">Click To Enter SubMenu</string>
<string name="menu_line_3">Menu Line 3</string>
<string name="menu_line_4">Menu Line 4</string>
<string name="menu_line_5">Menu Line 5</string>
<string name="menu_line_6">Menu Line 6</string>
<string name="menu_line_7">Menu Line 7</string>
<string name="menu_line_8">Menu Line 8</string>
<string name="menu_line_9">Menu Line 9</string>
<string name="menu_line_a">Menu Line A</string>
<string name="sub_menu_line_1">SubMenu Line 1</string>
<string name="sub_menu_line_2">SubMenu Line 2</string>
<string name="sub_menu_line_3">SubMenu Line 3</string>
MainActivity.java code
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;
/**
* Demo illustrating how to use MenuDialg
*/
public class MainActivity extends Activity
implements MenuDialog.MenuSelectListener
{
// main menu:
static final MenuDialog.MenuItem[] MAIN_MENU_LIST = {
new MenuDialog.MenuItem(android.R.drawable.ic_dialog_email, R.string.menu_line_1),
new MenuDialog.MenuItem(android.R.drawable.btn_plus, R.string.menu_line_2),
new MenuDialog.MenuItem(android.R.drawable.ic_menu_directions, R.string.menu_line_3),
new MenuDialog.MenuItem(android.R.drawable.ic_menu_compass, R.string.menu_line_4),
new MenuDialog.MenuItem(android.R.drawable.btn_star, 0), // only icon, no text
new MenuDialog.MenuItem(android.R.drawable.ic_dialog_info, R.string.menu_line_6),
new MenuDialog.MenuItem(android.R.drawable.ic_media_next, R.string.menu_line_7),
new MenuDialog.MenuItem(android.R.drawable.ic_input_delete, R.string.menu_line_8),
new MenuDialog.MenuItem(android.R.drawable.ic_lock_lock, R.string.menu_line_9),
new MenuDialog.MenuItem(android.R.drawable.ic_media_pause, R.string.menu_line_a)
};
// submenu:
static final MenuDialog.MenuItem[] SUBMENU_LIST = {
new MenuDialog.MenuItem(android.R.drawable.btn_minus, R.string.sub_menu_line_1),
new MenuDialog.MenuItem(android.R.drawable.btn_minus, R.string.sub_menu_line_2),
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
/** must be implemented to intercept menu key */
@Override
public boolean onKeyUp( int keyCode, KeyEvent event ) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
// intercept menu key
new MenuDialog( this, this, MAIN_MENU_LIST, R.drawable.logo1 ); // launch main menu
return true;
}
return super.onKeyDown( keyCode, event );
}
/**
* MenuDialog.MenuSelectListener implementation, gets invoked when the user selects a menu item
* to access submenu construct MenuDialog as a response
*/
@Override
public void onMenuSelect( int index, long id ) {
Toast.makeText( this, "selected item " + index + ", id=" + id, Toast.LENGTH_SHORT ).show();
if( id == R.string.menu_line_2 ) {
new MenuDialog( this, this, SUBMENU_LIST, R.drawable.android ); // launch submenu
}
}
}
create class name MenuDialog.java
import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
/**
* MenuDialog - displays the menu in a more traditional way
*/
public class MenuDialog extends Dialog {
static final int
MARGIN_X = 10,
MARGIN_Y = 0,
dummy_int = 0;
static final float
LINE_TEXT_SIZE = 30.0F,
dummy_float = 0;
Drawable mLogo;
ListView mListView;
/**
* Constructs and shows menu
* @param context - calling context
* @param menuSelectListener - callback interface
* @param menuList - list of menu items
* @param logoRes - image resource id or 0
*/
public MenuDialog( final Context context, final MenuSelectListener menuSelectListener, final MenuItem[] menuList, int logoRes ) {
super( context );
if( menuSelectListener == null || menuList == null ) {
return; // exception?
}
final Resources resources = context.getResources();
mLogo = new BitmapDrawable( BitmapFactory.decodeResource(resources, logoRes) );
requestWindowFeature( Window.FEATURE_NO_TITLE );
setContentView( createMenuLayout(context) );
BaseAdapter adapter = new BaseAdapter() {
@Override
public int getCount() {
return menuList.length;
}
@Override
/* * */
public Object getItem( int position ) {
return position;
}
/* * */
@Override
public long getItemId( int position ) {
int id = menuList[position].mTextResourceId;
if( id == 0 ) id = menuList[position].mIconResourceId;
return id;
}
/* * */
@Override
public View getView( int position, View convertView, ViewGroup parent ) {
if( convertView == null ) {
convertView = new TextView( context );
}
TextView textView = (TextView)convertView;
if( position < menuList.length ) {
if( menuList[position].mTextResourceId == 0 ) {
textView.setText( null );
} else {
textView.setText( resources.getString(menuList[position].mTextResourceId) );
}
textView.setTextSize( LINE_TEXT_SIZE );
textView.setTextColor( Color.WHITE );
if( menuList[position].mIconResourceId == 0 ) {
textView.setCompoundDrawablesWithIntrinsicBounds( 0, 0, 0, 0 );
} else {
textView.setCompoundDrawablesWithIntrinsicBounds( menuList[position].mIconResourceId, 0, 0, 0 );
}
}
return convertView;
}
};
mListView.setAdapter( adapter );
mListView.setOnItemClickListener( new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int clicked, long id ) {
menuSelectListener.onMenuSelect( clicked, id );
MenuDialog.this.dismiss();
}
});
super.show();
}
View createMenuLayout( Context context) {
LinearLayout linearLayout = new LinearLayout( context );
linearLayout.setOrientation( LinearLayout.HORIZONTAL );
linearLayout.setMinimumHeight( android.view.ViewGroup.LayoutParams.WRAP_CONTENT );
linearLayout.setMinimumWidth( android.view.ViewGroup.LayoutParams.FILL_PARENT );
if( mLogo != null ) {
ImageView imageView = new ImageView( context );
imageView.setLayoutParams( new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) );
imageView.setImageDrawable( mLogo );
linearLayout.addView( imageView );
}
mListView = new ListView( context );
mListView.setLayoutParams( new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) );
mListView.setPadding( MARGIN_X, MARGIN_Y, 0, 0 );
mListView.setBackgroundColor( Color.DKGRAY );
linearLayout.addView( mListView );
return linearLayout;
}
/**
* Menu item - used in MenuDialog constructor
* contains
* mIconResourceId or 0
* mTextResourceId or 0
* expected that at least one of these will be not 0
* the program will crash if non-zero resource id is invalid
*/
public static class MenuItem {
int mIconResourceId;
int mTextResourceId;
/**
* MenuDialog.MenuItem constructor.
* @param iconResourceId - valid icon resource id or 0
* @param textResourceId - valid text resource id or 0
* at least one of these must be non-zero
*/
public MenuItem( int iconResourceId, int textResourceId ) {
this.mIconResourceId = iconResourceId;
this.mTextResourceId = textResourceId;
}
}
/**
* Menu selection listener - used in MenuDialog constructor
*/
public static interface MenuSelectListener {
/**
* Gets invoked when a user selects a menu item
* @param index - selected index in MenuItem[]
* @param id - selected text resource id or icon resource id (if textResourceId == 0) in MenuItem[]
*/
public void onMenuSelect( int index, long id );
}
}
No comments:
Post a Comment
Comments