Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display a loading overlay on Android screen

I'm looking to display an overlay over the screen that shows a little loading ticker or possibly even some text whilst my app attempts to log into the server. My login screen is all inside of a vertical linear layout.

The effect I'm trying to achieve is something like this: http://docs.xamarin.com/recipes/ios/standard_controls/popovers/display_a_loading_message

like image 639
James Monger Avatar asked Aug 02 '13 15:08

James Monger


People also ask

Which view is used to displays loading spinner?

Spinner is used to display progress of those tasks whose total time of completion is unknown. In order to use that, you just need to define it in the xml like this. spinner. setVisibility(View.

What is screen overlay detected?

If you see a 'Screen overlay detected' error (see example image below), it's caused by conflict between a running app and a newly installed app requesting permission to display info on multiple screens (e.g., messengers, alerts, battery status, etc.). Apps. Settings. .

What is overlay permission in android?

When an app asks for permission to display overlays, the user will be sent to the general 'Display over other apps' permission list, so they'll have to find the app in the list and select it there. Not a big deal, but it does add a bit of friction that may prevent users from blindly giving malware access to overlays.


1 Answers

Maybe too late, but I guess somebody might find it useful.

Activity:

public class MainActivity extends Activity implements View.OnClickListener {      String myLog = "myLog";      AlphaAnimation inAnimation;     AlphaAnimation outAnimation;      FrameLayout progressBarHolder;     Button button;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          button = (Button) findViewById(R.id.button);         progressBarHolder = (FrameLayout) findViewById(R.id.progressBarHolder);          button.setOnClickListener(this);      }      @Override     public void onClick(View v) {         switch (v.getId()) {             case R.id.button:                 new MyTask().execute();                 break;         }      }      private class MyTask extends AsyncTask <Void, Void, Void> {          @Override         protected void onPreExecute() {             super.onPreExecute();             button.setEnabled(false);             inAnimation = new AlphaAnimation(0f, 1f);             inAnimation.setDuration(200);             progressBarHolder.setAnimation(inAnimation);             progressBarHolder.setVisibility(View.VISIBLE);         }          @Override         protected void onPostExecute(Void aVoid) {             super.onPostExecute(aVoid);             outAnimation = new AlphaAnimation(1f, 0f);             outAnimation.setDuration(200);             progressBarHolder.setAnimation(outAnimation);             progressBarHolder.setVisibility(View.GONE);             button.setEnabled(true);         }          @Override         protected Void doInBackground(Void... params) {             try {                 for (int i = 0; i < 5; i++) {                     Log.d(myLog, "Emulating some task.. Step " + i);                     TimeUnit.SECONDS.sleep(1);                 }             } catch (InterruptedException e) {                 e.printStackTrace();             }             return null;         }     }  } 

Layout xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context=".MainActivity">      <Button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Start doing stuff"         android:id="@+id/button"         android:layout_below="@+id/textView"         android:layout_centerHorizontal="true" />      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textAppearance="?android:attr/textAppearanceLarge"         android:text="Do Some Stuff"         android:id="@+id/textView"         android:layout_alignParentTop="true"         android:layout_centerHorizontal="true" />      <FrameLayout         android:id="@+id/progressBarHolder"         android:animateLayoutChanges="true"         android:visibility="gone"         android:alpha="0.4"         android:background="#000000"         android:layout_width="match_parent"         android:layout_height="match_parent">          <ProgressBar             style="?android:attr/progressBarStyleLarge"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:indeterminate="true"             android:layout_gravity="center" />     </FrameLayout> </RelativeLayout> 
like image 181
Kostya Buts Avatar answered Sep 21 '22 21:09

Kostya Buts