What I need to achieve is a Spinner with these properties:
The Spinner allows the user to select from a fixed set of strings. Those strings are localized, so an English user has different choices than a French user.
When I try to retrieve the value of the selected item of the Spinner, I don't want it to return the localized value of that selected item, but rather the "non-localized" one (in my case, the English value of that string).
How could I achieve this kind of thing? Would a custom Adapter suffice? I've been stucking here for a while.
In your string.xml put
<string-array name="language">
<item>English</item>
<item>Franch</item>
</string-array>
In Your XML where you use spinner
<Spinner
android:id="#+id/language"
android:layout_width="60dp"
android:layout_height="30dp"
android:entries="#array/language"></Spinner>
and finally on java file
Spinner language1= (Spinner) findViewById(R.id.language);
language.getSelectedView().toString())
it work for me..
Ok, a custom Adapter was sufficient to do the trick. I've also defined a layout resource (for elements in the spinner) containing two TextViews: one with visibility="Visible" and one with visibility="Gone". The former holds the localized value, while the latter holds the english value.
Related
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
i am currently working on one Android Application and i have to set layout according to country like if application is for Korea layout color must be blue and for india layout color must be red.
So how can i achieve that?
i know about multiple language
values-kr // for Korea
values //for English
values-fr // for french
but for layout i don't know.
Help Me.
Thanks in Advance
You can create a Style for each Values or in java code programming you can use a simple if statement that detects your location inside "oncreate method" and setbackground layout according to what you want using a drawable.
You have to determine your system language. You can use
Locale.getDefault().getLanguage();
to get the usual language code (e.g. "de", "en").
Then, create a base Activity for your app and override onCreate to set the theme according to your system language. Derive all your other activities from this base Activity. Check this tutorial.
follow type of languages
res/values/strings.xml
Contains English text for all the strings that the application uses,
including text for a string named title.
res/values-fr/strings.xml
Contain French text for all the strings, including title.
res/values-ja/strings.xml
Contain Japanese text for all the strings except title.
How to display Korean Words in android app
You may use this piece of code:
TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String locale = tm.getSimCountryIso();
and choose:
if (locale.equals(pk))
{
view = inflater.inflate(R.layout.hazel_quick_form, container, false);
} //fragment
Take example of TextView.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:textColor="#color/textcolor" />
you can have colors.xml file in different locale folders like values , values-fr etc.
In example textview #color/textcolor can be defined with those saperated colors.xml files with different colors for different locales.
look at this.
Android will do the next magic automatically based on locale.
how i add icon to spinner by xml where find the default value (not in the the list) ??
<Spinner
android:id="#+id/spinner1"
android:layout_gravity="center"
android:layout_weight="0.28"
android:prompt="#string/language_prompt"
android:textSize="12sp" <!-- icon insten of text -->
/>
i try android:Background="#drawable/world_language_icon" but doesn't work.
i dont find src or set drawable .
I dont find here (in google)
i need xml and not javaalso here i find in java
First of all, you should probably try proof reading your question before posting, and make sure you know exactly what you want to know.
how i add icon to spinner by xml
The icon is not added to the spinner itself, as what you use to populate the Spinner is an adapter. In order to add an icon to your Spinner items, you would need to create a custom adapter to set to the Spinner, and use a custom layout for the List Items.
where find the defult value (not in the the list) ??
That, I'm not sure what you are asking for. If your question is how to set a default item/value for the Spinner, it's not possible. The default item set will always be the first item on the List you populate the Spinner with. You can of course automatically select a different Spinner item in code, but even then you'll notice that the first item was selected before your other actions.
I have a spinner with three items in it
<string-array name="menuitems">
<item>one</item>
<item>two</item>
<item>three</item>
</string-array>
Then I set up my spinner like this
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.menuitems, R.layout.spinnerstyle);
adapter.setDropDownViewResource(R.layout.spinnerdropdownstyle);
spinner.setAdapter(adapter);
The spinner displays like this
One
One
Two
Three
My problem is for this spinner I don't want to display the selected item I want this
One
Two
Three
And obviously if two were selected only one and three would appear in the dropdown. I did search for this but couldn't figure out a way to get that affect I tried changing the menuitems array on selection but if I removed the item from there it wouldn't draw it in the selection. I assume there is a fairly easy way to get this affect.
thanks for any help.
Write a custom SpinnerAdapter which returns the items from the menu excluding the selected one.
You can try to set the promt in the xml to "One" like this:
<Spinner android:id="#+id/spinnerTest"
android:layout_marginLeft="50px"
android:layout_width="fill_parent"
android:drawSelectorOnTop="true"
android:layout_marginTop="5dip"
android:prompt="#string/SelectOne"
android:layout_marginRight="30px"
android:layout_height="35px"
/>
Then you should override the onItemClick methode and set the text of your variable in
"On nothing selected".
i have a personaliced spinner (that uses a adapter that uses a personalized xml layout file)
i want to pass him a array of strings, programatically created, instead of passing the tipical hard coded string array of strings.xml... but i can't!! if i try to pass a array of strings, eclipse tell me that he needs a reference of a item from resources (a hard coded string array from strings.xml)
also i tryed to use the answer of this another way: Android: Create spinner programmatically from array
but that way didn't work's for me, because that way must use the default layout android.R.layout.simple_spinner_item and i won't work with that layout, i need to use my personalized layout (R.layout.spinner_layout), and that method didn't let me to use personalized layouts for the spinner
this is my code:
String[] teams=(String[])Primera.getTeams().toArray();
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.teams_array, R.layout.spinner_layout);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
TeamsSpinner.setAdapter(adapter);
TeamsSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
i need to changue R.array.teams_array, to teams, but i can't because the reason i told some lines up
can anyone help me ?
thanks
You do not have to use the android.R.layout.spinner_item etc. like what is being used in the question you reference. You can use your own layout xml for the spinner item.
You do not have to use ArrayAdapter.createFromResource() you can use:
ArrayAdapter adapter = new ArrayAdapter(this, R.id.your_custom_spinner_item, teams);