Reputation: 67
I am a novice android developer. I am trying to create a splash screen. I have a png image. I have created an empty splash screen with a text box.I have 2 activities - MainActivity and SecondActivity.
code:
public class MainActivity extends AppCompatActivity {
private static int SPLASH_SCREEN_TIME_OUT=2000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i=new Intent(MainActivity.this,
SecondActivity.class);
//Intent is used to switch from one activity to another.
startActivity(i);
//invoke the SecondActivity.
finish();
//the current activity will get finished.
}
}, SPLASH_SCREEN_TIME_OUT);
}
}
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="250dp"
android:layout_height="200dp"
app:srcCompat="@drawable/geeks"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.447" />
</androidx.constraintlayout.widget.ConstraintLayout>
But app:srcCompat="@drawable/geeks" is in red color. Means it is not resolved. Where shall I copy the png image for the splash screen? I have not copied the image anywhere in android studio. Without the image view, the splash screen runs fine showing hello world.
Upvotes: 2
Views: 1241
Reputation: 31
Few things to keep in mind.
The image size should be low (not more than 150-200kb)
Instead of adding text and image on the activity, design a splash screen using Illustrator or any other medium and save it in png format.
Store this in the res/drawable
.
In the activity_main, just add line android:background ="@drawable/geeks"
.
In AndroidManifest.xml, add, android:theme="@style/Theme.<app name>.NoActionBar"
Upvotes: 2
Reputation: 3027
first you need to put your image in res/drawable
folder:
and then access it by app:srcCompat
or android:src
attribute of ImageView
inside your activity_main.xml
:
app:srcCompat="@drawable/company_logo"
android:src="@drawable/company_logo"
Upvotes: 1