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
Related
I have a listview that I want the user to be able to delete items from. If the user holds on a row long enough, the row's checkbox is made visible. However, the listview doesn't grow to accommodate the (now taller) row despite there being plenty of space. I had this issue before when the listview was in a scrollview but that's no longer the case. I understand I can reformat the text as a bandage solution, but I want to understand what is happening.
Here is the listview before and after the first entry is held. You can see the listview's height stays the same and doesn't expand to accommodate the two entries:
Here is the listView:
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Here is the listView item layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns="https://"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<CheckBox
android:id="#+id/deleteCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="#+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="5dp"
android:textColor="#android:color/black"
android:textSize="18sp" />
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:paddingRight="5dp"
android:textColor="#android:color/darker_gray"
android:textSize="18sp" />
</LinearLayout>
Here is how the layout is being inflated in my custom adapter:
public class CustomAdapter extends ArrayAdapter<CustomlistEntry> {
public CustomAdapter(Context context, ArrayList<CustomlistEntry> entries) {
super(context, 0, entries);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_custom_listview, parent, false);
}
CustomlistEntry entry = getItem(position);
TextView tvNumber = convertView.findViewById(R.id.number);
TextView tvDate = convertView.findViewById(R.id.date);
tvNumber.setText(entry.getNumber());
tvDate.setText(entry.getDateString());
return convertView;
}
}
and finally the adapter being set:
CustomAdapter adapter = new CustomAdapter(App.getApp(), arrayListSorted);
final ListView listView = activity.findViewById(R.id.listView);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int pos, long id) {
arg1.findViewById(R.id.deleteCheckBox).setVisibility(View.VISIBLE);
CustomAdapter adapter = (CustomAdapter)listView.getAdapter();
return true;
}
});
listView.setAdapter(adapter);
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 >
I'm beginner in android development and i a have question about Listview and android:onclick attribute.
I try to use attribute android:onclick ="myMethod" on each element of my listview instead of define OnItemClickListener/onItemClick (too much verbose for me)
MyMethod is executed but how can i get clicked element (current context) ?
Thanks in advance
As a beginner, setting an OnItemClickListener might seem a bit scary, but it's really not a big deal at all (and a core concept you should be learning anyway!).
So if you have a simple layout with a ListView in it like this...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/containerView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/myListview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
You can set up an OnItemClickListener with just this code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_listview_layout);
listView = (ListView)findViewById(R.id.myListview);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Every time an item is clicked, you can handle it right here.
}
});
}
So every time an item in your list is clicked, the onItemClick method is fired, and you can figure out which item was clicked by using the int position argument that comes in.
If i am right your question is about implementing on click listener on each listrow item instead of list row.
If that is correct this is how you should implement:
1.In your adapter of getView() method you might be inflating a layout for list row like below:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_row_layout, parent, false);
return convertView;
}
In the above code list_row_layout is the xml file i'm inflating.
Go to that layout and add the following line android:descendantFocusability="blocksDescendants" to the root level of the xml layout like below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_margin="4dp"
android:descendantFocusability="blocksDescendants"<!--This is the line i'm talking about-->
android:background="#drawable/rounded_border_layout"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="40dp"
android:layout_marginRight="10dp"
android:id="#+id/review_main_user_image_layout"
android:layout_marginLeft="10dp"
android:layout_height="54dp">
<ImageView
android:layout_width="40dp"
android:id="#+id/review_main_user_image"
android:background="#drawable/reviews_x_trail_user_image_border"
android:layout_height="40dp"
android:src="#drawable/ic_help_user" />
<TextView
android:typeface="monospace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Me"
android:textSize="10sp"
android:layout_gravity="center"
android:singleLine="true"
android:id="#+id/review_main_user_name"
android:layout_below="#+id/review_main_user_image" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/review_main_user_image_layout"
android:layout_toEndOf="#+id/review_main_user_image_layout">
<TextView
android:layout_marginLeft="8dp"
android:layout_width="wrap_content"
android:textColor="#color/black"
android:layout_height="wrap_content"
android:singleLine="true"
android:id="#+id/review_main_title"
android:text="Good Product For The Price"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:orientation="horizontal">
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/review_main_rating"
android:progressDrawable="#drawable/apptheme_ratingbar_small_holo_light"
android:numStars="5"
style="?android:attr/ratingBarStyleSmall" />
<TextView
android:layout_marginLeft="8dp"
android:text="12-Jun-1993"
android:id="#+id/review_main_date"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
That't it now in your adapter getView method you can implement on click listener for each widget you have in that layout.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ReviewsXTrailViewHolder reviewsXTrailViewHolder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_row_review_main, parent, false);
TextView title = (TextView)convertView.findViewById(R.id.review_main_user_image);
//title.setOnclickListener()
return convertView;
}
In that above code i have added on click listener for textview.I hope it will work
Comment below if that doesn't work
Working on custom spinner for the first time. I have a spinner with background using nine patch image. Spinnermode is in dialog mode. Now I set custom layout for spinner and spinner item. Run the code but I did not the desired solution. There are corners seeing on the spinner item's background even I have set the background of spinner item . Searched alot but did not find the exact solution. I used code from this site sourcecode form this site their result is perfect but when I tried there whole code did not get the desired result seeing white background in the spinner items.
main.xml
<Spinner
android:id="#+id/spinner1"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_below="#+id/scrollView"
android:layout_marginLeft="20dp"
android:background="#drawable/dropdown_button"
android:paddingLeft="2dp"
android:paddingRight="13dp"
android:paddingTop="5dp"
android:spinnerMode="dialog" />
</RelativeLayout>
spinner_item.xml
<?xml version="1.0" encoding="UTF-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinneritem"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:paddingLeft="10dp" />
spinner_item_list
<?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:background="#drawable/blank_buttons"
android:orientation="horizontal" >
<TextView
android:id="#+id/spinneritemlist"
style="#style/SpinnerText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical|center_horizontal"
android:gravity="center"
android:textColor="#android:color/black" />
</LinearLayout>
**in onCreate()**
ArrayList<CountryInfo> myCountries;
myCountries = populateList();spinner1 = (Spinner) findViewById(R.id.spinner1);
CountryAdapter myAdapter = new CountryAdapter(this, R.layout.spinner_item, myCountries);
spinner1.setAdapter(myAdapter);
function and adapter
public ArrayList<CountryInfo> populateList()
{
ArrayList<CountryInfo> myCountries = new ArrayList<CountryInfo>();
myCountries.add(new CountryInfo("USA")); // Image stored in /drawable
myCountries.add(new CountryInfo("Sweden"));
myCountries.add(new CountryInfo("Canada"));
return myCountries;
}
public class CountryAdapter extends ArrayAdapter<CountryInfo>
{
private Activity context;
ArrayList<CountryInfo> data = null;
public CountryAdapter(Activity context, int resource, ArrayList<CountryInfo> data)
{
super(context, resource, data);
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{ // Ordinary view in Spinner, we use android.R.layout.simple_spinner_item
return super.getView(position, convertView, parent);
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{ // This view starts when we click the spinner.
View row = convertView;
if(row == null)
{
LayoutInflater inflater = context.getLayoutInflater();
row = inflater.inflate(R.layout.spinner_item_list, parent, false);
}
CountryInfo item = data.get(position);
if(item != null)
{ // Parse the data from each object and set it.
TextView myCountry = (TextView) row.findViewById(R.id.spinneritemlist);
if(myCountry != null)
myCountry.setText(item.getCountryName());
}
return row;
}
}
My resulting images:
printscreen of perfect result of sample code on their site(not from running their code)
screenshot of non perfect result of sample code(from running their code in my app)
try this
<Spinner
android:id="#+id/spinner1"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_below="#+id/scrollView"
android:layout_marginLeft="20dp"
android:background="#drawable/dropdown_button"
android:paddingLeft="2dp"
android:paddingRight="13dp"
android:paddingTop="5dp"
android:spinnerMode="dialog"
android:popupBackground="#android:color/transparent" />//or desired color or drawable
also try this in spinner_list_item
<?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:background="#android:color/transparent"
android:orientation="horizontal" >
<TextView
android:id="#+id/spinneritemlist"
style="#style/SpinnerText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical|center_horizontal"
android:gravity="center"
android:background="#drawable/blank_buttons"
android:textColor="#android:color/black" />
</LinearLayout>
I want to implement a layout which resembles Bank's PassBook. So I thought should I implement table layout inside a list view.
Here is a sample passbook example.
DATE PARTICULARS DEBIT CREDIT BALANCE
12/06/2014 Withdraw 200 10000
11/06/2014 Deposit 200 10200
create xml layout like below
<LinearLayout 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"
tools:context=".MainActivity"
android:orientation="horizontal"
android:weightSum="3">
<TextView
android:id="#+id/date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/hello_world" />
<TextView
android:id="#+id/action_type"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:gravity="center"
android:layout_weight="1"/>
<TextView
android:id="#+id/amount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:gravity="center"
android:layout_weight="1"/>
</LinearLayout>
use this layout as list item for your ListView.
Use an ArrayAdapter for your ListView.
In there, you can inflate the Layout for the items of the list, be it a TableLayout, or something other.
private class MyListAdapter extends ArrayAdapter<SomeObject> {
public MyListAdapter(Context context, int resource, List<SomeObject> someObjectList) {
super(context, resource, someObjectList);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
//view recycling
if (convertView == null) {
// inflate the single row with whatever layout you like
view = getLayoutInflater().inflate(R.layout.table_row, parent, false);
} else {
view = convertView;
}
SomeObject item = getItem(position);
//fill the view with data
TextView date = (TextView) view.findViewById(R.id.date);
date.setText(item.getDate());
...
return view;
}
...
}
Here's a little to read about ListViews and ArrayAdapter:
https://github.com/thecodepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView1