0% found this document useful (0 votes)
8 views2 pages

Lab2Spiner.docx

The document contains an XML layout for an Android activity featuring a TextView and a Spinner. The associated Java class, SpinnerActivity, initializes the Spinner with a list of countries and displays a Toast message when an item is selected. The activity also includes a menu setup for additional options.

Uploaded by

bekeletamirat931
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Lab2Spiner.docx

The document contains an XML layout for an Android activity featuring a TextView and a Spinner. The associated Java class, SpinnerActivity, initializes the Spinner with a list of countries and displays a Toast message when an item is selected. The activity also includes a menu setup for additional options.

Uploaded by

bekeletamirat931
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Activity_spinner.

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=".SpinnerActivity" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample Spinner Examples" />

<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="58dp" />

</RelativeLayout>

SpinnerActivity.java
package com.example.smple_spinner;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class SpinnerActivity extends Activity implements
OnItemSelectedListener {
Spinner s1;
String str[]={"Ethipia", "USA","Indisha","chan","Koria","Rusha"};
//array decalation in the man maethod
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
s1=(Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> adt=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,str);
s1.setAdapter(adt);
s1.setOnItemSelectedListener(this);
// colling of the method in the array value
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.spinner, menu);
return true;
}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {

// TODO Auto-generated method stub


Toast.makeText(getApplicationContext(),
arg0.getItemAtPosition(arg2).toString(), 500).show();
//out put shown as the bottom

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
}
Output

You might also like