Change text size from prompt using an Android spinner - android

I have a spinner with a custom text view like in the code below:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:singleLine="true"
android:textSize="16sp"
android:ellipsize="end"
android:gravity="center_vertical">
</TextView>
My problem is that the Text Size of 16sp is applied to the selected item (the text from prompt) too and it's too large for the spinner (it doesn't fit the spinner) and if I change the textSize to 11sp then the items on the list will be too small. How can I change the size only for the selected item?

Well, maybe you can modify textsize in setOnItemClickListener, by the way you can simply create a button and, clicking on it, show a dialog with a list of element so you emulate spinner behavior. In this way you can customize button text size and list as you want

Related

How to bring spinner arrow closer to selected text?

I know this question is repeated but I don't get the Proper solutions.
I want to bring arrow closer to the Spinner (That means i want to decrease the width of spinner and decrease the space between text and down arrow) I have used default spinner. How can I do that? Please, guide me to get Solution.
Here is the XML code
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/relativeLayout15">
<Spinner
android:id="#+id/languageDropdown"
style="#style/SpinnerTheme"
android:layout_width="66dp"
android:layout_height="wrap_content"
android:entries="#array/Languages" />
</RelativeLayout>
As i am understanding your question, many time we face requirement to customise spinner UI and it is too tough to achieve this. I generally use this solution for such situations:
Solution: Hide your actual Spinner and show a TextView which just looks like our desired Spinner, Please have a look at below image for better understanding:
In above image I have created a phone number field where I am getting country code from user using Spinner. I created a TextView having text as country code with drawable[right] of down arrow. Please check below XML code for better understanding:
In above image Spinner width is 0dp so your Spinner will not be visible. Just remember one thin you have to handle click event of TextView and call performClick() method for Spinner. Please refer below image for this code:
On the click event of your TextView, just follow below way:
textViewLabelCodeRegister.id -> {
spinnerCountriesRegister.performClick()
}
That's It.
Make PopupWindow instead of Spinner.

Android Spinner - How to position dropdown arrow as close to text as possible when options have different length?

The options in my spinner has different length and currently the dropdown arrow is positioned far to the right based on the longest option, as shown in the screenshot below.
Is it possible to move the dropdown arrow so that it is dynamically positioned based on currently selected option?
Especially when the first option is just 'All', it looks weird when the dropdown arrow is so far away to the right.
Referring to Google Translate App where dropdown arrow is always positioned next to its text:
You cannot control the position of the original dropdown icon, the only way is to disable the default icon and add your own one into the dropdown.
First, disable the default dropdown icon by setting the background of the Spinner to #null:
<Spinner
android:id="#+id/spinner_main"
android:spinnerMode="dropdown"
android:background="#null"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
Then create a layout resource spinner_item_main.xml with only one TextView which we can set a drawable on its right side (you can download the arrow picture from here):
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:gravity="left"
android:textColor="#color/colorWhite"
android:drawableRight="#drawable/ic_arrow_drop_down_white_24dp"
/>
Finally Set this layout resource when you initialize the Spinner, You can also provide a resource as the dropdown view (as what I have done):
(I use kotlin)
spinner_main.adapter = ArrayAdapter<String>(this,
R.layout.spinner_item_main, objects).apply {
setDropDownViewResource(R.layout.spinner_dropdown_view_main)
}
Make it!
View this in My APP
Translating #尹小雨 answer to java
ArrayAdapter adapter = new ArrayAdapter<Category>(this,R.layout.spinner_item_main,objects); // creates new ArrayAdapter with custom textviews for all elements including the first one (which is what we want)
adapter.setDropDownViewResource(R.layout.spinner_dropdown_view_main); // Modifies all the elements when you click on the dropdown
spinner.setAdapter(adapter); // sets the adapter to the spinner

How to increase the clickable width of an Android spinner?

I have a Spinner which displays single digit numbers (1-6).
When I test on a phone, it's virtually impossible to click on a number because they are so narrow (font size is 12sp) and the only area which appears to be clickable is the text itself.
Ideally I'd like the user to be able click on a greater width on the Spinner drop down than just the text. I've tried padding with spaces but these are suppressed, and I've tried using PaddingLeft/Right but again there's no difference.
Here is the Spinner
<Spinner
android:id="#+id/spinner_period1"
android:layout_toRightOf="#id/spinner_weekday1"
android:layout_below="#id/col1day"
android:layout_height="wrap_content"
android:prompt="#string/enterperiod"
android:layout_width="80dp"
android:entries="#array/periodlist"
android:layout_marginRight="340dp"
android:layout_marginBottom="20dip"
/>
Note that I also set the Spinner text attributes using:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textview"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="12sp"
android:textColor="#768766"
/>
Any ideas? Thanks in advance
Mark
Try making the TextView wider in the android:layout_width attribute.
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textview"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textSize="12sp"
android:textColor="#768766"
/>
That could possibly fix your issue or make it better at least.
Try setting your TextView layout width to some arbitrary value rather than "wrap_content". That should make the whole thing clickable, as opposed to just the wrapped content.
Just adding some padding to the spinner helped me fix this issue.
<Spinner
android:id="#+id/spinner"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:padding="25dp"
android:scrollbarSize="20dp" />
Way 1:
Display the spinner's options as a dropdown by using android:spinnerMode. Therefore have a look at the API (Android API Spinner). Or play around with android:dropDownWidth.
Way 2 (I used it in an APP but at the moment I have no access to the code):
Maybe you could set another layout while populating your spinner with text - instead of assigning the values in the XML.
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
It's copied from the following link Android Spinner. Maybe you could use one of the layout templates of android.R .

changing spinner text color based on background color

I have a custom spinner layout with an image and a text view but I noticed that depending on the manufacturers skin you cant see the text because of the color ie. white on white, black on black.
I also noticed that none of my other non-custom spinners do this and seems to change automatically so my question is how can I get the text color to change so that it can be read?
this is a non-custom spinner
ArrayAdapter<CharSequence> cAdapter;
cAdapter = ArrayAdapter.createFromResource(this, R.array.colors,android.R.layout.simple_spinner_item);
int cSpinnerDD = android.R.layout.simple_spinner_dropdown_item;
cAdapter.setDropDownViewResource(cSpinnerDD);
color.setAdapter(cAdapter);
the custom spinner I just override the view to put the images in with text
here is the layout for it
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/icon_txt"
android:paddingLeft="25dp"
android:textSize="18sp"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Use the default text color of the current theme (stored in the resource ID android.R.attr.textColorPrimary) :
<TextView android:id="#+id/icon_txt"
android:textColor="?android:attr/textColorPrimary"
... />
I think changing your theme changes the appearance of the listview.
I was also stucked at this point but what i did was that i made a new android app project and selected blank activity (in place of empty activity) and it worked (i know that's not a good solution but it solved my issue)

how to set text color in list view in android app

in my app i have created a list view and added some text in the list. In my coding part the text are been added as an array adapter to have a check box. In the layout i have given a white color for the list view because of this the text appear to be very dull. How to set the text color to be as black.
set the text color in your xml
< TextView
android:id="#+id/list_content"
android:textColor="#000000"
android:gravity="center"
android:text="sample"
android:layout_margin="4dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
If the text is added as a part of a TextView, you should be able to call setTextColor(int) on it.

Categories

Resources