Custom layout for Spinner item
I have this
and this
with these code of xml
xml for adapter named spinadapt.xml
<?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="30dp"
android:background="#fff" >
<TextView
android:id="@+id/tvCust"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_toLeftOf="@+id/radioButton1"
android:gravity="left|center_vertical"
android:textColor="#000"
android:textSize="15sp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentRight="true" />
</RelativeLayout>
and main layout named activity_main.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">
<TextView
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:hint="Select item"
android:background="@drawable/spin"/>
</RelativeLayout>
and java code is class named MainActivity.java
public class MainActivity extends Activity
{
Spinner sp;
TextView tv;
String[] counting={"One","Two","Three","Four"};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp=new Spinner(this);
tv=(TextView)findViewById(R.id.spinner1);
tv.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
sp.performClick();
}
});
sp.setAdapter(new Adapter(MainActivity.this, counting));
sp.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
tv.setText(counting[arg2]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
}
}
and adapter class named Adapter.java
public class Adapter extends BaseAdapter
{
LayoutInflater inflator;
String[] mCounting;
public Adapter( Context context ,String[] counting)
{
inflator = LayoutInflater.from(context);
mCounting=counting;
}
@Override
public int getCount()
{
return mCounting.length;
}
@Override
public Object getItem(int position)
{
return null;
}
@Override
public long getItemId(int position)
{
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
convertView = inflator.inflate(R.layout.spinadapt, null);
TextView tv = (TextView) convertView.findViewById(R.id.tvCust);
tv.setText(Integer.toString(position));
return convertView;
}
}
this is working perfect
hope this will help
Custom layout of spinner in Android
I hope, this will help you..
1) In drawable folder--->add spinner_border.xml for border of spinner.
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/black" />
<corners android:radius="0dp" />
<stroke
android:width="1dp"
android:color="#B4925A" />
2)In layout design of spinner use this drop down "arrow_icon" for imageview src
<RelativeLayout
android:layout_width="210dp"
android:layout_height="wrap_content"
android:background="@drawable/spinner_border"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:gravity="center_horizontal"
android:entries="@array/spinnerItems"
android:spinnerMode="dropdown" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:src="@drawable/arrow_icon" />
</RelativeLayout>
If there any further query,please inform, Thanks...
How to customize a Spinner in Android
Create a custom adapter with a custom layout for your spinner.
Spinner spinner = (Spinner) findViewById(R.id.pioedittxt5);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.travelreasons, R.layout.simple_spinner_item);
adapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
R.layout.simple_spinner_item
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="@style/spinnerItemStyle"
android:maxLines="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />
R.layout.simple_spinner_dropdown_item
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="@style/spinnerDropDownItemStyle"
android:maxLines="1"
android:layout_width="match_parent"
android:layout_height="?android:attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee" />
In styles add your custom dimensions and height as per your requirement.
<style name="spinnerItemStyle" parent="android:Widget.TextView.SpinnerItem">
</style>
<style name="spinnerDropDownItemStyle" parent="android:TextAppearance.Widget.TextView.SpinnerItem">
</style>
Android: Custom Spinner Layout
row.xml to set up the layout on each row (in this case: one image and text each row):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"/>
<TextView
android:id="@+id/weekofday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Java:
public class AndroidCustomSpinner extends Activity {
String[] DayOfWeek = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.row, R.id.weekofday, DayOfWeek);
mySpinner.setAdapter(adapter);
}
}
Android Spinner Item Layout for Default Entries in XML
try this ((ArrayAdapter)preFilledSpinner.getAdapter()).setDropDownViewResource(android.R.layout.simple_spinner_item);
To know how it works just check the code of AppCompatSpinner
and below is the default code of AppCompatSpinner
to figure out how it works when you pass the entries.
final CharSequence[] entries = a.getTextArray(R.styleable.Spinner_android_entries);
if (entries != null) {
final ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(context,android.R.layout.simple_spinner_item, entries);
adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
setAdapter(adapter);
}
When we are passing entries through XML they will created a ArrayAdapter
and apply code adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
where you can see, R.layout.support_simple_spinner_dropdown_item
is set by default, which should be applicable from parameter but unfortunately they have make it fixed
Related Topics
Android: Running a Background Task Using Alarmmanager
Libz.So.1: Cannot Open Shared Object File
How to Pop Fragment Off Backstack
How to Change the Background of an Android Tab Widget
Converting Utc Dates to Other Timezones
Change Clickable Textview's Color on Focus and Click
How to Change the Tabs Images in the Tabhost
Error Inflating Class Androidx.Constraintlayout.Constraintlayout After Migration to Androidx
How to Have a Viewpager Inside of a Scrollview
Android: Using Objectanimator to Translate a View with Fractional Values of the View's Dimension
Android Tabwidget Detect Click on Current Tab
How to Finish an Android Application
How to Get Free and Total Size of Each Storagevolume
How to Get File Path from Sd Card in Android
Connect to Wifi in Android Q Programmatically
Android SQLite Database, Why Drop Table and Recreate on Upgrade