Question

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?

Was it helpful?

Solution

Just add a OnClickListener to your RelativeLayout

OTHER TIPS

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.

Add a "OnClickListener" to your RelativeLayout.

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

  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

RelativeLayout rl=(RelativeLayout)findViewById(R.id.RelativeMain1);
         relativeclic1.setOnClickListener(new View.OnClickListener(){
             @Override
             public void onClick(View v){

             }
         });

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top