Spiner_gujinfo
//aama java ma String[] name ma "select value" lkhele che ae toast nai tay baki bdhe value toast tse
Step 1:xml
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Select Item"
android:layout_marginBottom="5dp"/>
<Spinner
android:id="@+id/sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
Step 2:Java
package demoproject.aalap.com.spinner;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {
String[] name = {"select a value", "aaa", "sss", "ddd", "fff", "ggg"};
Spinner sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp=findViewById(R.id.sp);
// sp.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this);
sp.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, name);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(dataAdapter);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(position!=0){
Toast.makeText(this, name[position], Toast.LENGTH_SHORT).show();
}
}
//
public void onNothingSelected(AdapterView<?> arg0) {
}
}
Comments
Post a Comment