Title: How to create Spinner and perform action in Android
link : How to create Spinner and perform action in Android
How to create Spinner and perform action in Android
Open main XML layout and drop Spinner widget or paste below code:
</LinearLayout>
Now open Your Java File and paste below code:
Now Run your code...
<LinearLayout xmlns:android="http://schemas. android.com/apk/res/android"
android:layout_width="fill_ parent"
android:layout_height="fill_ parent"
android:orientation="vertical">
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_ parent"
android:layout_height="wrap_ content" />
Now open Your Java File and paste below code:
package sel.listG; //package name
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.AdapterView;
importandroid.widget.AdapterView. OnItemSelectedListener;
importandroid.widget.ArrayAdapter;
importandroid.widget.Spinner;
importandroid.widget.Toast;
importandroid.app.Activity;
public class MainActivity extends Activity implementsOnItemSelectedListener {
String values[]={"Select One","January", "July", "June", "Ravi", "Ravinder", "Abhishek", "Abhinav", "Harkishore", "Hardev"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState);
setContentView(R.layout.activi ty_main);
Spinner lv=(Spinner)findViewById(R.id. spinner1);
ArrayAdapter<?> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_ spinner_item, values);
//you can left the below one line because it is merging two layout
adapter. setDropDownViewResource( android.R.layout.simple_ spinner_dropdown_item);
lv.setAdapter(adapter);
lv.setOnItemSelectedListener(t his);
}
@Override
public voidonItemSelected(AdapterView<?> a, View v, int pos,long id)
{
Toast.makeText( getApplicationContext(),a. getItemAtPosition(pos)+" Selected", Toast.LENGTH_SHORT).show();
}
@Override
public voidonNothingSelected(AdapterView< ?> arg0) {
// TODOAuto-generated method stub
}
}
Now Run your code...
That's How to create Spinner and perform action in Android
That's an article How to create Spinner and perform action in Android
This time, hopefully can benefit for you all. Well, see you in other article postings.
You are now reading the article How to create Spinner and perform action in Android With link address https://u-see1.blogspot.com/2017/06/how-to-create-spinner-and-perform.html
Belum ada tanggapan untuk "How to create Spinner and perform action in Android"
Post a Comment