0

I have a custom Listview where it contains image & text and implemented the OnItemClickListener for list which should work only for image not for text. OnItemClick is working fine for image but there is a Fatal Exception when i click on text. Additionally image will be visible in list if it exists else it will be hide.

Tried with android:focusable="false", android:clickable="false" but still i am getting the below exception

java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable.getBitmap()' on a null object reference

OnItemClick:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


    final ImageView imageView1 = (ImageView) view.findViewById(R.id.imageList);
    final GlideBitmapDrawable bitmapDrawable = (GlideBitmapDrawable) imageView1.getDrawable();
    final Bitmap yourBitmap = bitmapDrawable.getBitmap();
    Dialog builder = new Dialog(this);
    builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
    builder.getWindow().setBackgroundDrawable(
            new ColorDrawable(android.graphics.Color.TRANSPARENT));
    builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialogInterface) {
            //nothing;
        }
    });

    ImageView imageView = new ImageView(this);
    imageView.setImageBitmap(yourBitmap);



    builder.addContentView(imageView, new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));
    builder.show();


}

How can i implement click only on image?

Yaron Schwimmer
  • 5,327
  • 5
  • 36
  • 59
user2269164
  • 1,095
  • 2
  • 15
  • 31
  • Implement custom adapter extending you class with BaseAdapter and in its getView() fuction u can implement the onClicklistener on your imageview – Adeel Turk May 09 '16 at 10:13

2 Answers2

0

In your custom layout xml, set

android:descendantFocusability="blocksDescendants"

to your root layout.

Edit Check my Custom ListView.

listitem.xml

<?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:gravity="center"

android:descendantFocusability="blocksDescendants"
android:layout_height="60dp">

<ImageView
    android:id="@+id/m_imageview"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:src="@drawable/ic_launcher"
    android:layout_height="50dp" />

<TextView
    android:id="@+id/m_textview"
    android:layout_width="0dp"
    android:layout_weight="3"
    android:layout_height="50dp"
    android:gravity="center|center"
    android:textColor="#ffffff"
    android:text="hello"/>

<Button
    android:id="@+id/m_buttonview"
    android:layout_width="0dp"
    android:layout_weight="2"
    android:text="OK"
    android:layout_height="50dp" />

</LinearLayout>

activity_main.xml

<RelativeLayout 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" >

<ListView
    android:id="@+id/lv_items"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector"
    android:dividerHeight="2dp"
    android:divider="#ffffff"
    tools:context=".MainActivity" />


</RelativeLayout>

CustomListViewAdapter.java

public class CustomListViewAdapter extends ArrayAdapter<RowItem> {

Context context;

public CustomListViewAdapter(Context context, int resourceId,
                             List<RowItem> items) {
    super(context, resourceId, items);
    this.context = context;
}

/*private view holder class*/
private class ViewHolder {
    ImageView m_pic;
    TextView m_title;
    Button m_btn;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    RowItem rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
    {
        convertView = mInflater.inflate(R.layout.listitem, null);
        holder = new ViewHolder();
        holder.m_title = (TextView) convertView.findViewById(R.id.m_textview);
        holder.m_btn = (Button)convertView.findViewById(R.id.m_buttonview);
        holder.m_pic = (ImageView) convertView.findViewById(R.id.m_imageview);

        convertView.setTag(holder);
    }
    else
        holder = (ViewHolder) convertView.getTag();

    holder.m_title.setText(rowItem.getTxt());

    holder.m_pic.setImageResource(rowItem.getImage());

   holder.m_btn.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {

           Toast.makeText(context, position + " clicked" , Toast.LENGTH_LONG).show();
       }
   });

    return convertView;
}
}

RowItem.java

public class RowItem {

private int image;
private String txt;

public RowItem(int imageview , String textview)
{
    this.image = imageview;
    this.txt = textview;
}


public int getImage() {
    return image;
}

public String getTxt() {
    return txt;
}

public void setImage(int image) {
    this.image = image;
}

public void setTxt(String txt) {
    this.txt = txt;
}
}

MainActivity.java

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

String m_txt[] = {"one" , "two" , "three" , "four", "five"};
int m_img [] = {R.drawable.ic_launcher ,
        R.drawable.ic_launcher ,
        R.drawable.ic_launcher ,
        R.drawable.ic_launcher ,
        R.drawable.ic_launcher};

ListView m_list;
List<RowItem> rowItems;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    m_list = (ListView)findViewById(R.id.lv_items);

    rowItems = new ArrayList<RowItem>();
    for (int i = 0; i < m_txt.length; i++) {
        RowItem item = new RowItem(m_img[i],m_txt[i]);
        rowItems.add(item);
    }

    CustomListViewAdapter adapter = new CustomListViewAdapter(this,
            R.layout.listitem, rowItems);
    m_list.setAdapter(adapter);
    m_list.setOnItemClickListener(MainActivity.this);

}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
                        long id) {
    Toast toast = Toast.makeText(getApplicationContext(),
            "Item " + (position) + ": " + rowItems.get(position).getTxt(),
            Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    toast.show();

}
}

This should helps you.

Happy coding.!!!

Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
0

use a recyclerview. a recylerview handles every list item as a view, it can hold clicklisteners for every view it holds and that view can hold clicklisteners for every view its holding.

locomain
  • 185
  • 1
  • 15