I am new with coding Java and xml for android applications and I wanted to know how I start/open a new activity when clicking on something. In this case I am using a relative layout to keep a image and text together as one object. What I want to do is when you click on it it will start/open the new activity. How do I do this? Could someone tell me step by step since I am quite new to this.
-
1It's all here http://developer.android.com/training/basics/firstapp/starting-activity.html#BuildIntent the official docs are usually a good place to start if you have tried nothing yet – ci_ May 31 '15 at 20:24
-
possible duplicate of [How to start new activity on button click](http://stackoverflow.com/questions/4186021/how-to-start-new-activity-on-button-click) – ci_ May 31 '15 at 20:30
3 Answers
First of all, if you want your layout to act (RelativeLayout
) like a button (do not handle onClick
on layout child components) firstly set in your xml layout file RelativeLayout
attribute
android:clickable="true"
Or you can do this directly in your code (in onCreate
method)
relativeLayout.setClickable(true);
Than you need to set onClickListener
for your layout.
You can do this simply by creating anonymous class
relativeLayout.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent startActivityIntent = new Intent(getApplicationContext(),YourDesiredActivity.class);
startActivity(startActivityIntent);
}
}
UPDATE
Layout is defined in xml file, of course in Android you can do this in code ,but it is better to use xml file.
In your IDEA you have folder res->layout
here you should place your layout files. For example layout with name `
relative_root_layout.xml
<xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative_layout"
android:layout_height="wrap_content" android:layout_width="wrap_content">
<ImageView
android:id="@+id/image_view">
android:layout_width="wrap_content"
android:src="@drawable/icon"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
ImageView>
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_toRightOf="@+id/image_view"
android:text="Relative layout">
TextView>
RelativeLayout>
But in case you have only text and image it is better to use
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@android:drawable/btn_image"
android:text="Button with Image"
android:gravity="left"
android:drawablePadding="10dp">
Button>
How you can access your widgets ?
This is very basic thing you have to know if you are developing for android, this is essential part. Please read documentation, read books, watch tutorial or whatever.
In short you need to inflate layout in activity onCreate()
method
RelativeLayout mRelativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.relative_root_layout);
mRelativeLayout = (RelativeLayout) findViewById(R.id.relative_layout);
mRelativeLayout.setOnClickListener(.....)
}
But again this very basic things you must know.

- 4,499
- 4
- 38
- 89
-
This is great thank you so much, but how do I define which relative layout it should listen to? Where do I define the relative layout ID? And also, when I put it into my java code of my current activity, it is giving me the following errors: "Cannot resolve symbol 'relative layout'" "Cannot resolve symbol 'intent'" "Cannot resolve symbol 'view'" and then also the same for the activity I am trying to open. I apolagise for this I am quite new to all of this. – Lion May 31 '15 at 22:57
-
I have added the code into my Java file for my activity. On creation of a project I get a public class extend "ActionBarActivity". I am not really sure what I am supposed to start with this since it has a line through it. Do I need to change anything? I also noticed that I already have a void bundle saveinstance state. Everything is in the screenshot below. This is what I have now: http://i.gyazo.com/49d7447e2e799cac7a928971e054c233.png @CROSP – Lion Jun 01 '15 at 08:24
-
Upload your full code somewhere and give me a link. You don't have to override onCreate method twice . You have to declare mRelativeLayout as your class member.You haven't created relative_layout_file in your res folder this is about your screenshot . – CROSP Jun 01 '15 at 09:03
-
http://pastebin.com/ygFDVZCE This is what I have by default with nothing changed. My main activity is called activity_selection_main and the one I want to go to is activity_selection_townhall. I want to have multiple relative layouts as buttons in a single activity leading to different other activities each. – Lion Jun 01 '15 at 10:42
-
Have a look http://pastebin.com/tLf9hkh8 this is your activity code. And this code for layout xml file activity_selection_main.xml in your case http://pastebin.com/QQRzH7B1. But if you want to have multiple items (clickable relative layouts) you must use ListView or RecyclerView with adapter. If my answer helpful please accept it. – CROSP Jun 01 '15 at 10:59
-
Hello, I was wondering if it was possible for us to get in contact privately, via e-mail or Skype or something if that is okay with you. It would be great if you could help a bit more. – Lion Jun 17 '15 at 06:36
You could set onClickListener
for any of your views.
ImageView image = (ImageView) findViewById(R.id.image);
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Youractivity.this, Moveactivity.class));
}
});

- 1,699
- 12
- 21
Starting a new activity is done by creating an Intent
and then calling startActivity
, e.g.
Intent intent = new Intent(context, AnotherActivity.class);
context.startActivity(intent);
You can wrap this code in an OnClickListener
as other answerers already suggested.
A second option is to add an android:onClick
attribute to your RelativeLayout
<RelativeLayout ...
android:onClick="clickMe">
<ImageView .../>
<TextView .../>
</RelativeLayout>
and in your activity
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void clickMe(View notused) {
Intent intent = new Intent(this, AnotherActivity.class);
this.startActivity(intent);
}
}
See startactivity for a complete example.

- 72,253
- 8
- 102
- 198
-
Where do I put this code? Can you please help me with that. Sorry I am really bad at this and very new. – Lion May 31 '15 at 21:09