Android spinner item disappear during scroll - android

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.

Related

android Setting style to programmatically created spinner

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

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.

How to remove radio button in spinner only in layout view android

I have strange problem with spinner.
My spinner display radio button when i don't use it, check this out:
Image 1 Radio when not use
Image 1 Radio when use spinner
Someone know how fix it? I want radio button when i click on spinner and dropdown list shows, but now when i dont use spinner.
Thanks in advance.
when you set adapter for your spinner you can set it's layout like this:
String[] arr = new String[]{"a","b","c"};
selectionSpinner = (Spinner) findViewById(
R.id.dr);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arr);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectionSpinner.setAdapter(spinnerArrayAdapter);
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000" >
<Spinner android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/dr" />
</RelativeLayout>
result:

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!

How do I change the font color of the selection list in a spinner?

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.

Categories

Resources