0

This is my 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"
    android:gravity="top"
    android:rotation="180">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center"
        android:id="@+id/cols">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/col1">
            <Button .../>
            <Button .../>
            <Button .../>
            <Button .../>
            <Button .../>
            <Button .../>
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/col2">
            <Button .../>
            <Button .../>
            <Button .../>
            <Button .../>
            <Button .../>
            <Button .../>
        </LinearLayout>

    </LinearLayout>
</LinearLayout>

I want click listener on the all buttons and I want to have the position when I click on a button. I tried several things without success.Indeed, I can't to have the clicked button position.

  • 1
    Which position do you need? The one of the clicked `Button` or the one of the point of the touch screen where you clicked (disregarding the specific view element). – deHaar Jul 19 '18 at 14:37
  • The one of the clicked Button – Michiel Sallaets Jul 19 '18 at 19:59
  • Please see [this question](https://stackoverflow.com/questions/17672891/getlocationonscreen-vs-getlocationinwindow) about `getLocationOnScreen()` and `getLocationInWindow()`, which are methods of the class `View` in Android – deHaar Jul 20 '18 at 06:34
  • I don't understand this example. I don't see the relationship with what I want to do ... – Michiel Sallaets Jul 20 '18 at 09:25
  • It gives you the coordinates of the view element (in your case `Button`) relative to the display or parent view. If this is not what you wanted, please describe your idea of *position* used in your question. – deHaar Jul 20 '18 at 09:28
  • No, it is not that. I want to put a "setOnClickListener (new View.OnClickListener () ..." on each button and when I press a button I want it to return an int indicating that position (for exemple first button press will return to me an int equal to 0) – Michiel Sallaets Jul 20 '18 at 09:33
  • Oh, its position **inside a datastructure**, the index? – deHaar Jul 20 '18 at 09:34
  • More simple it can also work with my code. When I click on a column I must have the position of the column (linearlayout). – Michiel Sallaets Jul 20 '18 at 09:41
  • OK, in that case I would store the `Button`s in a `List – deHaar Jul 20 '18 at 09:41
  • Ok then, see the answer by @SusmitAgrawal – deHaar Jul 20 '18 at 09:42
  • I will already do that it's not work. So I change my code. It's possible to have the position of the linearlayout col1, col2, ... When I click on ? – Michiel Sallaets Jul 20 '18 at 09:43
  • Please show some Java code you have tried so far. – deHaar Jul 20 '18 at 09:47
  • @deHaar and this method not work because I have this when I click: java.lang.ArrayIndexOutOfBoundsException: length=7; index=7 – Michiel Sallaets Jul 20 '18 at 09:57
  • Maybe due to using `i` instead of `j` in `play(player, i)`? Why are you iterating with `j` when using `i` as index? If the length of an array or list is 7 then its last index is 6 --> `i = 7` --> `IndexOutOfBoundsException` – deHaar Jul 20 '18 at 10:00
  • I try with j. Now, the apps not crash but j always is 6. It's not good. – Michiel Sallaets Jul 20 '18 at 10:03
  • It makes the loop until the end so j = 6. – Michiel Sallaets Jul 20 '18 at 10:04
  • I find it. I will change the button to image and it's works ! Thanks. – Michiel Sallaets Jul 20 '18 at 12:45

1 Answers1

0

Try this logic:

In your activity, do this:

LinearLayout layout = findViewById(R.id.col1);

then, in every buttons's onClick:

for(int i=0; i<layout.getChildCount(); i++) {
    if(layout.getChildAt(i).equals(thisButton))//Replace thisButton with the variable name
    {
        //do something
        // i  represents the button's position in its immediate parent.
        break;
    }
}            

The same logic can also apply for the LinearLayout col2

Susmit Agrawal
  • 3,649
  • 2
  • 13
  • 29
  • Oh, I have to put as much name as button? So I make the code multiply, I think it's not good at all. – Michiel Sallaets Jul 20 '18 at 09:20
  • You can use the same `for loop`, without the `if`, to initialise the buttons as well, provided you use an array of buttons. The statement would be something like: `buttons[i] = layout.getChildAt(i);` – Susmit Agrawal Jul 20 '18 at 09:36