In the below example I have created Custom View spinner. Here first I have created a row.xml layout , In row.xml I have added a TextView and Button. Then I have created drop.xml layout in this I have added two TextView with in LinearLayout, Then in next step In activity_main.xml layout I have added a Spinner and TextView. Now see In MainActivity I have created new MyClass and MySpinnerAdapter class and extends ArrayAdapter class and I have used getDropDownView method. You can see below program it will clearly describe you How to make custom Spinner in android.
Step(1)-Created row.xml layout-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/gotext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"/>
</LinearLayout>
Step(2)-Created drop.xml layout-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:id="@+id/gotext"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="italic"
android:id="@+id/address"/>
</LinearLayout>
Step(3)-Created actvity_main.xml layout-
<LinearLayout 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:orientation="vertical"
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="com.example.androidspinnertext.MainActivity" >
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textgo"/>
</LinearLayout>
Step(4)-MainActivity-
public class MainActivity extends Activity {
MyClass[]objGo={
new MyClass("FindNerd", "http://findnerd.com/account#url=/users/showDetails"),
new MyClass("Evon-tec", "http://www.evontech.com/"),
new MyClass("Android developers", "http://developer.android.com/training/index.html"),
new MyClass("JavaTpoint", "http://www.javatpoint.com//"),
new MyClass("Google-Play", "https://play.google.com/store")
};
Spinner spinner;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.textgo);
spinner=(Spinner)findViewById(R.id.spinner);
MySpinnerAdapter adapter=new MySpinnerAdapter(MainActivity.this,R.layout.row,objGo);
spinner.setAdapter(adapter);
}
AdapterView.OnItemSelectedListener onItemSelectedListenerGo =new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
MyClass obj =(MyClass)(parent.getItemAtPosition(position));
textView.setText(String.valueOf(obj.getTarget()));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
public class MyClass{
private String text;
private String target;
public MyClass(String text,String target)
{
this.text=text;
this.target=target;
}
public void setText(String text){
this.text =text;
}
public String getText(){
return this.text;
}
public void setValue(String target){
this.target=target;
}
public String getTarget(){
return this.target;
}
}
public class MySpinnerAdapter extends ArrayAdapter<MyClass>{
private MyClass[]myobjs;
public MySpinnerAdapter(Context context,int textViewResourceId,MyClass[]myobjs){
super(context,textViewResourceId,myobjs);
this.myobjs=myobjs;
}
public int getCount(){
return myobjs.length;
}
public MyClass getItem(int position){
return myobjs[position];
}
public long getItemId(int position){
return position;
}
public View getView(final int position,View convertView,ViewGroup parent){
LayoutInflater inflater =getLayoutInflater();
View spView =inflater.inflate(R.layout.row, parent, false);
TextView sp_GoText = (TextView)spView.findViewById(R.id.gotext);
sp_GoText.setText(myobjs[position].getText());
Button button=(Button)spView.findViewById(R.id.button);
button.setText(myobjs[position].getTarget());
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri =Uri.parse(myobjs[position].getTarget());
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}
});
return spView;
}
@Override
public View getDropDownView(int position,View convertView,ViewGroup parent){
LayoutInflater inflater=getLayoutInflater();
View dropDownView =inflater.inflate(R.layout.drop, parent, false);
TextView dd_GoText = (TextView)dropDownView.findViewById(R.id.gotext);
dd_GoText.setText(myobjs[position].getText());
TextView address =(TextView)dropDownView.findViewById(R.id.address);
address.setText(myobjs[position].getTarget());
return dropDownView;
}
}
}
0 Comment(s)