In Android API 23, I have a Popup menu with submenu items. I have defined the gravity to the end, as shown
popupMenu = new PopupMenu(this, v, Gravity.END);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add_reminder:
// Option1
// ... other options / not relevant for the question
popupMenu.inflate(R.menu.menu_contact_context);
popupMenu.show();
The menu has several submenus, as defined in xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="es.goodsal.mobile.ContactActivity">
<item
android:id="#+id/add"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/action_add"
>
<menu>
<item
android:id="#+id/action_add_reminder"
android:icon="#drawable/ic_action_add_reminder"
android:orderInCategory="100"
android:title="#string/action_add_reminder"
/>
<item
android:id="#+id/action_add_note"
android:icon="#drawable/ic_action_add_note"
android:orderInCategory="100"
android:title="#string/action_add_note"/>
<item
android:id="#+id/action_add_opportunity"
android:icon="#drawable/ic_action_add_opportunity"
android:orderInCategory="100"
android:title="#string/action_add_opportunity"/>
<item
android:id="#+id/action_add_agenda"
android:icon="#drawable/ic_action_add_agenda"
android:orderInCategory="100"
android:title="#string/action_add_agenda"/>
<item
android:id="#+id/action_add_task"
android:icon="#drawable/ic_action_add_task"
android:orderInCategory="100"
android:title="#string/action_add_task"/>
</menu>
</item>
<item
android:id="#+id/action_contact_edit"
android:icon="#android:drawable/ic_menu_edit"
android:title="#string/action_edit"/>
<item
android:id="#+id/action_contact_record"
android:icon="#drawable/ic_contact_default"
android:title="#string/action_contact"/>
<item
android:id="#+id/action_report"
android:title="#string/action_report"/>
<item
android:id="#+id/funnel"
android:icon="#drawable/ic_action_funnel"
android:title="#string/action_funnel"
>
<menu>
<item
android:id="#+id/action_funnel_progress"
android:orderInCategory="100"
android:title="#string/action_funnel_progress"
/>
<item
android:id="#+id/action_funnel_regress"
android:orderInCategory="100"
android:title="#string/action_funnel_regress"
/>
</menu>
</item>
</menu>
My problem is when the menu is show. The original menu is well aligned to the right (=Gravity.END), but the submenus are aligned to the left, and I don't find a way to define gravity for submenus (and, why is not the same?!?)
Not really an answer, but I just released a library for building better nested menus out of my frustrations with PopupMenu: https://github.com/saket/cascade
It's designed to be a drop-in replacement for PopupMenu:
- val popup = PopupMenu(context, anchor)
+ val popup = CascadePopupMenu(context, anchor)
popup.inflate(R.menu.nicolas_cage_movies)
popup.show()
Related
I want textColor as black even if textView is disabled. For it I use a selector but its not working, its giving me default grey textcolor in disabled state.
Selector file is as follows:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:state_focused="true" android:color="#color/black" />
<item android:state_enabled="false" android:state_focused="false" android:color="#color/black" />
<item android:state_enabled="true" android:state_focused="true" android:color="#color/black"/>
<item android:state_enabled="true" android:state_focused="false" android:color="#color/black"/>
<item android:color="#color/black"/>
</selector>
TextView in which selector is set is as follows:
<com.rengwuxian.materialedittext.MaterialTextView
android:id="#+id/policyEndDateText"
style="?android:attr/spinnerStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textColor="#color/textview_color_selector"
android:enabled="false"
android:hint="New Policy End Date"
android:textSize="#dimen/insurance_text_size"
app:met_floatingLabelTextSize="#dimen/insurance_floating_label_textSize"
app:met_hideUnderline="true"
app:met_floatingLabel="highlight"
app:met_primaryColor="#color/material_text"/>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorPrimary_purple"/>
<item android:drawable="#color/colorAccent_purple" android:state_pressed="true"/>
<item android:drawable="#color/colorAccent" android:state_focused="true"/>
</selector>
i use LayoutInflater to create dynamically ui with xml files but resualt is not suitable.
like inflater not set layout params?
my sample code:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.linearlayoutdef, null);
ImageView iv = (ImageView) inflater.inflate(R.layout.imageviewdef, null);
iv.setImageBitmap(op.getImage());
TextView tv = (TextView) inflater.inflate(R.layout.textviewdef, null);
//tv.setText(op.getType().toString());
tv.setText("Call");
Button be = (Button) inflater.inflate(R.layout.buttondef, null);
be.setText("Edit");
Button bd = (Button) inflater.inflate(R.layout.buttondef, null);
bd.setText("Delete");
ll.addView(iv);
ll.addView(tv);
ll.addView(be);
ll.addView(bd);
my sample xml files:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/linearLayout" >
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="linearLayout" >
<item name="android:orientation">horizontal</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">40pt</item>
<item name="android:background">#color/white</item>
<item name="android:layout_marginLeft">3pt</item>
<item name="android:layout_marginRight">3pt</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/imageViewStyle" android:contentDescription="#string/todo"
android:src="#drawable/stop"
>
</ImageView>
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="imageViewStyle" parent="#android:style/Widget">
<item name="android:layout_width">30pt</item>
<item name="android:layout_height">30pt</item>
<item name="android:layout_gravity">center</item>
<item name="android:layout_marginLeft">3pt</item>
<item name="android:background">#drawable/imageview_background</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<shape android:shape="rectangle" >
<stroke android:width="0.9pt" android:color="#808080"/>
</shape>
</item>
</layer-list>
witch result is:
but right result must be :
what can i do to get right result?
You need to set layout params to each view you add to your view group.
Example :
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
view.setParams(params);
I want to create round corner button. This is what I tried:
I have defined the button's background drawable button_bg.xml :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#color/grey">
<shape>
<solid
android:color="#ef4444" />
<stroke
android:width="1dp"
android:color="#992f2f" />
<corners
android:radius="16dp" />
</shape>
</item>
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#color/dark_grey"
>
<shape>
<solid
android:color="#ef4444" />
<stroke
android:width="1dp"
android:color="#992f2f" />
<corners
android:radius="16dp" />
</shape>
</item>
<item
android:state_enabled="true"
android:drawable="#color/blue"
>
<shape>
<solid
android:color="#ef4444" />
<stroke
android:width="1dp"
android:color="#992f2f" />
<corners
android:radius="16dp" />
</shape>
</item>
</selector>
Then, I apply it to my button in layout file:
<Button
android:text="#string/press_me"
android:layout_margin="12dp"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:background="#drawable/button_bg"
/>
But my button is still showing sharp corner instead of the round one, why??
(Please don't just throw me a link about how to make round corner, my above code followed those tutorials, I just want to know where am I wrong. Thanks)
remove android:drawable from the item node have it like this.
<item android:state_pressed="true"
android:state_enabled="true">
EDIT : (How can I specify the color from colors.xml for item then?)
Create a stateful color drawable for your button text color.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:color="#ffffff" />
<item android:state_focused="true" android:state_pressed="true" android:color="#000000" />
<item android:state_focused="false" android:state_pressed="true" android:color="#000000" />
<item android:color="#ffffff" />
</selector>
and then set this as your text color for your button .
android:textColor="#drawable/button_text_color"
Remove all android:drawable from item as below then it will work.
When you add the attribute android:drawable in item then this drawable overlapped your created shape. That's why you can't see your rounded shape.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"><shape>
<solid android:color="#ef4444" />
<stroke android:width="1dp" android:color="#992f2f" />
<corners android:radius="16dp" />
</shape></item>
<item android:state_enabled="true" android:state_pressed="true"><shape>
<solid android:color="#ef4444" />
<stroke android:width="1dp" android:color="#992f2f" />
<corners android:radius="16dp" />
</shape></item>
<item android:state_enabled="true"><shape>
<solid android:color="#ef4444" />
<stroke android:width="1dp" android:color="#992f2f" />
<corners android:radius="16dp" />
</shape></item>
</selector>
I made a general class for plural elements for JAXB.
#XmlTransient
public abstract class Plural<S> {
#XmlAnyElement(lax = true)
private Collection<S> singulars;
}
With following classes,
#XmlRootElement
public class Item {
//#XmlValue // problem with xsi:nill and ""
#XmlElement(nillable = true)
private String name;
}
#XmlRootElement
public class Items extends Plural<Item> {
}
When I declare the name as #XmlEement(nilalble = true), it works fine. But #XmlValue makes some problem between xsi:nil and "".
Is there any way to set nillable = true on #XmlAnyElement?
UPDATE ----------------------------------------------------------
When Item#name annotated with #XmlElement, following XML marshalls and unmarshalls successfully.
<items xmlns="http://jinahya.googlecode.com/xml/bind/test">
<item id="-4939700912221365683">
<name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</item>
<item>
<name>name</name>
</item>
<item>
<name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</item>
<item id="-8544902644460968391">
<name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</item>
<item id="525642804765733165">
<name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</item>
</items>
When Item#name annotated with #XmlValue, following output XML marshalls and unmarshals but failed on equality testing.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<items xmlns="http://jinahya.googlecode.com/xml/bind/test">
<item id="7812630870400466385">name</item>
<item>name</item>
<item id="-1067677982428177088"/>
<item id="5609376399324841172">name</item>
<item id="-4755856949417090129"/>
</items>
Which seems xsi:nil omitted on each item element. So Item#equals failed on each Items. The "" without xsi:nill parsed as empty element witch each came from nulls. Is it just #XmlValue's problem? #Blaise Doughan I learned about #XmlAnyElement(lax = true) from your blog entry anyway. Thanks.
And here comes what I want it be.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<items xmlns:xsi="..." xmlns="http://jinahya.googlecode.com/xml/bind/test">
<item id="7812630870400466385">name</item>
<item>name</item>
<item id="-1067677982428177088" xsi:nil="true">
<item id="5609376399324841172">name</item>
<item id="-4755856949417090129" xsi:nil="true">
</items>
I want to create a shadow for my custom dialog is that possible ?
GhazalActivity.public void viewShareMenu() {
Dialog share=new Dialog(this,R.style.shareDialogStyle);
share.setContentView(R.layout.share_popup_layout);
LayoutParams params = share.getWindow().getAttributes();
params.y = this.getResources().getDimensionPixelSize(R.dimen.topbar_height);
params.gravity=(Gravity.RIGHT|Gravity.TOP);
share.getWindow().setAttributes(params);
share.show();
}
styles.xml :
<style name="shareDialogStyle" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">#color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:fadeEnabled">true</item>
<item name="android:fadeDuration">1500</item>
<item name="android:shadowColor">#color/temp</item>
<item name="android:shadowDx">0</item>
<item name="android:shadowDy">5</item>
<item name="android:shadowRadius">10</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
share_popup_layout.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="150dip"
android:background="#color/bg_Ghazal_share_menu"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
style="#style/shareDialogButtons"/>
</LinearLayout>
is there any solution for doing that ?
I have created my own custom dialog with a dropped shadow, you can use it as your desire, in the first step you should create a Android shape for both shadow and dialog frame. Here is what I have supplied (dialog_frame_shadow.xml):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle"> <!-- this shape is used as shadow -->
<padding android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp"/>
<solid android:color="#44000000"/>
<corners android:radius="5dp"/>
</shape>
</item>
<item>
<shape android:shape="rectangle"> <!-- this is for dialog frame -->
<corners android:radius="5dp"/>
<stroke android:color="#ff272727" android:width="2dp" />
<gradient android:angle="90"
android:startColor="#ffa7a7a7"
android:centerColor="#ff6a6a6a"
android:endColor="#ffa7a7a7"
android:type="linear"/>
</shape>
</item>
</layer-list>
In the next step you should change your dialog theme as what is in the following:
<style name="my_dialog_theme">
...
<item name="android:windowBackground">#drawable/dialog_frame_shadow</item>
...
</style>
Now you are done, just create a new instance of the Dialog class and apply this theme to it (in Dialog constructor):
Dialog dialog = new Dialog(this, R.style.my_dialog_theme);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.mdialog);
dialog.show();
Here is its screenshot:
try this, create an xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#ffffff"
android:centerColor="#d3d7cf"
android:endColor="#2e3436"
android:angle="90" />
</shape>
make a View and set above xml to its backgroung like:
<View android:id="#+id/divider" android:background="#drawable/black_white_gradient"
android:layout_width="match_parent" android:layout_height="5sp"
<!--greater the height, more wider the shadow-->
/>
now if you want to drop shadow to the left of your view, align this view to you views left, if you want shadow on bottom, align this view under you view ...hope you get it now