How to save the id of a relativeLayout?

Asked

Viewed 59 times

0

Hello, good afternoon. I’m new to Android programming and am unable to save the ID of a relativeLayout to be compared.

Example:

 <RelativeLayout
        android:id="@+id/idAmc"
        android:layout_width="match_parent"
        android:layout_height="65dp"
        android:layout_marginBottom="15dp"
        android:background="@drawable/shadow_grey"
        android:clickable="true"
        android:onClick="selecionaMenuTec">

        <ImageView
            android:id="@+id/ic_amc"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="15dp"
            app:srcCompat="@drawable/ic_amc" />

        <TextView
            android:id="@+id/amc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/amc"
            android:textAlignment="center" />

        <TextView
            android:id="@+id/empresaAmc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginStart="12dp"
            android:layout_toEndOf="@+id/ic_amc"
            android:text="ABC Empresa"
            android:textAppearance="@style/TextAppearance.AppCompat.Body2"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/mediaAmc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/empresaAmc"
            android:layout_marginLeft="180dp"
            android:text="@string/media" />

        <TextView
            android:id="@+id/mediaValorAmc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:text="0%"
            android:textSize="25sp"
            android:layout_alignParentBottom="true"
            android:layout_toEndOf="@+id/mediaAmc" />


    </RelativeLayout>

How do I take the ID of this relativeLayout and save to a variable for future comparison?

I have tried using findViewById() but certainly not.

I want to do something like: `

public class menuTec extends AppCompatActivity{

public void selecionaMenuTec(RelativeLayout view)
{
    RelativeLayout layoutMenuTec = view;



    if( view == R.id.idAmc){



    }

}

}

  • You want to receive this value: idAmc?

  • You’re in a Fragment?

  • I want to get this idAmc. The idea is to check this idAMc in an if. And call a Fragment. @Luc

  • I want to receive this idAmc. The idea is to check this idAMc in an if. And call a Fragment @Leonardodias

1 answer

0


Your code isn’t working because you’re comparing one View with a whole.

The right thing would be to take the Id of view the method receives as a parameter, thus:

public void selecionaMenuTec(View view){
    if (view.getId() == R.id.idAmc) {
        // faça alguma coisa
    }  
}
  • Got it, I made a relativeLayout clicavél, when the user click on it will open a Fragment. How do I take the id of this layout and buy it with others? In my function selectMenuTec() I want to receive this layout, then take the id of this layout and save in a variable to be purchased with the other ids, to see which one to open. @Luc

  • I edited the answer

  • This I did directly in XML android:onClick="selectMenuTec". My problem is I want to save the id of this layout. I asked my question. @Luc

  • Change the Relativelayout parameter to View.

  • Edited according to your question, since the previous answer was not related to what you want.

  • Thank you very much @Luc

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.