Android Loading Control
- res/anim/rotate_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/img_loading"
android:pivotX="50%"
android:pivotY="50%" />
- res/drawable/loading_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item
android:drawable="@drawable/img1"
android:duration="200"/>
<item
android:drawable="@drawable/img2"
android:duration="200"/>
<item
android:drawable="@drawable/img3"
android:duration="200"/>
</animation-list>
- Add 4 Images in drawable
- activity_main.xml
<LinearLayout 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"
android:background="@android:color/white"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity" >
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:indeterminateDrawable="@anim/rotate_anim"
android:padding="10dp" >
</ProgressBar>
<TextView
android:id="@+id/txtmessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Pleasewait" />
<Button
android:id="@+id/btnOpenLoadingDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/OpenLoadingDialog" />
</LinearLayout>
- loading_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal"
android:padding="15dp" >
<ImageView
android:id="@+id/ImgLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/loading_anim" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ClickBacktoClose"
android:textColor="@android:color/darker_gray"
android:textStyle="bold" />
</LinearLayout>
- values/string.xml
<string name="OpenLoadingDialog">Open Loading Dialog</string>
<string name="ClickBacktoClose">Click Back to Close</string>
<string name="Pleasewait">Please wait …</string>
- values/style.xml
<style name="MyLoadingDialog" parent="@android:Theme.Dialog">
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowBackground">@android:drawable/dialog_frame</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
- MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
Button btnOpenLoadingDialog = (Button) findViewById(R.id.btnOpenLoadingDialog);
btnOpenLoadingDialog.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {Intent intent = new Intent(getApplicationContext(),
LoadingView_Dialog.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent); } }); }
// on Back Pressed
@Override public void onBackPressed() { LoadingView_Dialog.getInstance().finish(); return; }}
- LoadingView_Dialog.java
public class LoadingView_Dialog extends Activity {
static LoadingView_Dialog loadingView_Dialog_Activity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loading_dialog);
loadingView_Dialog_Activity = this;
ImageView ImgLoading = (ImageView) findViewById(R.id.ImgLoading);
final AnimationDrawable myAnimationDrawable = (AnimationDrawable) ImgLoading.getDrawable();
ImgLoading.post(new Runnable() {
@Override
public void run() {
myAnimationDrawable.start();
}
});
}
public static LoadingView_Dialog getInstance() {
return loadingView_Dialog_Activity;
}
}
- AndroidManifest.xml
<activity
android:name=".LoadingView_Dialog"
android:theme="@style/MyLoadingDialog" >
</activity>
No comments:
Post a Comment