简体   繁体   中英

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?

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.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM