Spinners in android using data adapter java and string use
button event toast make,
for the create a spinner have two method
1)data adapter using java
2)string file add spinner
create single new activity
for both of spinner activity we are use
1)activity_addspinarr .xml
2)addspinarr .java
3)strings.xml ( path > app>res>values> string.xml )
1)activity_addspinarr .xml
preview of activity_addspinarr .xml
code of activity_addspinarr .xml
<?xml version="1.0" encoding="utf-8"?>
<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.store.n01.addspinarr">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/spinner"
android:layout_alignLeft="@+id/spinner"
android:layout_alignStart="@+id/spinner"
android:layout_marginBottom="57dp"
android:layout_marginLeft="18dp"
android:layout_marginStart="18dp"
android:text="spinner"
android:textSize="35dp" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:layout_marginTop="111dp"
android:entries="@array/gender_array"
android:prompt="@string/gender_prompt" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/spinner1"
android:layout_alignStart="@+id/spinner1"
android:layout_below="@+id/spinner1"
android:entries="@array/education_array"
android:layout_marginTop="32dp" />
<Spinner
android:id="@+id/spinner3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_alignStart="@+id/textView2"
android:layout_below="@+id/spinner2"
android:layout_marginTop="67dp"
android:entries="@array/education_array"
android:prompt="@string/education_prompt" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="84dp"
android:background="@drawable/btn_all"
android:text="submit"
android:textSize="35dp" />
</android.widget.RelativeLayout>
button event toast make,
for the create a spinner have two method
1)data adapter using java
2)string file add spinner
create single new activity
for both of spinner activity we are use
1)activity_addspinarr .xml
2)addspinarr .java
3)strings.xml ( path > app>res>values> string.xml )
1)activity_addspinarr .xml
preview of activity_addspinarr .xml
code of activity_addspinarr .xml
<?xml version="1.0" encoding="utf-8"?>
<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.store.n01.addspinarr">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/spinner"
android:layout_alignLeft="@+id/spinner"
android:layout_alignStart="@+id/spinner"
android:layout_marginBottom="57dp"
android:layout_marginLeft="18dp"
android:layout_marginStart="18dp"
android:text="spinner"
android:textSize="35dp" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:layout_marginTop="111dp"
android:entries="@array/gender_array"
android:prompt="@string/gender_prompt" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/spinner1"
android:layout_alignStart="@+id/spinner1"
android:layout_below="@+id/spinner1"
android:entries="@array/education_array"
android:layout_marginTop="32dp" />
<Spinner
android:id="@+id/spinner3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_alignStart="@+id/textView2"
android:layout_below="@+id/spinner2"
android:layout_marginTop="67dp"
android:entries="@array/education_array"
android:prompt="@string/education_prompt" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="84dp"
android:background="@drawable/btn_all"
android:text="submit"
android:textSize="35dp" />
</android.widget.RelativeLayout>
2)addspinarr .java
data adapter for spinner 3
package com.example.store.n01;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class addspinarr extends AppCompatActivity {
private Spinner spinner1 ,spinner2;
private Button button4;
private AdapterView.OnItemSelectedListener addListenerOnSpinnerItemSelection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addspinarr);
addItemsOnSpinner2();
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}
public void addItemsOnSpinner2(){
spinner2 = (Spinner)findViewById(R.id.spinner2);
List<String> list = new ArrayList<String>();
list.add ("jan");
list.add("feb");
list.add("march");
list.add("april");
list.add("may");
list.add("june");
list.add("dec");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
}
public void addListenerOnSpinnerItemSelection(){
spinner1 =(Spinner)findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(addListenerOnSpinnerItemSelection);
}
//getselected item on click on submiit btn
public void addListenerOnButton(){
spinner1=(Spinner) findViewById(R.id.spinner1);
spinner2=(Spinner)findViewById(R.id.spinner2);
button4=(Button)findViewById(R.id.button4);
button4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(addspinarr.this,
"OnClickListener : "+
"\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem())+
"\nSpinner 2 : "+ String.valueOf(spinner2.getSelectedItem()),
Toast.LENGTH_SHORT).show();
}
});
}
}
3)strings.xml
add this code to xml file in given path
fore spinner1 and spinner2
<string name="gender_prompt">choose a gender</string>
<string-array name="gender_array">
<item>choose a gender</item>
<item> male </item>
<item> female </item>
</string-array>
<string name="education_prompt">choose education</string>
<string-array name="education_array">
<item>choose education </item>
<item>10th or below</item>
<item>10+2 or below</item>
<item>graduate</item>
<item>post graduate</item>
</string-array>
when button click make toast which has selected in
spinner 1 ,2 & 3
No comments:
Post a Comment