This is a very basic question
I'm trying to create a spinner in my android layout. Then i found a website teaching how to create an ArrayAdapter for that spinner
ArrayAdapter<Giveaway.GiveawayCategory> dataAdapter = new ArrayAdapter<Giveaway.GiveawayCategory>(this, android.R.layout.simple_spinner_item, new ArrayList<Giveaway.GiveawayCategory>(20));
dataAdapter.add(ImportGiveawayActivity.selectCategory);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
this.spinner.setAdapter(dataAdapter);
it works very fine but there is a giant margin between the items...
So i looked into simple_spinner_dropdown_item and found:
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"/>
probably the height is not good, or the style sets a huge margin, i tried to find those files but i couldn't
How can i override these values in my style.xml
this is what i want to change
Related
I have a spinner which is created programmatically like this:
spinnerLogger = new Spinner(context);
spinnerLogger.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
SpinnerAdapter spinnerAdapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_dropdown_item, spinnerNames);
spinnerLogger.setAdapter(spinnerAdapter);
in my code I also use xml created spinners which use a style to make sure the user understands they are spinners:
<Spinner
android:layout_width="90dp"
android:layout_height="wrap_content"
android:id="#+id/spinnerHour"
android:spinnerMode="dropdown"
style="#style/Widget.AppCompat.Spinner.Underlined" />
I want to apply the Widget.AppCompat.Spinner.Underlined style to the programmatically created spinner but can't seem to find how I can achieve this.
I tried by creating an xml containing only a spinner with the right style (xml's name is spinner_underlined and it's located in the layout folder):
<?xml version="1.0" encoding="utf-8"?>
<Spinner android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner"
xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/Widget.AppCompat.Spinner.Underlined"/>
when i try to apply this using
SpinnerAdapter spinnerAdapter = new ArrayAdapter<>(context, android.R.layout.spinner_underlined, spinnerNames);
it gives me a cannot resolve symbol 'spinner_underlined' error
You need change to R.layout.... not android.R.layout....
I am trying to customize the default appearance of a Spinner item.
I have defined a layout in a file by the name of :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:textSize="20sp" />
And here when initializing the spinner I am trying to assign this layout to it:
ArrayAdapter<String> nearByLocationsAdpt = new ArrayAdapter<String>(this, android.R.layout.spinner_item_layout,nearByLocationsArray);
But it keeps saying that spinner_item_layout cannot be resolved or
is not a field however it is present in the layout folder?
Another related question is it possible to view and edit the default spinner layout
xml file i.e. android.R.layout.simple_spinner_item
The problem is this:
android.R.layout.spinner_item_layout
You are using the default layout. You have to supply the one you created instead. Depending on the how you named your layout that would be something like this:
R.layout.my_spinner_item
I finished the English version of my application and now I am working on the Arabic localization. Arabic is a right-to-left language, so I need to adjust a lot of things in my layout, including my spinner display.
When I choose an item from the spinner selection menu, I see the following,
As you can see "Allergy" is aligned to the left and on top of the spinner arrow. I need it to be aligned to the right.
NOTE The Arabic webservices aren't finished yet, that's why the data in the image is still in English.
EDIT
Spinner mSpecialtySpinner = (Spinner) findViewById(R.id.specialty_appointment_spinner);
ArrayAdapter<Specialty> adapter = new ArrayAdapter<Specialty>(AppointmentReservationActivity.this,android.R.layout.simple_spinner_item, specialities);
adapter.setDropDownViewResource(R.layout.spinner_item);
mSpecialtySpinner.setAdapter(adapter);
Well you can have another layout for Spinner and pass it to ArrayAdapter's constructor.
make an xml file naming spinner_layout.xml in layout like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_width="fill_parent"
android:textSize="20sp"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:gravity="right"
/>
and now pass it in ArrayAdapter's constructor like this:
new ArrayAdapter<String>(this,R.layout.spinner_layout,conversionsadd);
Please remember:
what we have provided as R.layout.spinner_layout in ArrayAdapter constructor is layout of Spinner. and what we will provide as setDropdownView() is Spinner item (drop down item).
You can have a look at my another relative answer here
All you need to do is create a new spinner dropdown layout and set android:gravity="right" on the appropriate widget.
Call it with:
yourSpinner.setDropDownViewResource(R.layout.right_aligned_spinner_dropdown_item);
I have a spinner with a style. The style only contains a background for the spinner. The problem is,that no matter which image I use for the background, the text of the spinner is always pushed to the left.
This is the declaration of the Spinner in the XML -
<Spinner
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/minus"
android:layout_width="wrap_content"
android:layout_below="#+id/female"
android:id="#+id/spin"
android:gravity="center"
android:background="#drawable/spin"
android:layout_marginTop="10dip">
</Spinner>
Also, I get a warning under the android:gravity attribute that says it's an unknown XML attribute.
I can't figure why it does that.
Thanks
Continuing from my last comment above...
The following code modifies the Hello Spinner tutorial application to display the spinner text contents centered horizontally, as seen in the following two screenshots.
res/layout/my_spinner_textview.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/spinnerItemStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" />
public class HelloSpinner extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets_array,
// android.R.layout.simple_spinner_item);
R.layout.my_spinner_textview);
// adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(R.layout.my_spinner_textview);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
//No other modification needed.
This hopefully provides enough direction to fix the problem.
Use android:textAlignment="center" tag on spinner
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/state"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"/>
Add gravity in your xml file...
<Spinner
android:gravity="center">
</Spinner>
Or add gravity in your java code...
Spinner spinner = (Spinner)findViewById(R.id.spinner);
spinner.setGravity(17);
//17 = center
I had this problem as well and the accepted answer did not work to me.
If this also applies to you, make sure that no parent layout overrides the gravity of the spinner item. In my case, I had to set the gravity of the entire Spinner to right.
If you want to see the checkboxes in the dropdown menu while using your own layout:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
mContext, R.array.room_list, R.layout.spinner_layout);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
yourSpinner.setAdapter(adapter);
Please notice the "android" before "R.layout...." in the line where you put the drop down view resource!
I've created a spinner, from where a user can select a value. I CAN change the textcolor of the spinner itself like shown in this picture:
Unfortunately when I press the spinner a list of items, to be selected, is shown, but these have a white font color on white background, and I just haven't been able to change this:
I've googled the problem and others have experienced the same problem. None of the fixes suggested worked for me.
As far as I understand I have to make a custom list of some kind, but well.. I'm not able to make it work. Any suggestions?
My code looks like this (array_spinner is a array of strings):
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.row, array_spinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
And my row.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnerText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#000000"
android:textSize="14dp" />
Try this (untested). I'm basically reproducing simple_spinner_dropdown_item.xml but with a black text color:
Replace the line
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.row, array_spinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
with
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.row, array_spinner);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
Add this file in res/layout, called spinner_dropdown_item.xml:
<CheckedTextView
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textColor="#FF000000"
/>
Simple and Crisp
private OnItemSelectedListener your_spinner _name = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
I was having this same issue. My answer isn't the most proper answer, but it's a very quick solution for those using a basic app theme.
If you are creating the arrayadapter for the spinner using
createfromResource()
Do NOT use getApplicationContext(). Declare Myclass.this instead. My text now stays the same as the basic app theme. Hopefully this will help someone else.