Prevent view to be in front during animation - android

I have a button that shows or hides a view. When button get pressed the view is added to layout and a translate animation begins. Second button press will remove the view from the layout.
I user addView(view, 0) to make sure the view is behind all other views. It works generally fine.
The problem occurs when the view is shown and I press the button twice very fast (sort of double click). In that case the view jumps to front during the animation.
How can I prevent this?

Just disable( setEnabled(false) ) the button and enable it in AnimationListener.onAniationEnd().
UPD: ok, that's the code, which illustrates my idea:
final Button btn = (Button) findViewById(R.id.myButton);
View view = (View) findViewById(R.id.myView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btn.setEnabled(false);
// animation here
addView(view, 0);
}
});
// somewhere else
final Button btn = (Button) findViewById(R.id.myButton);
btn.setEnabled(true);
removeView(view);

Related

How to get the text value of a button and load it into another button in Android

I am creating a word game application , I have created a grid view consisting of buttons. When I click a button in the grid view a popup windows opens which contains all the English alphabets.Now when I click any letter in my pop up window , I want that letter to appear in my grid that is the string in my pop window's button must appear in my grid view's button. , how do I do it?
This is my button's code:
button1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View container = layoutInflater.inflate(R.layout.activity_popup,null);
popupWindow = new PopupWindow(container,800,1100,true);
popupWindow.showAtLocation(constraintLayout, Gravity.NO_GRAVITY,150,400);
b1=(Button) findViewById(R.id.b1);
String s1 = b1.getText().toString();
button1.setText(s1);
container.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionevent){
popupWindow.dismiss();
return true;
}
});
}
});
My popup window's code:
b1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
String s1 = "A";
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();
}
});
Screenshots of the app
This is my grid layout where the user must enter the letters
When I click any button on the grid this is the pop up window which is displayed.
If I run this , the app is getting stopped.
It might help us if you would share the exact exception that is thrown by your application.
If I unterstand you correctly, then b1 is a button contained in your activity_popup- layout. So you might want to try b1 = (Button) container.findViewById(R.id.b1) instead. If thats not working, post your exception!

Button to hide a TextView

I've been searching for a solution for this for a while but cannot seem to get one working. There are one or two on here about this subject but I can't seem to get them going. I'm also a novice in Android and while I've been on and off playing with it for a few years, I still understand next to nothing about what I'm writing.
Basically I've got a TextView and a button. Ideally I'd like to put some text in the TextView, press a button it's gone, press the button again and it's back.
I've narrowed it down to needing to understand what findViewById(R.id.button2) does but honestly I'm a bit lost.
I've added my button code but apologies that this is such a noob question
public void onClick(Button v){
TextView t1 = (TextView)findViewById(R.id.editText);
v.setVisibility(View.GONE);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView t1 = (TextView)findViewById(R.id.TextView);
v.setVisibility(View.GONE);
}
});
}
Your code has a couple of issues. I'm not going to give you the code because that won't really help you learn. Instead I'll explain things and let you try to figure it out or come back with more explicit questions.
You know that xml file you set using setContentView? Some of the tags in it had a property android:id="xxxx". That xxxx is the id of that view, its used so you can find that view in your code. The findViewById function walks through all the views on screen and finds a view with that id and returns it. That gives you a reference to the view so you can change it. For example, you can set its visibility, set its background color, or set an OnClickListener.
So to have a button toggle the visibility of another view, you need to be able to do the following things:
1)Find the view who's visibility you want to change
2)Figure out what its visibility currently is
3)Figure out what you want it to be (the opposite of what it currently is
4)Set that visibility
You need to write a function that does all that. Then you need to do this
1)Find the button you want to use to change the visibility
2)Tell it to call your function when its pressed.
Figure out how to do each of those steps individually, and you should be able to put it together. Good luck.
findViewById(R.id.button2) finds the view with the id button2.
You can check inside onClick whether t1 is visible or not (t1.setVisibility(View.GONE); not v.setVisibility(View.GONE);), and toggle between View.GONE and View.VISIBLE.
Remember that your findViewById() should have a real id. They are normally set on the activity_name.xml.
You are using a onClick inside a onClick. Personally I recommend setting the listener manually with setOnClickListener.
There's a lot of work for you, start with these tutorials. Keep trying and try to understand what you are doing.
Look like you need a toogle button feature, here is a piece of code.
Important: you must pay heed to #GabeSechan and #SkyDriver2500 answers.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
//your other code
Button button = (Button) findViewById(R.id.button2);
final TextView t1 = (TextView) findViewById(R.id.editText);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
t1.setVisibility(t1.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
}
});
}
I'm not sure if the code will help you now. But just in case, here it is
final boolean[] isTvVisible = {false};
final TextView t1 = (TextView)findViewById(R.id.editText);
t1.setVisibility(View.GONE);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (isTvVisible[0]) {
t1.setVisibility(View.GONE);
isTvVisible[0] = false;
} else {
t1.setVisibility(View.VISIBLE);
isTvVisible[0] = true;
}
}
});

Button state across different layouts in one activity?

I have 2 buttons to switch between 2 layouts in the same activity: clicking button1 on layout1, it goes to layout 2 (using setContentView). On layout2, clicking button2, it goes back to layout1. Then button1 is no longer responding OnClickListener. I looked into "Input Events" but still couldn't figure it out. What happened and how to fix it?
Thanks in advance!
Button submitBtn;
Button backBtn;
submitBtn = (Button)findViewById(R.id.button1); //on layout1
backButn = (Button)findViewById(R.id.button2); //on layout2
submitBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.layout2);
}
});
backBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.layout1);
}
});
You should re-assign listeners when you switch layout, cause when you're calling setContentView old view is destroyed, and new components are created.
You MUST set the contentview when it is time to change the layout, otherwise the views will be null.

How can I remove a Button in its own onClick method?

I want to remove a Button in his own onClick method after it's clicked. I tried it with the normal way:
layout.removeView(save);
But the button will not be removed and I get no error. If I want to add the Button I get an error because the button already excists.
I think it isn't working because I trie to remove the button during his OnClickHandler is active. So my Question is how can I remove the button after he is clicked?
Here's the complete, fully tested solution:
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
view.setVisibility(View.GONE);
}
});
You can also completely remove the view from the layout like this (also tested):
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
ViewGroup parentView = (ViewGroup) view.getParent();
parentView.removeView(view);
}
});
Try to set its state with button.setVisibility(Visibility.GONE)
How about just hide it? e.g. in your button onclick handler you can do something like:
button.setVisibility(View.GONE);

Android: How to only hear when my custom class is clicked

when I make a custom view class and add a clickListener it fires anywhere on the screen I click, even where the custom view is not. If I use the same code with a button from a layout it only fires when I click the button not anywhere on screen. Any ideas how to just only listen for when my custom class is directly clicked?
button only fire when pressed
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d(DEBUG_TAG, "button clicked");
}
});
stroke object fires when you press anywhere on screen, even outside of stroke's bounding box
Stroke stroke = new Stroke(this);
mainLayout.addView(stroke);
stroke.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// fires on every screen click :>(
Log.d(Main.DEBUG_TAG, this.toString()+"shape clicked");
}
});
I think your custom view just fills the whole screen. That's why it reacts on every click on the screen. You need to make it smaller and everything will work fine.

Categories

Resources