I want to built a simple android application that uses spinner and a button. The spinner contains two items. ITEM1->Google ITEM2-> Yahoo I want: If i select Item1(Google) from spinner and click the button, it redirects to google.com in WebView in android, and IF I select Item2(Yahoo) from spinner and click the same button, it redirects to yahoo.com.
Here is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="158dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="72dp"
android:onClick="onClick"
android:text="@string/buttn" />
</RelativeLayout>
Value item to spinner:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="section">
<item>Google</item>
<item>Yahoo</item>
</string-array>
</resources>
Java Code:
public class FirstPage extends ActionBarActivity implements OnItemSelectedListener
{
Spinner sp;
Button buttn;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.firstpage);
buttn= (Button) findViewById(R.id.button1);
sp = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> ar = ArrayAdapter.createFromResource(this, R.array.section, android.R.layout.simple_list_item_1);
ar.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp.setAdapter(ar);
addListenerOnButton();
}
private void addListenerOnButton()
{
buttn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try{
sp = (Spinner) findViewById(R.id.spinner1);
buttn= (Button) findViewById(R.id.button1);
}
}
});
}
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
}
public void onNothingSelected(AdapterView<?>
}
}
I don't know how to do that. Anyone please help me out. Thanks.