I have the following ActionBar tab in my app. I was wondering what is the best way to change the colors around to match my app.
Each tab has a different background for the contents. How do I add
separate background colors for each tab?
How do I change the light blue strip color to white to give it a 3D
look?
I saw the following code:
ActionBar ab = getActionBar();
//ab.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
But that line changes color for all the tabs to just one color.
The Tab code in my app is:
ActionBar ab = getActionBar();
//ab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff"))); not changing the tab color
//ab.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
ab.setNavigationMode( ActionBar.NAVIGATION_MODE_TABS );
Tab tab = ab.newTab()
.setText( "TY1" )
.setTabListener(
new MyTabListener( this, TY1.class.getName() ) );
ab.addTab( tab );
tab = ab.newTab()
.setText( "TY2" )
.setTabListener(
new MyTabListener( this, TY2.class.getName() ) );
ab.addTab( tab );
tab = ab.newTab()
.setText( "ty3" )
.setTabListener(
new MyTabListener( this, TY3.class.getName() ) );
ab.addTab( tab );
Any and all help is appreciated. I can use XML as well, if someone points me in the right direction.
You can set a custom View for each tab. Create a new layout resource for the tab (it can just be a TextView). Leave its background empty and make a nine-patch drawable for the selection indicator. Get a LayoutInflater using
LayoutInflater inflater = getSystemService(LAYOUT_INFLATER_SERVICE);
Then for each tab, you can do this:
Tab tab = ab.newTab()
.setText("TY1")
.setTabListener(new MyTabListener(this, TY1.class.getName()));
View tabView = inflater.inflate(R.layout.my_tab_layout, null);
tabView.setBackgroundColor(...); // set custom color
tab.setCustomView(tabView);
ab.addTab(tab);
You can change ActionBar's background color by defining custom style, as:
<resources>
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">ANY_HEX_COLOR_CODE</item>
</style>
</resources>
go to: http://jgilfelt.github.io/android-actionbarstylegenerator/#name=example&compat=holo&theme=light&actionbarstyle=solid&texture=0&hairline=0&backColor=E4E4E4%2C100&secondaryColor=D6D6D6%2C100&tabColor=33B5E5%2C100&tertiaryColor=F2F2F2%2C100&accentColor=33B5E5%2C100&cabBackColor=FFFFFF%2C100&cabHighlightColor=33B5E5%2C100
and create your action bar then download it. After you download it , you can use it in your project.
For those looking for more detailed example code on how to create a custom tab view and then change the tab's formatting, you can see my answer at https://stackoverflow.com/a/28389366/3268329. That question had to do with changing the tab text color when a particular tab is selected, but you can easily modify the code to change the background instead or do whatever else you need.
https://stackoverflow.com/a/18491600/3268329 (S.Thiongane's answer) also shows how to set up different background resources.
Related
When trying to change an icon to a downloaded drawable (or actually changing to any other icon during runtime), the icon changes once on the ActionBar.
I actually want to remove the ActionBar and leave only the bottomnav (tabs) for navigation, yet whatever i'm doing the icon changes only on the ActionBar.
The item inside bottom_nav_menu.xml:
<item
android:id="#+id/navigation_notifications"
android:icon="#drawable/ic_dashboard_black_24dp"
android:title="#string/title_notifications"
app:showAsAction="ifRoom"/>
The code that changes the icon:
#Override
public boolean onPrepareOptionsMenu (Menu menu){
menu.clear();
getMenuInflater().inflate(R.menu.bottom_nav_menu, menu);
menu.getItem(2).setIcon(this.bitmap_pic);
Log.e(TAG, "Icon Changed");
return super.onPrepareOptionsMenu(menu);
}
The result - Icon stays blank on BottomNav but appears on the ActionBar.
Expected result: BottomNav icon will be the image that shown on the top right.
Thanks
EDIT!
Issue was fixed after inflating the main_activity layout that contains the BottomNavView
Now the problem the picture isn't showing properly, attached a screenshot (Image is grey instead of showing the icon like in the ActionBar in the first picture):
Edit 2
Icon is still grey instead of showing the bitmap picture.
Added:
MenuItemCompat.setIconTintMode(bottomNavigationView.getMenu().getItem(2), PorterDuff.Mode.CLEAR);
But it still shows up like in the picture below
Edit 3
Fixed the issue using:
bottomNavigationView.setItemIconTintList(null);
I'm not sure if onPrepareOptionsMenu invoked for bottom navigation bar.
You should have to update navigation menu icon from onCreate method of that Activity.
Refer below code,
val menu = navigation.menu
val menuItem = menu.findItem(R.id.navigation_notifications) // find particular menu-item using its ID.
menuItem?.icon = this.bitmap_pic
Solution for Gray icon tint,
add below line.
MenuItemCompat.setIconTintMode(menuItem, PorterDuff.Mode.DST)
I set navigation bar color in my app like this:
getWindow().setNavigationBarColor(ContextCompat
.getColor(MainActivity.this, R.color.my_color));
and after change fragment I want to reset navigation bar color(come back to default). How do this ? I want not set in my style
android:navigationBarColor">#color/my_navigaton_bar_color< and when I want set default, call:
getWindow().setNavigationBarColor(ContextCompat
.getColor(MainActivity.this, R.color.my_navigaton_bar_color));
P.S. I was try save in variable getWindow().getNavigationBarColor() but it always -1 , a also try decode value from android.R.attrs.navigationBarColor but it also doesn't work
This can be done inside styles.xml using
<item name="android:navigationBarColor">#color/theme_color</item>
or
window.setNavigationBarColor(#ColorInt int color)
This image may also help you to indentify which color goes where
You can find details: here and here
Components (text and edit boxes) defined in layout XML looks very different from those i add programatically.
I've tried to apply the same textAppearance style programatically as the ones from my XML has. I tried calling setTheme() after adding the components too. No difference.
TextView tv10 = new TextView(getApplicationContext());
tv10.setText("Back width");
EditText tv11 = new EditText(getApplicationContext());
tv11.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_CLASS_NUMBER);
TextView comes out with small font and gray text, EditText comes out with black background and gray text.
The components must be added programmatically because of options the user choose. Those user choices are defined in the XML and follow the expected color scheme, which is the Android Studio defaults. (Black text on white background)
You shouldn't use Application Context for views. Only Activity context. Also you can pass style as parameter, when create view in code.
As Yamko said, style can be passed in the constuctor
var textView = TextView(context, null, R.style.LoginBodyTextViewStyle)
where the style can be something like
<style name="LoginBodyTextViewStyle" parent="android:Widget.TextView">
<item name="android:textStyle">normal</item>
<item name="android:fontFamily">sans-serif</item>
<item name="android:textSize">#dimen/text_size_default</item>
<item name="android:textColor">#color/grey</item>
</style>
I have a requirement in which I have to make an application that works from Android 3.0 onwards. Naturally I didnt go for ActionBarSherlock.
Now I replaced the background of the actionbar using the following code from the official google resource .
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="android:Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#drawable/navigation_bar</item>
</style>
As a result , I now have the proper action bar .
Now the problem is , when I press the home icon ( ic_launcher.png) , when the icon is in pressed state I see the activity title and a carrot sign appear . I do not want such. I just want the home button to be clickable and would like to see nothing while the button is pressed .
I did succeed in removing the activity title that appears by using
getActionBar().setDisplayShowTitleEnabled(false);
however the default "<" sign still appears when the home icon is pressed .
Question 1 : How can I remove the "<" sign when the home icon is in pressed state
Question 2 : How can I remove the default options icon (on the right side , the three dots) ?
Thanks a bunch
For home Icon remove
getSupportActionBar().setIcon(R.color.transfrant);
Remove the title bar
in manifest under the activity
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
Change the action bar name. Android manifest under the activity
android:label="#string/app_name"
change to
android:label=" "
Question 2 : How can I remove the default options icon (on the right side , the three dots) ?
remove this method from your Activity class
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
return super.onCreateOptionsMenu(menu);
}
I am creating a custom Dialog with the following style:
<style name="FullHeightDialog" parent="android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
I put the following construction of the dialog:
public CustomDialog(Context context) {
super(context, R.style.FullHeightDialog);
setContentView(R.layout.custom_dialog_layout);
}
The custom_dialog_layout root is RelativeLayout with layout_margin="20dp", and inside I put LinearLayout with 3 simple Buttons.
I expect that the Dialog stretches to the full height and to the content width, in this case the LinearLayout with the 3 buttons.
It works fine on Android Gingerbread, BUT on Android 4, it never stretches more than ~40% of the screen.
On Android 4, I have to programatically set:
LayoutParams params = getWindow().getAttributes();
params.width = LayoutParams.MATCH_PARENT;
To make it work as expected. Even then, it shows non expected results, but nontheless, acceptable.
Is this a Bug of Ice Cream Sandwich or I am doing something wrong?
your problem might come from android:style/Theme.Dialog since it can evolve depending on android version.
solutions :
Define fully your own style with no reference to android:style/Theme.Dialog
refers to android:style/Theme.Dialog but add margins/paddings layout definitions in your style items