Spinner Item Cant able to make empty - android

Spinner item initially text value should be empty so i added empty vaue in spinner item but it shows the first spinner item is empty .please rectify my mistake which i have done
Required
Initially spinner should be empty
when selected spinner it should show only two items
spinner.java
LoginStatus_List = new ArrayList<String>();
LoginStatus_List.add("");
LoginStatus_List.add("Approve");
LoginStatus_List.add("Reject");
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,LoginStatus_List);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnrLognStatus.setAdapter(dataAdapter1);
spnrLognStatus.setSelection(0);

You have to create a costum SpinnerAdapter to achieve this.
Try this
You can set the first spinneritem empty: " ". But in this simple solution the user is still able to select the displayed empty spinneritem.

You can add a kind of "title" into your spinner and ignore it if the user select it. It's not exactly an empty line but the title will act as an hint to guide the user.
private static final String TITLE ="Select the action";
...
ArrayList<String> LoginStatus_List = new ArrayList<String>();
LoginStatus_List.add(TITLE);
LoginStatus_List.add("Approve");
LoginStatus_List.add("Reject");
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,LoginStatus_List);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnrLognStatus.setAdapter(dataAdapter1);
spnrLognStatus.setSelection(0);
spnrLognStatus.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String selection = (String)spnrLognStatus.getSelectedItem();
if(TITLE.equalsIgnoreCase(selection)){
// Ignore the selected title
}else{
// Do want you want here
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

Related

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

refresh view in adapter class android

i want to update textview when spinner item selection in adapter class but it show updation in wrong item's price . i have lost many time to solve this here is my code
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity, android.R.layout.simple_dropdown_item_1line, arraylist.get(position).getProductUnit1());
// Setting the array adapter containing country list to the spinner widget
holder.tvproductBrandName.setAdapter(adapter);
OnItemSelectedListener countrySelectedListener = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> spinner, View container,
int position2, long id) {
i =Float.parseFloat(arraylist.get(position).getProductMRP1()[position2]);
holder.tvProductMRP.setText((arraylist.get(position).getProductMRP1()[position2]));
holder.tvProductOriginalPrice
.setText("Rs: "
+ ((i - Float.valueOf(arraylist.get(position)
.getOfferValue()))*(Float.valueOf(arraylist.get(position).getProductQTY()))) + "");
OriginaiPrice =String.valueOf (i - Float.valueOf(arraylist.get(position)
.getOfferValue()));
appSession.setProductOriginal(OriginaiPrice);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
};
// Setting ItemClick Handler for Spinner Widget
holder.tvproductBrandName.setOnItemSelectedListener(countrySelectedListener);
you can use hash map to check which position or value in spinner is selected and second hashMap to check textView value correspondence to spinner.
private final HashMap<Integer, Integer> selectedSpinnerItems;
private final HashMap<Integer, String> textViewValue;

How to add default value in Spinner other then adapter Value

i have some data coming from server and i have to show that value in Android Spinner.
Adapter that is attach to Spinner also getiing from server.
please let me know how to show initial value coming from server.and show adapter value after click on Spinner.
Spinner mySpinner = (Spinner) findViewById(R.id.householdspinner);
mySpinner.setAdapter(new ArrayAdapter<String>(Edit_Voter_Information.this,
android.R.layout.simple_spinner_dropdown_item,
householdIncome));
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
String item = arg0.getItemAtPosition(position).toString();
house_Hold_Income = item;
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
Initially i stored some values in String[] names. when activity is run spinner position 0(zero)is selected by default.
Check my code below:
ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.spinner_style, names);
spinner.setAdapter(arrayAdapter);
spinner.getSelectedItemPosition();//which returns names[0] initially.
if click other position(i) it returns names[i], where i =0,1,2,..

how to shorten spinner selected item text in android?

I have used android custom spinner.
My custom spinner look like bellow
My List data is like
List<String> list = new ArrayList<String>();
list.add("001-00434016457-Md. Mokhhlesur Rahman");
list.add("001-00434016724-Mohammad Nazim Ullah");
list.add("001-00434016725-Mohsin Kabir");
list.add("001-00434016881-Maruf Pervaz Khan");
list.add("001-00434017171-Md.Nazmul Hasan");
My Spinner Adapter and setting data in spinner is like bellow
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
My problem is spinner's item text is large like "001-00434016881-Maruf Pervaz"
I want to select only id like 001-00434016881.
I don't want to get full text of the spinner item selection. How can I remove name part like -Maruf Pervaz Khan from 001-00434016881-Maruf Pervaz on item selection from spinner.
Please Help me
Try this. If the row item maintains same pattern as 001-00434016881-Md.Some Name
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, adapter.getItem(position).substring(0,adapter.getItem(position).lastIndexOf("-")), Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});

Default text shown on spinner but hide from drop down list - Android

I want to show first item as a default text on spinner but in the drop down list that item is not shown like there is arraylist
final ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("number");
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
The number is always shown as a default text on spinner but after clicking on spinner there is one, two, three is shown in drop down list. when we select any number one, two three the default text number is not changed. How can i do it?
ArrayAdapter<String> adapte = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, spinnerArray);
adapte.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapte);
spin.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View arg1,int pos, long arg3)
{
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Try this, it show the default as Number..
May be this help you
String[] priceString = { "Number", "one, "two"};
Spinner priceSpinner = (Spinner) findViewById(R.id.price_spinner);
ArrayAdapter priceAdapter = new ArrayAdapter( this.getActivity(), android.R.layout.simple_spinner_item,priceString); priceSpinner.setAdapter(priceAdapter);

Categories

Resources