In this blog we will implement Single Item Select Custom Spinner Android.
first , you can Create Custom Spinner xml Layout, drop dwon Some widegts for example LianerLayout ,textView and ListView. explain how to used..
Step 1: you can Create xml Gradient inside res/drawable folder.
text_cover.xml
...................................................................................................................................................................
..................................................................................................................................................................
activity_main.xml
...................................................................................................................................................................
..................................................................................................................................................................
MainActivity.java
...................................................................................................................................................................
...................................................................................................................................................................
try this......
first , you can Create Custom Spinner xml Layout, drop dwon Some widegts for example LianerLayout ,textView and ListView. explain how to used..
Step 1: you can Create xml Gradient inside res/drawable folder.
text_cover.xml
...................................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:padding="7dp"> <solid android:color="#FFFFFF"/> <corners
android:bottomRightRadius="3dp"
android:bottomLeftRadius="3dp"
android:topLeftRadius="3dp"
android:topRightRadius="3dp"/>
<stroke android:width="2dip" android:color="#dce0e2" /> </shape>
..................................................................................................................................................................
activity_main.xml
...................................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical"
android:padding="10dp"> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_gravity="center"
android:src="@drawable/my_heder_img"/> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:orientation="horizontal"
android:weightSum="2">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="150dp"
android:layout_height="48dp" android:layout_weight="1"
android:gravity="center_vertical"
android:text="Select Item" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical"
android:weightSum="2">
<TextView
android:id="@+id/textView44"
android:layout_width="fill_parent"
android:layout_height="50dp" android:background="@drawable/text_cover"
android:drawableRight="@drawable/short_dwon"
android:gravity="center_vertical"
android:paddingLeft="18dp"
android:textSize="18dp" />
<ListView
android:id="@+id/spinAddGuest"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:background="@drawable/text_cover" android:visibility="invisible" />
</LinearLayout> </LinearLayout> </LinearLayout>
..................................................................................................................................................................
MainActivity.java
...................................................................................................................................................................
package com.example.sarvesh.testproject; import android.app.ActionBar; import android.app.Activity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; public class MainActivity extends Activity { ArrayList<String> item_array; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActionBar(); setContentView(R.layout.activity_main); item_array = new ArrayList<String>(); for (int i = 0; i < 10; i++) { item_array.add("Item" + i); } final ListView spinCount = (ListView) findViewById(R.id.spinAddGuest); final TextView text = (TextView) findViewById(R.id.textView44); text.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { spinCount.setVisibility(View.VISIBLE); } }); ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_single_choice, item_array); spinCount.setAdapter(spinnerArrayAdapter); spinCount.setChoiceMode(ListView.CHOICE_MODE_SINGLE); spinCount.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { text.setText(String.valueOf(item_array.get(position)));//get guest quantity spinCount.setVisibility(View.INVISIBLE); } }); } public void ActionBar() { final ActionBar abar = getActionBar(); abar.setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_image)); View viewActionBar = getLayoutInflater().inflate(R.layout.actionbar_title, null); /* * Center the textview in the ActionBar ! */ ActionBar.LayoutParams params = new ActionBar.LayoutParams( ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER); TextView textviewTitle = (TextView) viewActionBar.findViewById(R.id.actionbar_textview); textviewTitle.setText("Custom Spinner"); abar.setCustomView(viewActionBar, params); abar.setDisplayShowCustomEnabled(true); abar.setDisplayShowTitleEnabled(false); abar.setDisplayHomeAsUpEnabled(true); abar.setHomeButtonEnabled(true); } }
...................................................................................................................................................................
try this......
0 comments