Custom titlebar - system titlebar being shown for a brief moment? - android

I've got a custom layout I want to use as the titlebar of my android app. The technique found (linked at the bottom) works, but the system titlebar is displayed before onCreate() is called. Obviously that looks pretty jarring, as for a moment the system titlebar is shown, then my custom titlebar is shown:
// styles.xml
<resources>
<style name="MyTheme">
<item name="android:windowTitleSize">40dip</item>
</style>
</resources>
// One of my activities, MyTheme is applied to it in the manifest.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.my_activity);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_custom_header);
}
I could always hide the system titlebar and display my own in-line perhaps with each and every layout, but, that's not very friendly.
Thanks
http://www.londatiga.net/it/how-to-create-custom-window-title-in-android/

I think it's a framework limitation. I had the same problem in some of my applications and the ultimate solution was for me to tell the framework I didn't want a title bar at all and then create my own in my layouts. The include directive made it bearable for me, e.g.:
<include layout="#layout/title" />
When I used requestWindowFeature(Window.FEATURE_NO_TITLE) in my activities, I would have the same issue, I'd see the system title bar briefly while the activity was being build when it first loaded.
When I switched to using a theme to tell the framework I didn't want a title, the problem went away and I now see my own title directly on first load. The styling is easy for that:
<style name="FliqTheme" parent="#android:Theme.Black">
<item name="android:windowNoTitle">true</item>
</style>
I know this doesn't apply to your issue with the custom title, but like ptc mentioned, if you move your custom title into style/theme definitions (which you do by overriding the system title styles in your theme), I think you'll be on the right track.

The same problem happened to me today when I was trying to custom the title. I solved it by set the android:theme to android:style/Theme.NoTitleBar in the AndroidManifest.xml, and call setTheme() to the actual theme I want in my activity's onCreate callback function.

Try creating a custom theme style in XML and then set your activity's theme attribute in the AndroidManifest.xml file.
http://developer.android.com/guide/topics/ui/themes.html#ApplyATheme

The method through setting android:theme does not work for me, I have made it by adding the following code in onCreate() of my Activity subclass:
getWindow().addFlags(LayoutParams.FLAG_FULLSCREEN);

Related

Custom Title bar with Holo Themes

Like a lot of people, I would like to use a custom title bar in android but also use the Holo theme. I've seen a lot of posts recommending using Theme.Holo.NoActionBar but it still gives me the same error as when I change my custom theme to use Theme.Holo. I want to resolve once and for all, is it possible to use a custom title bar like this:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title_bar);
which seems to be the most common way to do it.
Well you can create another element layout which looks like title bar for you and in the current activity you can set requestWindowFeature(Window.FEATURE_NO_TITLE)
This way you get the holo theme aswell as custom title. This is commonly used practice in this scenario.
you can define parent="#android:style/Theme.Holo"
then just disable the windows action bar
like this
<item name="android:windowActionBar">false</item>
in theme.
It's working for me...

Change whole application's text color programmatically

I would like to change the whole application's text and background color in Java, is this possible? With this I mean to change the color of every item in the application (TextViews, ListView items, everything).
Is this possible?
I have tried using a custom-made style but I can't make it work. Here is the xml file (put in the res/layout/values folder):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Light">
<item name="android:textColor">#00FF00</item>
</style>
</resources>
Let's say I just want to change the text color for now.
Now I call this style in my application like this:
public void onCreate(Bundle icicle) {
Activity.setTheme(android.R.style.light);
super.onCreate(icicle);
setContentView(R.layout.main);
But I get the error light cannot be resolved or is not a field.
Update:
One way I found to do this programmatically is to restart the activity, calling
this.setTheme(R.style.Light);
onCreate(null);
However, this works only for the current activity and not for the whole application. It would be great if it were possible to do this launching another activity, not only the current one.
You're trying it in a bit to simple way. Like this you're just adjusting your general Activity's background instead of all the different Views that are out there.
In order to try and adjust every type of View (Button, TextView etc) you'll need to address all their own styles to overwrite them.
Per example if you want to adjust Button you'll need in your own general style:
<item name="android:buttonStyle">#style/ButtonHoloDark</item>
This will point at your own custom style, which takes its parent from the Android's standard Button.
<style name="ButtonHoloDark" parent="android:style/Widget.Button">
<item name="android:background">#drawable/btn_default_holo_dark</item>
<item name="android:textColor">#ffffff</item>
</style>
Be warned, doing this for every View will take you quite some themes and styles.
You can find a great example how to do this exactly in theHoloEverywhere lib, which basically does the same for creating a holo theme backported to Android 2.2 or so
Finally, drop the Activity.setTheme(android.R.style.light); stuff, and just set your own theme via the manifest.
Ok so I found one possible solution, which is to pass the theme information between the activities using intents and the putExtra method.
Code for the first activity (the caller):
Intent i = new Intent(this, ActivityToCall.class);
i.putExtra("key", R.style.Light);
startActivity(i);
Code for the second activity (the called one):
public void onCreate(Bundle icicle) {
int theme = getIntent().getIntExtra("key",-1);
this.setTheme(theme);
super.onCreate(icicle);
// other code...
I don't know if it's the best possible approach but at least it works.
The attributes you want change are:
<style name="AppTheme" parent="android:style/Theme.Holo">
<item name="android:textColorPrimary">...</item>
<item name="android:textColorSecondary">...</item>
</style>
There are a few other ways to change them: Please Refer to this information
You can then set this Theme via the Manifest or setTheme(R.style.AppTheme) in your Activity's onCreate(...)

Android: Issue with hiding title bar

I want to hide the title bar in all activities of my app. To do that, I put the following attribute in the tag:
android:theme="#android:style/Theme.NoTitleBar"
The title bar is now hidden, but my problem is that my app looks weird with that attribute.
Before:
http://imageshack.us/a/img831/6905/screenshot1355296053406.png
with the noTitleBar attribute:
http://imageshack.us/a/img211/581/screenshot1355295921669.png
It looks like there is less contrast..
It makes no difference if the noTitleBar attribute is in the application tag or in each activity tag.
Hope you can help me with my problem.
In onCreate() of activity write following:
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
It's probably because you have also removed the Theme of your application together with the title bar.
I'm not sure what your theme was before, but you should extend an existing theme. Something like this in your styles file:
<style name="MyTheme" parent="android:Theme.Light.NoTitleBar.">
</style>
Maybe your parent theme should be android:Theme.Black.NoTitleBar?
You've accepted no answer so I'm going to toss my 2 cents in here:
Adding this snippet of code might be helpful:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

android actionbar - removing the actionbar dynamically

I want to use an activity in two manners
It shows a list of items which can be edited, new ones added etc
It shows the list of items in order to choose one.
As the main logic of the Activity is to display the list of items I would like to handle these two cases in the same Activity. Nevertheless in the 1. I want to show the actionbar so that the user can navigate from there to wherever wanted. In the 2. case I dont want any actionbar to be shown, all the user can do is choose an item or press cancel/back.
What is the best way to achieve this. My first guess would be two themes which I set dynamically what of the two cases is required. But I wonder if there is also a way to easily remove the actionbar from the screen programmatically which would save me from declaring two themes etc. Any suggestion how you handle this requirement would be very helpful.
Thanks
How about this?
public void hideActionBar(){
getActionBar().hide();
}
Source: http://developer.android.com/guide/topics/ui/actionbar.html
Use this:
getActionBar().hide();
Android documentation for action bar hide method
If you want to actually remove the action bar (not create it in the first place) as opposed to create it but then just hiding it right away, you can do it via themes or via code.
Via theme or styled attributes:
Use one of the predefine system themes like Theme.Holo.NoActionBar or its variants. This is the easiest way to do it.
If you want to define using attributes inside your custom theme, then you could do this:
<resources>
<style name="MyTheme" parent="some_parent_theme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
</style>
</resources>
Now add this theme to your activity or application.
Via code (make sure you call this before setContentView in Activity.onCreate):
requestWindowFeature(Window.FEATURE_NO_TITLE);
For those who have problems with getActionBar().hide();
consider using:
getSupportActionBar().hide();

Custom title bar still shows original on startup

I'm using a custom title bar in my app and it all works fine except that when the app starts up, the original (standard) android title bar is shown for a brief time before it is replaced by my custom title bar.
This is not a problem when the app is already loaded in memory because the 'delay' is not apparent but if the app is not already in memory, it is very obvious.
There's nothing special about the code :
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
I thought about changing the style to have no window title and just include my custom title in the top of the layout but that doesn't seem right.
Thanks for any pointers.
Thomas Devaux has posted a smart solution. It worked in my app
Change the windowTitleBackgroundStyle to use color “#android:color/transparent”.
Also create a style for the text “android:windowTitleStyle” and set its “android:textColor” >to transparent as well.
For completeness to Lluis' answer, here's the full code you need to hide the default-title before the 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>
add a splash screen activity before the main activity loads, should have enough time for the next one to load properly
Are you able to use an app theme to set a custom title globally for your app? See here for a pretty good example. I had a similar problem and i seem to remember going this route fixed it.

Categories

Resources