How to hide the title bar through code in android describes a simple way to hide the window title bar, but it needs to be done before calling setContentView. What if I want to do it later? In my case, I'd like to do it after a web view has finished loading content and I no longer need to show progress in the title bar.
Here's a couple options that involve ditching the title bar altogether:
LayoutInflater
. This layout will essentially be the LinearLayout
or RelativeLayout
that holds all of the components for your title bar.titleBarLayout.setVisibility(View.VISIBLE);
when web view is done loadingPseudo code:
RelativeLayout activityLayout = (RelativeLayout) findViewById(R.id.my_layout);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
public void onWebViewFinishLoading() {
LinearLayout myTitleBar = inflater.inflate(R.layout.my_title_bar, activityLayout, false);
//Set the view to the top of the screen
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
myTitleBar.setLayoutParams(params);
//set up buttons, listeners, etc.
}
Personally, I'd go with the LayoutInflater
option. But it's up to you. I believe you can also add animations to your title bar being displayed with either option, which could be a nice addition.
Or call this before setContentView
:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
This will return false if a custom title bar is not supported, so you may want to check for that. This is called after setContentView
:
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
Assign the parent layout of the xml file (the LinearLayout
or RelativeLayout
that holds all of the views) an id with android:id="@+id/custom_title_layout"
.
Now,
LinearLayout titleBarLayout = (LinearLayout) findViewById(R.id.custom_title_layout);
And toggle the title bar to be there or not using :
titleBarLayout.setVisibility(View.GONE); //View.VISIBLE to show
If you are using API 11 and above
ActionBar actionBar = getActionBar();
actionBar.hide(); // slides out
actionBar.show(); // slides in
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With