-1

I have problem with some weird behavior with layouts and its ids.

Let's say I have main layout where I use 3 times some other layout by including it in xml layout.

When I get id of the button which is inside included layout it is the same for all included layouts. Is it correct behavior? I wanted to use it in OnClickListener to distinguish clicked button.

layout_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="wrap_content" >

    <Button
        android:id="@+id/btnDoSomething"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Do something" />

[... some other views ...]

</RelativeLayout>

activity_main.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"
    android:orientation="vertical">

    <include android:id="@+id/layout1"
        layout="@layout/layout_row" />

    <include android:id="@+id/layout2"
        layout="@layout/layout_row" />

    <include android:id="@+id/layout3"
        layout="@layout/layout_row" />

    [... some other views ...]

</LinearLayout>

MainActivity

import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.AnkoLogger
import org.jetbrains.anko.info

class MainActivity : AppCompatActivity(), AnkoLogger {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        info("${layout1.btnDoSomething.id}")
        info("${layout2.btnDoSomething.id}")
        info("${layout3.btnDoSomething.id}")
        //it logs the same id three times!
    }
}
ColdFire
  • 6,764
  • 6
  • 35
  • 51
user3626048
  • 706
  • 4
  • 19
  • 52

2 Answers2

1

You've used an include statement to include the same layout_row.xml layout file three times only changing the ID of the included layout.

They are exact copies of each other, so the android:id="@+id/btnDoSomething" entry returns the same ID each time - the ID is defined in strings.xml.

Benjamin Scholtz
  • 823
  • 6
  • 15
1

This behavior is correct because the include uses the same layout with same views ID. If you want to distinguish the buttons click you can create a distinct OnClickListener for each button.

layout1.findViewById(R.id.btnDoSomething).setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View view) {
      // button of layout1 click
   }
});

layout2.findViewById(R.id.btnDoSomething).setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View view) {
      // button of layout2 click
   }
});

layout3.findViewById(R.id.btnDoSomething).setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View view) {
      // button of layout3 click
   }
});
Marc Estrada
  • 1,657
  • 10
  • 20
  • I know I can do that but listeners are going to be almost the same. I will have to find some other way. Thank you for your answer. – user3626048 May 16 '18 at 17:27
  • 2
    You could possibly get the ID of the parent view, using the following guidelines: [Android get layout parent id](https://stackoverflow.com/questions/22759352/android-get-layout-parent-id). That way you can know which button is being pressed. Alternatively a recyclerview for the rows? – Benjamin Scholtz May 16 '18 at 17:58
  • That's a good idea. Anyway, let me study this case at home in order to find an elegant and efficient solution – Marc Estrada May 16 '18 at 18:05
  • @YodaScholtz using parent id is a good idea! RecyclerView is a little bit excessible in this case – user3626048 May 16 '18 at 18:06