How to inflate an optionsMenu vertically on a button click - android

I'm trying to inflate an options menu on a button click.
I managed to inflate it using openOptionsMenu(); in button's onClick event(as we are using a layout with custom fields like ImageView, Textbox on the action bar).
But the menu is inflated on the bottom. Right now my menu is inflated like below image:
I want it to be opened vertically(some thing like below).
How can I do it?
Below are my code samples:
Main Activity:
private Button optionsMenu;
optionsMenu = (Button)findViewById(R.id.optionsMenu);
In onCreate:
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_layout);
optionsMenu.setOnClickListener(menuInflate);
private OnClickListener menuInflate = new OnClickListener() {
#Override
public void onClick(View v) {
openOptionsMenu();
}
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Uri uri;
Intent i;
switch (item.getItemId()) {
case R.id.option1:
uri = Uri.parse("http://www.yahoo.com");
i = new Intent(Intent.ACTION_VIEW, uri);
startActivity(i);
return true;
case R.id.option2:
uri = Uri.parse("tel:92345678");
i = new Intent(Intent.ACTION_CALL, uri);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
options_menu.xml :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/option1" android:title="About"
android:orderInCategory="101" android:showAsAction="withText" />
<item android:id="#+id/option2" android:title="Help"
android:orderInCategory="102" android:showAsAction="withText" />
</menu>
menu_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/content_bg_color">
<RelativeLayout
android:id="#+id/menu_sub_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/header_bg"
android:paddingTop="5dip"
android:paddingBottom="5dip">
<com.abc.ThirdParty.SmartImageView
android:id="#+id/user_profile_image"
android:layout_width="54dip"
android:layout_height="54dip"
android:scaleType="fitXY"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="#drawable/portrait"/>
<TextView
android:id="#+id/username_text_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/user_profile_image"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:gravity="center"
android:textSize="22sp"
android:fontFamily="sans-serif-light"
android:textStyle="bold"
android:textColor="#android:color/white"/>
<Button
android:id="#+id/optionsmenu_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:layout_centerVertical="true"
android:gravity="center"
android:background="#drawable/ic_options_menu"/>
</RelativeLayout>
</RelativeLayout>

To show the menu items on click, set their showAsAction attribute to never as described in the docs :
never : Never place this item in the app bar. Instead, list the item in the app bar's overflow menu.
So set this for the items that you want in the overflow menu :
android:showAsAction="never"
UPDATE
Also, remove the optionsMenu image (the one with three dots) that you've put on the Toolbar manually. Android will put that icon itself if there are menu items with showAsAction attribute set to never or there isn't enough space in the Toolbar to show the items.
and remove these methods from the onClickListener of your image and put them inside your Activity class :
#Override
public boolean onCreateOptionsMenu(Menu menu)
#Override
public boolean onOptionsItemSelected(MenuItem item)
UPDATE #2
If you want a custom Toolbar with overflow menu button, there's no need to put a Button(with 3 dots icon) for that. If you want to customize the Toolbar put your whole layout code inside Toolbar in the xml and set that as the Activity's Toolbar inside onCreate like this :
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/content_bg_color">
<RelativeLayout
android:id="#+id/menu_sub_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/header_bg"
android:paddingTop="5dip"
android:paddingBottom="5dip">
<com.abc.ThirdParty.SmartImageView
android:id="#+id/user_profile_image"
android:layout_width="54dip"
android:layout_height="54dip"
android:scaleType="fitXY"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="#drawable/portrait"/>
<TextView
android:id="#+id/username_text_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/user_profile_image"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:gravity="center"
android:textSize="22sp"
android:fontFamily="sans-serif-light"
android:textStyle="bold"
android:textColor="#android:color/white"/>
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
Then set the toolbar in the onCreate like this :
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
and don't worry about the overflow menu button, it'll be added automatically in the cases discussed above.
Here's the output of this code :

Change your options_menu.xml like this
<item
android:id="#+id/option1"
android:orderInCategory="100"
android:title="About"
app:showAsAction="never" />
<item
android:id="#+id/option2"
android:orderInCategory="100"
android:title="Help"
app:showAsAction="never" />
And in your Activity..
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.option1) {
//perform with Option1 here
} else if (item.getItemId() == R.id.option2) {
//perform with Option2 here
}
return super.onOptionsItemSelected(item);
}
it will automatically put the button that you want no need to put manually!

Related

Button in navigation drawer menuitem not working

I just try to make one Navigation Drawer Menu item to specific view. I added two textView respectively left and right side. And SwitchCompat. But SwitchCompat is not working. I just try to out one message for test.
I searched many resources but couldn't find anything like that. Is this bad practice? or not good to use?
Here my codes:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="Pay" />
<android.support.v7.widget.SwitchCompat
android:id="#+id/paymentMethod"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:theme="#style/SelectionSwitch" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="4"
android:text="Card" />
</LinearLayout>
On my Activity:
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
toolbarMenu.findItem(R.id.menu_item_search).setVisible(id == R.id.nav_camera);
Fragment fragment = null;
Class fragmentClass = null;
if (id == R.id.nav_camera) {
View view = item.getActionView();
SwitchCompat payMethod = view.findViewById(R.id.paymentMethod);
payMethod.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(ActivityHome.this, "TEST", Toast.LENGTH_SHORT).show();
}
Actually after clicking more than 10 times it works but can't understand why.
create a layout then add to menu xml :
<item
android:id="#+id/nav_datasaver"
app:actionLayout="#layout/menu_item_datasaver" />
menu_item_datasaver is the linear layout which is created in layout floder and using to menu xml
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_download"
android:icon="#drawable/ic_download"
android:title="SAVED OFFLINE" />
<item
android:id="#+id/nav_referral"
app:actionLayout="#layout/menu_item_refer" />
<item
android:id="#+id/nav_contest"
app:actionLayout="#layout/menu_item_contest" />
<!-- <item
android:id="#+id/nav_login_button"
android:icon="#drawable/ic_download"
android:title="LOGOUT"/>-->
</group>

How to Display Badges with Menu Items that are displayed in the overflow menu

I am Working on Android Menu Items. I can able to add Badges with the Items that are displayed in the Action Bar .But i want to show the same Badges with the Overflow Menu Items. Is there any solutions to Add Badges to Menu Items in overflow menu Like the Sample Image and
Any help would greatly appreciated..!!!
can you please try following.
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/badge"
android:actionLayout="#layout/badge_layout"
android:title="Badges"
android:showAsAction="always">
</item>
</menu>
Here badge_layout is you menu item layout with the badge.
and following are code to implement in activity.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.badge).getActionView();
mCounter = (TextView) badgeLayout.findViewById(R.id.counter);
return true;
}
Finally i did it with the Help ofCustom PopUp Window + BadgeView
Here what i have Done.
Created Custom Layout For PopUp Window - custom_popup.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/icon"
android:layout_width="36dp"
android:layout_height="36dp"
android:background="#drawable/icon_menu_facebook"
android:text="Icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notifications" />
<TextView
android:id="#+id/badge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="36dp"
android:layout_height="36dp"
android:background="#drawable/icon_menu_facebook"
android:text="Icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notifications" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="36dp"
android:layout_height="36dp"
android:background="#drawable/icon_menu_facebook"
android:text="Icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notifications" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
2.Added Menu Item with Custom Icon (Overflow Menu Icon)- options_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/ViewSource"
android:icon="#drawable/ic_action_viewsource"
android:title="ViewSource"
app:showAsAction="ifRoom" />
<item
android:id="#+id/about"
android:icon="#drawable/ic_action_about"
android:title="About"
app:showAsAction="ifRoom">
<!-- "file" submenu -->
<menu>
<item
android:id="#+id/github"
android:icon="#drawable/icon_menu_github"
android:title="Github" />
<item
android:id="#+id/linkedin"
android:icon="#drawable/icon_menu_linkedin"
android:title="LinkedIn" />
<item
android:id="#+id/twitter"
android:icon="#drawable/icon_menu_twitter"
android:title="Twitter" />
<item
android:id="#+id/facebook"
android:icon="#drawable/icon_menu_facebook"
android:title="Facebook" />
</menu>
</item>
<item
android:id="#+id/notifications"
android:icon="#drawable/ic_action_name"
android:title="More"
app:showAsAction="always"></item>
</menu>
In MainActivity.java Added the following Code with in onOptionsItemSelected(MenuItem item)
case R.id.notifications:
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View CustomPopUp = layoutInflater.inflate(R.layout.custom_popup, null);
popupWindow = new PopupWindow(CustomPopUp, ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT);
if (Build.VERSION.SDK_INT >= 21) {
popupWindow.setElevation(5.0f);
}
ViewGroup actionBar = getActionBar(getWindow().getDecorView());
TextView tv_badge = (TextView) CustomPopUp.findViewById(R.id.badge);
BadgeView badge = new BadgeView(activity);
badge.setTargetView(tv_badge);
badge.setBadgeCount(45);
popupWindow.showAtLocation(actionBar, Gravity.TOP | Gravity.RIGHT, 0, -70);
popupWindow.setAnimationStyle(R.style.Animation);
linearlatout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
return true;
default:
return super.onOptionsItemSelected(item);
Thats all..!!! Happy Coding...!!!

change background color single item menu

I want popup menu like this
that show it after click on button
XML
<item
android:state_pressed="true"
android:id="#+id/fromFirstMonth"
android:title="از ابتدای سال"
android:drawable="#drawable/nav_item_background"/>
<item
android:state_pressed="true"
android:id="#+id/currentMonth"
android:title="این ماه"
android:drawable="#color/blueMenu"/>
<item
xmlns:showAsAction="always"
android:id="#+id/currentSession"
android:title="این فصل"
android:drawable="#color/white"/>
<item
xmlns:showAsAction="always"
android:id="#+id/selection"
android:title="تانتخابی"
android:drawable="#color/blueMenu"/>
</menu>
Java
hourglass.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(Products.this, hourglass);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.hourglass_item, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(Products.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();//showing popup menu
}
});
I want set different background color for each item. i set android:drawable but this does not worked.
I can with this code can change text color but i does not know how can change background color item
Menu menu=popup.getMenu();
MenuItem item = menu.getItem(0);
SpannableString s = new SpannableString("My red MenuItem");
s.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), 0);
item.setTitle(s);
If you want to use item tags and do this,as an alternative way you can tryBackgroundColorSpan and write the logic to reformat of the length of your strings.
s.setSpan(new BackgroundColorSpan(Color.RED), 0, s.length(), 0);
out put:
else
If i do that i'll crate a Dialog without a Title
private Dialog fakeDialogUseInstedOfMenuItem;
fakeDialogUseInstedOfMenuItem = new Dialog(MainActivity.this);
fakeDialogUseInstedOfMenuItem.requestWindowFeature(Window.FEATURE_NO_TITLE); // no Title
fakeDialogUseInstedOfMenuItem.setContentView(R.layout.my_custom_view);
and set a fixed or scroll view for that as the requirement
my_custom_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dp"
android:layout_height="200dp"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#789"
android:orientation="vertical">
<LinearLayout
android:id="#+id/first_lin"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option One"
android:textColor="#000"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FFF"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option Two"
android:textColor="#000"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FFF"></LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
and access items like this,
LinearLayout linearLayoutOneInDialog = (LinearLayout) fakeDialogUseInstedOfMenuItem.findViewById(R.id.first_lin);
out put:
But these things are optional ways

How can I move right quick filter bar in the ActionBar?

Hi, I'm using SherlockActivity and I need to move the quick filter bar to the right (sorry if I was not the real name)
How I can do?
I'm guessing you want to attach EditText in your ActionBar as your filter input field.
Define your menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/search"
android:icon="#android:drawable/search"
android:actionLayout="#layout/search"
android:showAsAction="never"
android:title="#string/about">
</item>
</menu>
In onCreateOptionsMenu() method add action layout to your item:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.actions, menu);
EditText add= (EditText)menu.findItem(R.id.search).getActionView()
.findViewById(R.id.title);
add.setOnEditorActionListener(this);
return(super.onCreateOptionsMenu(menu));
}
Define your action layout search.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Word:"
android:textAppearance="#android:style/TextAppearance.Medium"/>
<EditText
android:id="#+id/title"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dip"
android:layout_marginTop="4dip"
android:imeActionId="1337"
android:imeOptions="actionDone"
android:inputType="text"/>
</LinearLayout>
Your activity/fragment should implement TextView.OnEditorActionListener or you can add this as anonymous class in onCreateOptionsMenu:
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
v.getText(); //your input CharSequence to be handled by FilterQueryProvider for example
}
Is this a solution for you ?

Dropdown on image click in android

Please go through the below image
http://i.imgur.com/3WqhVCj.jpg
I dont have any action bar and I made my custom header using below layouts which is shown in Figure-2
[Linearlayout]
[Relativelayout]
[linearlayout]
Back Button
App Icon
My APP text
Overflow Icon
[/Linearlayout]
[/Relativelayout]
[/linearlayout ]
now i need the dropdown like shown in Figure-1 on clicking overflow icon which is shown in red box on top right in Figure-2
how could i do this? I have tried spinner it giving me popup but i need just dropdown shown in Figure-1.
plz suggest me
First of all create a function or method in your java class file:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater menuINF= getMenuInflater();
menuINF.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.***:
break;
}
And add menu content by adding menu folder in res folder and create menu.xml in that folder (res->menu->menu.xml)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_add" android:title="Add" android:icon="#drawable/icon"/>
<item android:id="#+id/menu_DashBoard" android:title="DashBoard" />
<item android:id="#+id/menu_Master_Entry" android:title="Master Entry" />
<item android:id="#+id/menu_Product_Section" android:title="Product Section" />
<item android:id="#+id/menu_Retailers_Orders" android:title="Retailers Orders" />
</menu>
This may help you Its works for me.
Use dialog (Example)
And to place dialog to correct height and width use
WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();
wmlp.gravity = Gravity.TOP | Gravity.CENTER_RIGHT;
BitmapDrawable bd=(BitmapDrawable) this.getResources().getDrawable(R.drawable.urdrawable);//urdrawable is your top bar image
int height=bd.getBitmap().getHeight();
//int width=bd.getBitmap().getWidth();
//wmlp.x = width; //x position
wmlp.y = height; //y position
Try this..
For list_popup.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:gravity="center"
android:background="#44444d"
>
<TextView
android:layout_height="50dp"
android:layout_width="match_parent"
android:text="text"
android:textSize="18dp"
android:textColor="#FFFFFF"
android:gravity="center"
/>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#999999"
/>
<TextView
android:layout_height="50dp"
android:layout_width="match_parent"
android:text="text1"
android:textSize="18dp"
android:textColor="#FFFFFF"
android:gravity="center"
/>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#999999"
/>
<TextView
android:layout_height="50dp"
android:layout_width="match_parent"
android:text="text2"
android:textSize="18dp"
android:textColor="#FFFFFF"
android:gravity="center"
/>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#999999"
/>
<TextView
android:layout_height="50dp"
android:layout_width="match_parent"
android:text="text3"
android:textSize="18dp"
android:textColor="#FFFFFF"
android:gravity="center"
/>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#999999"
/>
</LinearLayout>
java
LayoutInflater layoutInflater= (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
final View popupView = layoutInflater.inflate(R.layout.list_popup, null);
final PopupWindow popupWindow = new PopupWindow(popupView,200,LayoutParams.WRAP_CONTENT);
onClick.
show_options.setOnClickListener(new Button.OnClickListener(){
public void onClick(View arg0) {
if(popupWindow.isShowing())
popupWindow.dismiss();
else
popupWindow.showAsDropDown(show_options, 50, 0);
}
});
show_options is your overflow icon image name
Here is some example
http://rajeshandroiddeveloper.blogspot.in/2013/07/android-popupwindow-example-in-listview.html
http://www.androidhub4you.com/2012/07/how-to-create-popup-window-in-android.html

Categories

Resources