I tried to follow advices here:
How to change spinner text size and text color?
and here:
http://tekeye.uk/android/examples/ui/changing-android-spinner-text-size
but without success.
First I tried to re-use/change the android.R.layout resources. I go to the definition by clicking in Android Studio the Ctrl + B key combination on android.R.layout.simple_spinner_item.xml.
I find the path to the resource file out there. I copied the resource file and added a new layout spinner_item.xml in my Package.R.layout folder ( which has this path: /home/myusername/Android/Sdk/platforms/android-25/data/res/layout) and change the textColor of textview:
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:ellipsize="marquee"
android:textAlignment="inherit"/>
and then call it in adapter:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner2 = (Spinner) findViewById(R.id.blokkora);
//Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,
R.array.BlokkoraSorszama, android.R.layout.my_spinner);
// Specify the layout to use when the list of choices appears
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner2.setAdapter(adapter2);
}
but I get 'Can not resolve symbol 'my_spinner' out there. Why?
Then I tried also to make my_spinner.xml in the project's explorer of this project and use it as adviced in the link above, but get again the above error message.
Finally I tried to add my_stiles.xml too and use it as adviced but still get the above mentioned error message. What am I missing here?
/home/myusername/Android/Sdk/platforms/android-25/data/res/layout is the Android SDK path. You should not add your custom layouts here.
You should instead, add your custom layouts to your project path.
Something that looks like:
path/to/your/project/src/main/res/layout/
Resources like layouts and styles go into your projects /res folder.
Related
I know this question has been asked a lot (see: How to customize a Spinner in Android,
How to change layout of spinner in Android, How to change spinner text size and text color?, How to change spinner text color)
All the answers suggest to create a custom_spinner.xml file to accomplish this task. This file must be something like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top"
android:singleLine="true"
android:textColor="#color/#FFFFFF" />
However, no one says where to locate this file. In this answer https://stackoverflow.com/a/37859442/5616178 says this file must be in Drawable folder, but when I do that, an "Element TextView must be declared" error is raised by Android Studio. When I locate it in the layout folder, the R.layout won't resolve it either.
Thank you for your answers.
EDIT
I was able to solve the problem. As many of you said, the file must be located inside the res/layout since it's a layout resource. At first, my code looked like this:
citiesSpinner.setAdapter(
new ArrayAdapter<String>(SignUp.this,
android.R.layout.custom_spinner,
cityNames)
);
When I declared the adapter outside the constructor, i.e.:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
SignUp.this,
R.layout.custom_spinner,
cityNames);
citiesSpinner.setAdapter(adapter);
The R class was able to resolve the layout. I don't know why that happens, it would be useful if someone can explain it. Thank you again for your answers!
However, no one says where to locate this file.
That is a layout resource. It would go in res/layout/ by default.
When I locate it in the layout folder, the R.layout won't resolve it either.
Then you have some other problem. For example, perhaps there is a bug in your layout resource that is preventing the R class from being regenerated.
Your resource file must be in the layout folder
Your android:id="#android:id/text1" need to be android:id="#+id/text1"
Just declare your array like the links above recommended :
ArrayAdapter<String> adapter = new ArrayAdapter<String>this,R.layout.custom_spinner,yourArray);
I followed these instructions to add a spinner to my Toolbar which I am using as the action bar in my Android app.
However the text size is too small.
How do I get the correct text size for an action bar item?
For an example of the correct size see the Action Bar guide.
Try adding the below property to your spinner item, it worked for me :
android:textSize="?attr/actionBarSize"
Hope this helps..
Edit :
Okay, I wasn't telling you about toolbar, let me show an example which might help you :
I assumed in my answer that you have a custom layout defined for spinner, if not then have a look at below code :
Define spinnerLayout.xml file in your layout resource :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnerLayoutItems"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="?attr/actionBarSize"
android:textColor="#FF0000"
android:gravity="center"/>
Now, add this in your activity :
.....
Spinner spinner = (Spinner) findViewById(R.id.spinner); // Your spinner id in layout.
.....
ArrayAdapter<String> adapter = ArrayAdapter.createFromResource(this,
R.array.list,R.layout.spinnerLayoutItems);
spinner.setAdapter(adapter);
....
So, I was telling to set your textSize in spinner to "?attr/actionBarSize", you can set it anything, as big as you want.
I am sorry, I by mistake wrote in my previous answer : android:layout_height property, I have corrected it.
Hope this helps....
The accepted answer is wrong. Because ?attr/actionBarSize is 56dp!! Its HUGE for text. The right way to do it is set a style to the TextView as follows:
style="#style/ActionBarTitle"
If you are using AppCompat, you can also use this instead:
style="#style/Base.TextAppearance.Widget.AppCompat.Toolbar.Title"
Both achieve the same result. Styles in Android are repeated if you use support libraries.
I use a Spinner inside a layout.
I use this code:
...
<Spinner
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/spin_prod_promo" />
...
Now, when I assign it's adapter I use this code:
...
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(mcontext, android.R.layout.simple_spinner_item, mylabels);
// Drop down layout style
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner myspinner = (Spinner) V.findViewById(R.id.spin_prod_promo);
// attaching data adapter to spinner
myspinner.setAdapter(dataAdapter);
...
My problem is that, the text color of the spinner is white... thus unreadable, and I do not set it to be this way anywhere in the code.
The "funny thing" is that I use the exact same code in a different activity and the Spinner has the right colors.
The difference is that in this (BAD) case the spinner is located in a fragment.
So if the spinner is in an activity - colors are fine
If the spinner is located on a fragment - color is messed up
I tried using a custom layout for the items of the spinner, but all I succeeded was to make the spinner look disabled. Plus there is always an item in the dropdown that has the color of the background (it can only be seen if I click on it, then I can see the content as long as I keep the click on)
What is going on here?
How can I solve this problem?
Is there something I can do to set the colors of the spinner to be the colors of the theme?
All other widgets in the fragment have normal colors...
Thank you
Context mcontext = getActivity().getApplicationContext(); Is this wrong?
Yes, this is wrong. Only use getApplicationContext() when you know exactly why you are using getApplicationContext(). In particular, never use getApplicationContext() for inflating layouts, as it will screw things up by not using the right theme.
I would delete mcontext entirely, replacing it with getActivity() where needed in your fragment.
just try this..
String[]array={"A","B","C","D"};
ArrayAdapter<String> adapter=new ArrayAdapter<String> (**YourClassName.this**,android.R.layout.simple_list_item_checked,array);
listView.setAdapter(adapter);
NOTE YourClassName.this is your current class name.This code is working for me.
I found the solution. It is to declare a custom layout for the item of the Spinner and use it instead of the default one. And in the custom layout, I set the color of the TextView text to black.
It works this way.
So the code: (promospin_row.xml)
<?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="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:textColor="#000000"
android:id="#+id/tv_promo_txt"
android:paddingLeft="5dp"
android:paddingTop="5dp" />
</LinearLayout>
and the java code:
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(mcontext, R.layout.promospin_row, R.id.tv_promo_txt, mylabels);
// Drop down layout style
dataAdapter.setDropDownViewResource(R.layout.promospin_row);
Spinner myspinner = (Spinner) V.findViewById(R.id.spin_prod_promo);
// attaching data adapter to spinner
myspinner.setAdapter(dataAdapter);
This way, the item text becomes black. And no other anomalies.
Thank you #CommonsWare for your time
Just use null instead of getApplicationContext().
It must work.
I have tried it and its working.
In your Application you can use null instead of mcontext
I was having this problem too. This is not a solution but a potential workaround for others to try in case the above solutions don't work.
Set the background color of the popup that appears when the spinner is selected to something that contrasts the white text color.
<Spinner
...
android:popupBackground="#color/layout_background"
... />
Here I have defined layout_background as a dark greyish color #565656 in a seperate file called colors.xml located in my src/main/java/res/values folder.
Another solution is to indeed use GetApplication() as the context instead of
getActivity().getApplicationContext();
This way I can use the default item layout without having to declare a new custom layout.
So I guess, CommonsWare was right afterall. It is the context that blew things up.
Please #Commonsware, make your comment an answer so I can accept it, because you deserve it.
EDIT
I was writing this while you wrote your answer, that is why it seems I didn't see your answer :)
Just use getBaseContext() instead of mcontext.
Worked for me. I set the adapter of spinner inside onItemClick function of AutoCompleteTextView and had the same problem of text color until I used getBaseContext(). Hope this answer helps.
I know it is possible to change the font size of the Spinner menu that pops up when the button is clicked, and I know it is possible to change the font size of an ordinary button - but I can't seem to find anything on changing the size of the text in the actual spinner button itself. Is this possible? If so, how can it be done?
This is the generic layout from simple_spinner_item.xml, I've added a textSize attribute at the bottom, let's save it as spinner_item.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:textSize="36sp" />
And you use it just like any other layout in an adapter:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.spinner_dropdown_item);
spinner.setAdapter(adapter);
There is no "text in the actual spinner button itself", except what you put there. You determined the layout that appears in the Spinner, in both the closed and open states, by how you set up your SpinnerAdapter (e.g., via layout files supplied to the constructor and to setDropDownViewResource()). If you want to change anything about those layouts, you need to change your layout resources or change your SpinnerAdapter.
I'm trying to have my own font in a listview using Java in Android OS 2.3.x
After reading the following links, i'm stuck:
http://developer.android.com/guide/topics/ui/themes.html
http://stackoverflow.com/questions/4576441/custom-font-in-android-listview
http://androidforums.com/android-lounge/143874-custom-typface-listview-row-layout.html
i can't post something usefull here.
My main problems are:
Why can't i change the Font for a listview, using setTypeface?
Why can't i define a Font for ALL texts in my application without putting it in every activity again?
Is there a documentation, which handles this problems? The Android SDK documentation is lacking a huge amount of details, like at which version otf Fonts are working.
I know that i have to learn many things and i'm ready as much books about this topics as i can. But after two days guessing and trying around, i have to ask for help.
Propably someone can push me in the right direction ;)
To give you a small idea, i tried to clean up some code:
File: res/layout/scores.xml (snippet from it)
<ListView
android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:cacheColorHint="#00000000"
android:transcriptMode="disabled" >
</ListView>
File: src/notneededasinfo/ScoresActivity.java (snippet from it)
Resources myResources = getResources();
Typeface tf = Typeface.createFromAsset(myResources.getAssets(),"fonts/searstower.ttf");
TextView tv = (TextView) findViewById(android.R.id.list);
tv.setTypeface(tf);
Thx for help!
Well... first of all android:textStyle and setTypeface(Typeface) are not a property and function from ListView, it is from TextView.
Try to use this tutorial: http://developer.android.com/resources/tutorials/views/hello-listview.html
Anyway, you have to define a .xml file that defines the style for the itens in the ListView. At the link I posted before you can see on topic 4 the handler onCreate the following line:
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
So, you have to define the layout for the ListView item on the file list_item.xml used above as R.layout.list_item. On this file, list_item.xml, you define new TextViews or EditTexts or whatever and changes the fonts individualy for each View.
Hope it helps you...
TextView tv = (TextView) findViewById(android.R.id.list);
You need to have a text view to set the font. You are missing the list row view.
I have a answer for one of your questions
Why can't i define a Font for ALL texts in my application without
putting it in every activity again?
This is a known problem but easy to solve. Just implement a custom activity class which extends Activity and overwrite the setContentView() method. In this Method you can get all your TextViews for your current view and set the Typface
See here: Add custom font for complete android application