Android loading screen
Start Activity class
package com.gsm.androidproject;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.TextView;
public class StartActivity extends Activity {
//private Thread thread;
private TextView dbCreatedInfo;
private TextView loadingText;
private ProgressBar bar ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_layout);
/*
dbCreatedInfo = (TextView) findViewById(R.id.dbLoadLabel);
bar = (ProgressBar) findViewById(R.id.progressBar);
loadingText = (TextView) findViewById(R.id.loadingText);
thread = new Thread(){
@Override
public void run() {
try {
synchronized (this) {
//wait(100);
wait(5000);
runOnUiThread(new Runnable() {
@Override
public void run() {
dbCreatedInfo.setVisibility(View.VISIBLE);
bar.setVisibility(View.INVISIBLE);
loadingText.setVisibility(View.INVISIBLE);
//////////////////////////
}
});
}
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent mainActivity = new Intent(getApplicationContext(),MainActivity.class);
startActivity(mainActivity);
};
};
thread.start();*/
new loadingTask().execute();
}
private class loadingTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
//the ui shows loading effect
}
@Override
protected Void doInBackground(Void... params) {
// creating database asynchronously
DatabaseHelper database = new DatabaseHelper(getApplicationContext());
SQLiteDatabase db =database.getWritableDatabase();//to open the connection with the database for reading/writing
database.onCreate(db);
db.close();
return null;
}
@Override
protected void onPostExecute(Void result) {
// launch your News activity
Intent mainActivity = new Intent(getApplicationContext(),MainActivity.class);
startActivity(mainActivity);
// close this activity
finish();
}
}
}
Start Activity xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bckgnd"
tools:context=".StartActivity">
<TextView
android:id="@+id/loadingText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="31dp"
android:text="@string/loading_text" />
<TextView
android:id="@+id/dbLoadLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="31dp"
android:text="@string/created_db_text"
android:visibility="invisible" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/loadingText"
android:layout_centerHorizontal="true"
android:layout_marginBottom="18dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/progressBar"
android:layout_centerHorizontal="true"
android:layout_marginBottom="19dp"
android:text="@string/mobile_phone_market"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/black" />
</RelativeLayout>
package com.gsm.androidproject;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.TextView;
public class StartActivity extends Activity {
//private Thread thread;
private TextView dbCreatedInfo;
private TextView loadingText;
private ProgressBar bar ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_layout);
/*
dbCreatedInfo = (TextView) findViewById(R.id.dbLoadLabel);
bar = (ProgressBar) findViewById(R.id.progressBar);
loadingText = (TextView) findViewById(R.id.loadingText);
thread = new Thread(){
@Override
public void run() {
try {
synchronized (this) {
//wait(100);
wait(5000);
runOnUiThread(new Runnable() {
@Override
public void run() {
dbCreatedInfo.setVisibility(View.VISIBLE);
bar.setVisibility(View.INVISIBLE);
loadingText.setVisibility(View.INVISIBLE);
//////////////////////////
}
});
}
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent mainActivity = new Intent(getApplicationContext(),MainActivity.class);
startActivity(mainActivity);
};
};
thread.start();*/
new loadingTask().execute();
}
private class loadingTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
//the ui shows loading effect
}
@Override
protected Void doInBackground(Void... params) {
// creating database asynchronously
DatabaseHelper database = new DatabaseHelper(getApplicationContext());
SQLiteDatabase db =database.getWritableDatabase();//to open the connection with the database for reading/writing
database.onCreate(db);
db.close();
return null;
}
@Override
protected void onPostExecute(Void result) {
// launch your News activity
Intent mainActivity = new Intent(getApplicationContext(),MainActivity.class);
startActivity(mainActivity);
// close this activity
finish();
}
}
}
Start Activity xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bckgnd"
tools:context=".StartActivity">
<TextView
android:id="@+id/loadingText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="31dp"
android:text="@string/loading_text" />
<TextView
android:id="@+id/dbLoadLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="31dp"
android:text="@string/created_db_text"
android:visibility="invisible" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/loadingText"
android:layout_centerHorizontal="true"
android:layout_marginBottom="18dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/progressBar"
android:layout_centerHorizontal="true"
android:layout_marginBottom="19dp"
android:text="@string/mobile_phone_market"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/black" />
</RelativeLayout>