Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide Android title bar after window creation?

Tags:

android

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.

like image 840
Edwin Evans Avatar asked Aug 24 '11 22:08

Edwin Evans


2 Answers

Here's a couple options that involve ditching the title bar altogether:

  • Inflate a layout using LayoutInflater. This layout will essentially be the LinearLayout or RelativeLayout that holds all of the components for your title bar.
  • or if that sounds like too much of a hastle you can create a title bar in your xml of the activity with the visibility set to gone and use titleBarLayout.setVisibility(View.VISIBLE); when web view is done loading

Pseudo 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
like image 165
b_yng Avatar answered Nov 15 '22 11:11

b_yng


If you are using API 11 and above

ActionBar actionBar = getActionBar();
actionBar.hide(); // slides out
actionBar.show(); // slides in
like image 42
Paul Avatar answered Nov 15 '22 13:11

Paul