Android - change custom title view at run time
The problem is that the only Window
implementation (PhoneWindow
) uses a LayoutInflater
in its setFeatureInt
method and instantiates the new layout with inflate
and attachToRoot=true
. Consequently, when you call setFeatureInt
, the new layouts are not replaced but attached to the internal title container and thus drawn on top of each other.
You can workaround this by using the following helper method instead of setFeatureInt
. The helper simply removes all views from the internal title container before the new custom title feature is set:
private void setCustomTitleFeatureInt(int value) {
try {
// retrieve value for com.android.internal.R.id.title_container(=0x1020149)
int titleContainerId = (Integer) Class.forName(
"com.android.internal.R$id").getField("title_container").get(null);
// remove all views from titleContainer
((ViewGroup) getWindow().findViewById(titleContainerId)).removeAllViews();
// add new custom title view
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, value);
} catch(Exception ex) {
// whatever you want to do here..
}
}
I'm not sure whether the current setFeatureInt
behaviour is intended, but it is certainly not documented one way or the other which is why I'll take this to the android devs ;)
EDIT
As pointed out in the comments, the aforementioned workaround is not ideal. Instead of relying on the com.android.internal.R.id.title_container
constant you could simply hide the old custom title whenever you set a new one.
Let's assume you have two custom title layouts:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/custom_title_1" ...
and
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/custom_title_2" ...
and you want to replace custom_title_1
with custom_title_2
, you could hide former and use setFeatureInt
to add the latter:
findViewById(R.id.custom_title_1).setVisibility(View.GONE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_2);
Is it possible to show title and custom view on action bar at same time?
The custom view will replace the Title bar. I suggest you to include a textview for the app title in your custom view so that you have more ground to play.
How to change title of Activity in Android?
Try setTitle by itself, like this:
setTitle("Hello StackOverflow");
How to dynamically change a custom ActionBar title?
Give the TextView
an ID:
<TextView
android:id="@+id/title"
... />
Call findViewById()
on the custom View
to get it:
TextView title = (TextView) bar.getCustomView().findViewById(R.id.title);
And use the setText()
method to change your title:
title.setText("New title");
Custom title and setTitle in Android
I had to create a similar feature with a custom title. I highly doubt you will be able to do it by matching an id, that would be a huge security flaw if Android allowed that. What I did was extend Activity and then override the setTitle method in a fashion like...
TextView mCustomTitle;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout);
mCustomTitle = (TextView)findViewById(R.id.title);
}
@Override
public void setTitle(CharSequence title) {
//you can override the other setTitle as well if you need it
mCustomTitle.setText(title);
}
How to change the customize Window title bar and its icon for multiple activities
just Set imageview and textview id in your custom layout and set dynamically its value in particular Activity
ImageView image=(Imageview)getWindow().findViewById(your image view id);
TextView title=(Textview)getWindow().findViewById(your image view id);
set image.setImageResource(...);
and title.settext(title);
Thats it....
Android toolbar center title and custom font
To use a custom title in your Toolbar
all you need to do is remember is that Toolbar
is just a fancy ViewGroup so you can add a custom title like so:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?android:attr/actionBarSize"
android:background="@color/action_bar_bkgnd"
app:theme="@style/ToolBarTheme" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toolbar Title"
android:layout_gravity="center"
android:id="@+id/toolbar_title" />
</android.support.v7.widget.Toolbar>
This means that you can style the TextView
however you would like because it's just a regular TextView
. So in your activity you can access the title like so:
Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title);
Android Dialog with modifiable single line title
I would implement a custom dialog by extending the Dialog
class and creating a custom xml layout for it.
You'd need some custom button backgrounds for the plus / minus and top / bottom combos and some button listeners to manipulate the values.
Since you are going for a duration value, you are probably going to need more space than the existing dialog gives you anyway.
This way you can set the title to whatever you like. Let me know if you need a code example.
Example:
The dialog class:
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class DurationDialog extends Dialog {
private Button yearButtonPlus;
private Button yearButtonMinus;
private TextView dialogBody;
private TextView dialogTitle;
private String dialogBodyString;
private String dialogTitleString;
public DurationDialog(final Context context, String dialogBody, String dialogTitle) {
super(context,R.style.CustomDialogTheme);
this.dialogBodyString = dialogBody;
this.dialogTitleString = dialogTitle;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.my_dialog);
yearButtonPlus = (Button) findViewById(R.id.dialog_year_button_plus);
yearButtonMinus = (Button) findViewById(R.id.dialog_year_button_minus);
dialogBody = (TextView) findViewById(R.id.dialog_body);
dialogTitle = (TextView) findViewById(R.id.dialog_title);
dialogBody.setText(dialogBodyString);
dialogTitle.setText(dialogTitleString);
yearButtonPlus.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//do year increment here
}
});
//etc...
}
}
In your activity, you call this.showDialog(DURATION_DIALOG);
// DURATION_DIALOG is just an integer specified at the top of your Activity to identify the dialog for the next peice of code, which handles actually creating the dialog:
import android.app.Activity;
import android.app.Dialog;
public class MyActivity extends Activity {
private Dialog dialog;
//Lots of other activity stuff...
protected Dialog onCreateDialog(int id) {
switch (id) {
case DURATION_DIALOG:
dialog = new DurationDialog(Activity.this, "your title", "your body");
dialog.setOnDismissListener(onDismissListener);
break;
default:
dialog = null;
}
return dialog;
}
}
//put this listener in as an inner class of MyActivity:
private DialogInterface.OnDismissListener onDismissListener = new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
DurationDialog dialog = (DurationDialog) dialog;
//grab the duration stuff out of your dialog and do stuff with it....
}
};
Finally, you can set your dialog theme as is done above in your styles.xml
file. For example:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@drawable/bgnd_transparent</item>
<item name="android:windowIsFloating">true</item>
</style>
</resources>
Good luck!
Related Topics
How to Embed a Youtube Clip in a Webview on Android
Android Add Image to Webview from a Drawable
Background-Attachment: Fixed Interfering with Background-Size
What Are The Default Color Values for The Holo Theme on Android 4.0
Failed to Resolve: Recyclerview-V7
Using Multiple Text Colors in Android's Textview [ HTML.Fromhtml() ]
Textview with Background Color and Line Spacing
Android Stock Browser Not Respecting CSS Overflow
Android: Change Tab Text Color Programmatically
What Are Best Practices for Using Aes Encryption in Android
<Select> Not Working in Phonegap App on Android 2.3.3
Onlistitemclick Is Not Working for Listview
CSS Media Queries on Nexus 7, Display Resolution Not Working in Code
Android Custom Url to Open App Like in iOS
How to Handle Empty Response Body with Retrofit 2
Difference Between <Supports-Screens> and <Compatible-Screens> on Android