Search code examples
androidandroid-activityfullscreenonresume

How to properly make an activity FullScreen - Issues with Views when trying to hide all bars


I have a simple activity that I want to display in full screen - removing status bar, action bar and title bar.

I have few issues:

  1. During the 'hide' operation, when the activity launches - there is a noticeable lag. It seems that first it hides the ActionBar, and only afterwards removes entirely from the layout. I don't mind that, but the operation is noticeable and there's a lag between the hiding phase to the removal phase. How do I get rid of the lag?
  2. I want the activity to be in fullscreen also when resuming to it (e.g. going to another app, then back to this activity from Recent view). Problem is, when I implement onResume() to make the hidings - the activity behaves odd - TextView isn't showing, ImageView is suddenly not responding to clicks. When not using onResume() method at all - everything is working great, as expected.

My Activity code:

public class ListItemActivity extends AppCompatActivity implements View.OnClickListener {
    private final static String TAG = "ListItemActivity";
    private ScrollView mSvLyrics;
    private TextView mTvLyrics;
    private TextView mErrorMessageDisplay;
    private ProgressBar mLoadingIndicator;

    /**
     * Shows error message view while hiding other results related views
     */
    private void showErrorMessage(String s) {
        mLoadingIndicator.setVisibility(View.INVISIBLE);
        mSvLyrics.setVisibility(View.INVISIBLE);
        mErrorMessageDisplay.setText(s);
        mErrorMessageDisplay.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_list_item);

        /* Set click listener for close image */
        findViewById(R.id.iv_close).setOnClickListener(this);

        /* Init fields and needed params */
        mSvLyrics = findViewById(R.id.sv_lyrics);
        mTvLyrics = findViewById(R.id.tv_lyrics);
        mErrorMessageDisplay = findViewById(R.id.tv_item_error_message_display);
        mLoadingIndicator = findViewById(R.id.pb_item_loading_indicator);
        final Intent intent = getIntent();

        /* Set Track properties in the views */
        ((TextView)findViewById(R.id.tv_item_artist)).setText(intent.getStringExtra(LyricsAPI.KEY_ARTIST_NAME));
        ((TextView)findViewById(R.id.tv_item_title)).setText(intent.getStringExtra(LyricsAPI.KEY_TRACK_NAME));

        /* Fetch Lyrics for the track */
        URL lyricsUrl = AidUtils.buildUrl(intent.getStringExtra(LyricsAPI.KEY_TRACK_ID), LyricsAPI.RequestType.TRACK_LYRICS_GET);
        new AsyncFetchLyrics().execute(lyricsUrl);
    }

    @Override
    protected void onResume() {
        super.onResume();
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_list_item);
    }

    @Override
    public void onClick(View v) {
        finish();
    }

    public class AsyncFetchLyrics extends AsyncTask<URL, Void, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mErrorMessageDisplay.setVisibility(View.INVISIBLE);
            mSvLyrics.setVisibility(View.INVISIBLE);
            mLoadingIndicator.setVisibility(View.VISIBLE);
        }

        @Override
        protected String doInBackground(URL... urls) {
            URL url = urls[0];
            String response = null;
            try {
                response = AidUtils.getResponseFromHttpUrl(url);
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }

            String lyrics = null;
            try {
                lyrics = AidUtils.getLyricsFromJsonStr(response);
            } catch (LyricsAPIException e) {
                Log.e(TAG, "Failed to fetch data from lyrics API", e);
            }

            return lyrics;
        }

        @Override
        protected void onPostExecute(String lyrics) {
            if(null == lyrics) {
                showErrorMessage(getString(R.string.error_message));
                return;
            }

            if(lyrics.isEmpty()) {
                showErrorMessage(getString(R.string.no_results));
                return;
            }

            mLoadingIndicator.setVisibility(View.INVISIBLE);
            mErrorMessageDisplay.setVisibility(View.INVISIBLE);
            mTvLyrics.setText(lyrics);
            mSvLyrics.setVisibility(View.VISIBLE);
        }
    }
}

My XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:padding="0dp"
    android:gravity="center">

    <ImageView
        android:id="@+id/iv_close"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:adjustViewBounds="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="5dp"
        android:layout_marginRight="5dp"
        android:src="@android:drawable/ic_menu_close_clear_cancel"
        android:scaleType="fitCenter"
        android:clickable="true"/>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="30dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_item_artist"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/example_text"
                android:textSize="35sp"
                android:textStyle="bold"
                android:textColor="@color/app_text_color"
                android:textAlignment="center"
                android:maxLines="2"
                android:ellipsize="end"
                android:padding="5dp"
                android:background="@android:color/transparent">
            </TextView>

            <TextView
                android:id="@+id/tv_item_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/example_text"
                android:textSize="30sp"
                android:textAlignment="center"
                android:textColor="@color/app_text_color"
                android:maxLines="1"
                android:ellipsize="end"
                android:padding="5dp"
                android:background="@android:color/transparent">
            </TextView>
        </LinearLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ScrollView
                android:id="@+id/sv_lyrics"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:scrollbars="vertical"
                android:fillViewport="true"
                android:layout_marginTop="40dp"
                android:layout_marginBottom="25dp"
                android:layout_marginRight="20dp"
                android:layout_marginLeft="20dp"
                android:visibility="invisible">

                <TextView
                    android:id="@+id/tv_lyrics"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:textStyle="italic"
                    android:fontFamily="monospace"
                    android:text="@string/example_text"
                    android:textAlignment="center"
                    android:textSize="16sp"/>
            </ScrollView>

            <TextView
                android:id="@+id/tv_item_error_message_display"
                android:textSize="20sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:padding="20dp"
                android:text="@string/error_message"
                android:textStyle="italic"
                android:textColor="@android:color/holo_red_light"
                android:visibility="invisible" />

            <ProgressBar
                android:id="@+id/pb_item_loading_indicator"
                android:layout_height="42dp"
                android:layout_width="42dp"
                android:layout_gravity="center_vertical|center_horizontal"
                android:visibility="invisible" />
        </FrameLayout>
    </LinearLayout>
</RelativeLayout>

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.bilyrics"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="26" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:debuggable="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="com.example.android.bilyrics.MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.android.bilyrics.ListItemActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:theme="@android:style/Theme.AppCompat.Light.NoActionBar" >
        </activity>
        <meta-data
            android:name="android.support.VERSION"
            android:value="26.1.0" />
        <meta-data
            android:name="android.arch.lifecycle.VERSION"
            android:value="27.0.0-SNAPSHOT" />
    </application>
</manifest>

Solution

  • You can do it by applying the theme for the activity.

    In style.xml

     <style name="FullScreenTheme" 
           parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>
    

    And in manifest.xml

    <activity android:name=".MyActivity"
    android:label="@string/app_name"
    android:theme="@style/FullScreenTheme"/>