22

I have a RelativeLayout with a few TextView as children

<RelativeLayout
    android:id="@+id/shift_parent_name"
    android:layout_width="fill_parent"
    android:layout_weight="0.25"
    >
    <TextView
        android:id="@+id/shift_parent_nametitle"
        android:text="@string/shift_parent_nametitle"
        style="@style/header_text"
        />
    <TextView
        android:id="@+id/shift_parent_namefield"
        android:layout_alignParentRight="true"
        android:layout_below="@id/shift_parent_nametitle"
        style="@style/wrap"
        />

How do I go about using the RelativeLayout as a button to react to a click event if any part of the area is pressed?

Onik
  • 19,396
  • 14
  • 68
  • 91
skooter
  • 286
  • 1
  • 2
  • 11
  • If setting the RelativeLayout to clickable and focusable doesn't help, it may be because of neighbouring views. Try `shift_parent_name.bringToFront()` – AliAvci Mar 16 '20 at 18:00

6 Answers6

24

Just add a OnClickListener to your RelativeLayout

RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200
22

I have a RelativeLayout called "RelativeMain1". This is how i make it start Activity

RelativeLayout relativeclic1 =(RelativeLayout)findViewById(R.id.RelativeMain1);
         relativeclic1.setOnClickListener(new View.OnClickListener(){
             @Override
             public void onClick(View v){
                 startActivityForResult(new Intent(A_My_Galaxy.this,C_Student_Book_Planet.class), 0);
             }
         });

After you add the onClickListener to your layout, it should work.

Gimsi
  • 321
  • 2
  • 4
7

Add a "OnClickListener" to your RelativeLayout.

Note: Don't forget to add android:clickable="true" to your RelativeLayout.

Dhrumil Shah - dhuma1981
  • 15,166
  • 6
  • 31
  • 39
7
  1. Give your RelativeLayout an ID, like you typed shift_parent_name

  2. Set your RelativeLayout XML to android:clickable="true"

    Your final xml will be look like this:

    <RelativeLayout
    android:id="@+id/shift_parent_name"
    android:layout_width="fill_parent"
    android:layout_weight="0.25"
    android:clickable="true">
    
  3. then add the code in your onCreate method :

    RelativeLayout relative1 = (RelativeLayout) findViewById(R.id.shift_parent_name);
      relative1.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            startActivity(new Intent(Main.this, About.class) );
        }
    });
    

Make sure to change your activity names, Main.this and About.class.

The main activity called Main.java and the second is About.java

Community
  • 1
  • 1
Ihab Shoully
  • 2,311
  • 1
  • 20
  • 22
1
RelativeLayout rl=(RelativeLayout)findViewById(R.id.RelativeMain1);
         relativeclic1.setOnClickListener(new View.OnClickListener(){
             @Override
             public void onClick(View v){

             }
         });
0

You can simply just add this android:onClick="onClick"

Derek Dawson
  • 502
  • 5
  • 14