Spinner dropdown selected value not visible - Android - android

I'm newbie to Android, i'm trying to design a spinner dropdown widget, the user selected value is not set as selected value in dropdown box [not display in marked area in below image].
My design xml:
<Spinner
android:id="#+id/propertyid"
android:layout_width="fill_parent"
android:layout_marginTop="160dip"
android:padding="230dp"
android:layout_height="60dp"
android:background="#android:drawable/btn_dropdown"
android:prompt="#string/prompt"
android:spinnerMode="dropdown" />
Java code:
private Spinner spin;
List<String> list = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
list.add("list 1");
list.add("list 2");
list.add("list 3");
imgPreview = (ImageView) findViewById(R.id.imgPreview);
spin = (Spinner) findViewById(R.id.propertyid);
spin.setOnItemSelectedListener(this);
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
aa.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa);
spin.setSelection(0);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
spin.setSelection(position);
String item = list.get(position);
System.out.println("&&&&&&----&&&&&&"+position+item);
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
Please anyone help me to get the selection list value displayed

Just remove your
android:padding="230dp"
It is moving your item out of the boundary,
All the rest codes are fine enough.

Related

Change colour of selected items

I made a listview and a spinner with 3 options. I want that spinner to connect with Listview on other intent.
The spinner is on a Edit page so whenever I click on 1 item on the list I get to the edit page.
I want the first option on the spinner to make the selected item on the list green the second will make it yellow and then third will make it red. How do I do it?
public class EditList extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_list);
final Button button=(Button)findViewById(R.id.save);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(EditList.this,Vans.class);
startActivity(intent);
}
});
// Spinner element
spinner = (Spinner) findViewById(R.id.spinner);
// Spinner click listener
spinner.setOnItemSelectedListener(EditList.this);
// Spinner Drop down elements
List<String> categories = new ArrayList<>();
categories.add("רכב עובד");
categories.add("יש תקלה");
categories.add("רכב במוסך");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String work = parent.getItemAtPosition(position).toString();
if (position==0){
View listViewChildAt = Vans.listView.getChildAt(position);
listViewChildAt.setBackgroundColor(Color.GREEN);
}
Vans.listView.setBackgroundColor(position);
spinner.setSelection(position);
String takala = parent.getItemAtPosition(position).toString();
if (position==1){
View listViewChildAt = Vans.listView.getChildAt(position);
listViewChildAt.setBackgroundColor(Color.YELLOW);
}
String garage = parent.getItemAtPosition(position).toString();
if (position==2){
View listViewChildAt = Vans.listView.getChildAt(position);
listViewChildAt.setBackgroundColor(Color.RED);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}

How do you create a dropdown answer in android?

I'm looking for something similar to a drop down menu but when you click on it a block of text drops instead of a list of clickable items. Then you should be able to click to close it again when finished. My app is an informational app so, I want to make a list of several of these within a glossary in my app.
Any ideas??
you should use Spinner in android:
Here is the example :
Spinner mSipnner = (Spinner)findViewById(R.id.spin_beneficiary_targetcode);
List<String> categories2 = new ArrayList<String>();
categories2.add("Choose code");
categories2.add("001");
categories2.add("002");
categories2.add("003");
categories2.add("004");
categories2.add("005");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories2);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
////Spinner item selected
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String targetCode_item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
//Toast.makeText(parent.getContext(), "Selected: " + category_item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
// attaching data adapter to spinner
mSpinner.setAdapter(dataAdapter);

I want to display selected item with some title in Spinner,Like item is:item6

I am new in android ,I want work like, In Spinner if i select any other items form array,that item should get selected including with some text,For example,if My spinner having 10 items,if i select 6th item that item should display like "Your Item Is: Item6",This should be display in my spinner as first item,After select of items form the array,Please help me ,,Thanks in advance..
String[] categories = {"Select one","Automobile","Computers","Education"};
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
then onItemSelected Listerner Do :-
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
categories[0] = item;
ArrayAdapter<String> newdataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
spinner.setAdapter(newdataAdapter);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
ArrayAdapter arrayAdapter = null;
String[] list=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//you can replace the below list with your list
list = {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Pluto"};
final Spinner spinner = (Spinner) findViewById(R.id.spinner);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(arrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spinner.setPrompt("Your selected item is:"+list[position]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}

i hava two spinner in my application how to reload the selected spinner items

repeate selected spinner items like first i selcted A and B it displays AB second time selct BA i want to display AB and BA one below another
sp_spinner_a=(Spinner)findViewById(R.id.sp_spinner_a);
sp_spinner_b=(Spinner)findViewById(R.id.sp_spinner_b);
lay2=(LinearLayout)findViewById(R.id.lay2);
lay5=(LinearLayout)findViewById(R.id.lay5);
sp_spinner_a.setOnItemSelectedListener(this);
sp_spinner_b.setOnItemSelectedListener(this);
// Spinner Drop down elements
String[] AgeArr = {"SELECT", "A", "B"};
String[] iiemsArr = {"SELECT", "a", "b"};
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, AgeArr);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
sp_spinner_a.setAdapter(dataAdapter);
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, iiemsArr);
// Drop down layout style - list view with radio button
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
sp_spinner_b.setAdapter(dataAdapter1);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
String isChecking=(String) sp_spinner_a.getSelectedItem();
String isCheckingb=(String) sp_spinner_b.getSelectedItem();
if(isChecking.equals("SELECT")){
lay2.setVisibility(View.GONE);
lay5.setVisibility(View.GONE);
}else{
lay2.setVisibility(View.VISIBLE);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}

show initial value on spinner other than first value in adapter?

i have to display a spinner ,it is getting its content from adapter at run time . when spinner appear on screen Select a value should appear on it instead of first value in adapter.Please help
If you want a particular item in your collection to be selected have a look at:
Spinner.setSelection(int position)
Then You have to add that text at first position in you string array.
Spinner rangeSpinner = (Spinner)findViewById(R.id.rangeSpinner);
String[] items = new String[] {"Select Product","Bread","Milk"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.custom_spinner_textview_layout, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
rangeSpinner.setAdapter(adapter);
custom_spinner_textview_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18.0sp"
android:textColor="#color/grey_text"
android:gravity="left"/>
**Just try It fri...**
1.String[] _Purchesedlistarray = { "Required", "purchased" };
2.ArrayAdapter Purchesedadapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, _Purchesedlistarray);
Purchesedadapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
_PurchesedSpinner.setAdapter(Purchesedadapter);
3._PurchesedSpinner
.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
// TODO Auto-generated method stub
Appsconstent._Purchesed = parent.getItemAtPosition(
position).toString();
System.out.println("the Value is--------------->"
+ Appsconstent._Purchesed);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});

Categories

Resources