android Setting style to programmatically created spinner - android

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....

Related

override default android attr styles

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

Android spinner item disappear during scroll

I have created two spinner, but when I add a custom layout to adapter, during scroll some items disappear.
xml file:
<?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="25sp"
android:padding="20sp"
android:textColor="#000000"
android:textAlignment="center"/>
java file:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_text, list);
spinner.setAdapter(adapter);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, R.layout.spinner_text, list2);
spinner2.setAdapter(adapter2);
How can i resolve the problem?
This is probably happens when you are supporting multi languages in you application, where different languages requires different layout direction.
Try adding android:layoutDirection to your text view in the custom spinner item layout and this will be fixed.

Adding custom layout to listview in android not recognized

I am trying to add a custom layout to my listview where I am calling:
mylistAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, carList);
I was told I am able to replace the simple_list_item_single_choice with a custom xml. I made a xml and added it to the layout folder in my project and called it mytextview.xml, here it is:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:paddingTop="2dip"
android:paddingBottom="3dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="2dp" />
My issue is that when I go to change simple_list_item_single_choice to mytextview, it does not find my custom layout file. Anybody have any idea how to correct this?
Just replace your code
mylistAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, carList);
With this
mylistAdapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_single_choice, carList);
I hope this will help you.

Spinner layout cannot be resolved or is not a field

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

Android - Text is Pushed to the Left in a Spinner

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!

Categories

Resources