How to hide Android title bar after window creation?

How to hide the title bar through the code in android describes an easy way to hide the window title bar, but this must be done before calling setContentView. What if I want to do this later? In my case, I would like to do this after the web view has finished loading the content, and I no longer need to show the progress in the title bar.

+6
source share
5 answers

Here are a few options that include the complete removal of the header:

  • Fill the layout with LayoutInflater . This layout will essentially be a LinearLayout or RelativeLayout , which contains all the components for the header .
  • or if that sounds like too much of a hastle, you can create a headline in your xml activity with the visibility set to gone and use titleBarLayout.setVisibility(View.VISIBLE); when viewing a webpage loading

Pseudocode:

 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 would go with the LayoutInflater option. But it is up to you. I believe that you can also add animation to your title bar displayed with any option, which can be a nice addition.


Or call this before setContentView :

 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 

This will return false if the custom title bar is not supported, so you can check this. This is called after setContentView :

 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar); 

Assign the parent layout of the xml file ( LinearLayout or RelativeLayout , which contains all the views) id with android:id="@+id/custom_title_layout" .

Now,

 LinearLayout titleBarLayout = (LinearLayout) findViewById(R.id.custom_title_layout); 

And switch the title bar to be there or not to be used:

 titleBarLayout.setVisibility(View.GONE); //View.VISIBLE to show 
+2
source

If you are using API 11 and above

 ActionBar actionBar = getActionBar(); actionBar.hide(); // slides out actionBar.show(); // slides in 
+2
source

If you want to hide the title bar in action:

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); ... } 

Refer to http://mgmblog.com/2008/12/08/hide-the-title-bar-in-an-android-view-by-using-the-window-class/

+1
source

It worked for me

in onCreate

 this.getWindow().requestFeature(Window.FEATURE_PROGRESS); getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON); 

and then in the WebViewClient

  myWebView.setWebViewClient(new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); activity.setTitle("Loading..."); View title = getWindow().findViewById(android.R.id.title); View titleBar = (View) title.getParent(); titleBar.setBackgroundColor(Color.BLACK); titleBar.setVisibility(View.VISIBLE); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); activity.setTitle(""); View title = getWindow().findViewById(android.R.id.title); View titleBar = (View) title.getParent(); titleBar.setVisibility(View.GONE); } }); 
0
source

in xml use

 <activity android:name=".ActivityName" android:theme="@android:style/Theme.NoTitleBar"> 
-1
source

Source: https://habr.com/ru/post/895810/


All Articles