In my app I have a scenario and that is I want to show the user the desired range of numbers and these would be dynamic , let say 1,2,3 or some time ,1,2 or some time 1,2,3,4,5 I want user to select on of the number from my given range. so that I dont want user to fill by typing numbers. Please tell me how to achieve this and How can i control the range of numbers in autocomplete textview .
or what is alternate of this. Just keep in mind I have Edittext , and in that edittext I want user to select the number not just enter the number.
You can use AutoCompleteTextView in your layout:
XML Code
<AutoCompleteTextView android:id="#+id/EdtSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
JAVA Code
To bind array inside AutoCompleteTextView use the following code
AutoCompleteTextView EdtSearch = (AutoCompleteTextView) findViewById(R.id.EdtSearch);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, rangesyouwantasStringArray);
EdtSearch.setAdapter(adapter);
Why don't you use a Spinner? I think that for your purposes it is more efficient.
You can change your EditText to a Spinner and with its adapter, change the range programmatically.
Edit:
Here is the element that you have to insert instead of the edittext
<Spinner android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Then, in your Activity/Fragment code:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1);
spinner.setAdapter(adapter);
// Here add the range of numbers. For example:
for (int i = 0; i < 5; i++) {
adapter.add(Integer.toString(i));
}
Related
Based on the condition i would like “to show the drop down view of a spinner or show a default value and do not show the drop down”. I used setClickable(false) in the spinner object but it didn’t work. How to implement it?
Try:
((Spinner) spinner).getSelectedView().setEnabled(false);
spinner.setEnabled(false);
Disable or enable it before setting the adapter.
spinner.setEnabled(false);
spinner.setClickable(false);
spinner.setAdapter(typeAdapter);
enable/disable spinner it at the end of by writting this below line.
spinner.onSelectedItemListener
spinner.setEnabled(false); // programmatically
To disable click in single item spinner without it greys out the field:
create xml layout for spinner item spinner_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
/>
In the code:
ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, R.layout.spinner_item, SpinnerList); // replace SpinnerList with your own list you want to show
Spinner spin = (Spinner) findViewById(R.id.android_spinner); // replace android_spinner with the id of yours
if (SpinnerList.size() < 2) { // again change SpinnerList
spin.setBackgroundColor(Color.TRANSPARENT); // if you don't want to show the dropdown arrow
spin.setEnabled(false); // it will disable click in spinner
}
spin.setAdapter(adapter);
for now radio button image on shown on right side of my spinnerand when I click on it, it open option list in popup box
what I want is to show arrow on right side of spinner and options list should be drop down instead of popup box with white background.
see image
How Can I do this, Do I need to create a custom spinner?
Here is the code
XML
<Spinner
android:id="#+id/type_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/text_color"/>
Java
type_Spinner = (Spinner) findViewById(R.id.type_spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, new String[]{"Buy","Sale","Rent","Let"});
type_Spinner.setAdapter(adapter);
1st approach
Change Spinner in xml like this
<Spinner
android:id="#+id/type_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/btn_dropdown" />
Change theme of that Activity to
android:Theme.Holo
In java class
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, new String[]{"Buy","Sale","Rent","Let"});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
type_Spinner.setAdapter(adapter);
2nd approach
Instead of Spinner use a Button set background using android:background="#android:drawable/btn_dropdown" set gravity (not layout_gravity) to left|center_vertical open a PopupWindow on clicking of that Button set that Button as anchor of that PopUpWindow. In that PopUpWindow place a ListView and in OnItemClick change text with selected value in that Button using setText(java.lang.CharSequence)
Full code snippet for 2nd approach
If you use approach 1 then it will work on Post Gingerbread
version. Approach 2 will work on any version of android(not tested in
pre froyo).
In layout use the android:spinnerMode
android:spinnerMode="dropdown"
In activity use like this:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, new String[]{"Buy","Sale","Rent","Let"});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
type_Spinner.setAdapter(adapter);
I have spinner who collect data from DB and display on real time. The problem I am facing is that when the length of an item is quite long then the spinner Stretch itself and goes out of the screen/view. What I want is to have a fix length of my spinner and it should not stretch itself. Here is the view of my Spinner:
And below is the code:
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Java:
Spinner spinnerRub = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, lables);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Instead of using wrap content for the width , specify the width (say 120dp or..)based on your requirement.
This is the code for my AutoCompleteTextView :
String[] countries = getResources().getStringArray(R.array.countries_array);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.item_list ,countries);
textView = (AutoCompleteTextView) dialog.findViewById(R.id.autoCompleteTextView1);
adapter.setNotifyOnChange(true);
textView.setAdapter(adapter);
This is item_list :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#000"
android:drawableRight="#drawable/arrow">
and this is the output :
I want to change this pop-up kind of suggestion box , and want to show the suggestion list a little bit below from the AutoCompleteTextView ..similar to this : (please disregard the apple design, it’s just about where the results appear)
How can i do this ..Please suggest .
Thanks.!!
Use the android:dropDownAnchor, android:dropDownHorizontalOffset, android:dropDownVerticalOffset and maybe others on the AutoCompleteTextView in your layout. See AutocompleteTextView.
I used a ListView below the AutoCompleteTextView. I added my custom adapter to both the ListView and the AutoCompleteTextView. I the set the height and width of the AutoCompleteTextView dropdown to 0:
myListView.setAdapter(myCustomAdapter);
myAutoComplete.setAdapter(myCustomAdapter);
myAutoComplete.setDropDownHeight(0);
myAutoComplete.setDropDownWidth(0);
Since the listview is also attached to the same adapter it'll get updated when you update the autocomplete.
u need QSB (Quick search box).
refer to this http://developer.android.com/resources/articles/qsb.html
It needs a ContentProvider to fill the QSB data.
I have a selection for some items using a spinner widget. At the moment the spinner will load a simple Textview and show the name of the item.
It is possible to define an own view to show inside the spinner row? I would suspect it being similar to a custom List row.
I simply want to show an individual icon left from the spinner text for each item in the list.
Yes, you can do that - as you may know, Spinner is a type of AdapterView, which means that you can adapt your own data to show in such View.
The approach is pretty similar to the one with ListView.
Excerpt from ApiDemos:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, INTERPOLATORS);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
You can define your own layout and pass it to the SpinnerAdapter subclass that you use.
Yes, it is very easy.
The main thing is that, define TextView as the root element in the layout file.
Do as below:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, INTERPOLATORS);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
simple_spinner_dropdown_item.xml file :
<?xml version="1.0" encoding="utf-8"?>
<TextView>
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50px"
android:textColor="#000000"
android:textSize="20px"
android:gravity="center_vertical" >
</TextView>