Multiple Step Actions on one Button Click - java

I am currently trying to solve a java based problem on Android Studio.
I have puzzled my head over this problem looking in many forums and webpages to not find any solution in the last two days. So I am seeking for help here now.
I have programmed a Button that when clicked causes a textview to swipe out of the screen with an animation. After that I would like the old text ("First Text") of the Textview to be replaced with another text ("New Text") appearing on the same place where the old text was. All of this should happen with only one click on the button step-after-step.
My problem with my code is that the old text is replaced by the new text first and then causes the animation.
Does anybody now a solution for this problem?
I would be really very grateful for any help!
This is my code below.
public class FirstActivity extends AppCompatActivity {
Animation slideleft;
Button btn1;
TextView txt1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
btn1 = (Button) findViewById(R.id.btn1);
txt1 = (TextView) findViewById(R.id.txt1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
slideleft = AnimationUtils.loadAnimation(FirstActivity.this, R.anim.slide_left);
txt1.startAnimation(slideleft);
txt1.setText("New Text");
}
});
}
}

you can use onAnimationEnd method of animation listener and change the text inside it.
animation.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationEnd(Animation anim) {
txt1 = (TextView) findViewById(R.id.txt1);
txt1.setText("New Text");
}
});

you can place which event execute after the animation place your code
anim.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationStart(Animation arg0) {
}
#Override
public void onAnimationRepeat(Animation arg0) {
}
#Override
public void onAnimationEnd(Animation arg0) {
//your_code
}
});

Related

How I can create button (Java) which will display image. When I onclick button

It is code due to him I could make my button to start music, now I want my button to start my music + new image. (Initially image shoulndnt be visible, after clicking buttton it should start display new image and my music)
P.s with music are not problems
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (Button)findViewById(R.id.clickme);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.sample);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mp.start();
}
});
}
enter image description here
use this code :
private Button btn;
private ImageView imgDisplay;
private MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
btn=(Button)findViewById(R.id.clickme);
imgDisplay=(ImageView) findViewById(R.id.img_display);
imgDisplay.setVisibility(View.GONE); //or use INVISIBLE
mp = MediaPlayer.create(this, R.raw.sample);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handleMusicState(mp);
}
});
}
private void handleMusicState(MediaPlayer mediaPlayer){
if (mediaPlayer.isPlaying()) mediaPlayer.pause();
else mediaPlayer.start();
ImageDisplay();
}
private void ImageDisplay(){
if (mp.isPlaying()){
imgDisplay.setVisibility(View.VISIBLE);
}else{
imgDisplay.setVisibility(View.GONE);
}
}
In your XML add ImageView and set its property to invisible or Gone..
(Invisible will keep the image space in layout & Gone will
remove the image from layout & space will be filled by the next element.)
and in the Java create reference of ImageView & on button tap set its property to visible.
Example (Without following proper language syntax):
XML:-
<ImageView
.....imageId
.....width
.....height
/>
Java:-
ImageView image = findViewById(R.id.imageId);
image.setVisibility = View.INVISIBLE;
// on button click:
onButtonClick(){
image.setVisibility = View.VISIBLE;
}
Please use the accurate syntax that language have, here is only the logic is provided.

How to Visible/Invisible using OnClickListener?

I am working on FAQ page I don't want to use Expandable list view and stuff.
So I set 2 TextViews(1 for Question and 1 for Answer) and made one clickable.
The above image shows when the first textview mfaq is clicked it sets second one mAns to visible.
The below code works well to Set the mAns textview visible:
public class faq extends AppCompatActivity {
TextView mfaq,mAns;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_faq);
mfaq=findViewById(R.id.faq);
mAns=findViewById(R.id.ans);
mfaq.setOnClickListener(new View.OnClickListener() {
int counter=0; //setting counter to count onclick
#Override
public void onClick(View view) {
++counter; //incrementing counter first click
if(counter==1){
mAns.setVisibility(View.VISIBLE);
}
//this sets mAns visible , but when i click on it again i want it to hide the text view
counter=0; //resetting the counter
}
});
}
}
So I want to set the visibilty to gone when the textview is clicked again(Should function like Click-visible,ClickAgain-Invisible,Repeat).
Note-I am a beginner please try to explain me what the code is doing so I learn more :)
Thanks.
If I understand well you wanna hide/show your textview each time you click on the other text?
mfaq.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mAns.getVisibility() == View.VISIBLE){
mAns.setVisibility(View.GONE);
}
else
mAns.setVisibility(View.VISIBLE);
}
});

I Would Like To Have A Single Button Display A Different Line Of Text Each Consecutive Time It's Pressed in Android Studio

I'm extremely new to coding, so apologies if this question is trivial or the answer is easily found somewhere. I have searched, but I cannot find anything that helps.
Basically, I'm trying to code a simple, 2 button app in Android Studio.
Button1 is meant to simply display a series of commands to the user via text box.
Button2 merely resets.
My problem is, I would like Button1 to change what's displayed in the text view each time it is pressed, but I cannot figure out how to do so. I don't want to make 6 or 7 buttons.
Basically I would like it to run as follows;
Text = "Pick a number"
user presses Button1
Text = "Add 15" (This is as far as I've gotten)
user presses Button1
Text = "Multiply times 5"
user presses Button1 etc. etc. etc.
If anybody could please explain or usher me in the right direction, I would be greatly appreciative.
You can use button.setOnClickListener
public class MyActivity extends Activity {
EditText et;
Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout_id);
et = (EditText)findViewById(R.id.edittext);
button = (Button)findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Perform your Logic here.
et.setText("New text");
}
});
}
}
you can use a globle and a swithchcase
public class MyActivity extends Activity {
EditText et;
int CLICKS=0;
Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout_id);
et = (EditText)findViewById(R.id.edittext);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CLICKS++;
switch(clicks)
{
case 1:
et.setText("Pick a number");
break;
case 2:
et.setText("Add 15");
break;
case 3:
et.setText("Multiply times 5");
break;
}
}
});
}
}

Buttons outside the onCreate

Always in my apps I added buttons in void onCreate, but now I'm trying to do app with more buttons (about 10). I would like to all buttons active on start app.
In my opinion it is too much buttons to add in this onCreate and app will be starting to long.
I tried to put this:
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
myMethod();
}
})
out of onCreate
but AndroidStudio underlines setOnClickListener and view
I don't have ideas, how and where can i add button out of onCreate.
If you don't want to overcrowd your oncreate method, then create a clicklistener outside onCreate anywhere in activity and in onCreate just set it.
onCreate :
edit_a_member = (Button) findViewById(R.id.edit_member);
delete_a_member = (Button) findViewById(R.id.delete_member);
edit_a_member.setOnClickListener(handleClick);
delete_a_member.setOnClickListener(handleClick);
clickListener:
private View.OnClickListener handleClick = new View.OnClickListener() {
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.edit_member:
member_selected = EDIT_MEMBER_SELECTED;
callDialog();
break;
case R.id.delete_member:
callDeleteAlert();
break;
}
}
};
You can simply add a separate method for your buttons in the same class, e.g.:
public void onCreate(...){
//Standard setup of views or whatever you want to do here
this.addButtons();
}
private void addButtons(){
Button b1 = new Button("Hi");
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
myMethod();
}
});
Button b2 = new Button("Hi to you too");
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
myMethod();
}
});
}
This is an example. You can do this in soooo many ways. I feel like you should thoroughly learn Java's fundamental Object Oriented programming, because that's really what your question suggests you don't understand. Go follow a youtube tutorial. I always like "The New Boston"'s Java tutorial series on youtube.
PS: You can make code like this beautiful under the 'Words of wisdom': Don't repeat yourself
If you have to do a lot of work in your onCreate but you are worried that the UI will take too long to load you can always post a delayed runnable to a handler so in the onCreate method put :
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//add your code here
}
},10);
what this will do is your UI will load then the code in your Runnable will be executed 10 milliseconds after your UI loads thus your app will not take too long to load the UI, even though in your case I doubt it would be necessary.
If you are declaring the buttons in xml file :
Add these properties in each button Declaration in your Xml :
android:clickable="true"
android:onClick="onClick"
And now in Activity Class create a method like this :
public void onClick(View v){
switch(v.getId){
case R.id.{buttons_id_in_xml}
(Your Code)
break;
(Like for others)
}
}
If you want to add buttons dynamically :
Create a method to add the button like this:
void addButton(String buttonName, int button id){
Button button = new Button(this);
button.setText("Push Me");
(add it to parent Layout of xml)
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(id){
case id1:
(handle )
break;
(like for others)
}
}
});
}
The best way to do this is:
add implements View.OnClickListener to
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
// declare variables
private Button mBtn1;
private Button mBtn2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
// make an instance to the btns
mBtn1 = findViewById(R.id.btn1);
mBtn2 = findViewById(R.id.btn2);
// set onClickListener
mBtn1.setOnClickListener(this); // with "this" you are passing the view
mBtn2.setOnClickListener(this);
}
// implement onClick
#Override
public void onClick(View view) {
// check which btn was clicked by id
switch (view.getId()) {
case R.id.btn1:
btn1Clicked();
break;
case R.id.btn2:
btn2Clicked();
break;
}
}
private void btn1Clicked() {
// your code btn1 clicked
}
private void btn2Clicked() {
// your code btn2 clicked
}
Hope this helped. Cheers!

How to disable button runtime in click event?

I searched on Google and StackOverflow but I did not find really what I need. I want to disable a button runtime in a click event but it does not work. I have 2 buttons whose names are ButtonA and ButtonB
My code:
ButtonA.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
ButtonA.setEnabled(false);
ButtonA.invalidate();
ButtonB.setEnabled(true);
ButtonB.invalidate();
}
});
ButtonB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
ButtonB.setEnabled(false);
ButtonB.invalidate();
ButtonA.setEnabled(true);
ButtonA.invalidate();
}
});
I use onclick listener events in my list adapter class
If I refresh the screen it works but I want to do it runtime. When I click the button I want to see it will be disabled.
How can I fix this?
You can use clickable() method for this.
ButtonA.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
ButtonA.setClickable(false);
ButtonA.invalidate();
ButtonB.setClickable(true);
ButtonB.invalidate();
}
});
ButtonB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
ButtonB.setClickable(false);
ButtonB.invalidate();
ButtonA.setClickable(true);
ButtonA.invalidate();
}
});
Or you can try this:
Button b=(Button) findViewById(R.id.btnB);
b.setEnabled(true);
The thing is :
it should be defined by final,
like this;
final Button ButtonA,ButtonB;

Categories

Resources