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
Related
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...!!!
In the above image of skyscanner app, when i click on the search icon then it shows as like below image.
So, how can i overlay the search layout when i click on search icon ?
This may help you and fulfill your requirement. Although its not the perfect solution but it worked for me for setting the view above toolbar.
Use Popup window and inflate the layout of search.
Design the popupwindow location, animationstyle, background etc.. and refer this Using Immersive Full-Screen Mode for systemUiVisibility usage.
PopupWindow mpopup; //declare it globally.
//it will make main background transperent. where mRoot is the root layout.
mRoot.setAlpha(0.50f);
View popUpView = getLayoutInflater().inflate(R.layout.activity_search,
null);
popUpView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
// Creation of popup
mpopup = new PopupWindow(popUpView, RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT, true);
//this will set the transperency of background.and set the background color transperent.
mpopup.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
//set the animation style of popup view.
mpopup.setAnimationStyle(android.R.style.Animation_Dialog);
mpopup.showAtLocation(popUpView, Gravity.NO_GRAVITY, 0, 0);
FloatingActionButton btnSearch = (FloatingActionButton) popUpView.findViewById(R.id.btnsearch);
btnSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mpopup.dismiss();
//remove transperency
mRoot.setAlpha(1f);
}
});
activity_search.xml: (Design your layout according to your requirement below is just sample)
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/coorlay"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/lay_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/textSecondary"
android:orientation="vertical"
android:padding="10dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Write something" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:padding="10dp">
<CheckBox
android:id="#+id/check1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="check1" />
<CheckBox
android:id="#+id/check2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="check2" />
<CheckBox
android:id="#+id/check3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="check3" />
</LinearLayout>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/btnsearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:src="#drawable/places_ic_search"
android:tint="#color/white"
app:layout_anchor="#id/lay_search"
app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>
Now, when you click on search button of ur toolbar then the above view will display. and for background transperency of root layout you can set its alpha on buttonclick and remove alpha on button dismiss. as given in code.
This is the solution i have used
Create a custom layout for action bar and set it this way
ActionBar actionBar = getSupportActionBar();
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(R.layout.custom_actionbar);
View view = getSupportActionBar().getCustomView();
// find ids using view.
imageButton = (ImageButton)view.findViewById(R.id.action_bar_back);
In that custom layout add all views,which will be there before clicking search and after clicking search
which every view should be visible after search icon is clicked make there visibility as GONE in XML by default,
back, California, search icon visible by default
when user click on search icon make back, California,search icon visibility GONE and make X icon and search hotels visible
You can add search option by using following piece of code and customize it a per your needs.
#Override
public boolean onCreateOptionsMenu(final android.view.Menu menu) {
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final SearchView searchView = new SearchView(this);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setQueryHint(getString(R.string.hint_search));
searchView.setIconifiedByDefault(false);
ImageView searchIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
searchIcon.setImageResource(R.drawable.ic_search);
SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchAutoComplete.setHintTextColor(Color.parseColor("#888888"));
searchAutoComplete.setTextColor(ContextCompat.getColor(context, R.color.text));
ImageView searchCloseIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
searchCloseIcon.setImageResource(R.drawable.ic_clear);
ImageView voiceIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_voice_btn);
voiceIcon.setImageResource(R.drawable.ic_keyboard_voice);
voiceIcon.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
menu.getItem(0).collapseActionView();
return false;
}
});
menu.add(getString(R.string.title_search))
.setIcon(R.drawable.ic_search)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
MenuItemCompat.setOnActionExpandListener(menu.getItem(0), new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
AppGlobal.hideSoftKeyboard(context);
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
return true;
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
searchView.requestFocus();
AppGlobal.openSoftKeyboard(context);
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
return true;
}
});
return true;
}
You can use popup window or create layout inside toolbar like this :
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="?attr/colorPrimaryDark"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="40dp">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="California"/>
<TextView
android:id="#+id/subtitle"
android:layout_below="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="California"/>
<Button
android:id="#+id/single_button"
android:layout_below="#+id/subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="California"/>
<LinearLayout
android:id="#+id/ll_button"
android:layout_below="#+id/single_button"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="California"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="California"
android:layout_weight="1"/>
</LinearLayout>
<TextView
android:id="#+id/tv_spinner"
android:layout_below="#+id/ll_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="spinner"/>
</LinearLayout>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="11dp"
android:layout_marginEnd="11dp"
android:layout_marginTop="173dp" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
Its look like this :
you can show/hide views based on your requirement.
#Dipak please follow the link PersistentSearch this is more than you want,
I use same in my code to really awesome .
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!
I use popup menu with custom items.Problems like this
Red lines represents to default width size.
but i want custom smaller width size of items(represents red lines).
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainLayout.this, mSikBtn);
popup.getMenuInflater().inflate(R.menu.poupup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
return true;
}
});
popup.show();//showing popup menu
}
});
Layout xml file
<ImageView
android:id="#+id/popupBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:src="#drawable/selector_sik"
android:layout_weight="1"
android:layout_margin="10dip"
/>
popup_menu.xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/one"
android:title="A)."/>
...
<item
android:id="#+id/three"
android:title="E)."/>
</menu>
Similar question: stackoverflow.com
I need to custom popup menu
Just like you, I tried a lot to change the width of the popup menu but was unsuccessful.
Somehow, I found a solution you might be looking for.
First, instead of using popup menu, I used popup window.
Step1: Making a row layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="#android:drawable/list_selector_background"
android:orientation="vertical"
android:padding="3dp">
<TextView
android:id="#+id/ItemA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginRight="15dp"
android:layout_marginTop="3dp"
android:text="A)"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/ItemB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:text="B)"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/ItemC"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:text="C)"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/ItemD"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:text="D)"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
</LinearLayout>
Step 2: Made a method to initialize popup window:
private PopupWindow initiatePopupWindow() {
try {
mInflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = mInflater.inflate(R.layout.row, null);
//If you want to add any listeners to your textviews, these are two //textviews.
final TextView itema = (TextView) layout.findViewById(R.id.ItemA);
final TextView itemb = (TextView) layout.findViewById(R.id.ItemB);
layout.measure(View.MeasureSpec.UNSPECIFIED,
View.MeasureSpec.UNSPECIFIED);
mDropdown = new PopupWindow(layout,FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT,true);
Drawable background = getResources().getDrawable(android.R.drawable.editbox_dropdown_dark_frame);
mDropdown.setBackgroundDrawable(background);
mDropdown.showAsDropDown(pop, 5, 5);
} catch (Exception e) {
e.printStackTrace();
}
return mDropdown;
}
Step 3: Simply calling it in the onCreate():
public class MainActivity extends Activity {
ImageButton red, blue;
private PopupWindow mDropdown = null;
LayoutInflater mInflater;
Button pop;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pop = (Button)findViewById(R.id.button1);
pop.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
initiatePopupWindow();
}
});
}
}
Hope this helps you..If it does, accept my answer..:)
Here's the screenshot:
http://imageshack.com/a/img585/7388/dxjo.png
I don't think you can set layout:width in menu, so you might have to create your own view layout. This might help:
Set menu items centered and full-width with ActionBarSherlock
http://developer.android.com/guide/topics/resources/menu-resource.html
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