Highlight ListView row without Views in it - android

I have created ListView with checkbox and textview(with custom background) in every row.
When I click on a row, checkbox and textview get highlighted with a listview row, but I want to get highlighted only the listview row without checkbox and textview after I click on the row. Could you help me please?
This is what I want after click on a row: http://tinypic.com/r/14kfpza/5.
This is what I have after click on a row: http://tinypic.com/r/2mr5w9c/5.
Thank you
My adapter for listview:
public class MyAdapter<String> extends ArrayAdapter<String>{
Context ctx;
List<String> content;
public MyAdapter(Context context, List<String> objects) {
super(context, R.layout.listview_row ,objects);
this.ctx = context;
this.content = objects;
}
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
if(row == null){
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.listview_row, parent, false);
}
TextView desc = (TextView)row.findViewById(R.id.textView1);
String name = content.get(position);
desc.setText(name.toString());
return row;
}
}
listview_row.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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="Large Text"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:background="#drawable/textview_bg"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/textView1"
android:layout_marginRight="5dp"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="" />
</RelativeLayout>

Write this in ur listView xml .
android:addStatesFromChildren="false"
This will not send the click events to the children of listView

OK, I have solved it, with custom TextView, the setPressed method is overwritten. The same I have done with CheckBox.
public class TextViewForListView extends TextView{
public TextViewForListView(Context context) {
super(context);
}
public TextViewForListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TextViewForListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void setPressed(boolean pressed) {
if (pressed && (this.getParent() instanceof View) && ((View) this.getParent()).isPressed()) {
return;
}
super.setPressed(pressed);
}
}

Related

Show loader on Spinner (Dropdown) when it is fetching data from web service

In the image above, I have shown that when the user touches the drop-down spinner it will call the web api for getting data for the spinner. Then, that moment, I want to show the loader only on the spinner view on the left or right somewhere on the view itself like in the image, rather than on whole screen when it is getting data from the web service dynamically and hide that progress bar later when web service completely hit at the end (Ignore that search bar in image).
Just create an custom adapter for your spinner. Follow the instructions found here How to create Spinner-list using CustomAdapter in android .
Put the loading view in the layout inflated in the getView method in the adapter, and manipulate it via a callback from your async task used for fetching the result.
In this i am showing loader on start button and hiding loader when stop button is pressed so you can use according to your need.So , for this i have made three class CustomSpinner,Spinner_Custom_adapter and Activity class for using it
In main layout file
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
<www.your_packagename.com.spinnerwithloaderex.CustomSpinner
android:id="#+id/custm_spnr"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:background="#drawable/dropdown_create_sales"
android:paddingRight="15dp"
android:text="Hello World!" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<Button
android:id="#+id/start_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
<Button
android:id="#+id/stop_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop" />
</LinearLayout>
</LinearLayout>
CustomSpinner class
public class CustomSpinner extends android.support.v7.widget.AppCompatSpinner {
private Spinner_Custom_adapter spinner_custom_adapter;
public CustomSpinner(Context context) {
super(context);
}
public CustomSpinner(Context context, int mode) {
super(context, mode);
}
public CustomSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr, int mode) {
super(context, attrs, defStyleAttr, mode);
}
public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr, int mode, Resources.Theme popupTheme) {
super(context, attrs, defStyleAttr, mode, popupTheme);
}
public void setItems(Activity activity, ArrayList<String> spnr_Arr) {
spinner_custom_adapter = new Spinner_Custom_adapter(activity, spnr_Arr);
setAdapter(spinner_custom_adapter);
}
public Spinner_Custom_adapter getSpinner_custom_adapter() {
return spinner_custom_adapter;
}
public void showLoader() {
setEnabled(false);
spinner_custom_adapter.showLoader(true, true);
}
public void dismissLoader() {
setEnabled(true);
spinner_custom_adapter.showLoader(false, true);
}
}
Custom_Adapter class
public class Spinner_Custom_adapter<T> extends ArrayAdapter<T> {
private LayoutInflater flater;
private ProgressBar spinner_progress;
private TextView txtTitle;
private Boolean showOrNot = false;
Spinner_Custom_adapter(Activity context, ArrayList<T> list) {
super(context, R.layout.loader_spinner_lt, R.id.title, list);
flater = context.getLayoutInflater();
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = flater.inflate(R.layout.loader_spinner_lt, parent, false);
}
Object object = getItem(position);
String rowItem = null;
if (object instanceof String) {
rowItem = (String) object;
}
TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
txtTitle.setText(rowItem);
ProgressBar spinner_progress = (ProgressBar) convertView.findViewById(R.id.spinner_progress);
this.txtTitle = txtTitle;
this.spinner_progress = spinner_progress;
showLoader(showOrNot, false);
return convertView;
}
void showLoader(Boolean showOrNot, boolean notifyListOrNot) {
if (txtTitle != null && spinner_progress != null) {
this.showOrNot = showOrNot;
spinner_progress.setVisibility(showOrNot ? View.VISIBLE : View.GONE);
if (notifyListOrNot) {
notifyDataSetChanged();
}
}
}
}
Spinner single view layout xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="horizontal">
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/title"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/spinner_progress"
android:ellipsize="end"
android:singleLine="true"
android:text="Strawberry"
android:textColor="#CC0033"
android:textSize="16dp" />
<ProgressBar
android:id="#+id/spinner_progress"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:visibility="gone" />
</RelativeLayout>
and for using it
custm_spnr = (CustomSpinner) findViewById(R.id.custm_spnr);
ArrayList<String> items = new ArrayList<>();
items.add("Abcdefg");
items.add("hijklm");
items.add("nopqr");
items.add("stu");
items.add("vwxyza1b1c1");
items.add("d1e1f11g1h1");
custm_spnr.setItems(MainActivity.this, items);
findViewById(R.id.start_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
custm_spnr.showLoader();
}
});
findViewById(R.id.stop_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
custm_spnr.dismissLoader();
}
});

Button unclickable listView

I have a listView with a button in each row. The problem is that I can click in my listView item and something happens, but my Button does not work, nothing happens. I've read some things about the solution, but none helped me.
Here's my list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:descendantFocusability="blocksDescendants"
>
<TextView
android:id="#+id/text_branche_cours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="English"
/>
<TextView
android:id="#+id/text_trait"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" - "
/>
<TextView
android:id="#+id/text_designation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reading"
android:layout_marginRight="150dp"
/>
<Button
android:id="#+id/bAjouterJalon"
android:layout_width="58dp"
android:layout_height="wrap_content"
android:background="#drawable/ic_action_add_jalon"
/>
And here's my customadapter class:
public class CustomAdapter extends SimpleCursorAdapter {
private Context mContext;
private Context appContext;
private int layout;
private Cursor cr;
private final LayoutInflater inflater;
public CustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.layout = layout;
this.mContext = context;
this.inflater = LayoutInflater.from(context);
this.cr = c;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(layout, null);
}
#Override
public void bindView(View view, final Context context, final Cursor cursor) {
super.bindView(view, context, cursor);
final Button ajouter = (Button)view.findViewById(R.id.bAjouterJalon);
ajouter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent retour = new Intent(mContext,OngletJalonsNotes.class);
mContext.startActivity(retour);
}
});
}
}
Thank you for the help.
#EDIT - my listview is unclickable now !!
you are making button focusable & focus on touch false so it will not detect your touch tab on your button, remove these two lines from your button tag in xml :
android:focusable="false"
android:focusableInTouchMode="false"
Example:
<Button
android:id="#+id/bAjouterJalon"
android:layout_width="58dp"
android:layout_height="wrap_content"
android:background="#drawable/ic_action_add_jalon"
/>
*****************New changes*******************
CustomAdapter.class:
public class CustomAdapter extends SimpleCursorAdapter {
private Context mContext;
private Context appContext;
private int layout;
private Cursor cr;
private final LayoutInflater inflater;
public CustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.layout = layout;
this.mContext = context;
this.inflater = LayoutInflater.from(context);
this.cr = c;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(layout, null);
}
#Override
public void bindView(View view, final Context context, final Cursor cursor) {
super.bindView(view, context, cursor);
final Button ajouter = (Button)view.findViewById(R.id.bAjouterJalon);
final LinearLayout root_view = (LinearLayout)view.findViewById(R.id.item_root);
root_view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// List item Click detected
}
});
ajouter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent retour = new Intent(mContext,OngletJalonsNotes.class);
mContext.startActivity(retour);
}
});
}
}
List item xml:
<?xml version="1.0" encoding="utf-8"?><?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:id="#+id/item_root"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal"
android:padding="16dp">
<TextView
android:id="#+id/text_branche_cours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="English" />
<TextView
android:id="#+id/text_trait"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:text=" - " />
<TextView
android:id="#+id/text_designation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="150dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Reading" />
<Button
android:id="#+id/bAjouterJalon"
android:layout_width="58dp"
android:layout_height="wrap_content"
android:background="#drawable/ic_action_add_jalon" />
</LinearLayout>
In your list_item xml put android:descendantFocusability="blocksDescendants"
in the LinearLayout .

Expandablelist child on click listener not working

The onChildClickListener is not working on my expandable list.
The child layout is made from a custom LinearLayout by code and it adds some other child views into it from xml.
For the xml, all views are set to clickable=false.
The extended layout (The parent layout of the other views) is not set to anything clickable - wise since it's in code.
I believe I should set the custom linear layout to clickable false as well but since it's in code I'm not sure to do it
Here is my getChildView method:
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
String childText = (String) getChild(groupPosition, childPosition);
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = new ExtendedContentLinearLayout(context);
convertView).addViewToContentLayout(text);
ImageView leftImage = (ImageView) convertView.findViewById(R.id.child_left_image);
leftImage.setImageDrawable(context.getResources().getDrawable(R.drawable.folder_));
TextView childTextView = (TextView) convertView.findViewById(R.id.childTextView);
childTextView.setText(childText);
return convertView;
}
Extended LinearLayout:
public class ExtendedContentLinearLayout extends LinearLayout implements View.OnClickListener {
private View mTrigger;
private LinearLayout mContent;
public ExtendedContentLinearLayout(Context context) {
super(context);
mContent = new LinearLayout(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mTrigger = inflater.inflate(R.layout.nav_menu_board_expandable_children_outer, this, true);
this.addView(mContent);
mTrigger.setOnClickListener(this);
}
public ExtendedContentLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ExtendedContentLinearLayout(Context context, AttributeSet attrs, int defStyle) {
this(context, attrs);
}
#Override
public void onClick(View v) {
App.disableTouchEventForDefaultDuration();
toggleVisibility();
}
public void addViewToContentLayout(View view) {
mContent.addView(view);
toggleVisibility();
}
private void toggleVisibility() {
if (mContent.getVisibility() == View.GONE) {
mContent.setVisibility(View.VISIBLE);
} else {
mContent.setVisibility(View.GONE);
}
}
#Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mContent = null;
mTrigger = null;
}
}
The children views xml which I add to the custom layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:animateLayoutChanges="true"
android:layout_gravity="center_vertical"
android:clickable="false"
android:gravity="center_vertical"
android:id="#+id/triggerLayout"
android:background="#color/da_nav_drawer_dark"
android:layout_height="60dp">
<View
android:layout_width="4dp"
android:layout_height="match_parent"
android:background="#android:color/black" />
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginLeft="20dp"
android:clickable="false"
android:id="#+id/child_left_image"
android:layout_marginRight="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:textColor="#color/da_white"
android:id="#+id/childTextView" />
</LinearLayout>
Have you tried setOnChildClickListener listener on listview? Get more help Here, check this also.
I had to set the parent layout to clickable through code:
parent.setClickable(true);
that solved it.
thanks you for all the help

How to add footer button in horizontal listview

I want to add an button to the footer of listview, I'm using horizontal listview, The adapter class:
public ItemAdapter(Context context, int layout, Cursor c,String[] from, int[] to) {
super(context, layout, c, from, to,0);
this.layout = layout;
inflator= LayoutInflater.from(context);
}
I return the newView and bindView:
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = inflator.inflate(layout, parent, false);
TextView footer = (TextView)v.findViewById(R.id.bAdd);
return v;
}
#Override
public void bindView(View v, final Context context, Cursor c) {
final int id = c.getInt(c.getColumnIndex(ItemDb.ID));
final String name = c.getString(c.getColumnIndex(ItemDb.NAME));
}
and the activity class:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] from = new String[] { ItemDb.NAME, ItemDb.CONDITION, ItemDb.EMAIL};
int[] to = new int[] { R.id.etItemName, R.id.etItemCondition, R.id.etEmail };
itemAdapter = new ItemAdapter(this,R.layout.list_item, null, from, to);
this.setListAdapter(itemAdapter);
}
How can I add the button in listview footer?
Here is my 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" >
<ImageView
android:id="#+id/photo"
android:layout_width="85dp"
android:layout_height="85dp"
android:background="#drawable/photo_frame"
android:contentDescription="#string/imagedescription"
android:src="#drawable/ic_launcher" >
</ImageView>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/condition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/name"
android:layout_marginLeft="10dp"
android:text="#string/condition"
android:textColor="#000"
android:textSize="14sp" />
<TextView
android:id="#+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/condition"
android:layout_marginLeft="10dp"
android:text="#string/email"
android:textColor="#000"
android:textSize="14sp" />
</RelativeLayout>
</LinearLayout>
Override getCount() of listview adapter to return data.size()+1 elements;
In getView() insert something like this:
if (position == data.size()){
return getFooterView();
}
In getFooterView() inflate proper layout with button.

Checkbox in ListView changes when scrolling offscreen

I ran into an annoyance, and i couldn’t find an answer anywhere. So here I am.
I have a ListFragment in which I put data retrieved from a database. I’ve extended the SimpleCursorAdapter class and adapted it according to my needs.
My ListView items look like this:
<?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:gravity="center_vertical"
android:orientation="horizontal"
android:padding="5dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/txtDetailsListAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="Amount"
android:textSize="23dp" />
<TextView
android:id="#+id/txtDetailsListComment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="Comment"
android:textSize="15dp" />
</LinearLayout>
<CheckBox
android:id="#+id/chkDetailsDebt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
The hidden checkbox is displayed once I call startActionMode. The purpose of this checkbox is to be able to select mutliple list items and delete all of them at once.
So deleting works fine when there’s just a couple of entries and no need for scrolling. Yet once i am able to scroll and views are being recycled, the selected list items will change.
I’ve seen solutions but they use ArrayAdapters instead of an extended SimpleCursorAdapter.
Here is my custom SimpleCursorAdapter:
public class CustomDetailsCursorAdapter extends SimpleCursorAdapter {
private DetailsActivity activity;
#SuppressWarnings("deprecation")
public CustomDetailsCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, Activity activity) {
super(context, layout, c, from, to);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.details_custom_list_item, parent,
false);
bindView(v, context, cursor);
return v;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return super.getView(position, convertView, parent);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
view.setTag(cursor.getString(cursor.getColumnIndex(Persons.COLUMN_ID)));
TextView name = (TextView) view
.findViewById(R.id.txtDetailsListComment);
String comment = cursor.getString(cursor
.getColumnIndex(Debts.COLUMN_COMMENT));
if(comment.equals("")){
comment = "-";
}
name.setText(comment);
TextView debt = (TextView) view.findViewById(R.id.txtDetailsListAmount);
double amount = Double.parseDouble(cursor.getString(cursor
.getColumnIndex(Debts.COLUMN_AMOUNT)));
DecimalFormat df = new DecimalFormat("#.##");
debt.setText(activity.getCurrency()+df.format(amount));
boolean isMyDebt;
if (cursor.getInt((cursor.getColumnIndex(Debts.COLUMN_IS_MY_DEBT))) == 0) {
isMyDebt = false;
} else {
isMyDebt = true;
}
if (!isMyDebt) {
debt.setTextColor(activity.getResources().getColor(
R.color.LawnGreen));
} else {
debt.setTextColor(activity.getResources().getColor(
R.color.Red));
}
}
}

Categories

Resources