Reputation: 16152
I want to create custom Spinner Dialog
for my app. I have found this & this.
both are good.I have update my code as per requirement. i am using selector file for changing dialog background view when user tap on spinner.
My code is working perfectly but issue is in dialog view. its spreading that width but i want to get according to some fix size.
The current output of my Spinner View is as Follows:-
But i need it as Follows:-
Spinner_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/spinner_popup" // it's my selector file
android:orientation="horizontal" >
<TextView
android:id="@+id/Genere_name"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginLeft="5dp"
android:ellipsize="marquee"
android:gravity="center_vertical"
android:singleLine="true"
android:textColor="@android:color/black"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
spinner_popup.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/popup_header" android:state_selected="true"/>
<item android:drawable="@android:color/transparent" android:state_enabled="true"/>
<item android:drawable="@drawable/popup_header" android:state_pressed="true"/>
<item android:drawable="@drawable/popup_bg_box" android:state_enabled="false" android:state_focused="true"/>
<item android:drawable="@android:color/transparent"/>
</selector>
Main_spinner_view
<LinearLayout
android:id="@+id/search_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/search_bg"
android:orientation="horizontal"
android:weightSum="3" >
<Spinner
android:id="@+id/Search_Spinner"
android:layout_width="95dp"
android:layout_height="30dp"
android:layout_gravity="left|center_vertical|center_horizontal"
android:layout_marginLeft="2dp"
android:layout_weight="1.2"
android:background="@drawable/search_dropdown" />
<EditText
android:id="@+id/Search_EDT"
android:layout_width="170dp"
android:layout_height="30dp"
android:layout_gravity="center|center_horizontal|center_horizontal"
android:layout_marginLeft="3dp"
android:layout_weight="1.5"
android:background="@drawable/search_textfield"
android:hint="Search By Free Text"
android:inputType="textPersonName"
android:padding="5dp"
android:textSize="16sp" >
</EditText>
<Button
android:id="@+id/Search_GO"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="right|center_horizontal|center_vertical"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="0.3"
android:background="@drawable/search_icon"
android:textColor="@android:color/white" />
</LinearLayout>
Here i am calling my spinner adapter
Search_Spinner.setAdapter(new MyAdapter(GridViewActivity.this,
R.layout.spinner_item, Search_Category_List));
I think java code is not necessary but still i am paste here,
public class MyAdapter extends ArrayAdapter<String>
{
public MyAdapter(Context context, int textViewResourceId,
ArrayList<String> objects)
{
super(context, textViewResourceId, objects);
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent)
{
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView,
ViewGroup parent)
{
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.spinner_item, parent, false);
TextView label = (TextView) row.findViewById(R.id.Genere_name);
spinner_text = Typeface.createFromAsset(getAssets(), "desc.ttf");
label.setTypeface(spinner_text);
label.setText(Search_Category_List.get(position));
return row;
}
}
Upvotes: 4
Views: 4162
Reputation: 2097
Try create transperent activity for this
See below code for help. First of remove Spinner control and put button over there. Put button background like spinner so it shows like spinner.
on button click use this code
Intent bookIntent = new Intent();
bookIntent.setClass(MusicAppActivity.this, OptionActivity.class);
bookIntent.putExtra("cat", stockArr);
startActivityForResult(bookIntent, 1);
bookIntent.putExtra("cat", stockArr);
pass array in intent from here This is array which you want to display in new transperent activity.
OptionActivity
Now create one transperent activity called OptionActivity
Create list in that transperent activity. and set width and height of activity
Transparent activity code is below
public class OptionActivity extends Activity {
ListView l;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
Intent intent = getIntent();
String[] myStrings = intent.getStringArrayExtra("cat");
l = (ListView) findViewById(R.id.optionList);
l.setAdapter(new ArrayAdapter<String>(this, R.layout.headerlist,R.id.text1, myStrings));
l.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String book = l.getAdapter().getItem(position).toString();
Intent returnIntent = new Intent();
returnIntent.putExtra("SelectedBook",book);
setResult(RESULT_OK,returnIntent);
finish();
}
});
}
}
list.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/optionFinish"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/my_custom_background"
android:layout_marginTop="50dp"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/headerbg" >
<TextView
android:id="@+id/viewTournament"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:freezesText="true"
android:gravity="center_vertical|center_horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:paddingLeft="15dip"
android:paddingRight="40dip"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Catagory"
android:textColor="@android:color/white" />
<!-- <ImageButton
android:id="@+id/optionBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@android:color/transparent"
android:padding="5dip"
android:src="@drawable/options" /> -->
</RelativeLayout>
<ListView
android:id="@+id/optionList"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:cacheColorHint="#00000000"
android:background="@android:color/white"
android:paddingLeft="2dip"
android:paddingRight="2dip" >
</ListView>
</LinearLayout>
Upvotes: 1
Reputation: 5803
You could try to set the weight of the textview to 0.3 and the width to 0dip. You could also try to set the maxWidth on the textView. Also, the background color would have to be set for the textView and not for the spinner item as then the width would be the width of the textView.
you could also try to use the setDropDownWidth but you would have to set the minApiLevel to 16 for that. You could refer this for more : http://developer.android.com/reference/android/widget/Spinner.html#attr_android:dropDownVerticalOffset
Upvotes: 0
Reputation: 1532
use android:layout_width="wrap_content" instead of android:layout_width="fill_parent" in LinearLayout
Upvotes: 0