Splash screen is not being displayed
I have two classes as Splash.java
and Activity2.java
!! I have tried simple code for splash as:
public class Splash extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.splas开发者_如何学编程h);
Thread thread= new Thread()
{
@Override
public void run()
{
super.run();
startActivity(new Intent(Splash.this,Trial.class));
finish();
}
};
thread.start();
}
}
In my manifest I have given entry too. My code is running without error. Code of R.layout.splash
is as:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/d"
android:layout_gravity="center"/>
</LinearLayout>
Thanks!!
Try this way
setContentView(R.layout.splashscreen);
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
startActivity(new Intent(SplashScreen.this, Trial.class));
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
xml file
<?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:background="@drawable/screen1">
</LinearLayout>
public class Splash extends Activity {
RelativeLayout container;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
container = (RelativeLayout) findViewById(R.id.container);
Thread thread = new Thread(){
@Override
public void run() {
try {
sleep(3000);//sleeps for 3 second
Intent intent = new Intent(Splash.this,Activity2.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
} catch (InterruptedException e) {
// e.printStackTrace();
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
}
super.run();
}
};
thread.start();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
setSize();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
//Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
setSize();
}
}
private void setSize(){
if(container!=null) {
if (container.getChildCount() > 0) {
container.getChildAt(0).setMinimumHeight(getWindowManager().getDefaultDisplay().getHeight() * 4 / 10);
}
}
}
}
** your splash screeen layout **
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/shaded_rectangle"
android:id="@+id/container">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/splas"/>
</RelativeLayout>
**manifest**
<activity
android:name=".collegeconnect.activity.Splash"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
public class MainActivity extends AppCompatActivity {
private static int SPLASH_TIME_OUT = 4000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed
(
new Runnable()
{
@Override
public void run ()
{
Intent i = new Intent(MainActivity.this, HomeActivity.class);
startActivity(i);
finish();
}
},SPLASH_TIME_OUT
);
}
}
精彩评论