Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get rid of unnecessary root layouts for fullscreen activities

Tags:

How to get rid of unnecessary root layouts for fullscreen activities?

I have a simple fullscreen activity. The layout inspector shows a large hierarchy of root layouts provided by Android, that I don't need in fullscreen.

<code>Tools > Android > Layout Inspector</code>

Can I get rid of them?

  • If YES, how?
  • If NO, why not?

The testing setup

Open Android Studio. Create a new project. Accept the default settings. Choose "Empty Activity". You get this manifest:

<application     android:allowBackup="true"     android:icon="@mipmap/ic_launcher"     android:label="@string/app_name"     android:roundIcon="@mipmap/ic_launcher_round"     android:supportsRtl="true"     android:theme="@style/AppTheme">     <activity android:name=".MainActivity">         <intent-filter>             <action android:name="android.intent.action.MAIN" />              <category android:name="android.intent.category.LAUNCHER" />         </intent-filter>     </activity> </application> 

You get this layout/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context="com.blcknx.myapplication.MainActivity">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Hello World!"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintLeft_toLeftOf="parent"         app:layout_constraintRight_toRightOf="parent"         app:layout_constraintTop_toTopOf="parent" />  </android.support.constraint.ConstraintLayout> 

Call Tools > Android > Layout Inspector to see the generated root templates.

To make it fullscreen, just add an id HelloWorld to the TextView in the layout and update MainActivity.java:

public class MainActivity extends AppCompatActivity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         TextView layout = findViewById(R.id.HelloWorld);         layout.setSystemUiVisibility(                 View.SYSTEM_UI_FLAG_LOW_PROFILE                         | View.SYSTEM_UI_FLAG_FULLSCREEN                         | View.SYSTEM_UI_FLAG_LAYOUT_STABLE                         | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY                         | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION                         | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION         );     } } 

The flags are hiding the navigation, but they don't remove the unused layouts.

Your answer

Use a simple TextView. Show it fullscreen. Check by Tools > Android > Layout Inspector how far you removed the root layouts. Show a screenshot. Show your code.

like image 985
Blcknx Avatar asked Mar 01 '18 16:03

Blcknx


Video Answer


1 Answers

You can get rid of action_bar_container having changed your activity's theme from DarkActionBar to NoActionBar:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">     ... </style> 

Then you'll get following tree:

enter image description here

If you want to go even more and get rid of ContentFrameLayout, than you can do this:

      override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)          val contentView = findViewById(android.R.id.content)         val parent = contentView.parent as ViewGroup         parent.removeView(contentView)         LayoutInflater.from(this).inflate(R.layout.activity_main, parent)     }  

This will be the view hierarchy tree:

enter image description here

Not sure whether you should remove android.R.id.content view, maybe some libraries are assuming that there should exist such a view.

As to action_mode_bar_stubs: you should not be concerned about them, as long as those are ViewStubs, which do not affect overall performance, because they are lazily inflated into view hierarchy.

like image 167
azizbekian Avatar answered Sep 20 '22 20:09

azizbekian