Search code examples
androidandroid-layoutandroid-listviewandroid-relativelayout

How to add click event on Relative Layout which located over ListView


I have problem with this case, how to detect event click on Relative Layout over ListView. Here my screenshot design:

enter image description here

This is my XML layout:

*<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <!-- HEADER -->
    <LinearLayout
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText
            android:id="@+id/edtsearch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/searchicon"
            android:drawableRight="@drawable/deleteicon"
            android:drawablePadding="3dp"
            android:layout_weight="3"
            android:hint="Item name"
            android:padding="3dp"
            android:windowSoftInputMode="stateVisible"
            android:background="@layout/round_border_image"/>
    </LinearLayout >
    <!-- FOOTER -->
    <LinearLayout
        android:id="@+id/footer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_alignParentBottom="true"
        android:background="#b5b5b5">
    </LinearLayout>
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/footer"
        android:layout_below="@+id/header"
        android:layout_weight="1" >
        <ListView
            android:id="@+id/lvcustomer"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:divider="#b5b5b5"
            android:dividerHeight="1dp"
            android:listSelector="@layout/custom_list_selector">
        </ListView>
    </FrameLayout>
    <RelativeLayout
        android:id="@+id/layoutadd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="15dp"
        android:layout_marginBottom="15dp"
        android:padding="8dp"
        android:background="#607D8B">
        <ImageView
            android:id="@+id/btnadd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/addicon"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"/>
    </RelativeLayout>

</RelativeLayout>*

And here my code:

**protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_customer);
    Global.ViewCustomer=this;
    final Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/pillgothic300mgregular-webfont.ttf");
    getSupportActionBar().setDisplayShowHomeEnabled(false);
    getSupportActionBar().setDisplayShowTitleEnabled(true);
    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#d0d4d0")));
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.backicon);
    getSupportActionBar().setTitle("Back");
    getSupportActionBar().setTitle((Html.fromHtml("<font color=\"#000\">Back</font>")));
    LayoutInflater mInflater = LayoutInflater.from(this);
    AdapterDb = new DataProcessor(this);
    final SQLiteDatabase dbdelete = AdapterDb.getWritableDatabase();
    final SQLiteDatabase dbinsert = AdapterDb.getWritableDatabase();
    final SQLiteDatabase db = AdapterDb.getReadableDatabase();
    try {
        AdapterDb.importIfNotExist();
    } catch (IOException e) {
        e.printStackTrace();
    }
    final ListView lvcustomer=(ListView)findViewById(R.id.lvcustomer);
    ArrayList<CustomerDetail_class> custlist = AdapterDb.getAllCustomer();
    lvcustomer.setAdapter(new ListCustAdapter(getBaseContext(), custlist));

    lvcustomer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // TODO Auto-generated method stub
            final CustomerDetail_class listcust = (CustomerDetail_class) parent.getItemAtPosition(position);
            LayoutInflater li = LayoutInflater.from(ViewCustomer.this);
            final View inputdialogcustom = li.inflate(R.layout.custom_user_default, null);
            AlertDialog.Builder alert = new AlertDialog.Builder(ViewCustomer.this);
            alert.setView(inputdialogcustom);
            TextView tvuser = (TextView) inputdialogcustom.findViewById(R.id.tvuser);
            tvuser.setTypeface(myTypeface);
            tvuser.setTextSize(TypedValue.COMPLEX_UNIT_PX, getBaseContext().getResources().getDimensionPixelSize(R.dimen.lbltitlelistviewitem));
            alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                }
            });
            alert.setNegativeButton("No", null);
            alert.show();
        }
    });
    RelativeLayout layoutadd=(RelativeLayout)findViewById(R.id.layoutadd);
    layoutadd.bringToFront();
    layoutadd.setClickable(true);
    layoutadd.setEnabled(true);
    layoutadd.setFocusable(true);
    layoutadd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getBaseContext(),"Halo",Toast.LENGTH_SHORT).show();
        }
    });
}**

already added event onclick on RelativeLayout, but it will raise listview click event, so do you have best solution for this case?


Solution

  • You can set clickable = true in the Relativelayout to make it work, From the android doc:

    android:clickable

    Defines whether this view reacts to click events.

    Must be a boolean value, either "true" or "false".

    This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

    This corresponds to the global attribute resource symbol clickable.