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.
Related
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));
}
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've a spinner pointing on a CursorAdapter.
I set this adapter with a specific view.
I would like to fix the spinner's width, I tried a lot of stuff unsucessfully :
layout_width="100dp" // to force spinner with a specific size KO
reduce the layout width to 100dp // KO
use an ellipsize on a textview contained by the adapter // KO
I would like to block the spinner's width. Do you have an idea ?
You could try adding layout_padding to the left and right of it.
You can try creating a custom textview layout for your spinner to fix some properties.
For example, you can fix the width at 100 dp :
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:ellipsize="end" />
Then you can declare it like this:
mSpinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapterSpinner = ArrayAdapter.createFromResource(this, R.array.spinner_vals, R.layout.custom_simple_spinner_dropdown_item);
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>
Is it possible to set the width of a spinner dropdown list in code? I have a spinner populated with integers, and it does not look good with the list expanding to full width. Can I set the width to wrap the content somehow?
Spinner hSpinner = (Spinner) timerView.findViewById(R.id.timer_hour_spinner);
ArrayList<Integer> hList = new ArrayList<Integer>(21);
for (int i = 0; i <= 20; i++) { hList.add(i); }
ArrayAdapter hAdapter = new ArrayAdapter(RemindMe.this, android.R.layout.simple_spinner_item, hList);
hAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
hSpinner.setAdapter(hAdapter);
Thanks!
Linus
Just use:
setDropDownWidth(desiredWidth);
You can change the width of anything in code by adjusting its LayoutParams. The details of that varies by container (LinearLayout vs. RelativeLayout vs. ...).
However, I'm confused why you want to change the width "in code". Why not just set the width to be wrap_content in the layout XML?
It is straightforward to control the spinner dropdown appearance. Try this:
//Step 1. create the drop down list
static List<String> special_Spinner_Display_List = new ArrayList<String>();
// add your values to the list...(this is best done using a for loop)
special_Spinner_Display_List.add(item1);
special_Spinner_Display_List.add(item2); //etc., etc.
//Step 2. build the spinner
ArrayAdapter arrayAdapter_special = new ArrayAdapter(this,
R.layout.your_special_spinner, special_Spinner_Display_List);
arrayAdapter_special.setDropDownViewResource(R.layout.special_spinner_dropdown);
specialSpinner.setAdapter(arrayAdapter_special);
//Step 3. create an XML layout file called special_spinner_dropdown where you can
//style to your heart's content. Here's an example:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/SpinnerDropdown"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#D5ECED"
android:ellipsize="marquee"
android:singleLine="true"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="#color/black" />
That's it. Lemme know how it works!
Set dropdown width in xml file of spinner using the tag
android:dropDownWidth="#dimen/desired_width"
Explaination :
mDropDownWidth = pa.getLayoutDimension(R.styleable.Spinner_dropDownWidth,
ViewGroup.LayoutParams.WRAP_CONTENT);
this field is used for dropdown width when spinner item is initialized in Spinner class
for programatically changing spinner dropdown width above api level 16, use
mSpinner.setDropDownWidth(size_in_pixel);