I have an application where the requirement is to show the application version name on the right corner of the application title bar.
Is this possible?
Note that the version name is different color and different size also.
sorry cannot post an image because of not enough reputation.... :(
It is something like
--------------------------------------------------
Application title comes here... v1.23
--------------------------------------------------
It seems like impossible to do that.
The only workaround appears to be using a custom title bar as mentioned in this post android place a textview over application title
Code below for quick help:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
if ( customTitleSupported ) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
}
final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
if ( myTitleText != null ) {
myTitleText.setText("MY TITLE");
}
}
hope it helps somebody!
Related
I am changing the colour of action bar using the following code.
actionBar= getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(backgroundColor));
This works perfectly fine in onCreate() method(i.e when my activity starts).
But when I am using the same code in Android 4.1 to change color of action bar in onClick() method(i.e when user clicks say a button) then it changes the color of action bar to 'white' ONLY whatsoever be the value of backgroundColor.
In Android 2.3 it works perfectly fine in both cases, whether called from onCreate or onClick().
It seems to be dead end for me.
It seems like for Android 4.0 up to Android 4.1 (4.2 is ok) you cannot change the action bar background once the activity has started.
So I guess a solution in those cases is to restart the activity with a different background color.
Not very elegant but thats the best I can come up with.
Something like this perhaps:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
if (getIntent().hasExtra("color")) {
getSupportActionBar().setBackgroundDrawable(
new ColorDrawable(getIntent().getIntExtra("color", 0)));
}
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent it = getIntent();
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
it.putExtra("color", Color.BLUE);
startActivity(it);
}
});
}
A workaround I used to solve this problem was to maintain a reference to the ColorDrawable that was used to set the background initially. Then, to change its colour, modify the colour of the Drawable, and then tell the activity to invalidate its options, which causes the ActionBar to be redrawn. Here's my code to set the action bar color:
ColorDrawable sBackgroundDrawable;
...
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
protected void setActionBarColor(int color){
if(getSherlockActivity() != null && !mCallback.isNavDrawerShowing()){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN){
if(sBackgroundDrawable == null){
sBackgroundDrawable = new ColorDrawable(color);
getSherlockActivity().getSupportActionBar().setBackgroundDrawable(sBackgroundDrawable);
}else{
sBackgroundDrawable.setColor(color);
getSherlockActivity().invalidateOptionsMenu();
}
}else{
getSherlockActivity().getSupportActionBar().setBackgroundDrawable(new ColorDrawable(color));
}
}
}
Based off of what steve_the_kid mentioned I was able to solve this by saving the color drawable.
ColorDrawable actionBarBackground = new ColorDrawable();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setBackgroundDrawable(actionBarBackground());
actionBarBackground.setColor(someColor);
}
void onSomethingClick() {
actionBarBackground.setColor(someOtherColor);
}
Sorry for such a late reply,But to help others I am replying to this question after so long.
Just set the Title of the action bar or make change in the icons or menu along with changing the colorDrawables.A simple change will recreate the action bar with new color.Even if you need same title or icon every time set same title or same icon.
I hope this will help others
I am solving this issue. I need to set up custom font in titlebar, but my activity is using dynamically generated layout (thus setContentView(R.layout.somtehing) is not used).
I have tried, that to set up custom font in titlebar you can do that by these code:
type = Typeface.createFromAsset(getAssets(),"fonts/verdanab.ttf");
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.something);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.gal_title);
title = (TextView) findViewById(R.id.title);
title.setText(R.string.Text);
title.setTypeface(type);
Problem is, this code doesn't work, app doesn't see TextView title, because there isn't setContentView(). Any advices?
Thx
Initialize this line type = Typeface.createFromAsset(getAssets(),"fonts/verdanab.ttf"); after this line
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.gal_title);
Use this code,
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.something);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.gal_title);
Typeface type = Typeface.createFromAsset(getAssets(),"fonts/verdanab.ttf");
title = (TextView) findViewById(R.id.title);
title.setText(R.string.Text);
title.setTypeface(type);
I use greendroid to implement to actionbar under api 11.
Now i have a strange behaviour in my MainView (the startview works well)
public class MainView extends GDActivity {
private boolean ison = false;
private FinderThread finder;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//this.getActionBar().removeViewAt(0);
setActionBarContentView(R.layout.mainlayout);
getActionBar().setBackgroundColor(Color.parseColor("#bf27c3"));
setTitle("TEST");
setTitleColor(Color.WHITE);
addActionBarItem(getActionBar().newActionBarItem(NormalActionBarItem.class).setDrawable(R.drawable.person));
Looking:
The Actionbar text is empty and no home button. please help
You can try to set the Title and the Color of the Title with
getActionBar().setTitle("Title")
The same for the color. Try also to set the color via an RGB value.
I can't promise that this will work, because i don't use greendroid for my actionbars. But i understand that you don't use actionbarsherlock + greendroid because they are only compatible with a trick (a bit hacking xD)
i am setting custom title in my tab activity like this
boolean customTitleSupported;
customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
// customTitleSupported = false;
//check for custom title supported feature
if(customTitleSupported == true) {
customTitleBar();
} else {
requestWindowFeature(Window.FEATURE_LEFT_ICON);
String title = prefManager.getTitle();
setTitle(NAME + " " + title);
}
//Set contentview of activity
setContentView(R.layout.tabactivity);
if(customTitleSupported == false){
setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.logo);
}
Everything is working fine.But when i make customTitleSupported to FALSE to check if custom title support is not there in sdk then it should call normal setTitle() function.
But if i do this customTitleSupported = false;
i am getting following error
android.util.AndroidRuntimeException: You cannot combine custom titles with other title
features
i am not clearly understanding the problem if custom title is not supported why i cannot request for other title features.If i doest not request for custom title then else part of setting icon and title works but i want to handle both conditions together.
Thanks
You canot use other title features with FEATURE_CUSTOM_TITLE, and you cannot turn off a feature once requested: check doc
You just have to use setFeatureInt(int featureId, int value) and provide a layout resource ID at "value". Check this example: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/CustomTitle.html
I am learning android 3.0.
Can any one tell How can add Image in Title bar in android 3.0?
In developer site they are telling by default it will come.but for me it is not coming.
Thanks in advance.
You will have to create Custom Title Bar Layout to add your own image in titlebar.And then you can use following code to assign the titlebar to your view
boolean customTitleSupported;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
if (customTitleSupported) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_tittlebar_layout);
}
setContentView(R.layout.main);
}
here custom_tittlebar_layout refers to your custom titlebar layout with image.