Reputation: 231
I know this is common to ask, but yet I still don't know how to call the XML file. I'm just confused about how can I implement the proper calling progress bar when going to another activity. Yet there are lots of resources on site but I think this is different. All I want is When I click the button from first Activity I want to call the progress bar which separately designs as XML and then it goes to the second activity, whether it will set Visible etc. It is possible?
Reference spinKit: Github
When clicking the button in first activity it will pop the spin XML and then dismiss when second activity is already loaded.
MainActivity
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button clickButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clickButton =(Button) findViewById(R.id.buttons);
clickButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Activity2.class);
startActivity(intent);
}
});
}
}
ActivityMain
<?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">
<Button
android:id="@+id/buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Click"
android:textSize="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textColor="@color/white"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Spinkit class
import androidx.appcompat.app.AppCompatActivity;
public class Spinkit extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loadingspin);
ProgressBar progressBar = (ProgressBar)findViewById(R.id.spin_kit);
Sprite cubeGrid = new CubeGrid();
progressBar.setIndeterminateDrawable(cubeGrid);
}
}
Spinkit xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/orange"
android:gravity="center"
android:orientation="vertical">
<com.github.ybq.android.spinkit.SpinKitView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/spin_kit"
style="@style/SpinKitView.Large.CubeGrid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:SpinKit_Color="@color/white"
tools:ignore="MissingConstraints" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LOADING"
android:textStyle="bold"
android:textSize="40sp"
android:gravity="center"
android:shadowColor="@color/black"
android:textColor="@color/white"/>
</LinearLayout>
Upvotes: 0
Views: 758
Reputation: 23
Actually you have two activity. And now you want to show animation when changing activity. A professional Android App Developer chooses to use fragment since fragment makes thing easier. Since you are using activity hence I will include information of activity.
You directly can't show that animation. You must add an activity (you can directly code even if you want you can use layout either whatever you prefer).
Someone had mentioned in the comment that "it takes milisec to change activity". But if you want to delay than you can do that.
At first create an activity as another person said. Design it however you want. Then use the following code to change activity.
In MainActivity :
Intent intent = new Intent(this, SplashScreenActivity.class);
startActivity(intent);
//If you don't want user to back to the page by clicking back button then use finish();
finish();
In SplashScreenActivity :
//Use SpinKit however they have introduced https://github.com/ybq/Android-SpinKit#usage
ProgressBar progressBar = (ProgressBar)findViewById(R.id.progress);
Sprite doubleBounce = new DoubleBounce();
progressBar.setIndeterminateDrawable(doubleBounce);
new Handler().postDelayed(() ->{
Intent intent = new Intent(SplashActivity.this,HomeActivity.class);
startActivity(intent);
},1000);
//1000 is millisecond if you want to delay 10 sec than write 10000 instead of 1000
Here's a related git repo
In your activity :
Intent intent = new Intent(this,SplashScreenActivity.class);
intent.putExtra("data","home");
startActivity(intent);
In SplashScreenActivity :
//write it in global variable
Sprite doubleBounce = new DoubleBounce();
ProgressBar progressBar;
//onCreate method
progressBar = (ProgressBar)findViewById(R.id.progress);
Intent intent = getIntent();
String activity = intent.getStringExtra("data");
if(activity.equals("home")){
changeActivity(homeActivity.class);
}else if(activity.equals("secondActivity")){
changeActivity(secondActivity.class);
}
//Another class
private void changeActivity(Class activity){
progressBar.setIndeterminateDrawable(doubleBounce);
new Handler().postDelayed(() ->{
Intent intent = new Intent(this,activity);
startActivity(intent);
},1000);
}
Upvotes: 1
Reputation: 3765
This is how you can do it.
Create another activity named anything you want for loading.Eg - LoadingActivity
Make a layout file looking like this or however you want.
Then in the java class add this code in the onCreate
ProgressBar progressBar = (ProgressBar)findViewById(R.id.spin_kit);
Sprite cubeGrid = new CubeGrid();
progressBar.setIndeterminateDrawable(cubeGrid);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(this,OtherActivity.class));
finish();
}
Done!
Upvotes: 0