I am trying to inflate a menu when selecting items from the list for multiple deletion, but instead I get a new menu bar on top of the current one.
http://imgur.com/XRAEnBM.jpg
I used the default navigation drawer activity and then made my fragments.
I am using listView.setMultiChoiceModeListener this is my code
Java Code:
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.delete, menu);
return true;
}
XML Code in menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/delete_id"
android:title="Delete"
android:icon="#drawable/ic_action_delete"/>
</menu>
The Java code happens in on onResume() in one of my fragments.
I am a beginner so please be kind thanks :)
Here is the solution, just add these two lines in your stlyes
<item name="windowActionModeOverlay">true</item>
<item name="actionModeBackground">#color/colorPrimary</item>
Related
I am writing an app, where in a game a score is counted and displayed.
Now I would like to give the opportunity to switch ON/Off the speakers. I think, to use an ImageButton should be the solution.
But how do I "dim" the button, when speaker is Off, but the button has to remain active. invisibility or disable is not the solution. Is ist best, to change the Image in the button?
Does anybody has an example for a switch On/Off button please.
I am working on AndroidStudio4.1 and Kotlin
Is better to have a checkbox, because that view handles true/false states by default. You have to create a drawable for the button and then set it as a button.
Let's say your drawable is cb_selector_sound
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_sound_on" android:state_checked="true" />
<item android:drawable="#drawable/ic_sound_off" android:state_checked="false" />
</selector>
and then use it in the button like this:
<CheckBox
android:id="#+id/soundCheckbox"
android:button="#drawable/cb_selector_sound"
.../>
So then you can listen the checkbox changing
//find the view
soundCheckbox.setOnCheckeChangedListener {_, isChecked ->
if (isChecked) {
//turn sound on
} else {
//turn sound off
}
}
So the view worries about the appearance and you worried about the logic
I need to change the background color of my entire tab layout dynamically through code in my app. I just need to know the correct line to type. I don't need to change the selected tab or anything. I want to leave the style and text colors and the indicator line as is. I just want to change the background of the entire tab layout dynamically in code. Thank you for your help.
I have already tried the below line of code. Didn't work. Also included my TabLayout XML.
private TabLayout mTabLayout;
mTabLayout = (TabLayout) findViewById(R.id.tabs);
mTabLayout.setBackgroundColor
(this.getResources().getColor(R.color.dark_grey));
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
style="#style/CategoryTab"
android:layout_width="match_parent"
android:layout_height="#dimen/activity_generic_margin_28"
app:tabBackground="#color/app_bar"/>
Thank you for your help.
I solved it by checking the boolean that I have set for night theme prior to setting the content view in onCreate.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences nightModeSwitchState = getSharedPreferences("NightModeSwitchState", 0);
mNightModeSwitchState = nightModeSwitchState.getBoolean("NightModeSwitchState", false);
if (mNightModeSwitchState) {
setContentView(R.layout.activity_book_night);
} else {
setContentView(R.layout.activity_book);
}
I have two layouts made both include tabLayouts. One of them references a style file for tabLayout set to night mode colors with the dark background and the layout references a style file set to regular colors for the tablayout.
<style name="CategoryTab" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">#android:color/white</item>
<item name="tabSelectedTextColor">#android:color/white</item>
<item name="tabTextAppearance">#style/CategoryTabTextAppearance</item>
<item name="tabBackground">#drawable/tab_regular_theme</item>
<item name="android:textColorSecondary">#android:color/white</item>
</style>
<style name="CategoryTabNight" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">#android:color/white</item>
<item name="tabSelectedTextColor">#android:color/white</item>
<item name="tabTextAppearance">#style/CategoryTabTextAppearance</item>
<item name="tabBackground">#drawable/tab_layout_dark_mode</item>
<item name="android:textColorSecondary">#android:color/white</item>
</style>
The style files reference different drawables that control the background colors as you can see above.. and here are the drawables...
Here is night mode drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/dark_grey" android:state_selected="true"/>
<item android:drawable="#color/dark_grey"/>
</selector>
Here is regular mode drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/app_bar" android:state_selected="true"/>
<item android:drawable="#color/app_bar"/>
</selector>
Everything else is the layouts are identical as not to mess anything up just the style files for the tablayouts are changed. I hope this makes sense. I tested it and it works for me. I have tried so many other things and nothing worked. It is important to call Shared Preference to get the value before checking the boolean. I hope this helps someone.
I hope to find answer of this question and any help will be appreciated,
when i use blank\empty activity the icon of the menu item doesn't appear !
but using basic activity it works fine .
thanks in advance .
this is the menu xmlcode
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/one"
android:icon="#drawable/images"
android:showAsAction="always"
android:title="one"
/>
its works as i said for basic Activity but its not for empty\blank Activity
for the appearance of the menu icon
xmlns:app="http://schemas.android.com/apk/res-auto" put this in menu before your item tag and use 'app:' instead of 'android:' at "showAsAction" LIKE
use this:
app:showAsAction="always"
instead of
android:showAsAction="always"
scratching my head. if someone could help that would be great. I am switching from extends ActionBarActivity to extends AppCompatActivity with Toolbar and need help displaying items on toolbar.
I am using extends AppCompatActivity included android.support.v7.app.AppCompatActivity; then inside onCreate() have:
Toolbar jobListToolbar = (Toolbar) findViewById(R.id.job_list_toolbar);
setSupportActionBar(jobListToolbar);
then in xml file I have
<android.support.v7.widget.Toolbar
android:id="#+id/job_list_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
then in menu file I have items and everything seems to work, however not the way I want.
Toolbar/ActionBar is displayed on top of the screen, but no menu items showing on it. If I press phone original menu button then onCreateOptionsMenu(Menu menu) is initiated for the first time, and menu is showing. But it shows just above this phone button. But I want those buttons to show on Toolbar/ActionBar on top of the screen. I read somewhere that if phone has hardware menu button it will not show menu items in Toolbar/ActionBar, but I had it somehow done with extends ActionBarActivity, but there should be a way to do this with extends AppCompatActivity. Need to move to newer recommended way by google, but not sure how to show items on that Toolbar/ActionBar on the top of the screen.
After a day of research on how to switch to AppCompat Toolbar finally found the solution. Two must do steps (No 2 being the one that nobody talks about):
I.) couple good resources on how to set up Toolbar can be found in this video Toolbar Tutorial and also in this StackOverflow: toolbar button
II.) MAKE SURE YOU PLACE THIS CODE AT THE BOTTOM OF onCreate():
toolbar= (Toolbar) findViewById(R.id.job_list_toolbar);
setSupportActionBar(toolbar);
if(getSupportActionBar()!=null) {
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
My problem was that I had some code in onCreate() that modified the toolbar buttons. That code needed to be run before toolbar was opened. If only first two lines of the code above included toolbar will show, but no menu will be inflated and no buttons. When I called getSupportActionBar().setDisplayShowHomeEnabled(true); thats when onCreateOptionsMenu(Menu menu) is called (if no true enabled it is not called), also make sure you have getMenuInflater().inflate(R.menu.job_list, menu); inside onCreateOptionsMenu(Menu menu).
If the code that calls for menu to open getSupportActionBar().setDisplayShowHomeEnabled(true); is not at the end of onCreate() you might be scratching your head why the menu is not showing. So make sure you execute your relevant onCreate() code before you call the menu to open.
MyActivity:
extends AppCompatActivity
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarhome);
setSupportActionBar(toolbar);
activity_my.xml: If you want you can delete the last line 0 ep. They are the starting point of 0 to launching toolbar
<android.support.v7.widget.Toolbar
android:id="#+id/toolbarhome"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:titleTextAppearance="#style/Toolbar.TitleText"
app:layout_collapseMode="pin"
android:background="#color/colorPrimary"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetRight="0dp"
android:contentInsetEnd="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp" >
//designed the way you like
</android.support.v7.widget.Toolbar>
manifest:
<activity
android:name=".MyActivity"
android:theme="#style/AppTheme.NoActionBar"/>
style:
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"> </style>
<style name="Toolbar.TitleText" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
<item name="android:textSize">20sp</item>
<item name="android:textStyle">italic</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
i am using appcompat for my actionbar for 2.3。
i want collapseActionView when i click the options but not display at first。
![enter image description here][1]
As the img shows ,the menus do not show icons.
Could somebody help me .tks very much.
here are my codes.
<?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"
xmlns:tools="http://schemas.android.com/tools" >
<item
android:id="#+id/zhuangtai"
android:icon="#drawable/fa"
android:showAsAction="collapseActionView"
android:title="发状态"
app:showAsAction="collapseActionView"/>
<item
android:id="#+id/er"
android:icon="#drawable/sao"
android:showAsAction="collapseActionView|withText"
android:title="扫一扫"
app:showAsAction="collapseActionView|withText"/>
<item
android:id="#+id/sui"
android:icon="#drawable/sui"
android:showAsAction="collapseActionView|withText"
android:title="随份子"
app:showAsAction="collapseActionView|withText"/>
<item
android:id="#+id/haoyou"
android:icon="#drawable/haoyou"
android:showAsAction="collapseActionView|withText"
android:title="好友列表"
app:showAsAction="collapseActionView|withText"/>
</menu>
sorry for my english.
The android:showAsAction introduced in API Level 11. In this case you are using Api level 9. Read this tutorial. Its helps you.
collapseActionView The action view associated with this action item (as declared by android:actionLayout or android:actionViewClass) is collapsible.
Introduced in API Level 14.
So, one simple way is to remove collapseActionView