AnimationListener inside method - java

I'm working on an older project, and now I'm stuck a little bit.
I have 9 buttons on one layout, and they all are connected to their methods who call their Intents.
Like this.. button 1
public void button1_click(View view){
Intent intent = new Intent(getApplicationContext(), Btn1.class);
startActivity(intent);
}
Now I after a couple of years, I want to implement an simple animation where the button will do something (whats the animation it's irrelevant).
Now in my Animation method - startAnimation
private void startAnimation(final View view){
final Animation wigle = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.zoom_in);
view.startAnimation(wigle);
wigle.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
}
});
}
I want to wait for the animation to finish and then to start my intent.
The button 1 with this method looks like
public void button1_click(View view){
startAnimation(view);
Intent intent = new Intent(getApplicationContext(), Btn1.class);
startActivity(intent);
}
But my application starts the animation and then starts the Intent.
What should I implement to not to implement setAnimationListener to all buttons.

try this method
private void startAnimation(final View view){
final Animation wigle = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.zoom_in);
view.startAnimation(wigle);
wigle.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation) {
Intent intent;
switch (view.getId()){
case R.id.btn1_id:
intent = new Intent(getApplicationContext(), Btn1.class);
startActivity(intent);
break;
case R.id.btn2_id:
intent = new Intent(getApplicationContext(), Btn2.class);
startActivity(intent);
break;
.......
............
......
}
}
});
}
use method as
public void button1_click(View view){
startAnimation(view);
disableAllButtons();
}

Related

android button animation. How to get animation first, on click before activity

I made a button for Android that rotates on click, but when I set a button and new activity, when I click it's just set me to new activity.
I need just this: when I click on that button, first to do animation e.g. rotate, then to execute a new activity. Here is my code:
ImageButton pandaButton2 = (ImageButton) findViewById(R.id.pandaButton2);
pandaButton2.setOnClickListener(new OnClickListener(){
public void onClick(View v){
v.startAnimation(pandarotate);
startActivity(new Intent("com.example.threepandas.MENU"));
}
});
You can set animation listener, when finish animation start your activity.
Please refer this link
Example code (must update based on your purpose):
ImageButton pandaButton2 = (ImageButton) findViewById(R.id.pandaButton2);
pandaButton2.setOnClickListener(new OnClickListener(){
public void onClick(View v){
v.startAnimation(pandarotate);
}
});
pandarotate.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
startActivity(new Intent("com.example.threepandas.MENU"));
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
insert a delay for the length of the animation after the animation starts and before the activity starts
try {
Thread.sleep(1000); //1000 milliseconds is one second.
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
Here is the solution I use for this problem (got it from the Google I/O App Source Code on Github)
private static final int DELAY = 250;
private Handler mHandler;
#Override
public void onClick(final View view) {
switch (view.getId()) {
case R.id.button:
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
}
}, DELAY);
break;
}
}

Splash screen depends onClick to move on

Currently, my splash screen directly link to my mainActivity. The splash screen ends when the rotate duration finish. android:duration="8000"
Then I have alert dialog at the end there.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final ImageView iv = (ImageView) findViewById(R.id.imageView);
final Animation an = AnimationUtils.loadAnimation(getBaseContext(), R.animator.rotate);
final Animation an2 = AnimationUtils.loadAnimation(getBaseContext(), R.anim.abc_fade_out);
iv.startAnimation(an);
an.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
iv.startAnimation(an2);
finish();
Intent i = new Intent(getBaseContext(), MainActivity.class);
startActivity(i);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
new AlertDialog.Builder(this).setTitle("Promo Code").setMessage("Type in 'VC2001' for a 10% discount. Only applicable for VC-20U").setNeutralButton("Close", null).show();
}
`
So, is there any way to like end the splash screen after I click on close ?
This may really sounds stupid cause I'm really new in android
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(yoursplashscreen.this);
dialogBuilder.setTitle("Promo Code");
dialogBuilder.setMessage("Type in 'VC2001' for a 10% discount. Only applicable for VC-20U");
dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
// finish your splash screen here and set intent to your new activity
finish();
Intent i = new Intent(getBaseContext(), MainActivity.class);
startActivity(i);
}
});
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();

Open a class page with an Intent

I am using Android Studio, and I want to open a class page with an intent.
My code currently looks like this
public class StartUpScreen extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_up_page);
final ImageView MyImageView =(ImageView) findViewById(R.id.imageView);
final Animation Animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.startup);
final Animation Animation2 = AnimationUtils.loadAnimation(getBaseContext(), R.anim.abc_fade_out);
MyImageView.startAnimation(Animation);
Animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
MyImageView.startAnimation(Animation2);
finish();
Intent a = new Intent(this,MainPage.class);
startActivity(a);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
}
All looks good, but my Intent has a red line from (this,MainPage.class); I have looked and tried changing it with no luck.
Can anyone please help?

Make button visible with StaticClass and Save it

I still got confused with StaticClass code which given from my friend to alternative save besides Shared Preferences, already 3 days I tried learned the code and asked but there is still a little problem with a code
this is the latest following code in my selectlevel.class that i have perfected
public class selectlevel extends Activity {
Button f1, f2, f3;
ImageView f2lock, f3lock;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.selectlevel);
f1=(Button)findViewById(R.id.f1);
f1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
// TODO Auto-generated method stub
Intent level1 = new Intent ();
level1.setClassName ("com.example.game", "com.example.game.levelone");
startActivity (level1);
}
});
f2=(Button)findViewById(R.id.f2);
f2lock=(ImageView)findViewById(R.id.f2lock);
f2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
// TODO Auto-generated method stub
Intent level2 = new Intent ();
level2.setClassName ("com.example.game", "com.example.game.leveltwo");
startActivity (level2);
}
});
updateLevels();
}
static class PlayerProgress {
private static int progress = 0;
public static void updateProgress(int levelNumber) {
progress = levelNumber;
}
public static int getPlayerProgress() {
return progress;
}
}
public void updateLevels() {
int progress = PlayerProgress.getPlayerProgress();
switch(progress) {
case 1:
f2.setVisibility(View.VISIBLE);
f2lock.setVisibility(View.GONE);
break;
case 2:
break;
// You can expand this to as many levels you'd like.
}
}
and i had use this in my levelone.class to send update progress to 1
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
selectlevel.PlayerProgress.updateProgress(1);
finish();
but when levelone.class finish, f2 button still GONE and f2lock still VISIBLE
nothing is change in selectlevel.class
i wonder it can be visible like this and still visible if the game re-open because the button visibility is saved
can anyone help me to fix a problem in my code? or give explain with another code as solution?
try to call the updateLevels() function also in your onClick funtion like this:
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
selectlevel.PlayerProgress.updateProgress(1);
selectlevel.PlayerProgress.updateLevels();
finish();
}

How to work with onBackPressed() in Android?

I have Activity 'A', Activity 'B', Activity 'C', Activity 'D', Activity 'E', Activity 'E' . I want to exit the Activity click on onBackPressed(). When i run the app it goes to Activity 'A' it does not exit the app. How can i implement with this.
Here is my Code
public class MainActivity extends Activity {
TableLayout table_layout;
EditText firstname_et, lastname_et;
Button addmem_btn;
DatabaseHelper databaseHelper = new DatabaseHelper(this);
protected SQLiteDatabase db;
ProgressDialog PD;
ImageView imgButtonBack;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.performance_report);
table_layout = (TableLayout) findViewById(R.id.tableLayout_PerformanceReport);
//ImageView imgButtonBack;
imgButtonBack = (ImageView)findViewById(R.id.imagBackButton);
imgButtonBack.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, Employee_List.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
//finish();
}
});
}
}
#Override
public void onBackPressed()
{
AlertDialog.Builder alertbox = new AlertDialog.Builder(MainActivity.this);
alertbox.setTitle("Do you wish to exit ?");
alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// finish used for destroyed activity
finish();
}
});
alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// Nothing will be happened when clicked on no button
// of Dialog
}
});
alertbox.show();
}
}
Thanks to Appreciate.
You can try this in onBackPressed:
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
Which exits the application. And works fine for me. Hope it helps.
call this.finish() while you open your new activity(i.e going from activty A to some other activity). In your case write this
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, Employee_List.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
MainActivity.this.finish();
}
});
You can do something like this -
private exit = false;
public void onBackPressed() {
if (exit)
Home.this.finish();
else {
Toast.makeText(this, "Press Back again to Exit.",
Toast.LENGTH_SHORT).show();
exit = true;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
exit = false;
}
}, 3 * 1000);
}
}

Categories

Resources