I want to make a spinner that does not show the item i choose.For e.g i have a spinner with 3 rows 1 2 & 3. When i choose 2 the spinner should close and i see 2 on the spinner.I just want to make the spinner behave like a button that just open's spinner . any idea?
A spinner is just like any other view. If you want it to behave like a Button and start an action when you select an item, you can set a click listener to each of the views inside the spinner.
for (int i =0; i<spinAdapter.getCount(); i++){
spinAdapter.getView(i, null, null).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Do whatever you want here
}
});
}
I have not tried this code, but you could give it a go.
Related
I have a DialogFragment which I am populating with a ListView and a Cursor Adaptor. I was having issue with selecting a radio button and deselecting others. So I did this thing which works. But I wanted to know if I really need a loop there or there is another better way to do it. Here is my code:
viewHolder.radioButton.setOnClickListener(
new View.OnClickListener()
{ #Override
public void onClick(View v) {
for (int i = 0; i < selectionArrayAr.size(); i++){
selectionArrayAr.put(i, false);
}
selectionArrayAr.put(position, true);
notifyDataSetChanged();
}
}
);
I am setting all values to false in the Sparse Array and then finally setting the clicked radio button to true.
I am duplicating my linear layout dynamically and I have to set onClickListeners for buttons inside the linear layout.
for(int i = 0; i <10 ; i++){
// other code here
Button approve_btn = (Button) findViewById(R.id.rent_number_up_btn);
approve_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
approve_btn.setText(String.valueOf(i));
}
});
}
Everything works fine except that my button's text is always set to 9. I think that's because when the listener is called the value of i is 9 at that time. What I want the value of i at the time the button's listener is set and I am not sure how to do that.
How can I solve this problem? Any help is appreciated.
The issue is that you are setting click listener to the same button (by calling findViewById()) 10 times in a row. You get the value 9 because thats the last click listener which you added to the button.
for(int i = 0; i <10 ; i++){
// other code here
Button button = new Button(<Activity Instance>);
button.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
approve_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
approve_btn.setText(String.valueOf(i));
}
});
}
In above code you need to add those buttons in your linearlayout.
Hope this will help you,
Thanks
I am not sure what you want to do but:
Like #Shaishav said your are using same button (R.id.rent_number_up_btn) and your are replacing the click listeners on top of each other. The last value (of the your counter "i") before your loop finish is 9 , that's why it show 9 all the time. If you want to add 10 buttons inside your Linear layout just create new Button(context) every time when your loop starts and add this button to your layout via
yourLinearlayout.addView(yourNewButton);
Then if you set click listener to your new button maybe it will show different values :)
I created a spinner with 5 items and I also created 5 buttons. What I want to do is to associate a button to an item in the spinner. So when I click a button, a corresponding item will be selected. For example:
I have 5 items in my spinner:
hey, hi, ho, hello, sup
I have 5 buttons:
btn1, btn2, btn3, btn4, btn5
btn5 is associated to sup. So when I click btn5, sup should be selected in the spinner. How would I do this?
You have to use
spinner.setSelection(position);
If you need to enter hard code values for spinner then you need to do this
Button0.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Here you need specify which item of spinner you need to select.
spinner.setSelection(0);
}
});
Like this if you have 5 buttons you need to create 5 click event with setSelect(position) event.
You can do this by different ways as you prefer. I think using this you will have more control.
For dynamic spinner you need different logic where you need to create button dynamically and add click event them dynamically with index of your custom or base adapter.
Set an onClickListener on each button calling the following function with the button id as parameter:
function switchSpinner(int id){
Spinner spinner=(Spinner) findViewById(R.id.spinner);
int pos=-1;
switch(id){
R.id.btn1:
pos=0;
break;
R.id.btn2:
pos=1;
break;
R.id.btn3:
pos=2;
break;
R.id.btn4:
pos=3;
break;
R.id.btn5:
pos=4;
break;
}
spinner.setSelection(pos);
spinner.requestLayout() //add this only if the spinner does not change
}
I have a Activity in android that has 4 buttons.
The first 3 buttons fetches a json data from a weather API for 1 day, next 5 days and next 10 days respectively.
I have a 4th button placed at the bottom of the screen, which takes user to second activity.
I want to restrict the entry of user to second Activity if no button from top 3 is clicked.
If the data is fetched, I mean any one of the top 3 buttons have been clicked, allow him to go to second activity on 4th button click else show a message.
How can i check on click of 4th button if any of the top 3 buttons have been clicked before?
Thanks
Put a boolean field in your activity, name it clicked and set it to false on the onCreate method of your first activity, then in the onClick method of your 3 buttons, set it to true,
and in the onClick method of your 4th button check it, if it's true go startActivity, else launch a Toast
You can make the 4th button look disable in "OnCreate" with the function "setEnabled"(may be wrong),
and then just set "setOnClickListener" for the 4th button when you click any of the others.
ps.
Can provide code example if needed.
Why don't you use if statement? You can keep the clicked count data under the first three buttons. Like this;
import java.util.stream.*;
int[] btnMemory = new int[4];
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
btnMemory[0] = 1;
// your code
}
});
after, you can check it with if statement under 4th button;
button4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int sum = IntStream.of(btnMemory).sum();
if(sum >= 3)
// your code
}
});
I'm creating buttons dynamically ...
for(int i=0; i<colSize;i++){
final Button btn = new Button(this);
btn.setText(SectionName[i]);
btn.setTextSize(10);
btn.setPadding(8, 3,8, 3);
btn.setTextColor(Color.WHITE);
btn.setTypeface(Typeface.SERIF, Typeface.BOLD);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//***Every time that I click my button is selected !:)
btn.setSelected(true);
}
});
}
But how could I deselect the other buttons that were selected, I just want one Button selected! :)
The brutal way (works if you have few buttons) - save your button references and create private method which loops through your buttons and deselects once you don't need
Extend your button class and make it listen for custom event which is generated when one of the buttons is clicked
Look at the RadioGroup implementation
Variation of #1. Instead of creating separate listeners for your buttons create just one and reuse it for all buttons. Extend that listener from OnClickListener and add List field. Each time you assign listener to the button add button reference to that list. Now, when onClick is triggered simply loop through the list and disable "other" buttons
Declare a variable to store the Id of the Clicked Button ::
private int EnabledButton;
set an ID on every button when are created ::
btn.setId(i);
or a tag ::
btn.setTag(i);
then in that Listener get the "EnabledButton", and call a function to deselect the other buttons::
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EnabledButton=btn.getId();
DeselectButtons();
btn.setSelected(true);
}
});
The Function to deselect the other Buttons ::
public void DeselectButtons() {
for(int i=0; i<NumberofButtons;i++){
if (EnabledButton!= i)
this.findViewById(i).setSelected(false);
}
}