Default ActionBar with Settings button - java

Although I am new in android developing, lately, I have been trying to add a settings button in the default ActionBar that Android Studio provides (as shown in the picture below). However, all the tutorials I have stamped on start with changing the app Theme to: android:theme="#style/Theme.APpCompat.Light.NoActionBar"
Is there any way I can avoid changing Theme and just add that setting about in the default ActionBar?

You can do this by inflating your menu to the toolbar.
Override this method in your java class.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.your_custom_menu,menu);
return true;
}
You custom menu should look something like this:
<?xml version="1.0" encoding="utf-8"?>
>
<item
android:id="#+id/night_mode"
android:title="?attr/theme_text"
app:showAsAction="never"
android:icon="?attr/theme_icon"/>
<item
android:id="#+id/action_about"
android:title="#string/about_text"
app:showAsAction="never"
android:icon="?attr/about_icon"/>
<item
android:id="#+id/action_logout"
android:title="#string/action_logout_text"
app:showAsAction="never"
android:icon="?attr/logout_icon"/>
Here's something more about menus: https://developer.android.com/guide/topics/ui/menus

Related

Style applied to an AlertDialog not working correctly

I've been asked to match the look of an Alert Dialog in our app to the one used by the app's theme.
I managed to apply a style to all Alert Dialogs in the app using it as part of the app's theme, but there are situations where the style is not applying correctly.
It happens for example when the Alert Dialog contains a 'Single Choice List' as its' message.
The title looks fine, so is the background and the button bar, but the list itself is problematic.
At first, the radio buttons as well as their textual description were black colored, as if android is using the default color.
I somehow managed to color the radio buttons as I wish, by using these attributes:
<item name="android:colorControlNormal">#color/text_secondary</item>
<item name="android:colorControlActivated">#color/text_secondary</item>
But the text color still remains black, and I've tried EVERY possible text color attribute exposed by android.
It looks like this:
Now this is the full style defined for the Alert Dialogs:
<style name="GenericAlertDialog.Alter" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowMinWidthMajor">#android:dimen/dialog_min_width_major</item>
<item name="android:windowMinWidthMinor">#android:dimen/dialog_min_width_minor</item>
<item name="android:windowTitleStyle">#style/DialogTitle</item>
<item name="android:textColor">#color/text_secondary</item>
<item name="android:textColorPrimary">#color/primary</item>
<item name="android:background">#color/window_background</item>
<item name="android:colorAccent">#color/accent</item>
<item name="android:textColorAlertDialogListItem">#color/text_secondary</item>
<!--<item name="android:textColorSecondary">#color/text_secondary</item>-->
<item name="android:colorControlNormal">#color/text_secondary</item>
<item name="android:colorControlActivated">#color/text_secondary</item>
</style>
This is my Theme definition:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowBackground">#color/window_background</item>
<item name="android:colorPrimary">#color/primary</item>
<item name="android:colorAccent">#color/accent</item>
<item name="android:textColorPrimary">#color/text_primary</item>
<item name="android:textColorSecondary">#color/text_secondary</item>
<item name="android:textColorHint">#color/text_hint</item>
<item name="android:buttonStyle">#style/GenericButton</item>
<item name="android:checkboxStyle">#style/GenericCheckBox</item>
<item name="android:alertDialogTheme">#style/GenericAlertDialog</item>
<item name="alertDialogTheme">#style/GenericAlertDialog</item>
</style>
This is the code I'm using to create a custom Alert Dialog:
AlertDialog.Builder dialogBuilder = null;
try
{
dialogBuilder = new AlertDialog.Builder(i_OwnerActivity, R.style.GenericAlertDialog_Alter);
LayoutInflater layoutInflater = i_OwnerActivity.getLayoutInflater();
// Inflate the dialog's custom title view and set it's text to the matching one to this class
View customTitleView = layoutInflater.inflate(R.layout.dialog_title, null);
TextView customTitleTextView = (TextView) customTitleView.findViewById(R.id.DialogTitleText);
// Set text of customTitleView
dialogBuilder.setCustomTitle(customTitleView);
// Create an event handler for clicking on the negative button
dialogBuilder.setNegativeButton(R.string.action_dialog_negative_cancel, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface i_Dialog, int i_Which)
{
// Do Something
}
});
} catch (Exception e)
{
LogUtils.logException(AlterDialogUtils.class, e);
}
return dialogBuilder;
And finally, here's the code I'm using to create an Alert Dialog with a 'Single Choice List':
dialogBuilder.setSingleChoiceItems(R.array.squelch_modes, m_InitialState, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
// Do Something
}
});
What am I doing wrong? How can I change the color of the text?
It is also worth saying that I'm using AppCompat's AlertDialog.
Just found this old post with Google, but since there is no answer I will add what did the trick in my case:
remove the android: prefix from the textColorAlertDialogListItem in your alertdialog style
<item name="textColorAlertDialogListItem">#color/text_secondary</item>
I guess that's because of the parent being an AppCompat theme, but I am not sure about this. I still added both (with and without prefix) in my style...
I know it's probably late... but here's your answer: android.support.v7.app.AlertDialog in AppCompat support library use this layout by default (unless you provide your own adapter) for singleChoiceDialog:
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="?attr/textColorAlertDialogListItem"
android:gravity="center_vertical"
android:paddingLeft="#dimen/abc_select_dialog_padding_start_material"
android:paddingRight="?attr/dialogPreferredPadding"
android:paddingStart="#dimen/abc_select_dialog_padding_start_material"
android:paddingEnd="?attr/dialogPreferredPadding"
android:drawableLeft="?android:attr/listChoiceIndicatorSingle"
android:drawableStart="?android:attr/listChoiceIndicatorSingle"
android:drawablePadding="20dp"
android:ellipsize="marquee" />
The attribute used to set this up in the theme is singleChoiceItemLayout so you can override it with your own layout to get whatever UI you want.
If you just want to change the text color just define the attribute textColorAlertDialogListItem as you can see from the layout it's the one used for android:textColor.
In general when I need something like this i go and look at the source code since it is available. The support libraries source code can be found here, while most of the framework source code can be find here.

How to change the selected item's background color in android navigation drawer?

I am using Navigation Drawer (using Navigation Drawer Activity) in android studio and its working fine.
But I want to change the Background Color of selected item in Drawer's item List. I want to change default Color to my custom Color.
Any idea ???
Create another xml layout under res ⇒ drawable named list_item_bg_pressed.xml with following content.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"><gradient
android:startColor="#color/list_background_pressed"
android:endColor="#color/list_background_pressed"
android:angle="90" />
Create Selector
<item android:drawable="#drawable/list_item_bg_normal" android:state_activated="false"/>
<item android:drawable="#drawable/list_item_bg_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/list_item_bg_pressed" android:state_activated="true"/>
For more info see this link Sliding drawer

Changing theme of Android Application via CheckBox onClick

I am a new android developer and I am working on a project where I want to have custom color themes as an option in the settings in one of my apps. I have four check boxes, call them checkBox1, checkBox2, checkBox3, checkBox4. I also have four custom styles in my styles.xml file, call them theme1, theme2, theme3, theme4. Here is my .java file coding regarding handling the onClick of these CheckBoxes:
checkBoxListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(blueCheckBox.isChecked()){
//Change theme to blue
}
if(redCheckBox.isChecked()){
//Change theme to red
}
if(greenCheckBox.isChecked()){
//Change theme to green
}
if(bwCheckBox.isChecked()){
//Change theme to Black & White
}
}
};
blueCheckBox.setOnClickListener(checkBoxListener);
redCheckBox.setOnClickListener(checkBoxListener);
greenCheckBox.setOnClickListener(checkBoxListener);
bwCheckBox.setOnClickListener(checkBoxListener);
}
How can I set the style of my entire application to the corresponding custom declared styles I have in the body's of these if statements? Thank you for your time, I appreciate any help you can offer.
for this you have to create some themes under /style/
For Example :
<resources>
<style name="LightTheme" parent="#android:style/Theme.Light">
</style>
<style name="BlackTheme" parent="#android:style/Theme.Black">
</style>
</resources>
and at java side you can use it like...
if (lightThemeCheckBox.isChecked()) {
getApplication().setTheme(R.style.LightTheme);
} else {
getApplication().setTheme(R.style.BlackTheme);
}
i hope this will help you.
for more visit this link:
http://www.anddev.org/applying_a_theme_to_your_application-t817.html
Hi You are able to supply the button for Checkbox by set the Background for the CheckBox as Follows :
Below the Code is for backgound xml file for Checkbox: Name as checkbox_selection.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/check_active_320" android:state_checked="true" android:state_focused="true"/>
<item android:drawable="#drawable/check_inactive_320" android:state_checked="false" android:state_focused="true"/>
<item android:drawable="#drawable/check_inactive_320" android:state_checked="false"/>
<item android:drawable="#drawable/check_active_320" android:state_checked="true"/>
Here You found that there was having the drawable resource such as check_active_320 when User checked on CheckBox and check_inactive_320 for Checkbox which is not checked.
the code to set the above xml to CheckBox as Follows:
<CheckBox
android:id="#+id/radioread"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textread"
android:layout_alignBottom="#+id/textread"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:button="#drawable/checkbox_selection"
android:focusable="false" />
Here You can find the android:button as the value of our style.

DrawerLayout on top of Actionbar

When using the drawer layout is there a way to overlay the drawer view over the action bar? I do not want to hide the action bar when the drawer is shown. I want the action bar to simply stay put, but be sent to the background. An example of this is the iOS Play Music app...
My current implementation hides and shows the action bar when the drawer state changes, but I do not like this user experience.
public void onDrawerClosed(View view) {
getActionBar().show();
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().hide();
invalidateOptionsMenu();
}
I searched the web in order to find any good solution for this problem and haven't found. So I did a trick that did this.
First of all we need to request action bar overlay feature. So in onCreate() of your activity, before setContntView() call: requestWindowFeature(com.actionbarsherlock.view.Window.FEATURE_ACTION_BAR_OVERLAY);
It will make everything including navigation drawer draw behind action bar. We don't need this, so we need to set the top margin of our FrameLayout which hosts our fragments in the activity with the exact height of the action bar. In the layout file of the activity we need to do the following:
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize" />
It will make only navigation drawer appear behind the action bar.
Now we will hide the action bar when the navigation drawer is opened on half, and will show it when the drawer is nearly closed. To do this we need to make the following in the activity:
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
if(slideOffset > 0.5){
actionBar.setBackgroundDrawable(null);
actionBar.hide();
} else {
actionBar.show();
if(slideOffset < 0.1){
actionBar.setBackgroundDrawable(layerDrawable);
}
}
}
As you can see I also change the background drawable of the action bar to make it transparent when before I begin to hide it, and change it back with my custom background when I show it back.
My custom background is a layerListDrawable which is all transparent but have a divider in bottom with some shadow.
And to achieve this I have defined the following layer-list in XML:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="0dp" android:left="0dp" android:bottom="0dp" android:right="0dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/transparent"/>
</shape>
</item>
<item android:top="0dp" android:left="0dp" android:bottom="0dp" android:right="0dp">
<shape android:shape="rectangle">
<solid android:color="#afafaf"/>
</shape>
</item>
<item android:top="0dp" android:left="0dp" android:bottom="0dp" android:right="0dp">
<shape android:shape="rectangle">
<gradient
android:angle="270"
android:startColor="#88afafaf"
android:endColor="#33afafaf"
/>
</shape>
</item>
</layer-list>
And to get the background I need from this XML I do the following in the activity:
final ActionBar actionBar = getSupportActionBar();
final LayerDrawable layerDrawable = (LayerDrawable) getResources()
.getDrawable(R.drawable.shadow_divider);
final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
new int[] { R.attr.actionBarSize });
int topOffset = (int) (styledAttributes.getDimension(0, 0));
styledAttributes.recycle();
layerDrawable.setLayerInset(1, 0, topOffset - 3, 0, 2);
layerDrawable.setLayerInset(2, 0, topOffset - 2, 0, 0);
actionBar.setBackgroundDrawable(layerDrawable);
where R.drawable.shadow_divider is the XML layer-list I have defined earlier.
It looks really great! Hope it can help someone.
EDIT
I had a small bug here which can be a reason of a crush sometimes. Here is the fixed code:
`
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
**android:paddingTop="?attr/actionBarSize"** />`
It should be paddingTop not layout_marginTop!
That's the proper effect in Android defaults. If you want to emulate the iOS effect, you'll probably have to do it yourself, because AFAIK the drawer component of the support library doesn't allow such type of configurations.
Anyway, every time a programmer codes a lot just to emulate some fancy (and much probably not worthy) iOS effect in Android, a little cat dies...
I don't think you'll be able to do this with Android's provided Navigation Drawer (without doing something really hacky), since doing this really goes against Android design patterns. If you want a library that does what you're looking for, I've used this library before Android came out with it's own widget, and it does what you're looking for.
I found the answer in my question, also added an example project for reference. You can take a look to see if it works for you.
Android Navigation Drawer on top ActionBar
You can achieve this with ActionBarSherlock.
http://actionbarsherlock.com/
You will be able to do it but the drawer will look so awkward that you will not like it. I had the same requirement and I ended up using Radio Buttons customized to appear as tabs. You can also try hiding the tabs when the drawer opens and vice versa, but that certainly is not what you want here.
You can try to use ViewPager, tabbed indicator such PagerTabStrip and/or TabPageIndicator.
Source.

LeftNavBar doesn't honor android:showAsAction="always"

I'm using the following menu XML in my project:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/user_cp"
android:icon="#drawable/gear"
android:showAsAction="always"
android:title="#string/usercp"
/>
<item
android:id="#+id/pm"
android:icon="#drawable/sym_action_email"
android:showAsAction="always"
android:title="#string/private_message"
/>
<item
android:id="#+id/refresh"
android:icon="#drawable/ic_menu_refresh"
android:title="#string/refresh"
/>
<item
android:id="#+id/settings"
android:icon="#drawable/ic_menu_preferences"
android:title="#string/settings"
/>
<item
android:id="#+id/logout"
android:icon="#drawable/ic_menu_logout"
android:title="#string/logout"
/>
</menu>
When I my application loads up on Google TV (emulator or an actual device) LeftNavBarLibrary (http://code.google.com/p/googletv-android-samples/source/browse/LeftNavBarLibrary) ignores the android:showAsAction="always" property set for the first two menu items. These items are always placed inside the soft menu. Looking in LeftNavBarLibrary, in OptionsDisplay.java I see this reference to the "Show always" menu options and nothing else:
private void refreshExpandedState() {
// Menu icon.
setOptionExpanded(mView.getChildAt(1), mExpanded);
// "Show always" options.
ViewGroup optionsContainer = getOptionsContainer();
for (int i = 0; i < optionsContainer.getChildCount(); ++i) {
setOptionExpanded(optionsContainer.getChildAt(i), mExpanded);
}
}
Does this feature work with LeftNavBarLibrary or am I stuck implementing it myself? It's critical for this app that I use action items on the ActionBar in certain cases since the buttons are actual actions and not something that can be replaced with a tab.
Edit: Looks like LeftNavBarLibrary doesn't truly honor the options menu at all, it will show the options button regardless of whether or not it needs to.
You should not be able to place action items on LeftNavBar , it would always show as ordinary Option menu item.

Categories