I'm making an app that can show Fruit Options on screen. The fruits are:
Apple
Grape
Orange
On screen process, i put switching process, so it will switch:
Apple first for 2 secs
Then switch to Grape for 2 secs
Then Switch Orange for 2 secs
The switching process will work continuously.
Now i want to make a "Choose Button" and a "Spinner" so the user can choose a fruit the will see his choice on "spinner".
For Example: A user wants to choose an orange.
The user will wait till the "Orange" is shown on screen.
When "Orange" is being shown on the screen, directly he will press "Choose Button" then at the same time the "Spinner1" will be filled by "Orange Value".
*I don't want to use TextView.
Here is my code...
MAIN ACTIVITY
private MenuItem Apple;
private MenuItem Grape;
private MenuItem Orange;
private Spinner spinner;
private Button choose_button;
private Button btnPrice;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
choose_button = (Button) findViewById(R.id.button1);
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}
@Override
public void onClick(View v)
{ // i don't know what should i write here
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.i(TAG, "onCreateOptionsMenu");
Apple = menu.add("Apple");
Grape = menu.add("Grape");
Orange = menu.add("Orange");
@Override
public boolean onOptionsItemSelected(MenuItem fruit) {
Log.i(TAG, "Menu Item selected " + fruit);
if (fruit == Apple) {
frame.setViewMode(classfruit.apple);
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2000);
onOptionsItemSelected(Grape);}
catch(Exception ex){}}
}).start();
} else if (fruit == Grape) {
frame.setViewMode(classfruit.grape);
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2000);
onOptionsItemSelected(orange);}
catch(Exception ex){}}
}).start();
} else if (fruit == orange) {
frame.setViewMode(classfruit.orange);
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2000);
onOptionsItemSelected(apple);}
catch(Exception ex){}}
}).start();
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
// get the selected dropdown list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
btnPrice = (Button) findViewById(R.id.btnPrice);
btnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/* get fruit */
String fruit = String.valueOf(spinner1.getSelectedItem());
/* get price */
String price = getPrice(fruit);
public String getPrice(String fruit) {
String price = "0";
if (fruit.equalsIgnoreCase("Apple")) {
price = "1";
} else if (warna.equalsIgnoreCase("Grape")) {
price = "2";
} else if (warna.equalsIgnoreCase("Orange")) {
price = "3";
}
Main.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:entries="@array/fruit"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/btn_choose" />
String.xml
<string name="btn_choose">Choose</string>
<string-array name="fruit">
<item>Apple</item>
<item>Grape</item>
<item>Orange</item>
</string-array>