I want to create Three spinner. In first Spinner, I have to display countries name and according to selection of country name, I have to load the state name of that country in second Spinner and according to selection of state, I have to load cities name of that state in Third Spinner. Can any one post any example and please suggest which technique is better defining spinner data in Java or Xml.
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, locations);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(dataAdapter);
Layout xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dip"
android:text="#string/lblAcc" />
<!-- Spinner Dropdown -->
<Spinner
android:id="#+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
android:layout_marginTop="10dip"
android:entries="#array/acc_type" />
<!-- Select Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dip"
android:text="#string/lblSubAcc" />
<!-- Spinner Dropdown -->
<Spinner
android:id="#+id/spinner2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
/>
Resource xml should be like following
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Spinner Example</string>
<string name="action_settings">Settings</string>
<string name="lblAcc">Select Account Type</string>
<string name="lblSubAcc">Select Account Head</string>
<string-array name="acc_type">
<item>Income</item>
<item>Expense</item>
</string-array>
</resources>
And the Java class to use
public class SpinnerEx4Activity extends Activity implements
OnItemSelectedListener{
Spinner s1,s2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_ex4);
s1 = (Spinner)findViewById(R.id.spinner1);
s2 = (Spinner)findViewById(R.id.spinner2);
s1.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String sp1= String.valueOf(s1.getSelectedItem());
Toast.makeText(this, sp1, Toast.LENGTH_SHORT).show();
if(sp1.contentEquals("Income")) {
List<String> list = new ArrayList<String>();
list.add("Salary");//You should add items from db here (first spinner)
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
s2.setAdapter(dataAdapter);
}
if(sp1.contentEquals("Expense")) {
List<String> list = new ArrayList<String>();
list.add("Conveyance");//you should add items from db here(2nd spinner)
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter2.notifyDataSetChanged();
s2.setAdapter(dataAdapter2);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
With if else ladder you can add more spinner like 3rd spinner depending on 2nd one and so on.
Related
I am working with AutoCompleteTextView. I have added few items through Activity. After AutoCompleteTextView , I've given a "submit" button. I want to sync this button with AutoCompleteTextView . I want to click submit button when any item is selected and after clicking button, it should switch to that item's activity. Currently I don't have code with me. Please suggest me with any example.
Here is the code for AutoCompleteTextView
MainActivity.java
public class MainActivity extends Activity {
public static final String[] COUNTRIES = new String[] { "Belgium", "France",
"Italy", "Germany", "Spain" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// TODO Auto-generated method stub
// In the onCreate method
final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.actv_country);
//EditText e = (EditText) findViewById(R.id.editText1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.simple_dropdown_item_1line, COUNTRIES);
textView.setAdapter(adapter);
// textView.setThreshold(0);
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="#+id/actv_country"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
</AutoCompleteTextView>
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
Just to clarify first, I'm a newbie with Java and Android, and I'm still in a heavy learning process.
Anyway...
I'm working on a simple Parking SMS app, and I'm stuck with Spinner and a TextView.
Basically, I'm trying to do this: when I select a Parking zone from a Spinner (it has five parking zones), I want to populate a Parking zone description to a TextView below Spinner in the Layout.
Spinner has its own StringArray, while Parking zone descriptions are each in its own Strings.
So it looks like this:
activity_glavni.xml
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".Glavni">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scrollView" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></LinearLayout>
</ScrollView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/txGrad"
android:id="#+id/txOdaberiteGrad"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinnergradovi"
android:layout_below="#+id/txOdaberiteGrad"
android:layout_alignParentStart="true"
android:layout_marginTop="23dp" />
<Space
android:layout_width="20px"
android:layout_height="20px"
android:id="#+id/space" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txZona"
android:text="#string/txZona"
android:layout_below="#+id/spinnergradovi"
android:layout_alignParentStart="true" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinnerZona"
android:layout_below="#+id/txZona"
android:layout_alignParentStart="true"
android:layout_marginTop="23dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txPrikazZone"
android:layout_below="#+id/spinnerZona" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/editTextRegistracija"
android:layout_below="#+id/txPrikazZone"
android:layout_alignParentStart="true"
android:hint="Unesite broj registracije" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/btn_posalji"
android:id="#+id/btn_posalji"
android:layout_below="#+id/editTextRegistracija"
android:layout_alignParentStart="true"
android:layout_marginTop="33dp"
android:layout_alignParentEnd="false"
/>
strings.xml
<resources>
<string name="app_name">ParkingZagreb</string>
<string name="action_settings">Settings</string>
<string name="txGrad">Odaberite grad</string>
<string name="txZona">Odaberite zonu</string>
<string name="btn_posalji">Pošalji SMS!</string>
<string-array name="gradovi">
<item>Zagreb</item>
<item>Velika Gorica</item>
<item>Zaprešić</item>
</string-array>
<string-array name="zone">
<item>Prva zona</item>
<item>Druga zona</item>
<item>Treća zona</item>
<item>Četvrta zona (1)</item>
<item>Četvrta zona (2)</item>
</string-array>
<string-array name="opisi_zona">
<item>6 kn/h, maksimalno 2 h</item>
<item>3 kn/h, maksimalno 3 h</item>
<item>1,5 kn/h, bez ograničenja</item>
<item>5 kn/dan, 7-16 h</item>
<item>10 kn/dan, 7-20 h</item>
</string-array>
Glavni.java
public class Glavni extends ActionBarActivity {
public TextView txPrikazZone;
private Button btn_posalji;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_glavni);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
btn_posalji = (Button) findViewById(R.id.btn_posalji);
btn_posalji.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog alertDialog = new AlertDialog.Builder(Glavni.this).create();
alertDialog.setTitle("SMS poslan!");
alertDialog.setMessage("Provjerite SMS aplikaciju. Poruka bi trebala doći za nekoliko trenutaka.");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
});
// the first city selection Spinner
Spinner spinner_gradovi = (Spinner) findViewById(R.id.spinnergradovi);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.gradovi, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spinner_gradovi.setAdapter(adapter);
// the second Spinner with parking zones
final Spinner spinner_zona = (Spinner) findViewById(R.id.spinnerZona);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
this, R.array.zone, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spinner_zona.setAdapter(adapter2);
// this is where I'm trying to populate the TextView with the String-Array by selecting an item from the second Spinner
final String[] opisi_zona = getResources().getStringArray(R.array.opisi_zona);
spinner_zona.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
txPrikazZone.setText(opisi_zona[position]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_glavni, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Thanks for any help...
The reason for the null pointer is that you have not initialized txPrikazZone and you are trying to call setText.
You need to initialize it in your onCreate method like this:
txPrikazZone = (TextView) findViewById(R.id.txPrikazZone);
I hope this helps.
Your adapter it´s the same for both Spinners
EDIT
First try wrapping your ZonaX strings into a string-array
<string-array name="zonas">
...
</string-array>
And now in the code change the set of the ItemSelectedListener
final String[] zonas = getResources().getStringArray(R.array.zonas);
spinner_zona.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
txPrikazZone.setText(zonas[position]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
I was trying to implement an android spinner, a button, if I click should open a dropdown menu showing three things: "bluetooth, speak, headphones"
but all i see now is the button, (no text), and when I click on it, nothing happens. what am i missing?
here is my code:
in the xml i have:
<RelativeLayout
android:id="#+id/audio_routing"
android:layout_width="40dp"
android:layout_height="40dp"
android:padding="10dp" >
<Spinner
android:id="#+id/audio_routing_spinner"
android:layout_width="20dp"
android:layout_height="40dp"
android:contentDescription="#string/audio_routing_desc"
android:scaleType="fitXY"
android:src="#drawable/bluetooth" />
</RelativeLayout>
in the strings file i have:
<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<string name="audio_routing_desc">Audio Routing Button</string>
<!-- Audio Routing Drop Down List -->
<string-array name="audio_routing_array">
<item>Bluetooth</item>
<item>Headphones</item>
<item>Speaker</item>
</string-array>
in my java file i have:
void audio_routing() {
kcLogger.info("audio_routing", "audio_routing function is called");
Spinner spinner = (Spinner) mKCSWindowView.findViewById(R.id.audio_routing_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(service,R.array.audio_routing_array,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
kcLogger.info("audio_routing", "onItemSelected");
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
kcLogger.info("audio_routing", "onNothingSelected");
}
});
}
Change android:contentDescription to android:entries
<Spinner
android:id="#+id/audio_routing_spinner"
android:layout_width="20dp"
android:layout_height="40dp"
android:entries="#array/audio_routing_desc"
android:scaleType="fitXY"
android:src="#drawable/bluetooth" />
android:contentDescription is for accessibility features.
android:entries is used to set array data to UI like spinner, listview, gridview,etc.
I am having a problem with the spinner item colour.
I am fetching the Spinner items from database.
I changed the spinner item colour.
But when I select a Spinner item, the colour will change to white again.
Here is my code:
public class PersonalInformation extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.info)
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
ArrayAdapter<CharSequence> adapter =ArrayAdapter.createFromResource(this,R.array.spinner_array,R.layout.spinner_text);
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
// TODO Auto-generated method stub
}
});
And Here is my Spinner xml file code:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textColor="#000000"
android:textColorHint="#000000"
android:textSize="20dp">
</TextView>
Make custom xml file for your spinner item.
spinner_item.xml:
give your customized color and size to text in this file.
<?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="wrap_content"
android:textSize="20dip"
android:gravity="left"
android:textColor="#FF0000"
android:padding="5dip"
/>
Now use this file to show your spinner items like:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item,list);
You don't need to set drop down resource.it will take spinner_item.xml only to show your items in spinner.
How to change spinner text size and text color?
This question already has answers here:
How to make an Android Spinner with initial text "Select One"?
(36 answers)
Closed 9 years ago.
I'm trying my Spinner to display "Select City" before the Spinner has itself been clicked by the user.
How can I do this?
My current XML code is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.olacabs.customer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/page_background"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:gravity="center"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:text="#string/rate_card"
android:textColor="#color/white"
android:textSize="20dp"
custom:customFont="litera_bold.ttf" />
<Spinner
android:id="#+id/select_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:prompt="#string/selectCity" />
</LinearLayout>
Also, what does android:spinnerMode exactly do. I tried changing its value to dropdown but nothing happened and the application still showed a popup dialogue.
My activity that implements this XML file is:
public class RateCardActivity extends OlaActivity {
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.rate_card);
Spinner spinner = (Spinner) findViewById(R.id.select_city);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.select_city, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
}
you can do like this .. Create a custom adapter to insert..
Try the following code
In your resources
<string-array name="listarray">
<item>Select One</item>
<item>Item One</item>
<item>Item Two</item>
<item>Item Three</item>
</string-array>
In your onItemSelected Listener:
TextView selection;
spinnername.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onNothingSelected(AdapterView<?> parent) {
selection.setText("");
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos == 0) {
}else {
// Your code to process the selection
}
}
});
For more info go through this Link & Link
CHange :
extends OlaActivity to extends Activity