In my activity I have many buttons that launch other activities. The problem is that when I press a button I don't want to be able to press another, and so to launch 2 activities (or more). What is the best solution to block the other views (buttons) after one of them was pressed?
Just in each of your onClickListener for each button make button.setDisable(true);.
Don't forget to enable them in onResume().
Add one onClickListener() for all buttons. In listener switch the actions as per id.
public .. onClickListener(){
// Disable all buttons here..
button1.setDisabled(true);
button2.setDisabled(true);
button3.setDisabled(true);
...
switch(view.getId()){
case R.id.button1:
// Start some activiy ... or do some task...
break;
case R.id.button2:
...
break;. ...
}
Don't forget to enable all buttons in onResume()
public .. onResume(){
button1.setDisabled(true);
button2.setDisabled(true);
button3.setDisabled(true);
}
OR, Try This to enable/disable all views in a viewGroup...
private void enableDisableView(View view, boolean enabled) {
view.setEnabled(enabled);
if ( view instanceof ViewGroup ) {
ViewGroup group = (ViewGroup)view;
for ( int idx = 0 ; idx < group.getChildCount() ; idx++ ) {
enableDisableView(group.getChildAt(idx), enabled);
}
}
}
Pass the view parameter as the parent layout containing all buttons.
I haven't used this, but this should work. Try playing around this code.
Try this to disable the Buttons
button.setEnabled(false);
Related
My case :
- I have a RecyclerView with complex item (many ImageView, texts .. )
- I use 4 ImageView as 4 buttons (Like, Comment, Share, Dislike)
- I want to use TouchListener for each button, because I want to control animation, also click event. ( I know how to use ClickListener for each item so please focus on TouchEvent ) Example : Facebook Comment button of each Story on NewsFeed, when you touch onto the button, animation scale icon. Click event just go on after release the button.
Problem is : I just can get Parent View of each Item (ex : LinearLayout contain other components of each item) by using recyclerView.findChildViewUnder(x,y).
So how to know which button was clicked like when we use clickListener :
- button1.setClickListener(this);
- button2.setClickListener(this); (on View Holder)
And after that catch event in Activity or Fragment :
- switch(view.getId()) {
case R.id.button1: .. do something; break;
case R.id.button2: .. do something; break;
....
I google this problem all day but all sample and answer just use ClickListener like I said above.
Thank you!
inside recyclerview adapter use like this
#Override
public void onBindViewHolder(final RecyclerViewAdapterA.ViewHolder_a holder, final int position) {
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
You get parent view by by
View parent = recyclerView.findChildViewUnder(x,y);
Now you can easily get its corresponding views e.g.
Button button = (Button)parent.findViewById(R.id.button);
Hope you understand!!!
I have one activity and here i have 100 buttons, i want that when i press Button 1 then press another Button the Button 1 should get unpressed.
i know i can make this with
if(Button1.isPressed()) {
Button2.setPressed(false);
Button3.setPressed(false);
Button4.setPressed(false);
Button5.setPressed(false);
Button6.setPressed(false);
Button7.setPressed(false);
Button8.setPressed(false);
......................... }
else { do nothing }
.... BUT!
it's too much code
Coders will kill me or will just laugh on me.
any ideas?
maybe there is a way to unpress the all buttons from the activity?
Not the prettiest solution ever, but you could make an OnClickListener like this:
View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
ViewGroup parent = (ViewGroup) v.getParent();
for (int i = 0; i < parent.getChildCount(); i++) {
View current = parent.getChildAt(i);
if (current != v && current instanceof Button) {
((Button) current).setPressed(false);
}
}
((Button) v).setPressed(true);
}
}
and attach it to all of your buttons.
Then, whenever a button is clicked, it will iterate over all views that are in the same layout (or actually, view group) as the clicked button, and, for any of those views that are buttons except for the clicked button, it will call setPressed(false).
Note that this only works out of the box if all the buttons are in the same layout. If they are in nested layouts, you will have to adapt it a little.
Off topic: What do you need 100 buttons for? That's a lot of buttons. You may want to redesign your user interface
Ok so instead of looping through all the buttons on over and over again when one button is pressed, you can just store a variable which stores the button number of the button that was last pressed. Now, when the second button is pressed, disable the button that was pressed earlier, you get its index from the saved variable, enable the button that was pressed and store its index in the variable.
Heres an example pseudo code to give you and idea:
int buttonLastPressed = 0;
void onButtonClick(Button buttonPressed){
if(buttonLastPressed != 0){
disableButton(buttonLastPressed);
enableButton(buttonPressed);
buttonLastPressed = buttonPressed.getIndex()
}
}
Saves you from looping through each and every button to disable it.
Define id of button 1 to 100
When press button occurs save it's id in some member variable like previous_pressed
Before updating a previous_pressed value find and unpress previous pressed button like this
Button previous_pressed_button = (Button) findViewById(previous_pressed);
Now you have the previous pressed button, So upress it or whatever.
I have an onCheckChangedListener to show a textView depending on which radio button is selected. I have 1 question and 1 problem that I was wondering if anyone could help me with.
Question: Can you set the radio groups default check value to NO radio button so that none are checked to start with?
Problem: How can I use an IF statement to determine whether a text view is already "visible" and If it is then set it to "gone", I will include my current code.
code:
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
switch(arg1){
case R.id.rfolk1:
Folk1.start();
TvFolk1.setVisibility(View.VISIBLE);
TvFolk2.setVisibility(View.GONE);
Play.setVisibility(View.VISIBLE);
Pause.setVisibility(View.VISIBLE);
Stop.setVisibility(View.VISIBLE);
Play2.setVisibility(View.GONE);
Pause2.setVisibility(View.GONE);
Stop2.setVisibility(View.GONE);
break;
case R.id.rfolk2:
Folk2.start();
TvFolk2.setVisibility(View.VISIBLE);
TvFolk1.setVisibility(View.GONE);
Play2.setVisibility(View.VISIBLE);
Pause2.setVisibility(View.VISIBLE);
Stop2.setVisibility(View.VISIBLE);
Play.setVisibility(View.GONE);
Pause.setVisibility(View.GONE);
Stop.setVisibility(View.GONE);
break;
}
The View class includes a getVisibility() method. Compare that:
Eg:
if (TvFolk1.getVisibility() == View.VISIBLE)
TvFolk2.setVisibility(View.GONE);
To shorten down code, you can also make a method:
public static void goneIfVisible (View v)
{
if (v.getVisibility() == View.VISIBLE)
v.setVisibility(View.GONE);
}
And keep in mind in Java, variables are lowercased, only use uppercase for class names.
// If TextView is already showing and you want to hide it.
if (TvFolk1.isShown()) {
TvFolk2.setVisibility(View.INVISIBLE);
}
// For uncheck all radio button from radio button groups
RadioGroup rgButton = (RadioGroup)findViewById(R.id.radiobuttongroup);
rgButton.clearCheck();
I have a button and when I press it, i want to remove it (not make it invisible). I read that I can do that using layout.removeView(mybutton) but what is the layout ? and how can I get it in my activity
Button showQuestion;
private void initialize() {
showQuestion = (Button) findViewById(R.id.bAnswerQuestionShowQuestion);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.bAnswerQuestionShowQuestion:
showQuestion.setVisibility(View.INVISIBLE);
//Here i want to delete the button
question.setVisibility(View.VISIBLE);
theAnswer.setVisibility(View.VISIBLE);
answerQuestion.setVisibility(View.VISIBLE);
showChoices.setVisibility(View.VISIBLE);
showHint.setVisibility(View.VISIBLE);
break;
}
}
see link
ViewGroup layout = (ViewGroup) button.getParent();
if(null!=layout) //for safety only as you are doing onClick
layout.removeView(button);
i have a button and when i press it , i want to remove it (not make it
invisible)
=> You did as below:
showQuestion.setVisibility(View.INVISIBLE);
Try with:
showQuestion.setVisibility(View.GONE);
FYI, INVISIBLE just hide the view but physically present there and GONE hide as well remove the presence physically as well.
You can use
button.setVisibility(View.GONE);
Layout is the parent Layout of your Button, usually a RelativeLayout or LinearLayout.
You can get it as follows:
ViewParent layout = button.getParent();
Im going to write some android app, which will basically consists of two activities. So first should have a lot of buttons (100+) and on click on any of them I will just get some special id and move to second activity. But is there any alternative to declare that hundreds of buttons and copy/paste one piece of code to every of them setting almost same onClickLister? Is there any special construction? Thanks
Edit: every of buttons are actually indexed from 1 to n. And on the click second activity will be launched and get that index to show it. I cant basically use any spinner or smth else, because there will be 3 rows of clickable things and each of them carring different images
Edit 2: so, to give you an idea, im going to do some table of buttons like in Angry Birds menu when you actually choosing the level you want to play. So, on click you will get id of button and start second activity
Call the method to add buttons
private void addButton(){
LinearLayout view = (LinearLayout) findViewById(R.id.linear_layout_id_here);
Button btn = null;
int w = 50;
int h = 25;
for(int i=1; i<100; i++) {
btn = new Button(this);
btn.setLayoutParams(new LayoutParams(w,h));
btn.setText("button " +i);
btn.setTag(""+i);
btn.setOnClickListener(onClickBtn);
view.addView(btn);
btn = null;
}
}
Call this method for handling onclick on button
private View.OnClickListener onClickBtn = new View.OnClickListener() {
public void onClick(View view) {
final int tag = Integer.parseInt(view.getTag().toString());
switch (tag) {
case 1:
// Do stuff
break;
case 2:
// Do stuff
break;
default:
break;
}
}
};
You should use a ListView.
ListViews are great for handling a lot of items at the same time. They are also natural for the user. Additionally, you use only one click listener - OnItemClickListener.
There's a useful example on how to work with ListViews in the Android Referenence.
You may add buttons in code, something like this:
#Override
public void onCreate(Bundle savedInstanceState) {
/*your code here*/
GroupView gw =findViewById(R.id.pnlButtonscontainer); //find the panel to add the buttons
for(int i=0; i<100; i++) {
Button b = new Button(this);
b.setLayoutParameters(new LayoutParameters(w,h));
b.settext = i+"";
b.setOnClickListener(new OnClickListener(){
});
}
}
I coded directly into browser, so some syntax error may appear in my code, but this is the point, a way, not the only one, to add 100 buttons.