A defined a spinner in my XML file like so:
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/spinnerbackground"
android:layout_marginLeft="96dp" />
The background loads correctly, however the text representing the selected item is aligned fully to the left. The default spinner however has the text offset from the left by a small amount.
Does anyone know the code or XML I can use to replicate the effect of offsetting the item display text?
To make custom spinner background according to your textstyle you can use my code that I have used in one of my projects.
Inside string.xml Write:
<string-array name="spinner_array_environtment">
<item>Test</item>
<item>Production</item>
</string-array>
Inside your MainActivity.java :
public class MainActivity extends Activity {
Spinner spinner_environment;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner_environment = (Spinner) findViewById(R.id.spinnerview);
adapter =ArrayAdapter.createFromResource(this, R.array.spinner_array_environtment,R.layout.spinner_phone);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner_environment.setAdapter(adapter);
}
Inside spinner_phone.xml :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="13dp"
android:textColor="#4C4646" />
try this out. Hope it will help you.
Related
the many other answers do tell me that android:textColor is the one that does the trick. However no matter what attribute I change in my styles, the app still shows default Black text.In fact, I cannot even see an attribute called textColor or the likes of it in the layout editor - attribs tab.
See here: Spinner with custom text font and color
Here you can make a custom xml file in layout folder where you can add 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:textColor="#333333"
android:padding="10dp"
android:textStyle="bold" />
And then in your code mention it like this:
val adapter = ArrayAdapter.createFromResource(this, R.array.array_name, R.layout.custom_spinner) // where array_name consists of the items to show in Spinner
adapter.setDropDownViewResource(R.layout.custom_spinner) // where custom-spinner is
mycustom xml file.
And then set the adapter.
I hope my solution would work,
<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:textSize="20sp"
android:textColor="#ff0000" />
And then change your declaration of the spinner to use the R.layout.spinner_item:
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, R.layout.spinner_item);
spinner.setAdapter(adapter);
in my project I have this custom layout inflated in the actionbar:
Link to image
But i would like to have the spinner text of white color.
I've seen a lot of topics on the web about this, but I can't fix the problem yet.
How can I fix this problem? How can I change the text color of my spinner bysetting it to white color?
EDIT:
the xml code of spinner is this:
<Spinner
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#ffffff"
android:id="#+id/spinner"></Spinner>
It's very simple, I tried many things but nothing..
In your activity you should have something like this:
(Basically you are getting the Spinner and setting it an adapter. This adapter is the responsible for filling it)
Spinner spinner = (Spinner) findViewById(R.id.your_spinner_id);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.your_selected_spinner_item, myArrayList<String));
adapter.setDropDownViewResource(R.layout.your_dropdown_item);
spinner.setAdapter(adapter);
And you can have this XMLs: (put them on res/layout)
your_selected_spinner_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myLayoutID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="20sp"/>
And also
your_dropdown_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myDropdownLayoutID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="20sp"/>
Create a Layout file like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinner_ref_id"
android:layout_width="match_parent"
android:padding="20dp"
android:textColor="#fff"
android:textSize="20sp"
android:text="Hello"
android:layout_height="wrap_content">
</TextView>
Assuming that you are using ArrayAdapter as your Spinner Adapter.
Hope it helps!!!
i have created a My custom listActivty by extending ListActivity like this.
public class MainList extends ListActivity {
ListView listView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView = getListView();
setContentView(R.layout.list_main);
}
}
And declaring its xml like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</ListView>
<TextView android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:text="My Text"
/>
</LinearLayout>
i am using CursorAdapter as listActivity adapter.Its showing perfectly with MainList is empty.
My problem is that i want to show more than a textview when MainList is empty. Basically i need custom view(imageview,button etc) to be shown when list is empty.How should i do this.
Thanks
For the listview there is one default method to set empty view.
setEmptyView(view)
Try This.... In the place of view pass your custom view.
If you are not getting then tell me. I will post an example for this.
You may want to count the items in your CursorAdapter. If 0 is returned, hide/show parts from your XML? That's how I done similar, but they may be better ways.
Try using the android:id/empty for your custom view. I think you can apply the id to any view, not just a TextView
I followed the AutoCompleteTextView tutorial exactly. The layout gets wrong when a soft keyboard is involved.
After tapping inside the dropdown (to select or scroll), the layout is broken:
I tried various combinations of attributes on the AutoCompleteTextView but none seems to work. I also tried setting windowSoftInputMode. The dropdown always pops up above the textbox, and stays there forever.
In landscape orientation, the layout is OK:
Is the standard autocomplete useless? Should I use a different one / write my own?
The code I used is the following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Country" />
<AutoCompleteTextView android:id="#+id/autocomplete_country"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"/>
</LinearLayout>
MainActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES);
textView.setAdapter(adapter);
}
Instead of using AutoCompleteTextView, I used EditText and a ListView as shown here.
I am trying to experiment with a custom Spinner / ArrayAdapter. Following is my code:
myspinner=(Spinner)findViewById(R.id.myspinner);
myadapter=new ArrayAdapter<String>(this,R.layout.mytextview,R.id.mytv,sample_data);
myadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
myspinner.setOnItemSelectedListener(this);
myspinner.setAdapter(myadapter);
Following is the custom layout mytextview that contains mytv:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="#+id/mylayout" android:layout_width="match_parent" android:layout_height="wrap_content">
<View android:layout_width="match_parent" android:layout_height="15dip" android:id="#+id/myview1" android:background="#color/bordertop"></View>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="30dip" android:id="#+id/mytv"></TextView>
<View android:layout_width="match_parent" android:layout_height="15dip" android:id="#+id/myview2" android:background="#color/borderbottom"></View>
</LinearLayout>
When i'm press the spinner to bring the popup, Android crashes. The came logic works perfectly fine, when I comment out the setDropDownViewResource method. I still don't understand what view setDropDownViewResource actually sets. Can anyone show me how it looks like by default. And, if one wants to override it with a custom layout - does the layout need to have a specific structure?
here is what i have typically used for spinners:
Spinner distancespinner = (Spinner) findViewById(R.id.distance);
ArrayAdapter<CharSequence> distanceadapter = ArrayAdapter.createFromResource(
this, R.array.distance, android.R.layout.simple_spinner_item);
distanceadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
distancespinner.setAdapter(distanceadapter);
distancespinner.setOnItemSelectedListener(new distanceSelector());
(obviously replace distance with whatever you want)
<Spinner
android:id="#+id/distance"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:prompt="#string/app_name"
/>
to me it looks like your problem is in this line :
myadapter=new ArrayAdapter<String>(this,R.layout.mytextview,R.id.mytv,sample_data);
but then i dont know, your ArrayAdapter is based on instead of as i have and as per Spinner "the android.R.layout.simple_spinner_item ID references a layout for the standard spinner appearance, defined by the platform. Then setDropDownViewResource(int) is called to define the appearance for each item when the widget is opened (simple_spinner_dropdown_item is another standard layout defined by the platform)."
i think if you want to customize your spinner, .setDropDownViewResource(); is where to do it.