How do i set visibility of selected item of spinner to false - android

I have a background image set for my spinner, but when I select an item from the drop-down list of the spinner, my spinner background image is replaced by the selected item, I want my background image to remain there and I don't want the selected item to be shown instead of the background image of spinner. How do I do that?
Code for my custom adapter:
public class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context ctx, int txtViewResourceId, String[] objects) {
super(ctx, txtViewResourceId, objects);
}
#Override
public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
return getCustomView(position, cnvtView, prnt);
}
#Override
public View getView(int pos, View cnvtView, ViewGroup prnt) {
return getCustomView(pos, cnvtView, prnt);
}
public View getCustomView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View mySpinner = inflater.inflate(R.layout.custom_spinner, parent,
false);
TextView main_text = (TextView) mySpinner
.findViewById(R.id.text_main_seen);
main_text.setText(spinnerValues[position]);
return mySpinner;
}
}
Code for my custom spinner xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="#+id/text_main_seen"
android:layout_width="fill_parent"
android:layout_height="30sp"
android:background="#008000"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="30px" />
</RelativeLayout>
Code for my actual spinner.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Spinner
android:id="#+id/planets_spinner"
android:layout_width="100dp"
android:layout_height="50sp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#008000"
android:layout_weight="0.08"
android:drawSelectorOnTop="true" />
</RelativeLayout
and code of my mainActivity where i am calling the spinner is
Spinner mySpinner = (Spinner) findViewById(R.id.planets_spinner);
mySpinner.setAdapter(new MyAdapter(this, R.layout.custom_spinner,
spinnerValues));

I wouldn't use a spinner at all for this. Just use a Button made to look like a Spinner or whatever else you want it to look like. You can make a regular Button look like a Spinner with this attribute:
android:background="#android:drawable/btn_dropdown"
Or, make your own background that looks like a Spinner and use that.
Then, have the onClick event of your Button open up a "single-item-select" Dialog.

Related

How to remove the Shadow of the Spinner DropDownView

I use the widget Spinner with a custom adapter and custom views in spinnerMode dropdown. My problem is that I cannot remove the default shadow that casts the dropdown of the Spinner.
Here is the code inside the Activity
mSpinner = (Spinner)findViewById(R.id.spinner);
setSpinnerData();
customSpinnerAdapter = new CustomSpinnerAdapter(this,R.layout.spinner_item,CustomListViewValuesArr );
mSpinner.setAdapter(customSpinnerAdapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
itemSelect(pos);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Also this is the Spinner inside the Layout of the Activity:
<Spinner
android:id="#+id/spinner"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:spinnerMode="dropdown"
android:background="#null"
android:padding="0dp"
android:layout_margin="15dp"
android:dropDownVerticalOffset="#dimen/vertical_offset"/>
I use my own custom Adapter :
public class CustomSpinnerAdapter extends ArrayAdapter {
public ArrayList CustomListViewValuesArr;
LayoutInflater inflater;
Typeface myTypeFace;
SpinnerItem item = null;
public CustomSpinnerAdapter(Context context, int textViewResourceId, ArrayList CustomListViewValuesArr) {
super(context, textViewResourceId, CustomListViewValuesArr);
this.CustomListViewValuesArr = CustomListViewValuesArr;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.myTypeFace = Typeface.createFromAsset(context.getAssets(),"fonts/OpenSans-Regular.ttf");
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomDropDownView(position, convertView, parent);
}
public View getCustomDropDownView(int position, View convertView, ViewGroup parent) {
Log.d("","CustomListViewValuesArr" + position);
/********** Inflate spinner_rows.xml file for each row ( Defined below ) ************/
View row = inflater.inflate(R.layout.simple_spinner_dropdown_item, parent, false);
item = (SpinnerItem) CustomListViewValuesArr.get(position);
/***** Get each Model object from Arraylist ********/
TextView label = (TextView)row.findViewById(R.id.spinner_drop_down);
// Default selected Spinner item
label.setText(item.getFilterName());
label.setTypeface(myTypeFace);
return row;
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
Log.d("","CustomListViewValuesArr" + position);
/********** Inflate spinner_rows.xml file for each row ( Defined below ) ************/
View row = inflater.inflate(R.layout.spinner_item, parent, false);
item = (SpinnerItem) CustomListViewValuesArr.get(position);
/***** Get each Model object from Arraylist ********/
TextView label = (TextView)row.findViewById(R.id.spinner_item);
TextView hint = (TextView)row.findViewById(R.id.spinner_hint);
hint.setTypeface(myTypeFace);
// Default selected Spinner item
label.setText(item.getFilterName());
label.setTypeface(myTypeFace);
return row;
}
}
And finally I use custom views for the Dropdown
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:elevation="-6dp"
android:layout_height="35dp">
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/spinnerDropDownItemStyle"
android:singleLine="true"
android:id="#+id/spinner_drop_down"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:textColor="#color/spinner_font_color"
android:layout_marginLeft="20dp"
android:textSize="14sp"
android:layout_marginStart="20dp"
android:ellipsize="marquee">
</TextView >
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/color_hint_change_pass"/>
and for the Spinner Item
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:ellipsize="marquee"
android:background="#drawable/spinners_background"
style="#style/spinnerItemStyle"
android:layout_height="35dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/spinner_hint"
android:gravity="center"
android:textStyle="bold"
android:textColor="#color/spinner_font_color"
android:textSize="14sp"
android:id="#+id/spinner_hint"
android:layout_marginRight="5dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:layout_alignTop="#+id/spinner_item" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner_item"
android:textColor="#color/spinner_header_color"
android:gravity="center"
android:text="sss"
android:textSize="14sp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/spinner_hint"
android:layout_toEndOf="#+id/spinner_hint" />
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:src="#drawable/down_arrow_spinner"
android:layout_marginRight="17dp"
android:layout_marginEnd="1dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/imageView2" />
What I have tried and didn't worked :
Add Elevation to 0dp in the Spinner widget
Add null background to Spinner
Thanks in Advance !
Use this:
android:popupElevation="0dp"
Eventually I used an attributed of a spinner android:popupBackground="#null" with this the background of the pop up of the spinner is removed so and the shadow does. The only thing the I had to change was to Assign a background of #fff in my custom spinner dropdown item. So I changed it like this:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="35dp">
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/spinnerDropDownItemStyle"
android:singleLine="true"
android:id="#+id/spinner_drop_down"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:textColor="#color/spinner_font_color"
android:layout_marginLeft="20dp"
android:textSize="14sp"
android:layout_marginStart="20dp"
android:ellipsize="marquee">
</TextView >
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/color_hint_change_pass"/>
</RelativeLayout >

Android Spinner Initial Padding

I am creating an Android app that consists mainly of a form. This form needs to have EditText and Spinners side-by-side (for this I chose a LinearLayout). My final goal is to have the text in the Spinner align vertically with the text in the EditText; however, I also want to have padding on the options in the expanded Spinner. This padding messes up the Spinner alignment as it appears in the form.
Here is my code for a custom Spinner list item:
<TextView
android:id="#+id/transportation_spinner_item_textView"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#android:color/black"
android:textSize="18dp"/>
Here is my code for the main form:
<LinearLayout
android:id="#+id/linearLayout6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/divider3"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="100"
android:orientation="vertical">
<TextView
android:id="#+id/ip_minutes_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/ip_minutes"
android:layout_marginTop="4dp"
android:textSize="12sp"/>
<Spinner
android:id="#+id/ip_minutes_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog"
android:gravity="bottom"/>
</RelativeLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/ip_charge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="100"
android:layout_gravity="bottom">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="100"
android:editable="false"
android:hint="#string/ip_charge"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
And here is an image of how it looks:
Image of alignment with above code
I have been Googling and trying to adjust the padding/margins of different views for hours, so if anyone has any suggestions, that would be great. Thanks!
I'm not sure I fully understand, when you say,
This padding messes up the Spinner alignment as it appears in the
form.
Do you mean the DropDownView? that shows when the Spinner is clicked?
If so, simply create a separate layout for the DropDownView.
In your ArrayAdapter you will have the following methods,
public View getDropDownView(int position, View cnvtView, ViewGroup prnt)
public View getView(int pos, View cnvtView, ViewGroup prnt)
Just have them return two different Views, e.g
#Override
public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
return getCustomDropDownView(position, cnvtView, prnt);
}
#Override
public View getView(int pos, View cnvtView, ViewGroup prnt) {
return getCustomView(pos, cnvtView, prnt);
}
and
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View mySpinner = inflater.inflate(R.layout.custom_spinner, parent, false);
TextView main_text = (TextView) mySpinner.findViewById(R.id.spinner_text);
main_text.setText(spinnerValues[position]);
return mySpinner;
}
public View getCustomDropDownView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View mySpinner = inflater.inflate(R.layout.custom_dropdown_spinner, parent, false);
TextView main_text = (TextView) mySpinner.findViewById(R.id.spinner_text);
main_text.setText(spinnerValues[position]);
return mySpinner;
}

Custom Spinner with default down arrow in it

I have a custom layout for spinner. Its both selected and dropdown list item's background color will change depending on user demand. So i am changing depending on their values.. so far so good. But the problem is since i am using custom layout or changing background color, there is no arrow to show that it is a spinner. When i add arrow from layout then all even dropdown list have that arrow with them. I can manipulate it by setting arrow's background to null for drop down items but it is not a good way and dont always work. So how can i show spinner default arrow with my custom spinner without using spinner style. Dont forget that i am changing background of every item.
Here is my custom_spinner.xml
<?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:weightSum="6" android:id="#+id/spinnerMainLayout"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal" android:layout_width="match_parent"
android:weightSum="14"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp" android:layout_marginTop="8dp"
android:layout_height="wrap_content" android:layout_marginLeft="10dp"
android:text="New Text" android:textSize="19sp" android:layout_weight="5"
android:layout_gravity="center" android:layout_marginBottom="8dp"
android:gravity="center" android:layout_marginRight="4dp"
android:id="#+id/type" android:typeface="serif"/>
<View
android:layout_width="1dp" android:layout_marginLeft="8dp" android:layout_marginTop="4dp"
android:layout_height="match_parent" android:layout_marginBottom="4dp"
android:background="#666666" />
<TextView
android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_marginTop="8dp" android:layout_weight="9"
android:typeface="serif" android:maxLines="3" android:minLines="2"
android:layout_gravity="center" android:layout_marginBottom="8dp"
android:layout_marginLeft="10dp" android:layout_marginRight="5dp"
android:text="sd" android:textSize="18sp" android:gravity="center_vertical"
android:id="#+id/history" />
</LinearLayout>
</LinearLayout>
and here is my custom spinner class
class CustomSpinner extends ArrayAdapter<String> {
private String[] fixStrs = new String[]{"Definition","Result","Error"};
public CustomSpinner(Context ctx, int txtViewResourceId, String[] objects) {
super(ctx, txtViewResourceId, objects);
//fixStrs = objects;
}
#Override public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
return getCustomView(position, cnvtView, prnt);
}
#Override public View getView(int pos, View cnvtView, ViewGroup prnt) {
return getCustomView(pos, cnvtView, prnt);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View mySpinner = inflater.inflate(R.layout.custom_spinner, parent, false);
/*LinearLayout spinnerMain = (LinearLayout) mySpinner.findViewById(R.id.spinnerMainLayout);
spinnerMain.setBackgroundColor(Color.parseColor(colors[0]));*/
TextView fixText = (TextView) mySpinner.findViewById(R.id.type);
fixText.setText(fixStrs[position]);
fixText.setTextColor(Color.parseColor(colors[1]));
TextView historyText = (TextView) mySpinner.findViewById(R.id.history);
historyText.setText(history[position]);
historyText.setTextColor(Color.parseColor(colors[1]));
return mySpinner;
}
}
Create a different layout for the spinner dropdown and pass it to the .setDropDownViewResource method of the ArrayAdapter, then you must specify the id of the textview in this layout in the constructor call of your adapter

Can't create ListView of RelativeLayouts for Android

What I'm trying to accomplish is have a scrollable ListView that contains a RelativeLayout for each item. Each RelativeLayout contains a CheckBox and an EditText. My ArrayAdapter for the ListView contains the RelativeLayout type. When I create this ListView, each item is shown as the toString() of the layout instead of the layout itself. What should I be doing to accomplish this?
Init your list view
ListView listView = (ListView) findViewById(R.id.list_view);
Create the adapter layout for your listview like this (customview.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<requestFocus />
</EditText>
</<RelativeLayout >
Create the AdapterClass and extend BaseAdapter
public class CustomAdapter extends BaseAdapter {
#Override
public int getCount() {
...
}
#Override
public Object getItem(int position) {
return ....;
}
#Override
public long getItemId(int position) {
return ...;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.customview, parent, false);
TextView name = (TextView) row.findViewById(R.id.name);
CheckBOx check = (CheckBOx) row.findViewById(R.id.checkBox1);
EditText edit = (EditText) row.findViewById(R.id.editText1);
name.setText("Name");
edit.setText("Address");
return row;
}
Finally set the adapter to your ListView
listView.setAdapter(new CustomAdapter(contacts) );
Make your own subclass of ArrayAdapter and construct your RelativeLayout in getView()
The type you set the ArrayAdapter to is for populating the content not constructing the view. To use a non-standard view like you want to do, you'll have to override BaseAdapter
You can see an example here:
http://www.vogella.com/articles/AndroidListView/article.html#adapterown

List view first element no clickable

I have a list of element in a fragment.
I want to disable the title of list
http://bugs.haploid.fr/file_download.php?file_id=856&type=bug
here are the code the cell of my list:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/stations_listView_layout">
<com.infrabel.railtime.views.RobotoMediumTextView
android:id="#+id/header_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:paddingBottom="5dip"
android:layout_marginTop="29dip"
android:text="A"
style="#style/FragmentTitleSecondary">
</com.infrabel.railtime.views.RobotoMediumTextView>
<View
android:id="#+id/header_bar"
android:layout_width="match_parent"
android:layout_height="2dip"
android:background="#color/fragment_title_color">
</View>
<RelativeLayout
style="#style/stations_listView_inner_layout"
android:layout_width="fill_parent"
android:layout_height="60dip">
<com.infrabel.railtime.views.RobotoMediumTextView
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="30dip"
android:layout_gravity="center_vertical"
android:textSize="#dimen/menu_list_item_primary_text_size"
android:textColor="#color/grey_normal">
</com.infrabel.railtime.views.RobotoMediumTextView>
<com.infrabel.railtime.views.RobotoMediumTextView
android:id="#+id/details"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="25dip"
android:textSize="#dimen/fragment_title_text_size"
android:textColor="#color/grey_light">
</com.infrabel.railtime.views.RobotoMediumTextView>
</RelativeLayout>
</LinearLayout>
i tried to set android:clickable = false or android:focusable="false" and it doesn't work.
One way to make certain ListView item not clickable is:
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position == 0) {
// FIRST ITEM - DO NOTHING
} else {
// DO SOMETHING IF YOU WISH...
}
}
});
another way would be to modify your custom adapter:
list.setAdapter(new ArrayAdapter<String>(this, R.layout.item,
listItems) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final TextView v = (TextView) super.getView(position,
convertView, parent);
if(position == 0) {
v.setClickable(false);
// havent tested this thou...
}
}
});
got it , i have in my cell.xml
so the text will be drawn every time when i add cell.
we must declare this in the main xml, than change the text from our adapter:
String firstLetter = station.getTitle().substring(0, 1);
if (position == _alphaIndexer.get(firstLetter)) {
TextView header = (TextView) activity.findViewById(R.id.header_textview);
header.setText(firstLetter);
}

Categories

Resources