I use to change the difficulty from my game a Spinner Item and use the strings "Leicht","Mittel","Schwer" then i took as background an drawable file, but when i start the application i can see in the background the strings i used for the spinner.. can i hide the strings? that i see just the drawable background?
schwierigkeitsgradAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, new String[] {" Leicht"," Mittel"," Schwer"});
schwierigkeitsgradAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
schwierigkeitsgrad.setAdapter(schwierigkeitsgradAdapter);
and the xml code:
<Spinner
android:id="#+id/schwierigkeitsgrad"
android:layout_gravity="center_horizontal"
android:layout_marginTop="360dp"
android:background="#drawable/btnschwierigkeit"
android:textAlignment="center"
android:layout_width="220dp"
android:layout_height="90dp"
android:gravity="center_vertical|start"
android:foregroundGravity="center_vertical"
android:spinnerMode="dialog" />
try this to hide spinner text during selection:
Spinner spinner = (Spinner)findViewById(R.id.your_spinner);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// hide selection text
((TextView)view).setText(null);
// if you want you can change background here
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
To show spinner items use:
String[] test=new String[]{"easy", "medium", "heavy"};
ArrayAdapter<String> adapter= new ArrayAdapter<String>(yourActivity.this,android.R.layout.simple_spinner_item, test);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
schwierigkeitsgrad.setAdapter(adapter);
Related
I am using custom searchable spinner in my android application. In one of my activity, I am using two searchable spinners. one for Citys and one for area locations. On selection of 1st Spinner item, I am Changing the adapter of the 2nd spinner to show Area location of that respective City's. but when I select an item of the 2nd spinner, Text Color of the selected item of spinner changes to White. How to stop it.
I have Attached ScreenShots and Code below.
Before Selecting Any Item
After Selecting Items Of Both Spinner
Activity.java
citySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
// Create an ArrayAdapter using the string array and a default spinner layout
if(citySpinner.getItemAtPosition(i).equals("Mumbai"))
{
adapterArea = ArrayAdapter.createFromResource(getApplicationContext(),
R.array.mumbai, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapterArea.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
areaSpinner.setAdapter(adapterArea);
}
else if(citySpinner.getItemAtPosition(i).equals("Delhi"))
{
adapterArea = ArrayAdapter.createFromResource(getApplicationContext(),
R.array.delhi, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapterArea.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
areaSpinner.setAdapter(adapterArea);
}
else if(citySpinner.getItemAtPosition(i).equals("Thane"))
{
adapterArea = ArrayAdapter.createFromResource(getApplicationContext(),
R.array.thane, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapterArea.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
areaSpinner.setAdapter(adapterArea);
}
else if(citySpinner.getItemAtPosition(i).equals("Select City"))
{
buttonAdd.setEnabled(false);
buttonAdd.setVisibility(View.GONE);
adapterArea = ArrayAdapter.createFromResource(getApplicationContext(),
R.array.blank, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapterArea.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
areaSpinner.setAdapter(adapterArea);
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
areaSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(areaSpinner.getItemAtPosition(i).equals("Select Area")|| areaSpinner.getItemAtPosition(i).equals("Select City First!"))
{
buttonAdd.setEnabled(false);
buttonAdd.setVisibility(View.GONE);
}
else
{
buttonAdd.setEnabled(true);
buttonAdd.setVisibility(View.VISIBLE);
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Activity.xml
<com.toptoche.searchablespinnerlibrary.SearchableSpinner
android:id="#+id/spinnerCity"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginTop="24dp"
android:entries="#array/city_name"
app:hintText="Select City"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
<com.toptoche.searchablespinnerlibrary.SearchableSpinner
android:id="#+id/spinnerArea"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginTop="32dp"
app:hintText="Select Area"
app:layout_constraintEnd_toEndOf="#+id/spinnerCity"
app:layout_constraintStart_toStartOf="#+id/spinnerCity"
app:layout_constraintTop_toBottomOf="#+id/spinnerCity" />
Change your create adapter code.
Try this,
ArrayAdapter adapterArea=new ArrayAdapter(getBaseContext(),android.R.layout.simple_spinner_dropdown_item,getResources().getStringArray(R.array.mumbai));
areaSpinner.setAdapter(adapterArea);
In the first spinner the layout set from the library by default but in the second spinner, you are setting it programmatically which is android made and which colors are dependable on a theme of your project so my suggestion for you to change the layout with a customized one.
The code below fills the spinner with data but when i select an item in the spinner no event is triggered. Any ideas ?
spinner itself is inside of toolbar
toolbar_spinner_item
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:singleLine="true"
android:layout_width="150dp"
android:layout_height="48dp"
android:ellipsize="marquee"
android:background="#color/white"
android:textColor="#color/dark_grey"/>
some activity onCreate
ArrayAdapter spinnerAdapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.date_ranges, android.R.layout.simple_spinner_dropdown_item);
spinnerAdapter.setDropDownViewResource(R.layout.toolbar_spinner_item);
Spinner spinner = (Spinner)findViewById(R.id.toolbar_spinner);
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
return;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
return;
}
});
Just turning my comment into an answer ;-)
Instead of adding return; try to add a Log.d call for instance and see if it writes the log statement.
If you're using Android Studio, sometimes it skips certain lines when debugging.
I want to create a language selector for my app. I created a button in the menu layout and I want a spinner to open when one of option menu is clicked . I'm a beginner so I'd love if you could explain your answers.
first you have to create an xml layout where your spinner element will be placed
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!-- Spinner Element -->
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="Select Language"
/>
</LinearLayout>
then i your activity where you would like to show the snipper you should implement OnItemSelectedListener interface for handling the selections of the spinner
public class SnipperActivity extends Activity implements OnItemSelectedListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//here you get the reference to the spinner element declared in your xml layout
Spinner spinner = (Spinner) findViewById(R.id.spinner);
//set the listener to the spinner
spinner.setOnItemSelectedListener(this);
//here you create an arraylist for the items to be displayed in your spinner element
List<String> languages = new ArrayList<String>();
languages.add("English");
languages.add("Greek");
}
//define an adapter for the spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, languages);
//set the style of the snipper, in this case a listview with a radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_it em);
//attach the adapter to your spinner element
spinner.setAdapter(dataAdapter);
}
to handle spinner elements selection you have to ovveride the following method inside SnipperActivity class
#Override
public void onItemSelected(AdapterView parent, View view, int position, long id) {
// On selecting a spinner item
String language = parent.getItemAtPosition(position).toString();
//show a spinner item
Log.e("TAG", "Spinner item selected " + language);
}
Have a look at this article on creating custom dialogs:
http://android-developers.blogspot.co.uk/2012/05/using-dialogfragments.html
IMHO spinners are not very flexible. I'd use a listview in my dialog if I were you, but that choice is yours :)
First of all sorry for my english. I know it isn't very good but i hope you can understand me. I've been looking into this for some hours. I have created a spinner in a layout file setting the spinnerMode parameter to "dialog":
<Spinner
android:id="#+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/llBarra_list"
android:drawSelectorOnTop = "true"
android:prompt="#string/spinner_title"
android:background="#drawable/btn_dropdown_normal"
android:spinnerMode="dialog"/>
When user presses in the spinner, a dialog populates with some options defined in one ArrayList according to:
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity().getBaseContext(),android.R.layout.simple_spinner_item, listaHashes);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
String text = listaHashes.get(arg2);
lblHash.setText(text);
ArrayList<String> tweetsEsteHash= new ArrayList<String>();
tweetsEsteHash.add(text);
receiveTweets(tweetsEsteHash);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
The dialog doesn't fill parent's width and height, which is fine for my intentions. I need that when the user touches outside the dialog, the dialog dismisses itself. Should i create a custom spinner with a custom spinner dialog or are there some options which i can use to achieve this?
I have a spinner with a background image. But when I add array adapter to the spinner, the text is shown on the background image. I want to hide this text. There is also a TextView. My requirement is, when I click the image, the spinner should pop up and when I selects an item, the TextView gets updated with the selected item.
Spinner spin = (Spinner) d.findViewById(R.id.searchCriteria);
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// hide selection text
((TextView)view).setText(null);
// if you want you can change background here
}
public void onNothingSelected(AdapterView<?> arg0) {}
});
Create ghost_text.xml in your layouts folder with this text:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/transparent" />
Then use that file in your adapter like this:
ArrayAdapter<String> your_adapter = new ArrayAdapter<String>(this,
R.layout.ghost_text, your_list);
your_spinner.setAdapter(your_adapter);
You should also be able to use this technique with other resource-based adapters.
Probably you can define a xml-layout file, without any textview in it, and supply it to the adapter for the spinner.
e.g. empty_spinner_item.xml
<ImageView xmlns:android="http..."
android:layout_width=".."
/>
and then use any adapter:
spinner.setAdapter(new SimpleAdapter(this, data, R.layout.empty_spinner_item, ... ));