Splash Screen in Android
In this post I will tell you how to create a Android Splash Screen which will display a ProgresBar for 5 seconds. Once the ProgressBar gets disappeared, the login screen will be displayed.
The output screen will look something like this
Designing the Splash Screen
1. Create an Android project.
2. In the main.xml file add the following
android:layout_height=”wrap_content” />
3. In the MainActivity.java add the following code.
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
static int progress;
ProgressBar progressBar;
int progressStatus = 0;
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progress = 0;
progressBar = (ProgressBar) findViewById(R.id.progressbar);
//---do some work in background thread---
new Thread(new Runnable()
{
public void run()
{
while (progressStatus < 10)
{
progressStatus = doSomeWork();
}
handler.post(new Runnable()
{
public void run()
{
Intent i = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(i);
//---0 - VISIBLE; 4 - INVISIBLE; 8 - GONE---
progressBar.setVisibility(View.GONE);
}
});
}
//---do some long running work here---
private int doSomeWork()
{
try {
//---simulate doing some work---
Thread.sleep(500);
} catch (InterruptedException e)
{
e.printStackTrace();
}
return ++progress;
}
}).start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
4. Create a new java class name it as LoginActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class LoginActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setting default screen to login.xml
setContentView(R.layout.login);
}
}
5. Add a new .xml file in the res/layout name it as login.xml
6. Modify the AndroidManifest.xml add the following
7. Run the program.
More: Designing a Login Screen in Android
No comments:
Post a Comment