1

I'm trying to use Custom view that extends RelativeLayout and named CardView. A added some attr and created XML file for them, everything work correct, except onClick. So, at first I've created a special class for my view:

class CardView(context: Context, attrs: AttributeSet) : RelativeLayout(context, attrs) {

init {
    inflate(context, R.layout.card_view, this)
    val imageView: ImageView = findViewById(R.id.image)
    val textView: TextView = findViewById(R.id.caption)
    val line: ImageView = findViewById(R.id.line)

    val attributes = context.obtainStyledAttributes(attrs, R.styleable.CardView)
    imageView.setImageResource(attributes.getResourceId(R.styleable.CardView_image, 0))
    textView.text = attributes.getString(R.styleable.CardView_text)
    line.setBackgroundColor(attributes.getColor(R.styleable.CardView_lineColor, 0))
    attributes.recycle()
} }

I aslo have a xml file for my View:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="@dimen/cardSize"
android:layout_height="@dimen/cardSize"
android:background="@drawable/card_color_selector"
android:clickable="true">

<ImageView
    android:id="@+id/image"
    android:layout_width="60sp"
    android:layout_height="60sp"
    android:layout_centerHorizontal="true"
    android:layout_margin="3sp"
    tools:src="@color/colorPrimary" />

<TextView
    android:id="@+id/caption"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/text_normal"
    android:textSize="15dp"
    android:layout_alignParentBottom="true"
    android:layout_margin="10sp"
    android:gravity="center"
    android:textColor="@color/darkGray"
    tools:text="Caption" />

<ImageView
    android:id="@+id/line"
    android:layout_width="match_parent"
    android:visibility="visible"
    android:layout_height="5dp"
    android:layout_alignParentBottom="true"
    android:background="@color/colorPrimary" />

</RelativeLayout>

And finally I've added my customView in activity layout with link to onClick method in view model like here:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

    <variable
        name="viewModel"
        type="com.presentation.screen.HomeViewModel" />
</data>

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<com.project.presentation.utils.CardView
        android:id="@+id/card"
        android:layout_width="@dimen/cardSize"
        android:layout_height="@dimen/cardSize"
        android:clickable="true"
        android:onClick="@{() -> viewModel.onClick()}"
        app:image="@drawable/icon"
        app:lineColor="@color/colorPrimary"
        app:text="@string/text" />    

</android.support.constraint.ConstraintLayout>
</layout>

So everything is good, colors are changed as i'd written in card_color_selector.xml, but method onClick in my ViewModel has never called. Oh, by the way my view model is here, now I just want to logged all the clicks:

class HomeViewModel : BaseViewModel<Router>() {

    fun onClick() {
        Log.e("myLog", "HomeViewModel onClick")
    }
}

I've tried everything! Please help me to figure out what I'm doing wrong.

Ahmed Hegazy
  • 12,395
  • 5
  • 41
  • 64
lst11
  • 31
  • 1
  • 4

4 Answers4

3

I had a same problem.

Check your customview's clickable attribute!

If your child view in custom view is preventing touch event, your parent's

android:onClick will be ignored.

MJ Studio
  • 3,947
  • 1
  • 26
  • 37
0

In your

fun onClick() {
    Log.e("myLog", "HomeViewModel onClick")
} 

pass View In XML section for

onClick="method" you have to set parameter as View class in your method.

You can see this

Sergei Bubenshchikov
  • 5,275
  • 3
  • 33
  • 60
Tariqul Islam
  • 680
  • 6
  • 14
0

Change you're onClick method like thisTry like this

fun onClick(view: View) {
        Log.d("myLog", "HomeViewModel onClick")
    }
Aabauser
  • 533
  • 1
  • 4
  • 17
  • Unfortunately, I have already tryed this way, it doesn't change anything :( – lst11 Feb 05 '19 at 10:24
  • If you don't mind can I ask you one thing why you are using `Log.e` instead of Log.d or others?? Did you properly checked the log?? – Aabauser Feb 05 '19 at 10:42
  • yes, because there are another logs in my app, so i haven't any problem with them. – lst11 Feb 05 '19 at 10:46
  • that looks like i have to set onClickListener to my custom view, but in mvvm architecture i can't use anything like `findViewById`. That's why i'm truing to find another solution. – lst11 Feb 05 '19 at 10:49
0

I don't have enough reputation to comment, so can you try

    android:onClick="@{(v) -> viewModel.onClick(v)}"

and for the function

     fun onClick(view:View) {
    Log.e("myLog", "HomeViewModel onClick")
     }
Xuzan
  • 335
  • 1
  • 11
  • Yes, I've already tryed and i have no idea why doesn't it work. Could the problem be connected with attribute android:clickable="true" for layout in my Custom View? – lst11 Feb 05 '19 at 11:26
  • May be, only way to know is to test it. remove both clickable true attribute and test it. and make sure you have set value of homeViewModel. – Xuzan Feb 05 '19 at 11:47