activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.brandonmain.progressbarexample2.MainActivity">
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="10dp"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
/>
<TextView
android:id="@+id/LoadingCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Loading Complete"
android:layout_below="@+id/progressbar"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:textSize="24sp"
android:visibility="invisible"
/>
</RelativeLayout>
import android.os.Handler; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.ProgressBar; | |
import android.widget.TextView; | |
public class MainActivity extends AppCompatActivity { | |
private ProgressBar mProgressBar; | |
private TextView mLoadingText; | |
private int mProgressStatus = 0; | |
private Handler mHandler = new Handler(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mProgressBar = (ProgressBar) findViewById(R.id.progressbar); | |
mLoadingText = (TextView) findViewById(R.id.LoadingCompleteTextView); | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
while (mProgressStatus < 100){ | |
mProgressStatus++; | |
android.os.SystemClock.sleep(50); | |
mHandler.post(new Runnable() { | |
@Override | |
public void run() { | |
mProgressBar.setProgress(mProgressStatus); | |
} | |
}); | |
} | |
mHandler.post(new Runnable() { | |
@Override | |
public void run() { | |
mLoadingText.setVisibility(View.VISIBLE); | |
} | |
}); | |
} | |
}).start(); | |
} | |
} https://github.com/Salyder/progressbarExample/blob/master/app/src/main/java/com/example/brandonmain/progressbarexample2/MainActivity.java |
Comments
Post a Comment