How to add static header image in custom listview? - android

I will put a picture on top of a custom list view for the header image, but the image is repeated, how can I make it can't click and non-repeatable?
this is my xml code :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/abs__background_holo_light" >
<ImageView
android:id="#+id/detailImage"
android:layout_width="wrap_content"
android:layout_height="140dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:contentDescription="#string/app_name"
android:scaleType="centerCrop"
android:src="#drawable/pantaigoacina" />
<RelativeLayout
android:id="#+id/detailLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/detailImage"
android:background="#color/abs__background_holo_light" >
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="5dip"
android:layout_marginTop="20dip"
android:background="#color/abs__background_holo_light"
android:padding="5dip" >
<ImageView
android:id="#+id/list_image"
android:layout_width="15dip"
android:layout_height="15dip"
android:src="#drawable/arrow"
android:contentDescription="#string/app_name"/>
</LinearLayout>
<TextView
android:id="#+id/NamaLokasi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:text="Pantai Sipelot"
android:textColor="#040404"
android:textSize="18sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/detailLokasi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/NamaLokasi"
android:layout_marginRight="20dip"
android:layout_marginTop="5dip"
android:layout_toRightOf="#+id/thumbnail"
android:text="Deskripsi Konten, Lokasi Berada di Desa Wilayah Malang"
android:textColor="#343434"
android:textSize="12sp" />
</RelativeLayout>
</RelativeLayout>
this is my adapter :
String[] from = { "pantai", "detail" };
int[] to = { R.id.NamaLokasi, R.id.detailLokasi };
SimpleAdapter adapter = new SimpleAdapter(getActivity()
.getBaseContext(), aList, R.layout.fragment_detail, from,
to);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);

Remove the image form the xml.
ImageView iv = new ImageView(getActivity());
// set image to imageview
ListView list = getListView();
list.addHeaderView(headerView);
The docs
public void addHeaderView (View v)
Added in API level 1
Add a fixed view to appear at the top of the list. If addHeaderView is called more than once, the views will appear in the order they were added. Views added using this call can take focus if they want.
Note: When first introduced, this method could only be called before setting the adapter with setAdapter(ListAdapter). Starting with KITKAT, this method may be called at any time. If the ListView's adapter does not extend HeaderViewListAdapter, it will be wrapped with a supporting instance of WrapperListAdapter.
Parameters
v The view to add.
Edit:
#Override
public void onActivityCreated(Bundle savedInstanceStat)
{
super.onActivityCreated(savedInstanceState);
list = getListView();
ImageView imageHeaderView = new ImageView(getActivity());
imageHeaderView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.pantaigoacina));
list.addHeaderView(imageHeaderView);
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < DaerahWisata.pantai.length; i++) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("pantai", DaerahWisata.pantai[i]);
hm.put("detail", "Lokasi : \r\n" + DaerahWisata.detailp[i]);
aList.add(hm);
}
String[] from = { "pantai", "detail" };
int[] to = { R.id.NamaLokasi, R.id.detailLokasi };
SimpleAdapter adapter = new SimpleAdapter(getActivity()
.getBaseContext(), aList, R.layout.fragment_detail, from,
to);
setListAdapter(adapter);
}
Edit, if want to set imageview from other xml layout :
list = getListView();
Context context = getListView().getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.image_header, null);
list.addHeaderView(view);

Try below code:-
ImageView imageHeaderView = new ImageView(this);//use getActivity() on fragment
imageHeaderView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.deckblatt));
myList.addHeaderView(imageHeaderView);

Related

Android listview hide button

I have a problem how to hide the button in specific row in my listview with SimpleAdapter. This is my sample code.
alert_noti_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
alert_pop_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="view1"
android:textSize="20dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/confirm"
android:layout_toStartOf="#+id/confirm" />
<TextView
android:id="#+id/view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="10dp"
android:textStyle="bold"
android:visibility="visible"
android:layout_below="#+id/view1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/confirm"
android:layout_toStartOf="#+id/confirm" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/confirm"
android:layout_alignBottom="#+id/view2"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#drawable/ic_confirm" />
</RelativeLayout>
Activity :
List<Map<String, String>> listData = new ArrayList<Map<String, String>>();
Map<String, Object> names = new HashMap<String, Object>();
names.put("First","head1");
names.put("Second", "txt1");
names.put("First","head2");
names.put("Second", "txt2");
names.put("First","head3");
names.put("Second", "txt3");
...
listData.add((HashMap) names);
ListView lv = (ListView) dialog_mail.findViewById(R.id.listView1);
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(home.this,, android.R.layout.two_line_list_item, names);
SimpleAdapter adapter = new SimpleAdapter(home.this, listData,
R.layout.alert_pop_list,
new String[] {"First", "Second" },
new int[] {R.id.view1, R.id.view2});
lv.setAdapter(adapter);
how do i hide the button if the value is head2 and show to the rest of the list.
You need to custom a BaseAdapter instead of SimpleAdapter and write some code at getView() method。 just like:
public View getView(int p,View view,ViewGroup parent) {
if (view == null) {
view = LayoutInflate.xxxx;
}
Button btn = (Button) view.findViewById(R.id.btn);
DataModel d = list.get(p);
if (d.isHidden || d.data.equals("head2")) {
btn.setVisibility(View.GONE);
} else {
btn.setVisibility(View.visible);
}
return view;
}
so you need to create a DataModel to store some property.
public class DataModel {
public boolean isHidden;
public HashMap<String,String> dataMap;
//and so on.....
}

Android: Error implementing listview within fragment

I created an android actionbar tab application, with 4 tabs, one of the tab is a
fragment class which is used to contain a list view,to display a list of static information:
However, I have an error at this line of code:
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
R.layout.listoflockers, from, to);
These are my 2 xml files used to create a list view:
listoflockers.xml
<?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="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
lockerinforow.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="horizontal"
>
<ImageView
android:id="#+id/flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/email"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
/>
<TextView
android:id="#+id/cur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
/>
<TextView
android:id="#+id/size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
/>
</LinearLayout>
</LinearLayout>
public class myLockerFragment.java{
.....
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_mylocker, container, false);
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>
();
for(int i=0;i<10;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", "Locker No : " + i);
hm.put("cur","Location : " + currency[i]);
hm.put("size","Size : " + size[i]);
hm.put("flag", Integer.toString(flags[i]) );
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "flag","txt","cur","size" };
// Ids of views in listview_layout
int[] to = { R.id.flag,R.id.txt,R.id.cur,R.id.size};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
R.layout.listoflockers, from, to);
// Getting a reference to listview of main.xml layout file
ListView listView = ( ListView ) rootView.findViewById(R.id.listview);
// Setting the adapter to the listView
listView.setAdapter(adapter);
return rootView;
}
Please help
I am confused at this line:
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
R.layout.listoflockers, from, to);
giving me the error:
The method getBaseContext() is undefined for the type myLockerFragment
you can use getActivity() inside a Fragment to get the context that holds the Fragment itself
Do this-
SimpleAdapter adapter = new SimpleAdapter(getActivity(), aList,
R.layout.listoflockers, from, to);
SimpleAdapter adapter = new SimpleAdapter(getActivity().getApplicationContext(), aList, R.layout.listoflockeres, from, to);
You need to use the getActivity() method inside Fragments
Besides using getBaseContext() you need to use the getActivity() in Fragments to get the context of the Fragment.
Try out as below:
SimpleAdapter adapter = new SimpleAdapter(getActivity(), aList,R.layout.listoflockers, from, to);

Programmatically setColor,font to Textviews

I have created a list view with 3 items in each list view item using two separate .'xml's.
1) list_view.xml
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ListView">
</ListView>
2) list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Text here"
android:id="#+id/textView"
android:layout_alignTop="#+id/TextView"
android:layout_alignLeft="#+id/TextView"
android:layout_centerVertical="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="-"
android:id="#+id/seperator"
android:layout_alignBottom="#+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="000"
android:id="#+id/price"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="$"
android:id="#+id/prefix"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/price"/>
</RelativeLayout>
Now, I have created a list view with all the items using a 'SimpleAdapter'. Now I want to access the items in the list view (textView, separator,...) and change the color, size etc programmatically.
Here is the code of my MainActivity.java
public class MainActivity extends Activity {
public RelativeLayout relativeLayout;
public RelativeLayout.LayoutParams lp;
public SimpleAdapter mList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
ListView list = (ListView) findViewById(R.id.ListView);
//background color
list.setBackgroundColor(Color.YELLOW);
TextView itemText = (TextView)findViewById(R.id.textView);
itemText.setTypeface(Typeface.SANS_SERIF);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
//Dummy data
//add all elements to map
map.put("text", "click to edit0");
map.put("seperator", "-");
map.put("price", "100");
// add map elements to list
mylist.add(map);
//add all elements to map
map = new HashMap<String, String>();
map.put("text", "click to edit1");
map.put("seperator", "-");
map.put("price", "102");
//add all elements to map
mylist.add(map);
map = new HashMap<String, String>();
map.put("text", "click to edit2");
map.put("seperator", "-");
map.put("price", "104");
mylist.add(map);
//use adapter to set list items
mList = new SimpleAdapter(this, mylist, R.layout.list_item,
new String[] {"text", "seperator", "price"}, new int[] {R.id.textView, R.id.seperator, R.id.price});
list.setAdapter(mList);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
All worked fine until I tried to access the list view items and change them.
My code crashes and shows a null pointer exception at
TextView itemText = (TextView)findViewById(R.id.textView);
itemText.setTypeface(Typeface.SANS_SERIF);
I suspect this might be because I did not the setContentView to list_item.xml.
Is there a way to access the elements? If yes how?
for setting color follow the bellow code
String text = "<font color=#8469af>By tapping Checkout, you agree to our </font> <font color=#ffffff>terms.</font>";
tvTerms.setText(Html.fromHtml(text));
when you try to call findviewbyid you didnt create listitems yet so it returns null pointer
if you want to set type face with standart font use
android:typeface="serif"
but if you insist do it programmatically
first get list items from list then
(TextView) list_item.findViewById(R.id.textView);
As Talha pointed out the exception comes from the call on findViewById on this where your id textView does not exists, so it returns null
If you want to modify your list items programmatically you should implement your own adapter and apply your modifications in the getView method

Listview with linear straight progressbars

I'm new to android and now trying to make a small project.
It includes a listview with textviews and a linear progressbar (its value doesn't need to change).
Simple Adapter was used to fill data to textviews.
But to set progressbar value, which adapter should be used.
I can't image how to set value to progressbar.
Please help with this.
Here is my code:
main.xml
<?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="fill_parent">
<ListView
android:id="#+id/dblist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
</LinearLayout>
list_row.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:background="#drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:background="#drawable/image_bg"
android:layout_marginRight="5dip">
<ImageView
android:id="#+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="#drawable/ic_launcher"/>
</LinearLayout>
<!-- Title Of Song-->
<TextView
android:id="#+id/col1tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:text="Rihanna Love the way lie"
android:textColor="#040404"
android:typeface="sans"
android:textSize="15dip"
android:textStyle="bold"/>
<!-- Artist Name -->
<TextView
android:id="#+id/col2tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/col1tv"
android:textColor="#343434"
android:textSize="10dip"
android:layout_marginTop="1dip"
android:layout_toRightOf="#+id/thumbnail"
android:text="Just gona stand there and ..." />
<TextView
android:id="#+id/package_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/col1tv"
android:textColor="#343434"
android:textSize="10dip"
android:layout_marginTop="1dip"
android:layout_toRightOf="#+id/col2tv"
android:text=" puzzles, 0 solved" />
<!-- Rightend Duration -->
<!-- Rightend Arrow -->
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/arrow"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
main.java
String[] from = new String[] {"id", "name", "size"};
int[] to = new int[] { R.id.id, R.id.name, R.id.size };
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < pkg_id_col.size(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", id_col.get(i));
map.put("name", name_col.get(i));
map.put("size", size.get(i));
fillMaps.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.list_row_add_new, from, to);
lv_add_new.setAdapter(adapter);
The SimpleAdapter knows how to bind data only for TextViews and ImageView. To bind your ProgressBar use a SimpleAdapter.ViewBinder:
String[] from = new String[] {"id", "name", "size", "progress"};
int[] to = new int[] { R.id.id, R.id.name, R.id.size, R.id.theIdOfTheProgressBar}; // it seems that you don't have a `ProgressBar` in the layout
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < pkg_id_col.size(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", id_col.get(i));
map.put("name", name_col.get(i));
map.put("size", size.get(i));
map.put("progress", valueForProgressBar); // progress will hold the progress for your ProgressBar
fillMaps.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.list_row_add_new, from, to);
adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
public boolean setViewValue(View view, Object data, String textRepresentation) {
if (view.getId == R.id.theIdOfTheProgressBar) {
((ProgressBar) view).setProgress(Integer.parseInt(data));
return true;
}
return false;
}
});

Android listview onclicklistener not working with checkbox

In my listview there is a checkbox, textviews and an imageview. When I click on an item, the onclicklistnere is not working. I know this is because the checkbox has its own listener and it overrides the listview onclicklistener. Fine. When I set
android:focusable="false"
android:focusableInTouchMode="false"
in the checkbox, I can click on the list item but the checkbox becames orange as well (like the item).
So this is a half solution, does anyone know a better one?
This is the class:
public class ListViewTutorial2Activity extends Activity {
SimpleAdapter mSchedule;
ListView list;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (ListView) findViewById(R.id.SCHEDULE);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("train", "101");
map.put("from", "6:30 AM");
map.put("to", "7:40 AM");
mylist.add(map);
map = new HashMap<String, String>();
map.put("train", "103(x)");
map.put("from", "6:35 AM");
map.put("to", "7:45 AM");
mylist.add(map);
mSchedule = new SimpleAdapter(this, mylist, R.layout.row,
new String[] {"train", "from", "to"}, new int[] {R.id.TRAIN_CELL, R.id.FROM_CELL, R.id.TO_CELL});
list.setAdapter(mSchedule);
list.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = list.getItemAtPosition(position);
}
});
}
}
and row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="#+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:descendantFocusability="beforeDescendants">
</CheckBox>
<TextView android:id="#+id/TRAIN_CELL"
android:layout_width="50dip"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/FROM_CELL"
android:layout_width="70dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
<TextView android:id="#+id/TO_CELL"
android:layout_width="60dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
<ImageView
android:id="#+id/icon"
android:layout_width="22px"
android:layout_height="22px"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:layout_marginTop="4px"
android:src="#drawable/icon" >
</ImageView>
</LinearLayout>
Have you tried adding this to your CheckBox in your xml layout?
android:focusable="false"
android:focusableInTouchMode="false"
This worked for me and might be a better solution then what you had to go through.
You don't need to set any special properties on the CheckBox to do this. In the ListView, however, try the following:
android:clickable="true"
android:descendantFocusability="beforeDescendants"
If that doesn't work, you're probably doing something wrong elsewhere.
android:focusable="false"
It will solve the problem in this context
I meet this problem at RecyclerView, maybe this answer will be useful for somebody.
All of these answers don't helped me. Maybe this solutions isn't optimal, but here it is:
<FrameLayout
android:id="#+id/like"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/views_holder"
android:layout_toEndOf="#+id/btn_comment"
android:clickable="true">
<CheckBox
android:id="#+id/cb_like"
android:layout_width="wrap_content"
android:layout_height="#dimen/pressable_size"
android:gravity="center_vertical"
android:button="#drawable/ic_heart_checkbox"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
tools:text="324"
/>
</FrameLayout>
I have added ViewGroup wrapper (in this case - FrameLayout) - and set click listener to FrameLayout:
(In my case with ButterKnife):
#OnClick(R.id.like)
void onLikeClicked() {
cbLike.setChecked(!cbLike.isChecked());
if (onNewsClickListener != null) {
onNewsClickListener.onLikeClicked(getAdapterPosition());
}
}
R.id.like - FrameLayout, cbLike(CheckBox).
It works correctly.
I had the same problema even when i add this to the checkbox
android:clickable="false"
android:focusableInTouchMode="false"
android:focusable="false
Then I realized that my problem it was because i had a onStateChangedListener for the checkbox

Categories

Resources