Monday, April 19, 2021

Hide Title Bar

 Android Hide Title Bar and Full Screen Example

In this example, we are going to explain how to hide the title bar and how to display content in full screen mode.

The requestWindowFeature(Window.FEATURE_NO_TITLE) method of Activity must be called to hide the title. But, it must be coded before the setContentView method.

Code that hides title bar of activity

The getSupportActionBar() method is used to retrieve the instance of ActionBar class. Calling the hide() method of ActionBar class hides the title bar.

  1. requestWindowFeature(Window.FEATURE_NO_TITLE);//will hide the title   
  2. getSupportActionBar().hide(); //hide the title bar  

Code that enables full screen mode of activity

The setFlags() method of Window class is used to display content in full screen mode. You need to pass the WindowManager.LayoutParams.FLAG_FULLSCREEN constant in the setFlags method.

  1. this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  2.                WindowManager.LayoutParams.FLAG_FULLSCREEN); //show the activity in full screen 

Android Hide Title Bar and Full Screen Example

Let's see the full code to hide the title bar in android.

activity_main.xml

File: activity_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="first.javatpoint.com.hidetitlebar.MainActivity">  
  8.   
  9.     <TextView  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="Hello World!"  
  13.         app:layout_constraintBottom_toBottomOf="parent"  
  14.         app:layout_constraintLeft_toLeftOf="parent"  
  15.         app:layout_constraintRight_toRightOf="parent"  
  16.         app:layout_constraintTop_toTopOf="parent" />  
  17.   
  18. </android.support.constraint.ConstraintLayout>  

Activity class

File: MainActivity.java
  1. package first.tranning.com.hidetitlebar;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.Window;  
  6. import android.view.WindowManager;  
  7.   
  8. public class MainActivity extends AppCompatActivity {  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         requestWindowFeature(Window.FEATURE_NO_TITLE); //will hide the title   
  14.         getSupportActionBar().hide(); // hide the title bar  
  15.         this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  16.                WindowManager.LayoutParams.FLAG_FULLSCREEN); //enable full screen  
  17.         setContentView(R.layout.activity_main);  
  18.   
  19.   
  20.     }  
  21. }  

Output: Hiding the Title Only

android hide title bar example output 1

Output: Hiding the TitleBar and enabling FullScreen

android hide title bar example output 2

No comments:

Post a Comment

Audio/ Sound Project

 ..Mainfest.xml <? xml version ="1.0" encoding ="utf-8" ?> < manifest xmlns: android ="http://schemas.andr...