How to select an item in a spinner using a button - android

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
}

Related

How to create a custom spinner?

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.

Android 1 function for 3 buttons

I have 1 function to print the text of the button, and i use that function for the 3 button, how do i know in what button the user click? (sorry but my english is low)
Example:
public void function(View view) {
TextView text = (TextView) findViewById(R.id.textView);
Button button1= (Button) findViewById(R.id.button3);
Button button1= (Button) findViewById(R.id.button4);
Button button1= (Button) findViewById(R.id.button5);
text.setText(button <?> .getText()); }
is the number of the button the user click
Its any way to know in which button the user click?
Please help, Thanks.
you can define one click listener and set this listener for all buttons. For example:
private OnClickListener genericListerner = new OnClickListener() {
public void Click(View v) {
switch(v.getId())
{
case R.id.button3:
text.setText(button3.getText().toString());
break;
case R.id.button4:
text.setText(button4.getText().toString());
break;
case R.id.button5:
text.setText(button5.getText().toString());
break;
default:
//do something
}
};
and you set the listeners like the following:
button3.setOnClickListener(genericListener);
button4.setOnClickListener(genericListener);
button5.setOnClickListener(genericListener);
I am not sure whether this is what you are asking, but what this code basically does is that, if you click button 3 it sets the textview's text to button 3's text, if you click button 4 it sets the textview's text to button 4's text and if you click button 5, it sets the textview's text to button 5's text.
Hope this helps.
The view that is passed in is the one that was clicked, therefore it has the id.
view.getId() == R.id.button3
If you still need help, share more of your code.
Well you will require some onclick listeners for the button, you can then set an id or name in the onclick e.g
final Button button1 = (Button) findViewById(R.id.button_id);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
text.settext("Button1 Clicked")
}
});

Android Multiple Buttons with different Actions

I create buttons dynamically based on the size of an array list that i get from another object. For each entry of the arraylist i need to create three buttons each one with different action.
Like this i need to create 3 times the sizes of arraylist number of buttons. If i had only one set of buttons I can write onClick()-method which takes the id of the button but here i have 3 buttons for each entry and need to write 3 different actions for those three buttons.
How could this be done?
Similar thing I have done when i needed a textview for each of my array item.it was like-
String[] arrayName={"abc","def","ghi"};
for(int i=0;i<arrayName.length;i++)
{
TextView tv=new TextView(context);
tv.setPadding(20, 5, 40, 5);
tv.setText(arrayName[i]);
tv.setTextSize(1, 12);
tv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tv.setTextColor(Color.parseColor("#2554C7"));
tv.setClickable(true);
tv.setId(i);
layout.addView(tv);
}
In same way,you can add button in similar way. and in click event of each of them,you can code for their actions separately.(I have not tried this).So in each iteration,you will have 3 buttons for each of the array item.
Edit - 1:
You can differentiate ids like:
mButton1.setId(Integer.parseInt(i+"1"));
mButton2.setId(Integer.parseInt(i+"2"));
mButton3.setId(Integer.parseInt(i+"3"));
Then you can set click listener on each of the button like mButton1.setOnClickListener... and so on.
Declare a list of buttons:
private List<Button> buttons = new ArrayList<Button>();
Then add each button to this list:
buttons.add(0, (Button) findViewById(id in ur layout));
Inside a for loop give click listener:
Button element = buttons.get(i);
element.setOnClickListener(dc);
where dc is ur object name for your inner class that implements OnClickListener.
To access each button you can give:
Button myBtn;
#Override
public void onClick(View v) {
myBtn = (Button) v;
// do operations related to the button
}
Create the Common Listener class for your button action event and set the used into the setOnClickListener() method
Now set the unique id for the buttons.
Now suppose your class look like this :
class MyAction implements onClickListener{
public void onClick(View view){
// get the id from this view and set into the if...else or in switch
int id = view.getId();
switch(id){
case 1:
case 2:
/// and so on...
}
//// do operation here ...
}
}
set this listener in button like this way.
Button b1 = new Button(context);
b1.setId(1);
b1.setOnClickListenr(new MyAction());
Button b2 = new Button(context);
b2.setId(2);
b2.setOnClickListener(new MyAction());

Reduce code for Button onClick events

My question has three parts.
My Layout consists of 26 Buttons: ButtonA, ButtonB, ButtonC...ButtonZ
The buttons are arranged in order on the screen. When the button is clicked I want to capture the click event and filter a SQLiteDB of words by the first letter that was clicked.
How do I write the minimum amount of code that will capture the click, identify the button's corresponding letter, and return the letters that begin with the selected letter from the SQLite Database?
Below is my code that has not been optimized for code brevity.
//Create your buttons and set their onClickListener to "this"
Button b1 = (Button) findViewById(R.id.Button04);
b1.setOnClickListener(this);
Button b2 = (Button) findViewById(R.id.Button03);
b2.setOnClickListener(this);
//implement the onClick method here
public void onClick(View v) {
// Perform action on click
switch(v.getId()) {
case R.id.Button04:
//Toast.makeText(this,"test a",Toast.LENGTH_LONG).show();
// Do the SqlSelect and Listview statement for words that begin with "A" or "a" here.
break;
case R.id.Button03:
//Toast.makeText(this,"test b",Toast.LENGTH_LONG).show();
// Do the SqlSelect and Listview statement for words that begin with "A" or "a" here.
break;
}
}
If you have lots of characters and a button for each character, I would probably extend the "BaseAdapter" class and make a ButtonAdapter holding the alphabet as a character array, instead of actually making 28:ish buttons... ...if I understood the problem correctly?
The getView could look something like this:
public View getView(int position, View convertView, ViewGroup parent) {
Button button;
if (convertView == null) {
button = new Button(mContext);
button.setTag(alphabet[position]);
button.setOnClickListener(clickListener);
} else {
button = (Button) convertView;
}
button.setText(alphabet[position]);
return button;
}
If your layout is defined in XML, you may want to use the android:onClick parameter for your buttons. This way you can save the findViewById() and setOnClickListener() calls.
As alextsc says, you can bind the click listeners from XML if you wish you reduce code in your Activity. However, I think it might be nicer to define the buttons in code inside a loop given their nature. Just make a simple container in your layout (e.g. a LinearLayout) and add to buttons to that. We can exploit the fact that you can loop over characters (treating them like integers) like so:
for (char letter = 'a'; letter <= 'z'; letter++) {
Button button = new Button(this);
button.setText(String.valueOf(letter));
button.setOnClickListener(this);
container.addView(button);
}
As for your actual click listener, you don't really need to switch on their IDs to do what you want. You know that the data for the button is just their text value. Just extract that and use it in a uniform way:
public void onClick(View v) {
Button button = (Button) v;
String letter = button.getText().toString();
// filter using letter
}

Creating Buttons Dynamically, Could I select one and deselect the other buttons?

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);
}
}

Categories

Resources