0

I have a layout which contains an edittext and two button for increment and decrement of the value in editext. Now I am using include to use this button in my activity as I am having multiple use of that.

eg :-

 <include
    android:id="@+id/editTextOne"
    layout="@layout/include_edittext_with_buttons"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1" />

  <include
    android:id="@+id/editTextTwo"
    layout="@layout/include_edittext_with_buttons"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1" />

I can get the onClick events of my increment and decrement buttons but I don't understand how will i catch that the onClick of the particular include has been clicked. ?

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <androidx.appcompat.widget.AppCompatEditText
        android:id="@+id/editText"
        android:layout_width="@dimen/_120sdp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.AppCompatImageButton
        android:id="@+id/btnPlus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/_10sdp"
        android:onClick="onClick"
        app:layout_constraintBottom_toBottomOf="@id/editText"
        app:layout_constraintEnd_toEndOf="@id/editText"
        app:layout_constraintTop_toTopOf="@id/editText" />

    <androidx.appcompat.widget.AppCompatImageButton
        android:id="@+id/btnMinus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/_10sdp"
        android:onClick="onClick"
        app:layout_constraintBottom_toBottomOf="@id/editText"
        app:layout_constraintStart_toStartOf="@id/editText"
        app:layout_constraintTop_toTopOf="@id/editText" />

</androidx.constraintlayout.widget.ConstraintLayout>

NOTE :- I get that working using binding. editTextOne.btnPlus.setOnClickListener { Toast.makeText(this, "Plus", Toast.LENGTH_SHORT).show() } and similar for the other's but what i want is I can write the plus and minus click listener once and then they update the values of the edittext in which they are included only.

Not sure if that would be possible any better suggestions ?

WhiteSpidy.
  • 1,107
  • 1
  • 6
  • 28
  • Can you share include_edittext_with_buttons layout file? – Ekrem Feb 21 '21 at 09:06
  • Don't use include Id to attach events on buttons, use button's or layout Ids to attach onClickListener. – Hafiza Feb 21 '21 at 09:09
  • @Ekrem added code – WhiteSpidy. Feb 21 '21 at 09:11
  • 1
    @hafiza am already using button id's that's what causing problem as I have same layout included multiple times if i click on first layout's plus button then also it will trigger the second layouts plus button as they are similar – WhiteSpidy. Feb 21 '21 at 09:13
  • What I would do in your case is create a function that gets a view (of the specific one you are including) that will init it's functionality. That way, each click of a button will belong only to the relevant children of the specific layout and will save lots of coding (unless you need specific behavior for each layout you are including) – Dan Baruch Feb 21 '21 at 09:27

2 Answers2

2

there is a solution for solving this problem. first, assign an id for each of the includes.

 <include
       android:id="@+id/inc1"
       layout="@layout/include_layout" 
        ...
  />


  <include
       android:id="@+id/inc2"
       layout="@layout/include_layout"
        ...
  />

then add oclick in view's xml code.

//include_layout.xml"

 <androidx.appcompat.widget.AppCompatImageButton
    android:id="@+id/ib"
    android:onClick="onClick"
    ...
   />
    

after implementing onClick function in your code you can specify which view exactly clicked by checking its parent id.

    

    public void onClick(View view) {

        if (view.getId() == R.id.ib) {

            int parent_id = ((View) view.getParent()).getId();

            if (parent_id == R.id.inc1) {
              //clicked view in first include          
            } 
            else if (parent_id == R.id.inc2) {
             //clicked view in second include
            }
        }
        
        
    }
behrad
  • 1,228
  • 14
  • 21
1

You can get views like that.

    // First View
    val includedView1 = findViewById<ConstraintLayout>(R.id.editTextOne)

    val btnPlus1 = includedView1.findViewById<Button>(R.id.btnPlus)
    val btnMinus1 = includedView1.findViewById<Button>(R.id.btnMinus)

    // Second View
    val includedView2 = findViewById<ConstraintLayout>(R.id.editTextTwo)

    val btnPlus2 = includedView2.findViewById<Button>(R.id.btnPlus)
    val btnMinus2 = includedView2.findViewById<Button>(R.id.btnMinus)
Ekrem
  • 395
  • 3
  • 12
  • Yap I know I can get using this but that would be a lot of code to do if I have like four to five edittext although the use of both the buttons are same which is to update the edittext present in their own layout. – WhiteSpidy. Feb 21 '21 at 09:23
  • Maybe you can use custom views. https://stackoverflow.com/a/17413018/7937174 – Ekrem Feb 21 '21 at 09:26