Double Spinner for Android - android

I'm new to this forum, but i came here because i need some help with some spinners i'm trying to create for an Android app.
I have created 1 spinner and that works fine, but now i want to add a second spinner and i want the if statement i have to work depending on the two selections of the spinners.
For example, if item 1 is selected on spinner 1 and item 3 is selected on spinner 2 then do the if statement.
But i don't know how to get that to work. could anyone help me please.
This is the code i have now for 1 spinner:
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.weight_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
public class MyOnItemSelectedListener implements OnItemSelectedListener
{
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id)
{
final String itemSelected = parent.getItemAtPosition(pos).toString();
if (valueEntered.getText().length() == 0)
{
valueEntered.setText(String.valueOf(0));
}
if (itemSelected.equals("Stones"))
{
float valueInput = Float.parseFloat(valueEntered.getText().toString());
Toast.makeText(parent.getContext(), "The scale is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
valueEntered.setText(String.valueOf(convertSToK(valueInput)));
}
}
I really need some help, many thanks,
Davide Sousa

Use a dialog like the spinner:
private Dialog b1()
{
final String[] items = {
"Item 1",
"Item2",};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Hi this is a spinner"));
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch(item) {
case 0:{
/* Item 1 */ break;}
case 1:{
/* Item 2 */break;}
}
}
});
return builder.create();
}
and then show the dialog with b1().show();

Related

How to detect whether spinner item selected or not?

Normally spinner can get item by setOnItemSelected but what if the user didn't click to select at all? Then how should I edit the code for detect whether the user click on the spinner to select item or not? Can anyone please advice me?
Here's my code:
s = (Spinner) findViewById(R.id.Rg);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.gender_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
gender = adapterView.getItemAtPosition(i).toString();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
Toast.makeText(Register.this, "Please select your gender", LENGTH_SHORT).show();
return;
}
});
you can use
Spinner.getSelectedItem()
or
Spinner.getSelectedItemId() //return the id of the selected item (should override the method"getItemId" in the adapter class )
or
Spinner.getSelectedItemPosition() //return the position of the selected item (index in the adapter)

Using a spinner to create a certain number of timepickers

I am designing a medication reminder app and I have chosen to use a spinner so that the user can choose to select "Once A Day", "Twice A Day" or "Three Times A Day".
I would then like to use the answer from the spinner to create 1, 2 or 3 timepickers as necessary.
The only problem is that I am getting really stuck on how to do this.
Can somebody please point me in the right direction??
Many Thanks
Sarah
You can create Array in strings.xml
<string-array name="time_arrays">
<item>Once A Day</item>
<item>Twice A Day</item>
<item>Three Times A Day</item>
</string-array>
Spinner arrayadapter Adapter
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.time_arrays));
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
yourspinner.setAdapter(dataAdapter);
Initialize
private Spinner spnSemester;
ArrayAdapter<String> timeAdapter;
List<String>courseList= new ArrayList<>();
spnTime = (Spinner) findViewById(R.id.spnTime);
courseList.add("Once A Day");
courseList.add("Twice A Day");
courseList.add("Thrice A Day");
timeAdapter= new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item,courseList);
spnTime.setAdapter(timeAdapter);
timeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
use on click Listener to get value
spnTime.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Store selected value here
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Initially design your xml layout declaring Spinner and three TimePicker, more detail about Spinner here
Then hide and show timePicker as required.
public class MainActivity extends Activity implements
AdapterView.OnItemSelectedListener {
String[] spinner_values = { "Once A Day", "Twice A Day", "Three Times A Day" };
#Override
protected void onCreate(Bundle savedInstanceState) {
//setup timepickers as required
timepicker1=(TimePicker)findViewById(R.id.timePicker1);
timepicker2=(TimePicker)findViewById(R.id.timePicker2);
timepicker3=(TimePicker)findViewById(R.id.timePicker3);
//initially set visibility to GONE for all three timers
//setup spinner
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setOnItemSelectedListener(this);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, spinner_values);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
Switch(position){
case 2 :
//show timePicker3
case 1:
//show timepicker2
case 0:
//show timePicker1
break;
default
//hide all timerPickers
break;
}
}
}

populating spiner2 from 1 and being able to select spinner2 value and toast selection

struggling with this so heres what I have.
Spinner 1 array.
<!--for spinner 1-->
<string-array name="Manu_array">
<item>Manu 1xsd</item>
<item>Manu 2xrsd</item>
<item>Manu 3x4rsd</item>
</string-array>
Spinner 2 arrays.
<!--for spinner 2-->
<string-array name="Manu 1xsd">
<item>a1</item>
<item>a2</item>
<item>a3</item>
<item>a4</item>
</string-array>
<string-array name="Manu 2xrsd">
<item>bg 1</item>
<item>bg 2</item>
</string-array>
<string-array name="Manu 3x4rsd">
<item>z1</item>
<item>z2</item>
<item>zd4</item>
<item>xs5</item>
<item>fg34</item>
</string-array>
So spinner 1 I would select "Manu 3x4rsd" then spinner 2 "xs5" I would Toast "Manu 3x4rsd " + "xs5"
My Java code for Spinner 1 selection:
final Spinner[] spinner1 = {(Spinner) findViewById(R.id.Spinner1)};
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.Manu_array, R.layout.textview);
spinner1[0].setAdapter(adapter);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner1[0].setAdapter(adapter);
spinner1[0].setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
// storing string resources into Array
Manu= getResources().getStringArray(R.array.Manu_array);
Toast.makeText(getBaseContext(), "You have selected : " + Manu[index],
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// do nothing
}
});
My Java Code for Spinner 2 selection
final Spinner Spinner2= (Spinner) findViewById(R.id.Spinner2);
Spinner1[0].setOnItemSelectedListener(new adapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
int arrayId = 0;
switch (position) {//get array id according to selected position
case 0:
arrayId = R.array.Manu_1xsd_array;
break;
case 1:
arrayId = R.array.Manu_2xrsd;
break;
case 2:
arrayId = R.array.Manu_3x4rsd.array;
break;
};
ArrayAdapter<CharSequence> adapterL = ArrayAdapter.createFromResource(settings.this, arrayId, R.layout.textview);
Spinner1.setAdapter(adapterL);
String Spinner1 = Spinner1.getSelectedItem().toString();
Toast.makeText(getBaseContext(), "You have selected : " + Spinner1,
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// do nothing
}
The issue is whilst I can see the values populated in spinner 2 and scroll through them and it appears I can select them, I have no code to do anything on selecting them and generate the toast. I have tried to create a listener within the spinner but I am unable to do anything to get it to function as desired.
So I suspect I am going about tackling the problem in completely the wrong way.
Once I can toast the spinner 1 and spinner 2 selected values I should be able to combine them to read from a list of strings to combine them to make a command.
wanted to use spinners for visual effect rather than have a great big list to scroll through just to select the values required.
The following code populates the second spinner according to the selected value from the first spinner, and displays a toast when an item from the second spinner is selected.
final Spinner s1= (Spinner) findViewById(R.id.Spinner1);
final Spinner s2= (Spinner) findViewById(R.id.Spinner2);
final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.Manu_array, R.layout.textview);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int arrayId = 0;
switch (position) {//get array id according to selected position
case 0:
arrayId = R.array.Manu_1xsd_array;
break;
case 1:
arrayId = R.array.Manu_2xrsd;
break;
case 2:
arrayId = R.array.Manu_3x4rsd.array;
break;
}
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, arrayId, R.layout.textview);
s2.setAdapter(adapter);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
s2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String msg = s1.getSelectedItem().toString() + s2.getAdapter().getItem(position).toString();
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

show an open spinner

I have Async task to provide me list of some cities and after I have the list, I want to show me an OPEN spinner directly without any dialogs. My code opens spinner with propper list, but clickListener doesn't seems to work.
my code:
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerCities);
listAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner = new Spinner(this);
spinner.setAdapter(listAdapter);
spinner.performClick();
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> aView, View selectedItemView,
int position, long longID) {
System.out.println("ON ITEM CLICK LISTENER HERE");
}
public void onNothingSelected(AdapterView<?> aView) {
}
});
What I am missing here?
So after several hours, I figured it out. I need to use Dialog, but result is the same I have wanted. Here is source in case, someone is find useful.
private void MyMethod(){
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,
(String[])MyClass.getListOfCities().toArray());
AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setIcon(icon);
ad.setTitle("Title");
ad.setSingleChoiceItems( listAdapter, -1, new OnClickListener() {
public void onClick(DialogInterface dialog, int position) {
// DO something when I click on item
dialog.dismiss();
}
});
ad.show();
}

How to capture List Dialog result - Android

I am new to Android. I am stuck, need help.
I have a List as my Activity. OnItemLongClickListener for any element of a List, I am displaying a Dialog with Custom List (Red, Green, Blue) & on the selection of any of the items (Red, Green, Blue) I need to change the back color of the selected item (on which the event raised the Dialog) of a List (main activity).
The dialog box pops, but I am stuck how to get the selected item (of Dialog). Below is my code.
public class SimpleList extends ListActivity
{
String[] contactNames = {"Name 1", "Name 2", "Name 3", "Name 4", "Name 5", "Name 6"};
ArrayAdapter<String> contactAdpater;
String itemSelected;
String choosenColor;
private final Context context = this;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ArrayList<String> myContactList = new ArrayList<String>(Arrays.asList(contactNames));
OnItemLongClickListener itemChangeColorListener = new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View arg1, int position, long arg3) {
itemSelected = parent.getItemAtPosition(position).toString();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
final String[] colorNames = {"Red","Green","Blue"};
builder.setTitle("Pick a Colour!")
.setItems(colorNames, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
choosenColor = colorNames[which];
//Toast.makeText(getApplicationContext(), colorNames[which], Toast.LENGTH_SHORT).show(); <<-- its working fine here
//I am not able to access parent here... I want to perform,
**//parent[position].setBackgroundColor(Color.RED); in case Red is selected from Dialog**
}
});
builder.show();
return false;
}
};
contactAdpater = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,
myContactList);
setListAdapter(contactAdpater);
getListView().setOnItemLongClickListener(itemChangeColorListener);
}
}
Yes got your problem
You have to use view's setBackgroundColor(int color); for changing the color if selected item of first ListView with the color selelcted from Dialog.
So In onClick you must use :
choosenColor = colorNames[which];
if(choosenColor.equals("Red"))
{
view.setBackgroundColor(Color.RED);
}
else if(choosenColor.equals("Blue"))
{
view.setBackgroundColor(Color.BLUE);
}
else if(choosenColor.equals("Green"))
{
view.setBackgroundColor(Color.GREEN);
}

Categories

Resources