After Searching on google for hours, I couln't find a single tutorial which described how to hide and show system bar in Android tablets.
System bar in Android Tablets is Status Bar + Navigation Bar.
Here I am not talking about hiding system bar for a short time and then showing it again when you touch the screen. I am talking about hiding the Bar permanently and showing it only when you want to.
For hiding the system bar temporarily and showing it again on touch you can use the following code
But this will show the System Bar when you touch the screen. What If you want to hide it permanently? Is it possible ?
The answer is Yes. Read on to know how.
We cannot implement this functionality directly in our app because Android Source code doesn't allow us to do it. Then how? Guess?
Yes ! we will change the source code of Android and implement this functionality. This is the benefit of working on an Open Source Platform. You have the code and you can modify and play with it.
So lets start
Step 1: The most basic step , setting up Android Environment so that you can run the source code and debug it (if required). Follow this if you don't how to do it.
Step 2: Download the Android source code from here and set up the environment as explained there. This is the official website of Android Source code. Download and set up the code. The demo which I will show in this post is on Android_4.0.999. You might download a different version but not sure whether it will work on that. Warning :- Android source code for the version Android_4.0.999 is 22.4 GB. And building for the first time will take 6 to 8 hours depending on your system capability.
Step 3: After building the source code first time, we are now ready to make changes to hide the System Bar in Android Source code. I use eclipse as the IDE for editing and creating the Android application so all the instructions that will be shown here will be based on that. Search for the folder named SystemUI. Its in Android_4.0.999/frameworks/base/packages. Open this in eclipse as an existing project. It will show two projects. Select DreamsDockLauncher.
Step 4: Eclipse will show you many errors. Just Ignore them. SystemUI is dependent on many other projects which we have not included here and thats why its showing so many errors. Now go to src->com.android.systemui.statusbar.tablet->TabletStatusBarView.java.
This is the class which is creating and displaying system bar. Now we will modify it.
Step 5: In my implementation, my tablet is connected to a server and it will receive message from server saying whether I should I hide the bar or show it. The bar should be hidden when the tablet is restarted. TabletStatusBarView.java has a method called addStatusBarWindow() . This will create and add Bar to WindowManager. We have to modify this method.
Make WindowManager.LayoutParams lp as an Instance variable instead of a local variable.Then add the below code
public WindowManager.LayoutParams lp;
private void addStatusBarWindow() {
final View sb = makeStatusBarView();
lp = new WindowManager.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_NAVIGATION_BAR,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
| WindowManager.LayoutParams.FLAG_SPLIT_TOUCH,
// We use a pixel format of RGB565 for the status bar to save memory bandwidth and
// to ensure that the layer can be handled by HWComposer. On some devices the
// HWComposer is unable to handle SW-rendered RGBX_8888 layers.
PixelFormat.RGB_565);
// We explicitly leave FLAG_HARDWARE_ACCELERATED out of the flags. The status bar occupies
// very little screen real-estate and is updated fairly frequently. By using CPU rendering
// for the status bar, we prevent the GPU from having to wake up just to do these small
// updates, which should help keep power consumption down.
lp.gravity = getStatusBarGravity();
lp.setTitle("SystemBar");
lp.packageName = mContext.getPackageName();
WindowManagerImpl.getDefault().addView(sb, lp);
WindowManagerImpl.getDefault().removeView(sb);
}
We will allow our method to add the view to WindowManager and then remove it.
Now if you build and run it in the emulator, System bar would be gone.
Step 6: Since you do not have a server side code, we will create a dummy app which will broadcast messages and SystemUI will listen for it. We will name the dummy app as TestingSystemBar. It will have two buttons Show System Bar and Hide System Bar.
Code for two buttons
public void showSystemBar(View v)
{
Intent intent=new Intent();
intent.putExtra("type", "show");
intent.setAction("CheckingNotificationMessageForSystemBar");
sendBroadcast(intent);
}
public void hideSystemBar(View v)
{
Intent intent=new Intent();
intent.putExtra("type", "hide");
intent.setAction("CheckingNotificationMessageForSystemBar");
sendBroadcast(intent);
}
Step 7: We will create BroadcastReceiver in SystemUI. For more information on BroadCastReceiver you can see this and this.
Add this code in TabletStatusBarView.java.
public class HideSystemBar extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.v("HideSystemBar", "Hiding system bar"+intent.getStringExtra("type"));
if(intent.getStringExtra("type").equalsIgnoreCase("hide"))
{
Log.v("HideSystemBar", "Hiding system bar1");
try
{
WindowManagerImpl.getDefault().removeView(mStatusBarView);
}
catch(Exception e)
{
}
}
else
{
try
{
WindowManagerImpl.getDefault().addView(mStatusBarView, lp);
}
catch(Exception e)
{
}
}
}
}
You can catch the exception and do whatever you want to do with it.
Step 8: Register the BroadcastReceiver dynamically. Modify the code like this
protected void createAndAddWindows()
{
addStatusBarWindow();
addPanelWindows();
mContext.registerReceiver(new HideSystemBar(),new IntentFilter("CheckingNotificationMessageForSystemBar"));
}
This will add register the BroadcastReceiver.
Done. Now run both the app in same emulator.
Note : To open a tablet emulator use this command
Thats it. Done. Keep playing with open source code to learn more. In my next post I will write about RecentApp option that you find in System Bar.
Was the post useful? Or any changes that you would suggest?
System bar in Android Tablets is Status Bar + Navigation Bar.
Here I am not talking about hiding system bar for a short time and then showing it again when you touch the screen. I am talking about hiding the Bar permanently and showing it only when you want to.
For hiding the system bar temporarily and showing it again on touch you can use the following code
View v = findViewById(R.id.view_id);
v.setSystemUiVisibility(View.STATUS_BAR_HIDDEN);
But this will show the System Bar when you touch the screen. What If you want to hide it permanently? Is it possible ?
The answer is Yes. Read on to know how.
We cannot implement this functionality directly in our app because Android Source code doesn't allow us to do it. Then how? Guess?
Yes ! we will change the source code of Android and implement this functionality. This is the benefit of working on an Open Source Platform. You have the code and you can modify and play with it.
So lets start
Step 1: The most basic step , setting up Android Environment so that you can run the source code and debug it (if required). Follow this if you don't how to do it.
Step 2: Download the Android source code from here and set up the environment as explained there. This is the official website of Android Source code. Download and set up the code. The demo which I will show in this post is on Android_4.0.999. You might download a different version but not sure whether it will work on that. Warning :- Android source code for the version Android_4.0.999 is 22.4 GB. And building for the first time will take 6 to 8 hours depending on your system capability.
Step 3: After building the source code first time, we are now ready to make changes to hide the System Bar in Android Source code. I use eclipse as the IDE for editing and creating the Android application so all the instructions that will be shown here will be based on that. Search for the folder named SystemUI. Its in Android_4.0.999/frameworks/base/packages. Open this in eclipse as an existing project. It will show two projects. Select DreamsDockLauncher.
Step 4: Eclipse will show you many errors. Just Ignore them. SystemUI is dependent on many other projects which we have not included here and thats why its showing so many errors. Now go to src->com.android.systemui.statusbar.tablet->TabletStatusBarView.java.
This is the class which is creating and displaying system bar. Now we will modify it.
Step 5: In my implementation, my tablet is connected to a server and it will receive message from server saying whether I should I hide the bar or show it. The bar should be hidden when the tablet is restarted. TabletStatusBarView.java has a method called addStatusBarWindow() . This will create and add Bar to WindowManager. We have to modify this method.
Make WindowManager.LayoutParams lp as an Instance variable instead of a local variable.Then add the below code
public WindowManager.LayoutParams lp;
private void addStatusBarWindow() {
final View sb = makeStatusBarView();
lp = new WindowManager.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_NAVIGATION_BAR,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
| WindowManager.LayoutParams.FLAG_SPLIT_TOUCH,
// We use a pixel format of RGB565 for the status bar to save memory bandwidth and
// to ensure that the layer can be handled by HWComposer. On some devices the
// HWComposer is unable to handle SW-rendered RGBX_8888 layers.
PixelFormat.RGB_565);
// We explicitly leave FLAG_HARDWARE_ACCELERATED out of the flags. The status bar occupies
// very little screen real-estate and is updated fairly frequently. By using CPU rendering
// for the status bar, we prevent the GPU from having to wake up just to do these small
// updates, which should help keep power consumption down.
lp.gravity = getStatusBarGravity();
lp.setTitle("SystemBar");
lp.packageName = mContext.getPackageName();
WindowManagerImpl.getDefault().addView(sb, lp);
WindowManagerImpl.getDefault().removeView(sb);
}
We will allow our method to add the view to WindowManager and then remove it.
Now if you build and run it in the emulator, System bar would be gone.
Step 6: Since you do not have a server side code, we will create a dummy app which will broadcast messages and SystemUI will listen for it. We will name the dummy app as TestingSystemBar. It will have two buttons Show System Bar and Hide System Bar.
Code for two buttons
public void showSystemBar(View v)
{
Intent intent=new Intent();
intent.putExtra("type", "show");
intent.setAction("CheckingNotificationMessageForSystemBar");
sendBroadcast(intent);
}
public void hideSystemBar(View v)
{
Intent intent=new Intent();
intent.putExtra("type", "hide");
intent.setAction("CheckingNotificationMessageForSystemBar");
sendBroadcast(intent);
}
Step 7: We will create BroadcastReceiver in SystemUI. For more information on BroadCastReceiver you can see this and this.
Add this code in TabletStatusBarView.java.
public class HideSystemBar extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.v("HideSystemBar", "Hiding system bar"+intent.getStringExtra("type"));
if(intent.getStringExtra("type").equalsIgnoreCase("hide"))
{
Log.v("HideSystemBar", "Hiding system bar1");
try
{
WindowManagerImpl.getDefault().removeView(mStatusBarView);
}
catch(Exception e)
{
}
}
else
{
try
{
WindowManagerImpl.getDefault().addView(mStatusBarView, lp);
}
catch(Exception e)
{
}
}
}
}
You can catch the exception and do whatever you want to do with it.
Step 8: Register the BroadcastReceiver dynamically. Modify the code like this
protected void createAndAddWindows()
{
addStatusBarWindow();
addPanelWindows();
mContext.registerReceiver(new HideSystemBar(),new IntentFilter("CheckingNotificationMessageForSystemBar"));
}
This will add register the BroadcastReceiver.
Done. Now run both the app in same emulator.
Note : To open a tablet emulator use this command
out/host/linux-x86/bin/emulator -sysdir out/target/product/generic/ -system out/target/product/generic/system.img -ramdisk out/target/product/generic/ramdisk.img -data out/target/product/generic/userdata.img -skindir "your sdk location"/android-sdk-linux/platforms/android-16/skins -skin WXGA800-7in -scale 0.7 -memory 512 -partition-size 1024
Thats it. Done. Keep playing with open source code to learn more. In my next post I will write about RecentApp option that you find in System Bar.
Was the post useful? Or any changes that you would suggest?