HighLife
HighLife

Reputation: 4344

Turning an Entire Relative Layout Into a Button

My application has an intro page made up of some text and image elements in a relative layout. I would like to be able to click any part of the screen and have it go to the next activity. Is it possible to use a entire relative layout as a button? If so how would you do this?

Upvotes: 3

Views: 1821

Answers (3)

Shumon Saha
Shumon Saha

Reputation: 1445

Add android:onClick="myFunction" in the RelativeLayout of the XML file and make the following function in the corresponding Activity file:

public void myFunction(View view)
{
   ...
}

I think you will have to add android:onClick="myFunction" for all the nested XML tags too which are nested inside the main RelativeLayout.

Upvotes: 0

David Snabel-Caunt
David Snabel-Caunt

Reputation: 58371

You can grab the root view as follows and add a click listener to it:

findViewById(android.R.id.content).setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         //make your call to startActivity();
     }       
});

This should require less maintenance than retrieving a specific layout.

Upvotes: 1

Christopher Souvey
Christopher Souvey

Reputation: 2910

You can add android:clickable="true" to the XML for your RelativeLayout and use a standard OnClickListener as you would for a button.

Depending on what you're trying to do (perhaps touching anywhere to dismiss a screen?), you could also look into extending onTouchEvent(MotionEvent event) in your Activity, which would pick up any touches in the entire activity that were not responded to by views.

Upvotes: 2

Related Questions