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);
Related
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.
Often i like default android style defined for some widget.
Often i like to just slightly modify this default style, not create my own style from a scratch.
Now, i use android.R.layout.simple_spinner_dropdown_item as dropDownViewResource on my spinner adapter - like this:
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
I can open this layout xml file and copy its content to modify it. But the most important part of that xml file is attribute style, it looks like this: style="?android:attr/spinnerDropDownItemStyle"
Now, my question is: how do i find/view/copy this style spinnerDropDownItemStyle, so i can for example just change background color.
Thanx for your help
here you can find style spinnerDropDownItemStyle:
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/themes.xml
But, if you looking for change background of Spinner, you'll need to search for 9 patch images:
https://developer.android.com/studio/write/draw9patch.html
And here a good example:
http://www.gersic.com/blog.php?id=57
Ty
My suggestion to you is . you can make a seprate custom layout and put it on your spinner setDropDownResource()
adapter.setDropDownViewResource(android.R.layout.CUSTOM_LAYOUT);
Now you can easily make a style for your layout
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
I am trying to, somewhat clone the design of an activity from a set of slides on Android UI design. However I am having a problem with a very simple task.
I have created the layout as shown in the image, and the header is a TextView in a RelativeLayout. Now I wish to change the background colour of the RelativeLayout, however I cannot seem to figure out how.
I know I can set the android:background property in the RelativeLayout tag in the XML file, but what do I set it to? I want to define a new colour that I can use in multiple places. Is it a drawable or a string?
Additionally I would expect there to be a very simple way to this from within the Eclipse Android UI designer that I must be missing?
I am a bit frustrated currently, as this should be an activity that is performed with a few clicks at maximum. So any help is very appreciated. :)
You can use simple color resources, specified usually inside res/values/colors.xml.
<color name="red">#ffff0000</color>
and use this via android:background="#color/red". This color can be used anywhere else too, e.g. as a text color. Reference it in XML the same way, or get it in code via getResources().getColor(R.color.red).
You can also use any drawable resource as a background, use android:background="#drawable/mydrawable" for this (that means 9patch drawables, normal bitmaps, shape drawables, ..).
The above answers are nice.You can also go like this programmatically if you want
First, your layout should have an ID. Add it by writing following +id line in res/layout/*.xml
<RelativeLayout ...
...
android:id="#+id/your_layout_id"
...
</RelativeLayout>
Then, in your Java code, make following changes.
RelativeLayout rl = (RelativeLayout)findViewById(R.id.your_layout_id);
rl.setBackgroundColor(Color.RED);
apart from this, if you have the color defined in colors.xml, then also you can do programmatically :
rl.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.red));
You can use android:background="#DC143C", or any other RGB values for your color. I have no problem using it this way, as stated here
The
res/values/colors.xml.
<color name="red">#ffff0000</color>
android:background="#color/red"
example didn't work for me, but the
android:background="#(hexidecimal here without these parenthesis)"
worked for me in the relative layout element as an attribute.
If you want to change a color quickly (and you don't have Hex numbers memorized) android has a few preset colors you can access like this:
android:background="#android:color/black"
There are 15 colors you can choose from which is nice for testing things out quickly, and you don't need to set up additional files.
Setting up a values/colors.xml file and using straight Hex like explained above will still work.
4 possible ways, use one you need.
1. Kotlin
val ll = findViewById<LinearLayout>(R.id.your_layout_id)
ll.setBackgroundColor(ContextCompat.getColor(this, R.color.white))
2. Data Binding
<LinearLayout
android:background="#{#color/white}"
OR more useful statement-
<LinearLayout
android:background="#{model.colorResId}"
3. XML
<LinearLayout
android:background="#FFFFFF"
<LinearLayout
android:background="#color/white"
4. Java
LinearLayout ll = (LinearLayout) findViewById(R.id.your_layout_id);
ll.setBackgroundColor(ContextCompat.getColor(this, R.color.white));
Android studio 2.1.2 (or possibly earlier) will let you pick from a color wheel:
I got this by adding the following to my layout:
android:background="#FFFFFF"
Then I clicked on the FFFFFF color and clicked on the lightbulb that appeared.
Kotlin
linearLayout.setBackgroundColor(Color.rgb(0xf4,0x43,0x36))
or
<color name="newColor">#f44336</color>
-
linearLayout.setBackgroundColor(ContextCompat.getColor(vista.context, R.color.newColor))
The answers above all are static. I thought I would provide a dynamic answer. The two files that will need to be in sync are the relative foo.xml with the layout and activity_bar.java which corresponds to the Java class corresponding to this R.layout.foo.
In foo.xml set an id for the entire layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/foo" .../>
And in activity_bar.java set the color in the onCreate():
public class activity_bar extends AppCompatActivty {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.foo);
//Set an id to the layout
RelativeLayout currentLayout =
(RelativeLayout) findViewById(R.id.foo);
currentLayout.setBackgroundColor(Color.RED);
...
}
...
}
I hope this helps.
I have added a imagebutton dynamically to my Tablerow, now i want to define the style for the image button to a style that i have previously defined. I have tried the setTheme method that doesnot seem to work
Thanks for any help
k after a lot of searching this is what i found and it works perfectly.
1) if you want to create a button/imagebutton/anyView at runtime with a predefined style the you can do so just follow the steps below
2a) Create a new xml file in res/layout and put that one element you want to create for example I wanted to create a minusButton. This is minusb.xml in res/layout
<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
style="#style/MinusButton"/>
2b) Then add the following lines to your java activity code where you would like to create it
ImageButton bt = (ImageButton) getLayoutInflater().inflate(R.layout.minusb, null);
bt.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,0f));
tr.addView(bt);