Step 01
Make an xml file in res->values name it as arrays
<string-array name="douse">
<item>Half</item>
<item>One</item>
<item>One and Half</item>
<item>Two</item>
<item>Two and half</item>
</string-array>
Step 02
Cording of java file
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class Addpills extends Activity implements AdapterView.OnItemSelectedListener{
Spinner spin_douse;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addpills);
//load page
spin_douse =(Spinner)findViewById(R.id.sp_I_douse_addpills);//get spinner to java from xml
ArrayAdapter adapter_01 = ArrayAdapter.createFromResource(this, R.array.douse, android.R.layout.simple_spinner_item);
//add the string array which is on [res->values->arrays.xml] using arrayadapter to the spinner
spin_douse.setAdapter(adapter_01);
spin_douse.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i,
long l) {
//cord what happen when you select value on spinner
// TODO Auto-generated method stub
TextView res_text=(TextView)view;
Toast.makeText(this, "You select "+res_text.getText(), Toast.LENGTH_SHORT).show();
//make a message when click the selection
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
No comments:
Post a Comment