How to set background of an spinner dialog - android

Hi I want to set the background for the dialog of my spinner. I am using following code
<Spinner android:id="#+id/my_ac_debt_card_spinner"
android:layout_toRightOf="#+id/my_ac_debt_card_text" android:entries="#array/my_ac_debt_array"
android:layout_marginRight="10dip" android:layout_marginLeft="50dip"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#drawable/my_ac_spinner_bg" android:paddingLeft="5dip"
android:gravity="center_vertical" android:popupBackground="#drawable/my_ac_round_rect"/>
Java code is like this:
Spinner spin = (Spinner) findViewById(R.id.my_ac_debt_card_spinner);
final ArrayAdapter<String> a = new SpinnerAdapter(
getActivity(),
android.R.layout.simple_spinner_dropdown_item,
debit_array);
spin.setAdapter(a);
But I am not able to set background of the popup dialog.
Can anyone please help me to solve this issue.
Thanks in advance.

Try putting this code into your ListAdapter for the Spinner in getDropDownView:
if (parent != null && parent instanceof ListView) {
((ListView) parent).setBackgroundResource(R.drawable.background);
}

Try changing
android:popupBackground="#drawable/my_ac_round_rect" to
to
android:background="#drawable/my_ac_round_rect"
Also, here's a good blog I found on customizing a spinner: http://www.gersic.com/blog.php?id=57

Related

populating a spinner items with multiple lines

Some items in my spinner are long and I would like the spinner to wrap the text over multiple lines depending on how long the text is. I looked over serval solutions for this but they are not working for me.
Here showing the text not fitting:
Here is my code in Java:
public void addItemsOnSpinner3() {
spinner3D = (Spinner) findViewById(R.id.activities1);
List<String> list = new ArrayList<String>();
list.add("Academic Year Opening Program");
list.add("Academic Year Closing Program");
list.add("LR1: Defining Primary Care, Health Care Disparities & Vulnerable Populations");
list.add("LR2: Community Resources and the Elderly");
list.add("LR3: Inter-professional Teams and the Homeless");
list.add("LR4: Quality Improvement and Veterans");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
R.layout.multiline_spinner_row, list);
dataAdapter.setDropDownViewResource(R.layout.multiline_spinner_row);
spinner3D.setAdapter(dataAdapter);
}
I created a custom row called "multiline_spinner_row.xml" where singleline is changed to false:
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="false"
xmlns:android="http://schemas.android.com/apk/res/android" />
All this does is make my selection wrap the text but not the items:
Any help would be greatly appreciated! Thanks in advance!
in spinner set this property:
android:spinnerMode="dialog"
in text view set these properties:
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
This actually worked for me (in Xamarin Android, but the problem is the same):
Instead of
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
write
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleExpandableListItem1);
create an xml file like this, your xml will need to have a parent layout to wrap the TextView.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#333333"
android:textSize="15sp"
tools:text="123" />
</RelativeLayout>
make sure your TextView has an id, then create your adapter like this.
private ArrayAdapter<CharSequence> stringAdapter = new ArrayAdapter<>(context, R.layout.spinner_textview, R.id.textView, displayTexts);
use this consturctor
public ArrayAdapter(#NonNull Context context, #LayoutRes int resource,
#IdRes int textViewResourceId, #NonNull List<T> objects) {
this(context, resource, textViewResourceId, objects, false);
}
or any constructor that has textViewResourceId as a parameter
that's it. you won't need to override anything else.
Check out the below image to view the effect.
enter image description here
enter image description here
Try this
I solved the problem with simple coding.
Just add Android.Resource.Layout.SimpleExpandableListItem1 when assigning the Adapter for Spinner as display below.
please check more on
https://notostuck.blogspot.com/2020/08/how-to-populating-spinner-items-with.html

How to increase the height of the Spinner's dropdown Listview (without using Custom ArrayAdapter)

I'm new to android and struggling in changing the height of the dropdown list's height of a spinner.
I'm using the following code to inflate my spinner.
code
String[] fil_array = { "Starts with", "Contains" };
ArrayAdapter<String> m_FilterAdap = new ArrayAdapter<String>(
Mse_Customer.this, android.R.layout.simple_spinner_item, fil_array);
m_Filter.setAdapter(m_FilterAdap);
Please can anyone guide me how to achieve it.
Please have a look over the attached image for more clarity.
The second image working correctly with the same type of code.Really confused why it is happening.
don't use android.R.layout make your own custom layout
layout_spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8.0dip"
android:text="Text"
android:textSize="20.0sp" />
String[] fil_array = { "Starts with", "Contains" };
ArrayAdapter<String> m_FilterAdap = new ArrayAdapter<String>(
Mse_Customer.this, R.layout.layout_spinner_item, fil_array);
m_Filter.setAdapter(m_FilterAdap);
Create custom_spinner_item.xml
<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="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp" />
Then in your code, replace:
ArrayAdapter<String> m_FilterAdap = new ArrayAdapter<String>(Mse_Customer.this, android.R.layout.simple_spinner_item, fil_array);
with
ArrayAdapter<String> m_FilterAdap = new ArrayAdapter<String>(Mse_Customer.this, R.layout.custom_spinner_item, fil_array);
create custom drop_down_spinner_item in xml and set:
layout_height="wrap_content" and minHeight="40dp";

listview adding spinner in each row?

I am facing problem while putting Spinner in layout. My acctual requirement is to place Spinner once, at the top of layout.
This output I am getting:
I have Relative layout
<Spinner
android:id="#+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="#array/string_array"
android:prompt="#string/spinner_msg"
/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/notesTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
</TextView>`
MyActivity class is extended by ListActivity here's onCreate method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView=getListView();
listView.setOnItemClickListener(listener);
spinner1=(Spinner)findViewById(R.id.spinner1);
getDetailsCursor();
String[] from = new String[] {"db_column"};
int[] to = new int[] { R.id.notesTextView};
curAdapter=new SimpleCursorAdapter(MyActivity.this, R.layout.mylist, null, from, to);
setListAdapter(curAdapter);
registerForContextMenu(listView);
}`
Here is the code in which spinner inflates on the top of listview :
listView= (ListView) findViewById(R.id.list);
LayoutInflater inflater = LayoutInflater.from(this);
View mTop = inflater.inflate(R.layout.spin, null);
authorityView.addHeaderView(mTop);
R.layout.spin is that layout which contains only spinner.
And inside the List you have to inflate textView only. as you are doing in mylist.xml ,
just remove spinner from it and made seprate xml for spinner.
so spinner is once, at the top of layout (ListView).
I have some difficulty making out what your actual question is, some layout.xml code would help here, too. I think, that you are placing the spinner inside the listitem.xml instead of the main.xml, so that it gets replicated for each item in your listview. Please share some code.
Since you declare both the TextView and the Spinner in mylist.xml, you get both those elements in each Item of your List.
If you only want one spinner, you shouldn't use a ListActivity, but instead create a normal Activity with a Spinner and a ListView in the Layout. Then define another layout to use for the list items (e.g. with only the TextView).

Android: Custom Spinner Layout

I am trying to make a fully custom spinner. I am running into difficulties with making the layout that pops up when you press on it. Here is my code for my adapter:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.my_array, R.layout.spinnertext);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
From what I have read in the documentation, the layout used apears to be set by the line:
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Although every time I change it to a new layout that I make, it makes the app fail when I try and use the spinner. I have tried to look for what "android.R.simple_spinner_dropdown_item" looks like so as to figure out if I am maybe missing anything.
All of my layouts I have tried have been linear or relative layouts, with only a textView.
How can I make a custom layout pop up when the spinner is selected?
row.xml to set up the layout on each row (in this case: one image and text each row):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"/>
<TextView
android:id="#+id/weekofday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Java:
public class AndroidCustomSpinner extends Activity {
String[] DayOfWeek = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.row, R.id.weekofday, DayOfWeek);
mySpinner.setAdapter(adapter);
}
}

ResourceNotFoundException using Spinner

I would like to create a customized popup dialog which contains a spinner. The dialog needs to be launched from an Adapter class, below is my code:
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.myPopup);
Spinner spinner = (Spinner)dialog.findViewById(R.id.spinner);
ArrayAdapter<String> arrayadapter = new ArrayAdapter<String>(mContext, 0);
arrayadapter.add("AddSomeStrings");
spinner.setAdapter(arrayadapter);
dialog.show();
This code is executed fine, but sometime after the "show()", I see an exception: Resources$NotFoundException. The last item in the callstack is Resources.loadXmlResourceParser. If I don't assign the spinner using findViewById, but instead assign it via spinner = new Spinner(dialog.getContext()), then I don't get the error (but then of course I cannot see my dialog).
myPopup layout contains:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:prompt="#string/group_prompt"
/>
</LinearLayout>
Any thoughts what I am doing wrong? thanks!
Your using the array adapter with Constructor Context and TextView ID, but your just passing it 0 as the text view resource id.
See the API here : ArrayAdapter
Try:
ArrayAdapter<String> arrayadapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item);
or other resource of you choice

Categories

Resources