2

When we are opening the facebook apps for android, we would be shown a page with blue background and the word "facebook", before we are shown the contents of the app. I wish to add a page when user opens my apps, similar with the facebook apps. How to implement it?

Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
Jennifer
  • 83
  • 1
  • 2
  • 9

2 Answers2

2

It is called splashscreen. this is how you implement:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    /* code for Splashscreen that appears for 3s when app start*/
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent i = new Intent(MainActivity.this, UserManual.class);
            startActivity(i);
            finish();
        }
    }, 3000);

  }

}

Splashscreen waits for 3 seconds and then next activity starts.

Note: I guess you are a beginner to Android development. So just for the sake of information, this not the only way to implement. there are other ways too. Happy coding..:)

Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
2

To achieve this, create a "WelcomeActivity" and make it your Main activity.

In your AndroidManifest.xml

       <activity
            android:name="your.package.name.WelcomeActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

then in your WelcomeActivity.java, do this

public class WelcomeActivity extends Activity {                         
    private static final int DELAY_BEFORE_GOING_TO_MAIN_ACTIVITY = 2000; //2 seconds
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

        // this will give you a full screen, with no action bar at the top
        getActionBar().hide();

        setContentView(R.layout.activity_welcome);                      

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(WelcomeActivity.this,MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                finish();
            }
            }, DELAY_BEFORE_GOING_TO_MAIN_ACTIVITY);                
        }
}

This will show WelcomeActivity.java activity in full screen, then transition to your Main Activity after 2 seconds.

You can add a background, a logo to your activity_welcome.xml layout and there you have it.

Michael
  • 3,699
  • 4
  • 26
  • 27