Android Spinner Guidance - android

I'm trying to create an android drop down box but finding it difficult to find a suitable example that isn't overly complicated.
What I want is a simple drop down box with 4 or 5 options in it. Those options should be placed in an xml file that the spinner reads in.
All the examples have dealt with multiple spinners on a page of seem to have bloated code that doesn't make sense.

In res > Values > Strings
<string-array name="mySpinnerOptions">
<item>Cow</item>
<item>Dog</item>
<item>Cat</item>
<item>Horse</item>
</string-array>
In your content_myView file
<Spinner
android:id="#+id/mySpinner"
android:entries="#array/mySpinnerOptions"
android:layout_width="your_width"
android:layout_height="your_height"/>

Here's a simple example that you can get the gist of from steps 1 and 2.
https://www.mkyong.com/android/android-spinner-drop-down-list-example/
Summary:
Create string array in strings.xml
Add android:entries="#array/your_array" to your Spinner component in your layout xml

Related

Text in android app (beginner...)

My problem is very simple: I've started an app for playing Darts. The app will have several activities ('pages').
One page will be about the rules of the game. I'll be using a scroll layout because it's quite some text. But how to get the text there?!
I assume working with strings is not the best way? Do I use the XML file to get the text on screen then or does it work via Java (Assetmanager)?
Maybe there are sample apps in which large chunks of text are used?
I know this really might seem like a trivial question but I haven't a clue where to begin.
Thanks in advance!
You should put your string in your strings.xml in your res\values folder.
You can define strings by ID which allows easier internationalization (i18n), so that you can easily adjust the strings used in your app to locale (which is done automatically using resource identifiers, and it falls back to strings.xml if it can't find a strings-hu.xml in case you have Hungarian locale set as system language).
You can also define string-array and the like in XMLs. Then all you need is create a layout XML with a ScrollView in it that has a TextView in it and then you set android:text="#string/rules" for that TextView and you're done.
It is so simple my friend.
You can simply use TextView and in "android:text" you refer to the string that you delared in strings.xml file (by its name)
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="#string/text_name"
/>
If your text is dynamic, you can modify it in Java code!
Make a String Resource like this.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="string1"> your text </string>
<string name="string2"> your text </string>
</resources>
and access like this if you are in Activity.
getResources().getString(R.string.string1);
If you are a begginer you should read some tutorials after post a question...
I give you a three nice tutorials below :
Want to Learn How to Program for Android? Start Here
Android Programming Tutorial
Android Development with Android Studio or Eclipse ADT
About your question, if you don't know how to use the string.xml resource just read the string-resource guide
Hope it helps.

Simple hard coded ListView in Android

Is it possible to do something like this in Android? (basically the equivalent of select and option in HTML):
<ListView android:layout_height="match_parent"
android:layout_width="match_parent">
<ListViewItem>TEST</ListViewItem>
<ListViewItem>TEST1</ListViewItem>
</ListView>
I have tried this, it obviously doesn't work. All I can find online is information about using adapters, but as I have some hard coded values I don't need to use an adapter - I'm happy to put it directly in the XML file.
Is this possible?
1 ] Create a String array in strings.xml
2 ] Use the following attribute inside Listview tag
android:entries="#array/your_array_name"
Done !
No It is not possible in android but used android:entries properties to load list item using String resource array.

How to alphabetically sort code in XML file inside Intellij IDEA 13 for android?

I used eclipse for training in android but now using Intellij IDEA 13. I am used to the graphical string editor of eclipse which can alphabetically sort the string.xml file for me.
I want to do the same in intelliJ but couldn't find any solution.
This is how code look like
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ActionBar App</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
</resources>
and I want to sort it alphabetically like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="action_settings">Settings</string>
<string name="app_name">ActionBar App</string>
<string name="hello_world">Hello world!</string>
</resources>
How to do this ? Please help!
The arrangement feature allows you to setup rules to arrange the order this appear in a file. Arrangement of attributes, particularly for Android files, was add via IDEA-72907. You can get a predefined arrangement style for android by going to Settings > [Project Settings] > Code Style > XML {Arrangement Tab} Click on the "Set from..." link near the top right and select Preferred Style > Android. That will populate the XML style (and arrangement) settings with a predefined style that you can tweak.
That said, I am unsure if the arrangement feature has the ability to sort by the attribute's value. I know it can sort the attributes in a particular order. If I get a chance, I'll see if I can find the syntax to sort by the attribute's value. In the meantime, you can take a look.
Also, you can try either the Lines Sorter or SortSelection third party plug-ins. A bit more basic, but one might work for you.
I found this link Rearrange Attributes in Android XML Files with IntelliJ IDEA 13
I chose the second option.
As mentioned go to the settings/Editor/Code Style/XML/Arrangement
Reordering the arrangement and changing to be reorder by name:
This won't sort the strings.xml by name, so
copy and paste into an excel worksheet and sort by A-Z
I am posting this two years after the fact, but there's another way to sort lines in IntelliJ IDEA that I'd like to mention here -- i.e. by using the IDE's Vim features.
If you want to sort a range of lines alphabetically -- say, lines 3 through 6 -- just do the following:
Open the Tools menu and make sure that 'Vim Emulator' is checked.
Type the following command in your editor:
:3,6sort
That's it -- your lines will now be sorted. All you need to do now is disable Vim if you don't use it. You can do this by Opening the Tools menu and unchecking 'Vim Emulator'.
Note that this isn't an XML sort -- it's an alphabetic line sort so spaces and so on could get in the way of a successful sort. But for simple cases like the one you mention above, this should work.
Darryl
You can't sort XML elements by its attribute value, e.g. 'name' via File → Settings → Editor → CodeStyle → XML → Arrangement. You only can sort the attribute order inside a tag or can sort the elements by its tag name.
For Android there is a plugin called AndroidXMLSorter (can only sort by attribute 'name', only in Android Projects)
If the entries are written in a single line you can select the text and use: Edit → Sort Lines (no need for a plugin)

Find which resources are filtered

From the image above in a working android project. I would like a way to get the filters so I can compare them to something else I'm working on. For example I'd like to have an array that contains "mcc311,mnc490","mcc311,870".....,"mcc312,mnc530" etc. So I'm looking for a way to get the filters that the project actually uses.
Figured out a way to do this and it was pretty simple. In values-mcc310-mnc120 for example, i would put in a xml file defining a variable signifying what mcc and mnc identification im using.
For example, in values-mcc311-mnc870 i would have a xml file called networkcode.xml with contents of:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="network_id">MCC311/MNC870</string>
</resources>
this way i can read in that value of network_id at run time and know for certain what filter android is using.

Sending a generated value to XML for formatting

I would like to begin by saying I don't have the best java knowledge because I'm still learning it in school, but I decided to go off on my own and attempt to create an android application.
Right now, I have my program generating two different random numbers. I'm trying to get these generated ints to be shown in the application. I want it to be done through XML formatting tho because of the control I have over what I want it to look like.
My issue is that I can't seem to figure out how to pass the values over to the XML file. I'm not sure if I should be using "android:text" and somehow integrating the name of the ints in that(Which i've attempted.) or what.
I've tried to google for an explanation of what can be done, but I think my lack of ability to really put my issue into words is holding me back.
You won't be using the XML file to set the Text. You use the xml file to set the look and feel, like this (note the android:id= line):
<TextView android:id="#+id/randomInt1"
android:layout_height="wrap_content" android:layout_width="wrap_content"
<TextView android:id="#+id/randomInt1" />
android:layout_height="wrap_content" android:layout_width="wrap_content"/>
then in your java code in onCreate for the activity that will be showing the ints you will do something like this:
int random_int1=0;
int random_int2=100;
((TextView)this.findViewById(R.id.randomInt1)).setText(random_int1);
((TextView)this.findViewById(R.id.randomInt2)).setText(random_int2);
The way it works is you need to make a "link" between the java and the xml.
This can be done in java with the following line(quick example I pulled out of my code):
TextView nameText = (TextView) findViewById(R.id.nameTextView);
then this can be set using:
nameView.setText(PutStringHere);
This is a fundamental part of android development. It's the link between the java and the xml. You can edit other things though the java TextView.

Categories

Resources