I have trouble in changing the background color and text color of my buttons in my app.
I made a new theme to play around and try it out.
My manifest looks like this:
<application
...
android:theme="#style/OrangeWhite">
<activity android:name=".MainActivity">
...
</application>
My theme.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="OrangeWhite" parent="android:Theme.Light">
<item name="android:windowBackground">#color/OrangeWhite_bg_color</item>
<item name="android:textColor">#color/OrangeWhite_wh_text</item>
<item name="android:colorButtonNormal">#color/OrangeWhite_bg_btn</item>
</style>
</resources>
Changing text or background color of an activity works just fine. The problem is the button. Do I need to specify on every button to use the theme OrangeWhite?...
If you are using AppCompat theme as parent than try below code
If you need to change the style of a specific button, you can define a new style, inheriting one of the parent styles described above. In the example below I just changed the background and font colors:
<style name="AppTheme.Button" parent="Widget.AppCompat.Button.Colored">
<item name="colorButtonNormal">#color/Red</item>
<item name="android:colorButtonNormal">#color/Red</item>
<item name="android:textColor">#color/White</item>
</style>
Then you just need to apply this new style on the button with:
android:theme="#style/AppTheme.Button"
Android action bar tabs are left-aligned by default, I want to align it the center of the action bar. Is there any way we can make it center?
I was able to center-align the actionbar by customizing my application theme
In my AndroidManifest.xml, I added a android:theme property:
<application
android:label="#string/app_name"
android:icon="#drawable/ic_launcher"
android:theme="#style/CustomActionBarTheme">
...
I then added a new themes.xml file to my project, and added :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarTabBarStyle">#style/MyActionBarTabBar</item>
</style>
<style name="MyActionBarTabBar">
<item name="android:gravity">center</item>
</style>
</resources>
The tabs should now be center-aligned.
EDIT:
It works only if your actionbar is displayed on 2 lines (in portrait)
You can use the ViewPagerIndicator plugin to customize your tabs.
Hope it helps!
I'm looking through the Holo.Light theme, and I can't seem to find the magic style to override to get rid of the title text that briefly shows up when my app first launches.
How can I do that?
Try:
getActionBar().setDisplayShowTitleEnabled(false);
For v.7:
getSupportActionBar().setDisplayShowTitleEnabled(false);
I think this is the right answer:
<style name="AppTheme" parent="Theme.Sherlock.Light.DarkActionBar">
<item name="actionBarStyle">#style/Widget.Styled.ActionBar</item>
<item name="android:actionBarStyle">#style/Widget.Styled.ActionBar</item>
</style>
<style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse">
<item name="android:displayOptions">showHome|useLogo</item>
<item name="displayOptions">showHome|useLogo</item>
</style>
I'm very new to Android so correct me if I'm wrong, but I think if you decide to use navigation tabs on the Action Bar, they seemed to not be completely left aligned because the title text color is only transparent and hasn't gone away.
<style name="CustomActionBarStyle" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:displayOptions">useLogo|showHome</item>
</style>
worked for me.
Got it. You have to override
android:actionBarStyle
and then in your custom style you have to override
android:titleTextStyle
Here's a sample.
In my themes.xml:
<style name="CustomActionBar" parent="android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/CustomActionBarStyle</item>
</style>
And in my styles.xml:
<style name="CustomActionBarStyle" parent="android:style/Widget.Holo.ActionBar">
<item name="android:titleTextStyle">#style/NoTitleText</item>
<item name="android:subtitleTextStyle">#style/NoTitleText</item>
</style>
<style name="NoTitleText">
<item name="android:textSize">0sp</item>
<item name="android:textColor">#00000000</item>
</style>
I'm not sure why setting the textSize to zero didn't do the trick (it shrunk the text, but didn't make it go away), but setting the textColor to transparent works.
In your Manifest
<activity android:name=".ActivityHere"
android:label="">
Write this statement under
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayShowTitleEnabled(false);
if extending AppCompactActivity use getSupportActionbar() if extending Activity use getActionBar() else it may give
null pointer exception
using android:label="" in AndroidManifest.xml for activity also works but your app won't appear in Recently used apps
you have two choice:
first:
getActionBar().setDisplayShowTitleEnabled(false);
If usign getActionBar() produced null pointer exception, you should use getSupportActionBar()
Second
getSupportActionBar().setTitle("your title");
or
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().hide();
You can change the style applied to each activity, or if you only want to change the behavior for a specific activity, you can try this:
setDisplayOptions(int options, int mask) --- Set selected display
or
setDisplayOptions(int options) --- Set display options.
To display title on actionbar, set display options in onCreate()
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE);
To hide title on actionbar.
getSupportActionBar().setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
Details here.
i use this code in App manifest
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:logo="#drawable/logo2"
android:label="#string/title_text"
android:theme="#style/Theme.Zinoostyle" >
my logo file is 200*800 pixel
and use this code in main activity.java
getActionBar().setDisplayShowTitleEnabled(false);
it will work Corectly
I tried this. This will help -
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar should be called after setSupportActionBar, thus setting the toolbar, otherwise, NullpointerException because there is no toolbar set.
Hope this helps
The only thing that really worked for me was to add:
<activity
android:name=".ActivityHere"
android:label=""
>
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().hide();
hope this will help
If you only want to do it for one activity and perhaps even dynamically, you can also use
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(actionBar.getDisplayOptions() ^ ActionBar.DISPLAY_SHOW_TITLE);
as a workaround just add this line incase you have custom action/toolbars
this.setTitle("");
in your Activity
Use the following:
requestWindowFeature(Window.FEATURE_NO_TITLE);
While all of these are acceptable, if your activity only contains a main activity, you can edit res\values\styles.xml like so:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
I've updated parent and added the .NoActionBar property to the Light theme from Android Studios Create Blank Application Wizard.
Simply extends your java file from AppCompatActivity and do this:
ActionBar actionBar = getSupportActionBar(); // support.v7
actionBar.setTitle(" ");
In your manifest.xml page you can give address to your activity label. the address is somewhere in your values/strings.xml . then you can change the value of the tag in xml file to null.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title_main_activity"></string>
</resources>
I am new to Android so maybe I am wrong...but to solve this problem cant we just go to the manifest and remove the activity label
<activity
android:name=".Bcft"
android:screenOrientation="portrait"
**android:label="" >**
Worked for me....
I remove the default appbar and run a custom toolbar instead using Theme.AppCompat.Light.NoActionBar then add a textview or something that extend to full toolbar width using attribute layout_width=match_parent and it pushes the title out of the toolbar. If you want to do this, you must study how to make a toolbar.
ActionBar actionBar = getActionBar();
actionBar.setTitle("");
I've inherited the Holo Light Theme and customized the background of the ActionBar with the following:
Content of styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="ActionBar" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:background">#drawable/actionbar_background</item>
</style>
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/ActionBar</item>
</style>
</resources>
Content of actionbar_background.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#raw/actionbar_background"
android:tileMode="repeat" />
Instead of being repeated, the image is stretched, any idea of why android:tileMode="repeat" is not applied?
Thanks in advance
Drawable d=getResources().getDrawable(R.drawable.background_image_name);
getActionBar().setBackgroundDrawable(d);
The above code sets the background image for the action bar.
Hope it helps.
Ok, thanks to Romain Guy on #android-dev IRC channel, it's a known bug on honeycomb / Android 3.0 which will be fixed on the next release. Since then, the only solution is do it from code, and it works :-)
final ActionBar actionBar = getActionBar();
BitmapDrawable background = new BitmapDrawable (BitmapFactory.decodeResource(getResources(), R.raw.actionbar_background));
background.setTileModeX(android.graphics.Shader.TileMode.REPEAT);
actionBar.setBackgroundDrawable(background);
You can easily do this thing. If you would like to change Action Bar background image then you place this code to your res/styles.xml file.
<style name="Theme.MyAppTheme" parent="#android:style/Theme.Holo">
<item name="android:actionBarStyle">#style/Theme.MyAppTheme.ActionBar</item>
</style>
<style name="Theme.MyAppTheme.ActionBar" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:background">#drawable/top_black_bg</item>
</style>
For this you have to select an image from "drawable" folder . Here I select an image "tp_black_bg.png"
After that don't forget to declare this theme to your AndroidManifest.xml file
<application
.
.
.
android:theme="#style/Theme.MyAppTheme" >.............</application>
Now you can reopen any XML layout file , you can easily see the effect. In the same way you can also able to change the background color of ActionBar.
Thanks.
mActionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.navbar));
Use getSupportActionBar() from android.support.v7 for backward compatability.
I have a custom title bar
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activities);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
Which works basically fine. The problem is that until the above code is called the default title bar is shown. I don't want a title bar there at all, in other words before mine shows up no title shall show up.
Adding this to the manifest:
<application android:theme="#android:style/Theme.NoTitleBar">
leads to a force close. My manifest looks like this
<application android:icon="#drawable/icon" android:label="#string/app_name"
android:theme="#style/My_Theme">
Where I need my_Theme since it sets the background color, setting the background color in my customer theme leads to a gray area around my colored backround. So even without the force close I am not sure if the no title will help.
Any idea?
I had the same issue as you.
The issue is with something you have in your style.
Try this out:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="My_Theme">
<item name="android:windowTitleSize">35dp</item>
<item name="android:windowTitleBackgroundStyle">#android:color/black</item>
</style>
</resources>
This one is the only one for me, which prevents the default-title before my custom title is initiated:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="CustomWindowTitleStyle">
<item name="android:textColor">#android:color/transparent</item>
</style>
<style name="CustomTheme" parent="#android:style/Theme.Holo">
<item name="android:windowActionBar">false</item>
<item name="android:windowTitleBackgroundStyle">#android:color/transparent</item>
<item name="android:windowTitleSize">50dp</item>
<item name="android:windowTitleStyle">#style/CustomWindowTitleStyle</item>
</style>
</resources>
First thing why are you using a custom title bar if your app has NoTitleBar? That is silly!
Needless to say, this is your problem and you must remove that flag.
Anyhow, the best way to add custom title bar is in xml only. This avoids your app double loading the title bar; which users will see.
../res/styles.xml
<resources>
<style name="AppTheme parent="#style/android:Theme.Light">
<item name="android:windowNoTitle">false</item>
<item name="android:windowTitleSize">30dp</item
<item name="android:windowTitleStyle">#layout/custom_title</item>
</style>
</resources>
Then you dont need that code about requestAnything.
you should also check whether customTitle is supported by it or not.
Boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
if (customTitleSupported) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);
}
Your App crashes because in your code you calling Title Bar from window features and in other side you disabling it through manifest. Basically You cant do this, its logically incorrect.
You need to modify your title bar not to remove it.