I have two spinners in my app. One spinner statically loads List items from XML file to display. Other spinner gets list of strings from database and displays. I am using same XML attributes for both spinners. But the spacing between individual items is different for both. The spinner with static list of strings has more spacing between items. The spinner which loads items from database has some sort of wrapped height. The items are close to each other making it difficult for user to select.
Any solution for this problem?
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Spinner
android:id="#+id/spinner3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
java code
static_sp = (Spinner) findViewById(R.id.spinner1);
List<String> array_karant = Arrays.asList(getResources().getStringArray(R.array.karant_list));
ArrayAdapter<String> karant_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_karant);
karant_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
static_sp.setAdapter(karant_adapter);
static_sp.setSelection(0);
static_sp.setOnItemSelectedListener(new select_karant());
database_sp = (Spinner) findViewById(R.id.spinner3);
return_likes = db.getAllLikeList();
ArrayAdapter<String> like_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, return_likes);
like_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
database_sp.setAdapter(like_adapter);
database_sp.setOnItemSelectedListener(new select_like());
Link to image is here:
Thanks,
Sameer
create a layout simple_spinner_item
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
and change your code
static_sp = (Spinner) findViewById(R.id.spinner1);
List<String> array_karant = Arrays.asList(getResources().getStringArray(R.array.karant_list));
ArrayAdapter<String> karant_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_karant);
karant_adapter.setDropDownViewResource(R.layout.simple_spinner_item);
static_sp.setAdapter(karant_adapter);
static_sp.setSelection(0);
static_sp.setOnItemSelectedListener(new select_karant());
database_sp = (Spinner) findViewById(R.id.spinner3);
return_likes = db.getAllLikeList();
ArrayAdapter<String> like_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, return_likes);
like_adapter.setDropDownViewResource(R.layout.simple_spinner_item);
database_sp.setAdapter(like_adapter);
database_sp.setOnItemSelectedListener(new select_like());
If u want spacing u have to added the padding to the textview
create a layout simple_spinner_item.xml, like this
<?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:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"/>
The last two lines is for add space between each item, then you have modify your class, in this line:
karant_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_karant);
Because you are using the android layout and we need use our layout (delete the word android.):
karant_adapter = new ArrayAdapter<String>(this, R.layout.simple_spinner_item, array_karant);
Related
I n the below I am facing an problem of that the adapter of the spinner mentioned belwo is not recoginzed despite it followed some example.
the values to be displayed is saved in
<string-array /> in the rsources.
the eror I am getting is:
can not resolve construuctor "ArrayAdapter(....,int, int, strng[])
I would appreciate it if you can provide solution quickly
adapter:
mGearTypesItems = this.getResources().getStringArray(R.array.millenium_gear_types);
//ERROR:
//CANTO resolve constructor array adapter
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
R.layout.spinner_item_for_type,
R.id.textviewForGearTypeItems,
mGearTypesItems);
spinner_item_for_type
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textviewForGearTypeItems"
android:layout_width="wrap_content"
android:layout_height="38dp"
android:layout_gravity="right"
android:gravity="right|center_vertical"
android:layout_alignParentRight="true"
android:paddingLeft="#dimen/text_left_padding"
android:textSize="17sp"
android:background="#color/Aqua"
android:textColor="#ff0000"/>
Spinner ui
<Spinner
android:id="#+id/type_spinner"
android:layout_width="match_parent"
android:layout_height="44dp"
android:layout_gravity="right"
android:layout_marginBottom="6dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:entries="#array/millenium_gear_types"/>
UPDATE
//Error:
//Canot resolve Symbole createFromResource..from a fragment context
ArrayAdapter spinnerArrayAdapter = new
ArrayAdapter.createFromResource(getActivity,
R.array.millenium_gear_types,
R.layout.spinner_item_for_type);
That is the wrong way to declare and initialize an ArrayAdapter object. You can initialize it using the static method createFromResource().
ArrayAdapter spinnerArrayAdapter = ArrayAdapter.createFromResource(this,
R.array.millenium_gear_types
R.layout.spinner_item_for_type);
To learn more about ArrayAdapters, read the documentation.
I hope this helps. Merry coding!
Please change this
mGearTypesItems = this.getResources().getStringArray(R.array.millenium_gear_types);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
R.layout.spinner_item_for_type,
R.id.textviewForGearTypeItems,
mGearTypesItems);
to this
mGearTypesItems = getActivity.getResources().getStringArray(R.array.millenium_gear_types);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(getActivity(),
R.layout.spinner_item_for_type,
R.id.textviewForGearTypeItems,
mGearTypesItems);
I am building an Android application and I need to change the text color of all cells in the ListView.
This is the code I use to populate and connect the ListView with the Activity file:
protected void onCreate(Bundle savedInstanceState) {
...
// Create a temp array.
ListView lv = (ListView) findViewById(R.id.gamesList);
displayGames = new ArrayList<String>();
// Populates ListView
updateList();
// Create Array Adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,displayGames);
lv.setAdapter(adapter);
}
My problem is that the text of the TextView in the cells does not have the correct color even though I specifically changed it on the XML file. See this code below:
This is the XML code for the creation of the ListView:
<ListView
android:id="#+id/gamesList"
android:layout_width="396dp"
android:layout_height="342dp"
android:layout_marginStart="9dp"
android:layout_marginEnd="10dp"
app:layout_constraintBottom_toTopOf="#+id/linearLayout6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout5"
app:layout_constraintVertical_bias="0.854" />
I defined the Text cells in a new XML file called simple_list_item_1.xml.
<TextView
android:id="#+id/game"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#color/white"
android:textColorHint="#color/white"
tools:ignore="MissingConstraints" />
I am running the app on a Pixel 2 emulator using API 22 Android 5,1.
While setting Adapter to ListView you are referring android.R.layout.simple_list_item_1 which is default system resource and not the one you created in your xml. You need to use R.layout.simple_list_item_1
// Create Array Adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_1,displayGames);
Change android.R.layout.simple_list_item_1 to R.layout.simple_list_item_1
// Create Array Adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_1,displayGames);
I'm trying to show simple spinner
mSpinnerHeaderType = (Spinner) findViewById(R.id.spinner);
String[] items = new String[]{Constants.TYPE_112R, Constants.TYPE_314R};
ArrayAdapter<String> adapter = new ArrayAdapter<>(mContext, android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinnerHeaderType.setAdapter(adapter);
In layout xml
<Spinner
android:id="#+id/spinner"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|left"
android:spinnerMode="dropdown"></Spinner>
But when I click on spinner,
either 1. the dropdown list width is almost zero
OR 2. there are no itmes in dropdown
I tried, 1. giving spinner width as match_parent in xml layout and 2. using dropDownWith property for spinner etc, but nothing working
See image below:
What wrong I'm doing?
try this.
mSpinnerHeaderType = (Spinner) findViewById(R.id.spinner);
String[] items = {Constants.TYPE_112R, Constants.TYPE_314R};
ArrayAdapter<String> adapter = new ArrayAdapter<>(mContext, android.R.layout.simple_spinner_item, items);
mSpinnerHeaderType.setAdapter(adapter);
xml file
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"></Spinner>
I was able to set Color to the first item in the Spinner using the following line of code. But how to give color to the item other than the first item selected by the user.
List<String> spinnerArray =getContacts();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,R.layout.spinner_effect, spinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner contactSpinner = (Spinner) findViewById(R.id.SpinnerchooseContact);
contactSpinner.setAdapter(adapter);
((TextView) contactSpinner.getChildAt(0)).setTextColor(Color.GRAY);
create a new xml file, named dropdown.xml, define you style in this layout
<?xml version="1.0" encoding="utf-8"?>
<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/listPreferredItemHeight"
android:ellipsize="marquee"
android:textColor="#aa66cc"/>
then set to it your adapter
adapter.setDropDownViewResource(R.dropdown);
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";