Add Image To Item (spinner)
Hi I am an amateur in android .I would like to add images to my spinner's item. Unfortunately, I dont know how to do it . Below is my xml file and MainActivity. Thanks for reading
Solution 1:
Use custom Spinner
activity_main
<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/simpleSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
</RelativeLayout>
custom_spinner_items.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><ImageViewandroid:id="@+id/imageView"android:layout_width="50dp"android:layout_height="50dp"android:padding="5dp"android:src="@drawable/ic_launcher" /><!--Make sure image is present in Drawable folder--><TextViewandroid:id="@+id/textView"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_gravity="center"android:padding="@dimen/activity_horizontal_margin"android:text="Demo"android:textColor="#000" /></LinearLayout>
MainActivity.java
package example.abhiandriod.customspinnerexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;
publicclassMainActivityextendsAppCompatActivityimplementsAdapterView.OnItemSelectedListener{
String[] countryNames={"India","China","Australia","Portugle","America","New Zealand"};
int flags[] = {R.drawable.india, R.drawable.china, R.drawable.australia, R.drawable.portugle, R.drawable.america, R.drawable.new_zealand};
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting the instance of Spinner and applying OnItemSelectedListener on itSpinnerspin= (Spinner) findViewById(R.id.simpleSpinner);
spin.setOnItemSelectedListener(this);
CustomAdapter customAdapter=newCustomAdapter(getApplicationContext(),flags,countryNames);
spin.setAdapter(customAdapter);
}
//Performing action onItemSelected and onNothing selected@OverridepublicvoidonItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
Toast.makeText(getApplicationContext(), countryNames[position], Toast.LENGTH_LONG).show();
}
@OverridepublicvoidonNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
returntrue;
}
@OverridepublicbooleanonOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.intid= item.getItemId();
//noinspection SimplifiableIfStatementif (id == R.id.action_settings) {
returntrue;
}
returnsuper.onOptionsItemSelected(item);
}
}
CustomAdapter.java
package example.abhiandriod.customspinnerexample;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
publicclassCustomAdapterextendsBaseAdapter {
Context context;
int flags[];
String[] countryNames;
LayoutInflater inflter;
publicCustomAdapter(Context applicationContext, int[] flags, String[] countryNames) {
this.context = applicationContext;
this.flags = flags;
this.countryNames = countryNames;
inflter = (LayoutInflater.from(applicationContext));
}
@OverridepublicintgetCount() {
return flags.length;
}
@Overridepublic Object getItem(int i) {
returnnull;
}
@OverridepubliclonggetItemId(int i) {
return0;
}
@Overridepublic View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.custom_spinner_items, null);
ImageViewicon= (ImageView) view.findViewById(R.id.imageView);
TextViewnames= (TextView) view.findViewById(R.id.textView);
icon.setImageResource(flags[i]);
names.setText(countryNames[i]);
return view;
}
}
Post a Comment for "Add Image To Item (spinner)"