My app is about users picking three buttons: Sad, Happy and Confuse. If they click the Sad button, the image changes into a sad emoji. If they click the Happy button, happy emoji is displayed. Simple.
But, when I run my app, and I click any of the buttons, the alpha animation works. If I then click another button, the alpha animation does not work anymore. The alpha animation only works once. The alpha animation is only for the images.
Here's my Java code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Happy_Btn = findViewById(R.id.Happy_Btn);
Sad_Btn = findViewById(R.id.Sad_Btn);
Confuse_Btn = findViewById(R.id.Confuse_Btn);
Feeling_txtView = findViewById(R.id.Feeling_txtView);
Face_Img = findViewById(R.id.Face_Img);
Animation animation = new AlphaAnimation(0f, 1.0f);
animation.setDuration(2000);
Feeling_txtView.setText("You are feeling happy!");
Happy_Btn.setEnabled(false);
Sad_Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Sad_Btn.setEnabled(false);
Happy_Btn.setEnabled(true);
Confuse_Btn.setEnabled(true);
Feeling_txtView.setText("You are feeling sad...");
Face_Img.setAnimation(animation);
Face_Img.setImageResource(R.drawable.sad);
}
});
Happy_Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Sad_Btn.setEnabled(true);
Happy_Btn.setEnabled(false);
Confuse_Btn.setEnabled(true);
Feeling_txtView.setText("You are feeling happy!");
Face_Img.setAnimation(animation);
Face_Img.setImageResource(R.drawable.happyface);
}
});
Confuse_Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Sad_Btn.setEnabled(true);
Happy_Btn.setEnabled(true);
Confuse_Btn.setEnabled(false);
Feeling_txtView.setText("You are feeling confused?");
Face_Img.setAnimation(animation);
Face_Img.setImageResource(R.drawable.confuse);
}
});
}
(EDIT)
I made few changes to my code and then when I run it, my issue is solved! But I'm not sure if this is the right way to do it. What I did was I first cut these two lines:
Animation animation = new AlphaAnimation(0f, 1.0f);
animation.setDuration(2000);
and then paste them into each buttons (Sad, Happy and Confuse)
My app works the way I wanted it to be.
you should renew AlphaAnimation
you do it is the right
Related
I have an app that consists of 3 Activities
MainActivity
CalculatorActivity
InformationActivity
My MainActivity has a confirm button that onClick starts the CalculatorActivity and everything is done correct and working as intended.
CalculatorActivity has 2 buttons, one calculateButton that checks something and shows a message and a learnMorebutton that starts the InformationActivity.
When I am on the
CalculatorActivity for the first time everything is fine and working.Pressing the learnMoreButton navigates me to the InformationActivity.That activity looks like this :
InformationActivity:
goBackButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switchActivity();
}
});
}
public void switchActivity(){
final Intent intentObj = new Intent(this,CalculatorActivity.class);
startActivity(intentObj);
}
A goBack button that gets me back to CalculatorActivity.Going back seems to break the functionality.Although the layout is there and everything looks as it should be, pressing the buttons (calculateButton,learnMoreButton) does nothing.
CalculatorActivity :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
final Button calculateButton = (Button) findViewById(R.id.calculateId);
final Button learnMoreButton = (Button) findViewById(R.id.learnMoreButtonId);
there are some more TextView and EditText that dont show up here but you get the point.Some more methods that do the calculations ,getters and setters.
This method
public void switchActivity(){
final Intent intentObj = new Intent(this,Information_activity.class);
startActivity(intentObj);
}
But I am not using onResume() , onPause() or any methods from the lifecycle apart from onCreate().
From some search that I have done I found out that I am doing something wrong with how I manage the activity lifecycle but I still can't find the solution.The dev documents didn't help me that much and a post with kinda the same problem as mine is old.
My question is, how the navigation from InformationActivity to CalculatorActivity should be done, so the functionality doesn't break when CalculatorActivity comes back to interact with the user.Which method should be called onResume()? , onRestart()? and how should it look like?
Thanks anyone who is willing to help.
P.S: As I mentioned , I have read the documents for the lifecycle of an Activity but I haven't found the solution.
instead of starting new activity everytime, finish the informationactivity.
goBackButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
You are creating too much activities moving going back and forth this way. You can use either destroy the activity with finish(); or you can also go back to previous activity using onBackPressed();
goBackButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
Try this out
goBackButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InformationActivity.this.finish();
}
});
}
Instead of saying where to go back, you can just finish the activity and it will automatically switch you to the previous one.
I think your activities are hierarchical thus you should be able to do the following from your main calculator activity:
Intent i = new Intent(this, InformationActivity.class);
startActivityForResult(i);
Your back button add this code:
goBackButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setResult(Result.OK);
finish();
}
});
You are all suggesting the same thing.Adding
Information_Activity.this.finish()
fixed the broken functionality , though you are all correct I can pick only one answer.
Thanks
The Android code consists of two parts Java and the XML. The XML code has helped display the buttons and switches, what code to write so as to get an output of On and Off from it.
Here is an example of basic code that allows a user to access the camera once a button has been pushed in an app:
Button camera = (Button) findViewById(R.id.button1);
camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}
}
});
button1 corresponds to the android id for button in the xml script. You can then use the object that you've set the button to to set the OnClickListener. Inside of the the camera.setOnclickListener brackets is what you want to happen once the button is clicked. Public void onClick is a function that tells android to do something. I tried to put this in basic terms. If you'd like more specifics, there's really good android studio tutorial videos out there. This one is my favorite:
https://www.youtube.com/watch?v=QAbQgLGKd3Y&list=PL6gx4Cwl9DGBsvRxJJOzG4r4k_zLKrnxl
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
}
}
or
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/self_destruct"
android:onClick="selfDestruct" />
public void selfDestruct(View view) {
// do something
}
if I understand your question you are asking how to react to user click on a button or a switch..
if so, you should create an instance of OnClickListener and implement the onClick() method..
simple example:
Button b = findViewById(R.id.btn_id);
b.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});
The problem is that I have an Android app that doesn't seem to show the xml layout when I put this while loop into the class file. The loop is as follows:
while(!clicked){
button_a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player= MediaPlayer.create(GameActivity.this, R.raw.a);
player.start();
clicked = true;
letterTapped = 0;
}
});
}
The whole project works completely fine without it so I'm pretty sure that there must be something wrong with the loop that I am overlooking.
If you want me to put any other bits of code up here I will be more than happy to.
To stop listening as soon as the button is pressed, you can use this code:
button_a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player= MediaPlayer.create(GameActivity.this, R.raw.a);
player.start();
letterTapped = 0;
// Ignore further clicks
button_a.setOnClickListener(null);
// Disable button so the user knows that he can't click again
button_a.setEnabled(false);
}
});
I have a game quiz, and if a user's answer is wrong a popup screen pops up and stays on screen for 3 seconds. I do this with Handler. Since the game is time limited, they come up with solution to press back button to remove popup from the screen :) and move along. So I disabled the back button for that popup activity. BUT, now I have another problem. It seems that my Handler count time from the last click, so if I click back button on that popup screen Handler count time from that click. If I click it again, it starts from beggining. I've tried to click it 10-12 times and my popup screen was on for half a minute. :) And that's not good. How to make my popup be on for 3 seconds no metter if click back button or not during his time on?
My popup class:
public class WrongAnswer extends Activity{
TextView wrong;
String correctAnswer, correct;
public final int delayTime = 3000;
private Handler myHandler = new Handler();
public void onUserInteraction(){
myHandler.removeCallbacks(closePopup);
myHandler.postDelayed(closePopup, delayTime);
}
private Runnable closePopup = new Runnable(){
public void run(){
finish();
}
};
#Override
public void onBackPressed() {
//do nothing
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.wrong);
Bundle extras = getIntent().getExtras();
if(extras !=null) {
correct = extras.getString("correctAnswer");
}
inicijalizujVarijable();
myHandler.postDelayed(closePopup, delayTime);
}
private void inicijalizujVarijable() {
wrong = (TextView) findViewById(R.id.tvWrong);
Typeface pogresanFont = Typeface.createFromAsset(getAssets(), "Bebas.ttf");
wrong.setTypeface(pogresanFont);
Wrong.setText("Wrong answer!\nCorrect answer is:\n\n" + correct);
}
}
Remove this:
public void onUserInteraction(){
myHandler.removeCallbacks(closePopup);
myHandler.postDelayed(closePopup, delayTime);
}
It's resetting your handler on any interaction, which results in the behaviour you describe.
As a side note, it seems rather heavy to dedicate an Activity to this functionality, I think you'd be better off using a Dialog or similar.
You can show popup as Dialog and setCancelable() to false.
http://developer.android.com/reference/android/app/Dialog.html#setCancelable(boolean)
I have a linearLayout that disappears when I hit a button, it comes back when I press the button again. But it does it so fast, it doesn't look nice.
I do this via:
disappearView.setVisibility(View.GONE);
I would like to add some animation... If I just set visibity to invisible the space where the layout was is still there. So I tried this:
if (disappearView.getVisibility() == View.VISIBLE){
Animation out = AnimationUtils.makeOutAnimation(this, true);
disappearView.startAnimation(out);
disappearView.setVisibility(View.INVISIBLE);
disappearView.setVisibility(View.GONE);
}
else {
Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
disappearView.startAnimation(in);
disappearView.setVisibility(View.VISIBLE);
}
This does the animation too fast and disappears. You can't see it at all. Do I need to use a thread to start gone after invisible is set...or a delay? Or is there a better way of doing all this?
I'm not sure exactly what you're trying to accomplish...do you want the LinearLayout to fade out over a little bit of time rather than instantly disappear? And then once it fades out be removed from the parent via View.GONE?
If so, you can use an AlphaAnimation for the fade out and then attach a listener like EvZ posted:
AlphaAnimation fadeOutAnimation = new AlphaAnimation(1, 0); // start alpha, end alpha
fadeOutAnimation.setDuration(1000); // time for animation in milliseconds
fadeOutAnimation.setFillAfter(true); // make the transformation persist
fadeOutAnimation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
linearLayout.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) { }
#Override
public void onAnimationStart(Animation animation) { }
});
linearLayout.setAnimation(fadeOutAnimation);
You can try to use onAnimationEnd : Animation.AnimationListener