as you can see from the code above, the color of the text is set to blue only when I open the drop-down menu while if the menu is not opened the spinner text remains white, I want to make sure that the color of the spinner text is blue even if it is not opened and not white, how can I go about implementing this? the code below is xml and kotlin
file.xml:
<Spinner
android:id="#+id/spinner"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_alignBottom="#id/textviewtipologia"
android:layout_marginLeft="100dp"
android:layout_marginTop="-40dp"
android:background="#F5F5F5"
android:popupBackground="#F5F5F5"
android:theme="#style/spinnerstyle" />
<style name="spinnerstyle" parent="Base.Widget.AppCompat.Spinner">
<item name="android:textColor">#color/customSpinner</item>
<item name="android:textColorHighlight">#color/customSpinner</item>
<item name="android:textColorHint">#color/customSpinner</item>
</style>
file.kt
spinner = findViewById<Spinner>(R.id.spinner)
var image: ImageView = findViewById<ImageView>(R.id.imgauto)
var listaTipologia: List<TipologiaVeicolo> =
APISupport.getListaTipologieVecioli(firebaseid, email, key)
val tipologieAuto: ArrayList<String> = ArrayList()
var count = 0
for (i in listaTipologia.indices) {
if (count == 0) {
idVeicoloSelect = listaTipologia[i].ID
}
tipologieAuto.add(listaTipologia[i].DESCRIZIONE)
count++
}
You can try to create the spinner in the following way:
First add the spinner in your layout file:
layout.xml
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Now create an spinner_item.xml layout file adding a TextView in that which will work as the selected item of the spinner:
spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- You can change content of the textview as your need-->
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#F5F5F5"
android:textSize="18sp"
android:gravity="left"
android:padding="5dip"
/>
Now create a string-array in your strings.xml file which will be the content of the spinner:
strings.xml
<string-array name="languages">
<item>Hindi</item>
<item>English</item>
<item>Spanish</item>
<item>German</item>
<item>Japanese</item>
</string-array>
Now the final thing is to add code in your activity class:
activity.java
// call this method in onCreate() method of activity class
public void setSpinner(){
Spinner spinner = findViewById(R.id.spinner1); // get the spinner in the layout file
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.languages,
R.layout.spinner_item); // create adapter for spinner and set the item layout
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // set the drop down view to the adapter
spinner.setAdapter(adapter); // set adapter to the spinner
spinner.setOnItemSelectedListener((OnItemSelectedListener) this); // set listener to the spinner to get the data whenever item is selected from spinner
}
// override this method to get the selected item of the spinner whenever item of the spinner is selected
// also implements OnItemSelectedListener to your activity class
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// this is the text selected from the spinner
String text = parent.getItemAtPosition(position).toString();
// do whatever you want from the text
Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();
}
Hope this helps 🙏
Related
I have a spinner widget in my activity. If I run the app on my device (Pie version) the spinner works fine but if I run it on emulator (which has lollipop version- requirements of work)- spinner doesn't show any items at all.
So this is my activity code for the spinner (inside onCreate):
spinner = (Spinner) findViewById(R.id.spinner_reminder_times);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(getApplicationContext(),
R.array.reminder_times_array, android.R.layout.simple_spinner_dropdown_item);
// Specify the layout to use when the list of choices appears
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the spinnerAdapter to the spinner
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(this);
And this is the implemented method:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getSelectedItemPosition()) {
case 0:
// do stuff
break;
case 1:
// do stuff
break;
case 2:
// do stuff
break;
}
}
The spinner xml in activity_layout:
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner_reminder_times"
android:theme="#style/ThemeOverlay.AppCompat.Light"
android:spinnerMode="dropdown">
</Spinner>
What may be the problem? how comes it works perfectly on my device but on android lollipop emulator doesn't show anything?
Thanks!
Create a new layout file, called my_spinner_dropdown_list_item.xml (probably change textColor once you confirm it works):
<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:textColor="#FF0000"
android:layout_height="?android:attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"/>
And assign this layout as your Spinner's new dropdown resource:
spinner = (Spinner) findViewById(R.id.spinner_reminder_times);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(getApplicationContext(),
R.array.reminder_times_array, R.layout.my_spinner_dropdown_list_item);
// Specify the layout to use when the list of choices appears
spinnerAdapter.setDropDownViewResource(R.layout.my_spinner_dropdown_list_item);
// Apply the spinnerAdapter to the spinner
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(this);
enter code here
android:popupBackground="#59b0b9"
// try this to change the popupBackground color
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner_reminder_times"
android:theme="#style/ThemeOverlay.AppCompat.Light"
android:popupBackground="#59b0b9"
android:spinnerMode="dropdown"/>
When I use spinner with radio button, selecting color in radio button seems default. I want to change the default color of radio button set in spinner in Android. How can I do it?
string file for spinner:
<string-array name="Departments">
<item>Body Exterior</item>
<item>Body Interior</item>
<item>Chassis</item>
<item>EESE</item>
<item>Powertrain</item>
<item>Vehicle Engineering</item>
<item>Other</item>
xml file for spinner:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Spinner
android:id="#+id/spnDepartments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="3"
android:gravity="left" />
</LinearLayout>
java file for spinner:
Spinner spnDepartments;
ArrayAdapter<CharSequence> adapter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
spnDepartments = (Spinner) findViewById(R.id.spnDepartments);
adapter1 = ArrayAdapter.createFromResource(this,R.array.Departments,
android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
spnDepartments.setAdapter(adapter1);
spnDepartments.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
You can control the color of the radio buttons within your spinner by setting the theme attributes colorAccent (for the selected radio button) and colorControlNormal (for the unselected buttons).
Normally, these are set in your app's (or your activity's) theme (the style referenced from android:theme in your manifest). But if you want to override them for a single Spinner, you can do that too.
First, create a <style> that has the colors you want:
<style name="SpinnerTheme">
<item name="colorAccent">#00f</item>
<item name="colorControlNormal">#00f</item>
</style>
Then, apply this style as your Spinner's theme:
<Spinner
android:theme="#style/SpinnerTheme"
.../>
Android:buttonTint="#color/yourcolor"
but it only works with API 21 or above
I am customizing spinner in Android / Xamarin project. It's set to SimpleSpinnerDropDownItem. I would like to add icon which is with selected / default item. What I've already did with style is Spinner background (it's white and rounded), also there is style for background in drop down list.
Spinner implementation
Spinner spinner = FindViewById<Spinner>(Resource.Id.spinner);
spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spinner_ItemSelected);
var adapter = ArrayAdapter.CreateFromResource(
this, Resource.Array.organisation_array, Android.Resource.Layout.SimpleSpinnerItem);
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
spinner.SetSelection(1);
spinner.Adapter = adapter;
XML in Activity
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:background="#drawable/rounded_shape"
style="#style/DropDownSpinner.My"
android:minHeight="50dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp" />
XML Styles
<style name="DropDownSpinner.My" parent="#android:style/Widget.Holo.Light.Spinner">
<item name="android:popupBackground">#drawable/menu_dropdown_panel_spinner</item>
</style>
I hope this code work for you :
private void spinner_ItemSelected (object sender, AdapterView.ItemSelectedEventArgs e)
{
Spinner spinner = (Spinner)sender;
spinner.GetItemAtPosition(e.Position)
.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.favicone,0);
// Reset the others
}
create a custom adapter with custom layout.
public class MyAdapter extends ArrayAdapter<String>{
you should have a special layout for selected item (layout with text and a start icon)
below is the psudo code
#Override
public View getDropDownView(int position, View cnvtView, ViewGroup prnt){
...
int selected = mySpinner.getSelectedItemPosition();
if(position == selected){
return special layout with the start icon
}
return normal layout
}
Is there a way to set the dropDown background for a MultiAutoCompleteTextView in the xml layout? Or must it always be done programmatically?
In the adapter you have created for the MultiAutoComplete, use a custom layout file instead of android's simple_list_item_1.
In that way you will be able to change the layout of the DropDown menu and to add the background you want.
find below a clear example of a MultiAutoComplete using a custom xml layout :
Main Activity, MainActivity.java:
public class MainActivity extends Activity {
private MultiAutoCompleteTextView multiAutoComplete;
private ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the string-array defined in strings.xml
String[] items = getResources().getStringArray(R.array.itemlist);
//set your custom xml file to be able to edit it
adapter = new ArrayAdapter<String>(this,android.R.layout.custom_drop_down_menu,items);
//multiAutoComplete field
multiAutoComplete = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoComplete);
// set adapter for the multi auto complete fields
multiAutoComplete.setAdapter(adapter);
// specify the minimum type of characters before drop-down list is shown
multiAutoComplete.setThreshold(2);
// comma to separate the different colors
multiAutoComplete.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
//toast message when the user clicks an item of the drop-down list
multiAutoComplete.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
Toast.makeText(getBaseContext(),"you added: "+arg0.getItemAtPosition(arg2),Toast.LENGTH_LONG).show();
}
});
}
}
custom MultiAutoComplete layout, custom_drop_down_menu.xml :
<!-- add your background image here -->
<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:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingLeft="6dp"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#000"
android:background = "#drawable/imagename"/>
Strings file ( to add your string-array items), Strings.xml :
<resources>
<string name="multi_auto_complete_items">Multi Auto Complete items</string>
<string-array name="itemlist">
<item >item1</item>
<item >item2</item>
<item >item3</item>
<item >item4</item>
<item >item5</item>
</string-array>
</resources>
Main Activity layout, activity_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"
tools:context=".MainActivity" >
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="#string/multi_auto_complete_items"
android:textAppearance="?android:attr/textAppearanceMedium" />
<MultiAutoCompleteTextView
android:id="#+id/multiAutoComplete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text2"
android:layout_marginTop="20dp"
android:ems="13" />
</RelativeLayout>
I have CustomListView ,ie contain a background image and TextView,
When an item is selected i need to change the background image and font color, currently i can change the background of selected row of the listview using an xml, but i can't change the text color.
By default my text color is black when am clicking an item in listview i need to change the text color to white.
am using following layout for my customlistview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="29dp"
android:id="#+id/appcategoryLinearLayout"
android:background="#drawable/appcategorybg1"
android:gravity="left|center_vertical"
>
<TextView
android:gravity="left|center_vertical"
android:text="fdsfsdfsdfdsfdsfdsf"
android:paddingLeft="8dp"
android:textSize="8dp"
android:textColor="#color/black"
android:id="#+id/appCategoryNameTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
The only way to write your own ListAdapter and write custom controls which would have the visual properties you define.
I am not sure if this will help you but you can do some thing like this
Use TextView variables as global
TextView t ;
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
if(t != null)
{
//reset the color to black
}
LinearLayout lay = (LinearLayout)v;
t = lay.getChildAt(0);
//now set text to bold
}
};