This article explains and demonstrates various ways to hide the ActionBar in an Android Application.
Action Bar is a primary toolbar within the activity that may display the activity title, application-level navigation affordances, and other interactive items.
Although, Action Bar is important feature for android applications, but sometimes we have the need to hide it in either entire app, or some particular activity or during some particular work. There are various ways to hide Action Bar, demonstrated below:
Different ways to hide Action Bar:
- Hide Action Bar from entire App using styles.xml: If you want to hide Action Bar from the entire application (from all Activities and fragments), then you can use this method. Just go to res->values->styles.xml and change the base application to “Theme.AppCompat.Light.NoActionBar“.
Below is code snippet for this method and changes are made to styles.xml:
- Hide Action Bar from any particular activity using Java code: If you want to hide Action Bar from particular activity just add few lines of code in activity.java file as mentioned in code snippet below:
MainActivity.java
package
com.geeksforgeeks.hideactionbar;
import
android.support.v7.app.AppCompatActivity;
import
android.view.View;
import
android.os.Bundle;
public
class
MainActivity
extends
AppCompatActivity {
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Take instance of Action Bar
// using getSupportActionBar and
// if it is not Null
// then call hide function
if
(getSupportActionBar() !=
null
) {
getSupportActionBar().hide();
}
}
}
chevron_rightfilter_none - Hide Action Bar while user interaction using Window Manager:
Another way to hide Action Bar is through Window Manager by setting WindowManager flag. This approach makes a lot easier to hide the Action Bar when user interacts with your application. You can use “setFlags” function as described below in code .Also you have to se flags before setContentView() of Activity. Here is java file to hide Action Bar:
MainActivity.java
package
com.geeksforgeeks.hideactionbar;
import
android.support.v7.app.AppCompatActivity;
import
android.view.View;
import
android.os.Bundle;
public
class
MainActivity
extends
AppCompatActivity {
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
// set Windows Flags to Full Screen
// using setFlags function
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
}
}
chevron_rightfilter_none
styles.xml
< resources > <!---Base application theme. --> < style name = "AppTheme" parent = "Theme.AppCompat.Light.NoActionBar" > <!---Customize your theme here.--> < item name = "colorPrimary" >@color/colorPrimary</ item > < item name = "colorPrimaryDark" >@color/colorPrimaryDark</ item > < item name = "colorAccent" >@color/colorAccent</ item > </ style > </ resources > |
Implementaion of the above approach:
- Create an Android app and choose any one of the above approach to hide the ActionBar.
- Define the activity. Here we simply print “GeeksforGeeks” on the screen using a Text View in Constraint Layout:
activity_main.xml
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
androidx.constraintlayout.widget.ConstraintLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
tools:context
=
"com.geeksforgeeks.gfg.MainActivity"
>
<
TextView
android:id
=
"@+id/textView"
android:layout_width
=
"129dp"
android:layout_height
=
"55dp"
android:text
=
"Geeks For Geeks"
tools:layout_editor_absoluteX
=
"148dp"
tools:layout_editor_absoluteY
=
"306dp"
/>
</
androidx.constraintlayout.widget.ConstraintLayout
>
chevron_rightfilter_none - Copy the Java code as per the approach chosen from above.
- Compile and run the app.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.