I used custom spinner adapter for color spinner in android application. The drop down is working fine. But once I select a color(item) from spinner, it is not selectable. Also I do not need to show selected item as it is selected. I only want to identify the selected color without displaying it.
Below is code for my CustomSpinnerAdapter:
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
TextView rowView=null;
if(convertView == null){
convertView=inflater.inflate(R.layout.spinner_layout, null);
}
rowView=(TextView) convertView.findViewById(R.id.spinnerColorview);
rowView.setBackgroundColor(Color.parseColor(itemList.get(position)));
return convertView;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView rowView=null;
if(convertView == null){
convertView=inflater.inflate(R.layout.spinner_layout, null);
}
rowView=(TextView) convertView.findViewById(R.id.spinnerColorview);
rowView.setBackgroundColor(Color.parseColor(itemList.get(position)));
return convertView;
}
EDIT: MORE INFORMATION
My drop down list in spinner not selectable. When I clicked on the spinner it is displaying the list. But when I select one item from that list, nothing happen. I cannot identify selected item.
When I print the position inside getView(int position, View convertView, ViewGroup parent) method, it prints all item ids.
I only need to identify selected item and I do not need to display it at the top of spinner as it usually does. This is my spinner_layout.xml
<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="40dp"
android:clickable="true"
android:orientation="horizontal"
android:paddingLeft="40dp"
tools:context=".MainActivity" >
<TextView
android:id="#+id/spinnerColorview"
android:layout_width="200px"
android:layout_height="50px"
android:clickable="true"
android:gravity="center_vertical"
>
</TextView>
</LinearLayout>
I added style="?android:attr/spinnerItemStyle" to the textview and it works. Not sure that the best solutions but it's a start and a quick fix.
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/spinnerItemStyle"
android:id="#+id/TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:ellipsize="end"
android:gravity="left|center"
android:singleLine="true"
android:text="Option One"
android:textColor="#color/Petrol"
android:textSize="#dimen/font_size_big"
android:textStyle="bold"/>
Related
How can I remove the padding from around the Spinner's text?
My layout:
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="#ff00ff"
android:spinnerMode="dropdown" />
<TextView
android:id="#+id/textLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Label"
android:layout_marginStart="8dp"
android:background="#0000ff"
android:textColor="#ffffff"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
Activity does not do anything special just put some content into the Spinner. Please let me know if you need it, I'll attach it then.
I'd like to make the Spinner's text to be exactly aligned to Label while the purple box also aligned to blue one. I tried to set padding to 0dp, but it does not change anything. Also tried removing the background, but it also does not change this padding.
And of course if I mess with margins, I can make the texts to be in line, but then the boxes will not stay where they are now.
How shall I do this?
Using a custom overridden View seems to work but why are padding on the top and bottom?
I tried even changing all padding to 0, but still.
Here is my actual adapter:
public class MyAdapter extends ArrayAdapter {
public MyAdapter(#NonNull Context context, int resource, String[] params) {
super(context, resource, params);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
view.setPadding(0, 0, 0, 0);
return view;
}
}
You can try adding this to the adapter.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
view.setPadding(0, view.getPaddingTop(), view.getPaddingRight(), view.getPaddingBottom());
return view;
}
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
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
My XMLs are
activity_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<ListView
android:id="#+id/categoriesList"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
And the customAdaptor
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<TextView
android:id="#+id/categoryIDText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible">
</TextView>
<TextView
android:id="#+id/categoryName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20px" >
</TextView>
</LinearLayout>
So, within each row, I have two textViews. One is hidden, and other is visible.
Now, when I click on any row, the only clickable area is the one where TextView's string ends.
i.e. if my TextView categoryName has one element set as Hello then the listview's row looks like
Hello [BLANK SPACE HERE]
And when i touch the are where it is BLANK SPACE, it does not respond to the touch. But when I touch the area, where it is Hello, then it responds.
What should I do to make the entire row clickable?
Thanks
EDIT
listView.setAdapter(new CategoryAdapter(this, categories));
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
}
And my custom adaptor
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_view_category, parent, false);
TextView textViewID = (TextView) rowView.findViewById(R.id.categoryIDText);
TextView textViewName = (TextView) rowView.findViewById(R.id.categoryName);
textViewID.setText(""+data.get(position).getId());
textViewName.setText(data.get(position).getName());
return rowView;
}
Apparently I had to change the android:layout_width root element of my activity.xml file.
That did the trick for me.
The root element of your listview item has android:layout_width="wrap_content" change it to match_parent (in your customAdaptor xml)
Add android:clickable="false" to the TextView. This will make the touch event fall through to your underlying row.
Have you tried setting the visibility to "gone"?
<TextView
android:id="#+id/categoryIDText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone">
</TextView>
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