disable spinner item in XML - android

I Have a simple spinner and would disable an element.
Could I disable elements from the xml files?
Or i need to code something in JAVA?
<Spinner
android:id="#+id/stato"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:entries="#array/feedbacktypelist"
android:layout_below="#+id/textview">
</Spinner>
arrays.xml
<resources>
<string-array name="feedbacktypelist">
<item>#string/stato1</item>
<item>#string/stato2</item>
<item>#string/stato3</item>
<item>#string/stato4</item>
<item>#string/stato5</item>
</string-array>
</resources>
strings.xml
<resources>
<string name="stato1">ITALIA</string>
<string name="stato2">SPAGNA</string>
<string name="stato3">GERMANIA</string>
<string name="stato4">REPUBBLICA CECA</string>
<string name="stato5">INGHILTERRA</string>
</resources>

You can do two things, comment out the line in the resource array via highlighting the row and doing ctrl+/
or
You can remove the android:entries="#array/feedbacktypelist" line and add the list yourself but skipping the entry you don't want (most common me thinks) see how here How to create Spinner-list using CustomAdapter in android

Related

Setting XML attribute with a variable [duplicate]

This question already has answers here:
Android xml reference within xml doesn't work
(2 answers)
Closed 5 years ago.
I'm trying to set an XML attribute with a variable in the resources in Android studio with no luck.
Something like this works:
<resources>
<string name="Key">#string/Key</string>
</resources>
But something like this doesn't:
<resources>
<string name="Key" custom-attribute="#string/Key"/>
</resources>
How do you use variables in XML attributes?
This is NOT the same question as Android xml reference within xml doesn't work.
The first example works correctly, I am able to use variables setting a field value. The second example does not work, I cannot set an attribute.
<resources>
<string name="Key">Some Value</string>
</resources>
Should give you same result as:
<resources>
<string name="Key" value="Some Value"/>
</resources>
Because of that I would recommend using the first example as that works properly.
Try this:
<string name="Key1">Some Text</string>
<string name="Key2">#string/Key1</string>
update:
<string name="Key1">Some Text</string>
<string name="Key2">
<some-attribute>#string/Key1</some-attribute>
</string>
Here we can define resources in res/ folder.
You can create your own file and use below examples.
The value thing won't work.
<resources>
<string name="button">Try Again</string>
<dimen name="margin">56dp</dimen>
<bool name="isCorrect">false</bool>
<color name="background">#fff</color>
<drawable name="icon">#drawable/ic_about_us</drawable>
<integer name="count">56</integer>
<string-array name="days">
<item>Monday</item>
<item>Sunday</item>
</string-array>
</resources>
And Use by R.id.nameOfResource
Here is much more to explore about them.
Hope this helps.
Update: We can also define resources like this in build.gradle script.
android {
buildTypes.each {
it.resValue 'string', 'serverLink', "https://mylink.com"
}
}

Use string array to populate Spinner

I'm in the rookie leagues when it comes to Android apps and am looking to populate a Spinner with an Array or strings (it's a converter app) below is an extract from my XML file and I'm looking to populate the the Spinner:
......
<string name="TemperatureString">Temperature</string>
<string name="WeightString">Weight</string>
<string name="VolumeString">Volume</string>
<string name="SpeedString">Speed</string>
<string name="LengthString">Length</string>
<string name="AreaString">Area</string>
<string name="EnergyString">Energy</string>
<string name="PresureString">Presure</string>
<string name="MemoryString">Memory</string>
<string-array name="Convert_Type">
<item>#string/TemperatureString</item>
<item>#string/WeightString</item>
<item>#string/VolumeString</item>
<item>#string/SpeedString</item>
<item>#string/LengthString</item>
<item>#string/AreaString</item>
<item>#string/EnergyString</item>
<item>#string/PresureString</item>
<item>#string/MemoryString</item>
</string-array>
From this, I'm trying to populate my spinner (#+id/MainSpinner) - I'm not sure what I'm doing here but for the activity_main.xml I have the following:
<Spinner
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/MainSpinner"
tools:listitem="#layout/support_simple_spinner_dropdown_item"/>
I know there's a way of doing this via Java but I'm even worse at Java!
For this reason, I'd like to keep this within xml if possible.
Also, If someone can want's to point me towards links to bring on my Java and xml skills that would be great - I've started with Udacity and have found them good but there's a lot to take in for a non-IT graduate (I work in finance but find this kinda thing really interesting!)
Thanks in advance!
Simplest way to bind ListView and Spinner control with String Array is
android:entries = "#array/nameofarray"
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/MainSpinner"
tools:listitem="#layout/support_simple_spinner_dropdown_item"
android:entries="#array/Convert_Type"/>
If you want to change the theme of each item of Spinner then put below style into res/values/styles.xml
<style name="ItemTextAppearance">
<item name="android:textColor">#f00</item>
<item name="android:textStyle">bold</item>
<item name="android:typeface">monospace</item>
</style>
and the set
android:theme="#style/ItemTextAppearance"
of spinner.
Use entries attribute in the spinner tag
<Spinner
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/MainSpinner"
android:entries="#array/Convert_Type"
tools:listitem="#layout/support_simple_spinner_dropdown_item"/>

Why is it telling me to use #string/ where ever i am using android:text="' ? And that it's hardcoded

The error says that for hardcoded you must use #string/.. what is wrong with just using "android:text= "price" ??
<LinearLayout
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"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:text="PRICE" />
A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings:
String
XML resource that provides a single string.
String Array
XML resource that provides an array of strings.
Quantity Strings (Plurals)
XML resource that carries different strings for pluralization.
All strings are capable of applying some styling markup and formatting arguments.
for more information, read it here
Also it's better to use a string resource, that way if you have to change the text you change only one variable
So in your res folder there will be another folder called values, and then access the strings.xml file, inside that file you'll put a string resource to use in your app, like so:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">yourAppName</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="price">PRICE</string>
</resources>
and then in your xml file, you will change android:text="PRICE" to android:text = "#string/price"

No resource found when using '#string/value' in layout.xml

just started programming android and I am trying to make a simple xml file but it seems it got some problem with the android:text=#string/..
I have got this error:
error: Error: No resource found that matches the given name (at 'text'
with value '#string/ false_button').
For each of the strings..
The code is:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width = "match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding= "24dp"
android:text="#string/question_text"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/true_button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/false_button"/>
</LinearLayout>
</LinearLayout>
You need to have the resources that you want to reference in your layout in strings.xml under res/values/
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="true_button">True Button</string>
<string name="false_button">False Button</string>
</resources>
#string/true_button refers to a resource in strings.xml
You can also have hardcoded string but not recommended
android:text="True Button"/>
add following to your string.xml file under values folder
<string name="true_button">true</string>
<string name="false_button">false</string>
Make sure that you are created string value with the name of false_button in the string.xml
<string name="false_button">false</string>
or just give name within the quotes "" in the android:text attribute
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="true_button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="false_button"/>
Click on res folder then click on values and then open strings.xml and add following content.
<string name="true_button">whatever text you want to write to your button</string>
<string name="false_button">whatever text you want to write to your button</string>
and save your file. Then clean your project by clicking Project -> clean and then run it again.
Hope this will help you.
make sure that in res/values/
you have all the resources present there as follows
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="true_button">True</string>
<string name="false_button">False</string>
</resources>
otherwisely you can hardcode values at yours layout where you specified button as
android:text="True "/> or android:text="False"/>
In your android project, look for the res directory. Open it and inside it look for values directory. In values, open strings.xml and check if it has a flase_button value stored in it . If not you can add a new value using name as false_button and value as what you want to display on the button.
When you open the strings.xml in IDE like eclipse, you can switch views of the .xml file by selecting proper tab given at the bottom of your eclipse screen. Select strings.xml view and
modify the current strings.xml to
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">app</string>
<string name="false_button">FALSE</string>
</resources>
Hope this was helpful.
You just need to add this in your Strings.xml class
just add <string name="false_button">False Button</string> line in your strings.xml
so that it will look like :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="false_button">False Button</string>
</resources>
All of these responses that refer to adding the string to the string.xml file make sense and work, but fail to address the original problem: this book ("Big Nerd Ranch: Android Programming") goes through these steps one at a time, mentions referencing the string in the layout XML file, but fails to mention adding to the strings.xml file. Fail.

Organizing Strings.xml

I'm making an android app and since I've just started I want to try get the most organised code/resources. In my strings.xml file so far I have this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">GameController</string>
<string name="stop">Stop</string>
<string name="start">Start</string>
<string name="preferences">Preferences</string>
<string name="back">Back</string>
</resources>
All of the strings except app_name are used in an options menu. But since I will be adding much more strings I was thinking that it might be better to do something like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">GameController</string>
<string name="menu_stop">Stop</string>
<string name="menu_start">Start</string>
<string name="menu_preferences">Preferences</string>
<string name="menu_back">Back</string>
</resources>
Is it the best way or should I use another system?
It depends on where the strings will be used. If "stop" will never be used anywhere but in a menu, calling it "menu_stop" is a good idea. If it'll be used all over the place then it should just be called "stop".
Also, XML comments are very useful for organizing resources.
<resources>
<string name="app_name">GameController</string>
<!-- Menu Strings -->
<string name="menu_stop">Stop</string>
<string name="menu_start">Start</string>
<string name="menu_preferences">Preferences</string>
<string name="menu_back">Back</string>
</resources>
Finally, if you find you have tons and tons of string resources you may want to go so far as to separate them into different xml files: menu_strings.xml, dialog_strings.xml, etc.
menu_strings.xml
<resources>
<!-- Menu Strings -->
<string name="menu_stop">Stop</string>
<string name="menu_start">Start</string>
<string name="menu_preferences">Preferences</string>
<string name="menu_back">Back</string>
</resources>
dialog_strings.xml
<resources>
<string name="dialog_cancel_yes">Yes, cancel.</string>
<string name="dialog_cancel_no">No, do not cancel.</string>
</resources>
This is kind of a subjective question, really. You should use whatever you find easier to handle. I certainly do the second type of naming when I'm using layouts and drawables (e.g. button_x, ninepatch_x, icon_x, etc.), just because it keeps them next to each other, and is easier to narrow down quickly with Content Assist. In XML, you can use comments to group them together, and add white space, just anything that makes it easier for you to find what you need, and quickly.

Categories

Resources