ActionBar doesn't have the same view for different devices - java

There is the folliwing problem: I've made some Android app and there are 3 menu items (1 dropdown and 2 simple items):
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:icon="#android:drawable/ic_menu_sort_by_size"
android:showAsAction="ifRoom"
android:orderInCategory="100"
android:title="СSort">
<menu>
<item
android:id="#+id/girls_action_all"
android:title="All"/>
<item
android:id="#+id/girls_action_best"
android:title="Best"/>
<item
android:id="#+id/girls_action_new_people"
android:title="New users today"/>
<item
android:id="#+id/girls_action_50_plus"
android:title="Top 50"/>
<item
android:id="#+id/girls_action_100_plus"
android:title="Top 100"/>
</menu>
</item>
<item
android:id="#+id/girls_action_home"
android:icon="#drawable/ic_menu_home"
android:orderInCategory="101"
android:showAsAction="ifRoom"
android:title="My page"/>
<item
android:id="#+id/girls_action_search"
android:icon="#android:drawable/ic_menu_search"
android:orderInCategory="102"
android:showAsAction="ifRoom"
android:title="Search"/>
</menu>
But one item ("Search") doesn't appear on the Action Bar if I use some devices; it appears in the Menu (after click by "Menu" button). I don't like it. Is there any way to fix it?

Try to implement
<item
android:id="#+id/girls_action_search"
android:icon="#android:drawable/ic_menu_search"
android:orderInCategory="102"
android:showAsAction="always"
android:title="Search"/>

Change android:showAsAction="ifRoom" to android:showAsAction="always" for the search menu item.
android:showAsAction="ifRoom" will do as it is told - it will display the menu in the action bar if there is enough room; if not, it will overflow to the menu. Currently, all your menu items have this flag set and will compete for the limited space.
android:showAsAction="always" will force the item to be displayed in the action bar. This may cause them to overlap with other UI items if there is not enough room.
Or set android:showAsAction="never" for some of the other items that you don't want in the action bar.

Related

How to hold the overflow menu after I click it?

I have added some checkboxs in my overflow menu. I want my overflow menu to hold rather than disappear once I click the checkbox in the overflow menu. How can I do that? Thanks for help.
This is my menu xml file.
<item
android:id="#+id/action_check"
android:title="#string/action_check"
android:orderInCategory="1"
app:showAsAction="never"
android:visible="true"
android:checkable="true"/>
<item android:id="#+id/notification"
android:orderInCategory="2"
android:title="#string/notification"
app:showAsAction="never"
android:visible="true"
android:checkable="true"/>
<item android:id="#+id/about"
android:orderInCategory="3"
android:title="#string/about"
app:showAsAction="never"></item>
This is how i did it.
Add following code in your activity where option menu is implemented.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//your checking other stuff
item.setChecked(!item.isChecked());
//main part for holding onto the menu
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
item.setActionView(new View(this));
item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return false;
}
});
return false;
}
By adding the line: item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); I am marking the item as it has expandable/collapsible behavior so it will call the setOnActionExpandListener.
This line here: item.setActionView(new View(this)); is a view when item is in expanded state.It is just a dummy view because we will never let it expand how i am going to explain next.
You see that i am returning false from both the methods of setOnActionExpandListener to suppress expansion and collapsing of the item so the view we gave in previous step would never show up and menu would remain open.
Following would be your menu file:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="all">
<item
android:id="#+id/action_check"
android:orderInCategory="1"
android:title="Title 1"
app:showAsAction="never" />
<item
android:id="#+id/notification"
android:orderInCategory="2"
android:title="Title 2"
app:showAsAction="never" />
<item
android:id="#+id/about"
android:orderInCategory="3"
android:title="Title 3"
app:showAsAction="never" />
</group>
</menu>
Notice the line group android:checkableBehavior="all" is to tell that all items in the group will have checkable behavior so that you don't have to write checkable in each item.
group is a collection of menu items that shares certain traits, for more information see
<group android:id="#+id/group>
<item
android:id="#+id/action_check"
android:title="#string/follow"
android:orderInCategory="1"
app:showAsAction="never"
android:visible="true"
android:checkable="true"/>
<item android:id="#+id/notification"
android:orderInCategory="2"
android:title="#string/notification"
app:showAsAction="never"
android:visible="true"
android:checkable="true"/>
<item android:id="#+id/about"
android:orderInCategory="3"
android:title="#string/about_us"
app:showAsAction="never"/>
</group>

android : spinner prompt not showing in API >23

I'm making an android app, and I'm using Spinners in Dialog Mode, defined as follows :
<Spinner android:id="#+id/new_order_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/SpinnerTheme"
style="#style/UnderlinedSpinner"
android:layout_below="#+id/new_order_hint_address"
android:prompt="#string/myString"
android:spinnerMode="dialog"
android:layout_marginBottom="5dp" />
where the styles are
<style name="SpinnerTheme">
<item name="colorControlNormal"> #color/colorPrimaryDark </item>
<item name="android:textColorSecondary"> #color/colorPrimaryDark </item>
<item name="android:colorControlHighlight"> #color/colorPrimaryDark </item>
<item name="android:colorControlActivated"> #color/colorPrimaryDark </item>
<item name="android:minHeight"> 35dp </item>
<item name="android:showDividers"> middle </item>
<item name="android:divider"> #color/colorAccent </item>
<item name="android:dividerHeight"> 0.5dp </item>
</style>
<style name="SpinnerLabel" parent="TextAppearance.Design.Hint">
<item name="android:paddingLeft">#dimen/input_label_horizontal_spacing</item>
<item name="android:paddingRight">#dimen/input_label_horizontal_spacing</item>
<item name="android:textSize"> 14sp </item>
<item name="android:textColor">#color/hintText</item>
</style>
<style name="UnderlinedSpinner" parent="Base.Widget.AppCompat.Spinner.Underlined"/>
In API <23, everything works fine, but in API 23, the prompt is not shown. I've tried setting in from the java code, I've read all the similar questions and answers and have had no luck whatsoever.
Thanks in advance
Ok, after a lot more of trial and error, I found out that the issue was in fact that the color of the prompt text was the same as the color of the background of the prompt (both white).
The reason behind it relays on the "textColorPrimary", which I'm not setting in the style of the spinner, but I did set in the style of the Tabhost where it is located. It looks like it inherited this property.
Therefore, the solution is just adding that parameter to the style, with the color desired.
The only thing I'm not sure is why this only happens in APIs > 23, and not the others.

Showing alert like menu alert in android

I like to show alert like this:
And when I click the first settings icon, show an alert like this one:
To achive that you have to group the subitems and embed in a one menu item.
Here is an example you can refer.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/id_group1"
android:icon="#drawable/v3_actionbar_setting"
android:orderInCategory="120"
android:showAsAction="always"
android:title="#string/action_settings">
<menu>
<item
android:id="#+id/subitem1"
android:icon="#drawable/actionbar_settings"
android:title="subitem 1"/>
<item
android:id="#+id/subitem2
android:icon="#drawable/image2"
android:title="subitem 2">
</item>
</menu>
</item>
<item
android:id="#+id/group2"
android:icon="#drawable/group2"
android:orderInCategory="90"
android:showAsAction="always"
android:title="group 2">
<menu>
<item
android:id="#+id/subitem21"
android:icon="#drawable/subitem1"
android:title="subitem1"/>
<item
android:id="#+id/subitem22"
android:icon="#drawable/sub2"
android:title="sub2"/>
</menu>
</item>
Hope this helps :)
Take a good look at this: Android Menus
You'll possibly find all you need.

Change button color when clicked

OK, when I use setBackground on a button, the button no longer shows any feedback when its clicked. How can I fix this. I just want the button to get darker or some type of feedback so the user knows it was clicked.
Hope this makes sense!
Create this drawable and bind it to button background
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/YOURIMAGE" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/gradient" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/gradient" />
<item android:drawable="#drawable/YOURIMAGE" />
</selector>
Apply it to button
android:background="#drawable/button"
You need to create a selector inside the XML and have it linked to state_pressed = "true". Inside of the item, you can specify the shape of the object. I've linked to the Android developer site so you can see other options available as well.
<item android:state_pressed="true">
<shape>
<solid android:color="#73E5E4D7" />
</shape>
</item>
<item>
<shape>
<solid android:color="#E6E5E4D7" />
</shape>
</item>
Android Developer State List
hope this works well. first assign your button into a button type reference in your activity class.
ex: newBtn=(Button) findViewById(R.id.yourButton);
after that set an listener to that button.
yourButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
yourButton.setBackgroundColor(int colorCode);
}
});

ActionBar text's background color not changing

I put an ActionBar into my app and changed the background color, as I'll show below. Oddly enough, the color behind the text on the ActionBar won't change. (picture)
I looked around and found this:
<item name="android:actionBarItemBackground">#null</item>
But I would prefer not to use it because then my minSdk has to be set to 14, whereas I have it at 11 now. I did try boosting it up to 14 to see if this tag would work, but I again couldn't get the color to change.
Does anyone have any experience with this? Many thanks in advance for your help.
Styles.xml:
<style name="AppBaseTheme" parent="Theme.AppCompat">
</style>
<!-- Application theme. -->
<style name="MyAppTheme" parent="AppBaseTheme">
<item name="android:colorBackground">#color/background</item>
<item name="android:background">#color/background</item>
<item name="android:textColor">#color/myWhite</item>
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar" parent="#style/Widget.AppCompat.ActionBar">
<item name="android:background">#drawable/actionbar_background</item>
<item name="actionMenuTextColor">#color/myWhite</item>
<!--Support Library compatibility-->
<item name="background">#color/myBlack</item>
</style>
drawable/actionbar_background:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/myBlack" />
</shape>
menu/login.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom" />
<item android:id="#+id/action_settings"
android:title="#string/settings_label"
android:showAsAction="never" />
</menu>
And finally, inside the LoginActivity class, the onCreateOptionsMenu method:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return super.onCreateOptionsMenu(menu);
}
Thanks again!
I figured it out. Turns out I was using the wrong attribute -- instead of using android:background in the styles.xml file, you need to use android:windowBackground. android:background changes all sorts of other things, too.

Categories

Resources