Android Studio My Button on the second Activity doesn't Work - java

I think it a very easy Question. I am new to Android Studio and I could not find an answer.
I have an activity that switches per Button to another activity. However, on my second activity, my simple Button just to change the Textview which doesn't work.
Layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StartActivity">
<TextView
android:id="#+id/twText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="127dp"
android:layout_marginLeft="127dp"
android:layout_marginTop="88dp"
android:layout_marginEnd="127dp"
android:layout_marginRight="127dp"
android:layout_marginBottom="624dp"
android:text="Ihr Passwort war Richtig!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btnChecke"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="134dp"
android:layout_marginLeft="134dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="189dp"
android:layout_marginRight="189dp"
android:layout_marginBottom="371dp"
android:text="Check"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
onCreate method in my second activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
tw = findViewById(R.id.twText);
btnCheck = findViewById(R.id.btnChecke);
btnCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tw.setText("work");
}
});
}
And this is how I am starting the second activity (onCreate method from my first activity).
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
tw = findViewById(R.id.twText);
Button btnCheck = findViewById(R.id.btnChecke);
btnCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView("Work"R.layout.activity_main);
}
});
}
I have checked the variable names in the layout and looks like everything is fine.
Thank you in advance.

This answer is based on the problem which I found before the latest edit from the OP. Here is the version of the edit where I found the problem.
First of all, something is very wrong about the onClickListener of your button. You are setting the content view again, instead of starting your second activity. It looks like, you are just setting the content view of your StartActivity which actually does not launch the MainActivity that you are trying to start.
In order to start a new activity, you need to add the following in your button onClickListener.
btnCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// setContentView(R.layout.activity_main); // This is not the way for starting another activity
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
});
Once your MainActivity has started, you should be able to change the text with no problem.

Bundle bundle = getIntent().getExtras()
if(bundle.getString("changeTextView")
TextView .... = (TextView) ....setText();
Hi. If I good understand, you should use Intent with "putExtra()".
Exmple:intent.putExtras("changeTextView");
After that in your onCreate() method you can put:

Related

Show custom progress bar in Android Studio

I know this is common to ask, but yet I still don't know how to call the XML file. I'm just confused about how can I implement the proper calling progress bar when going to another activity. Yet there are lots of resources on site but I think this is different. All I want is When I click the button from first Activity I want to call the progress bar which separately designs as XML and then it goes to the second activity, whether it will set Visible etc. It is possible?
Reference spinKit: Github
When clicking the button in first activity it will pop the spin XML and then dismiss when second activity is already loaded.
MainActivity
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button clickButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clickButton =(Button) findViewById(R.id.buttons);
clickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Activity2.class);
startActivity(intent);
}
});
}
}
ActivityMain
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Click"
android:textSize="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textColor="#color/white"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Spinkit class
import androidx.appcompat.app.AppCompatActivity;
public class Spinkit extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loadingspin);
ProgressBar progressBar = (ProgressBar)findViewById(R.id.spin_kit);
Sprite cubeGrid = new CubeGrid();
progressBar.setIndeterminateDrawable(cubeGrid);
}
}
Spinkit xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="#color/orange"
android:gravity="center"
android:orientation="vertical">
<com.github.ybq.android.spinkit.SpinKitView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/spin_kit"
style="#style/SpinKitView.Large.CubeGrid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:SpinKit_Color="#color/white"
tools:ignore="MissingConstraints" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LOADING"
android:textStyle="bold"
android:textSize="40sp"
android:gravity="center"
android:shadowColor="#color/black"
android:textColor="#color/white"/>
</LinearLayout>
Actually you have two activity. And now you want to show animation when changing activity. A professional Android App Developer chooses to use fragment since fragment makes thing easier. Since you are using activity hence I will include information of activity.
You directly can't show that animation. You must add an activity (you can directly code even if you want you can use layout either whatever you prefer).
Someone had mentioned in the comment that "it takes milisec to change activity". But if you want to delay than you can do that.
At first create an activity as another person said. Design it however you want. Then use the following code to change activity.
In MainActivity :
Intent intent = new Intent(this, SplashScreenActivity.class);
startActivity(intent);
//If you don't want user to back to the page by clicking back button then use finish();
finish();
In SplashScreenActivity :
//Use SpinKit however they have introduced https://github.com/ybq/Android-SpinKit#usage
ProgressBar progressBar = (ProgressBar)findViewById(R.id.progress);
Sprite doubleBounce = new DoubleBounce();
progressBar.setIndeterminateDrawable(doubleBounce);
new Handler().postDelayed(() ->{
Intent intent = new Intent(SplashActivity.this,HomeActivity.class);
startActivity(intent);
},1000);
//1000 is millisecond if you want to delay 10 sec than write 10000 instead of 1000
Here's a related git repo
If you want to call SplashScreen while changing every single page than just a data with intent.
In your activity :
Intent intent = new Intent(this,SplashScreenActivity.class);
intent.putExtra("data","home");
startActivity(intent);
In SplashScreenActivity :
//write it in global variable
Sprite doubleBounce = new DoubleBounce();
ProgressBar progressBar;
//onCreate method
progressBar = (ProgressBar)findViewById(R.id.progress);
Intent intent = getIntent();
String activity = intent.getStringExtra("data");
if(activity.equals("home")){
changeActivity(homeActivity.class);
}else if(activity.equals("secondActivity")){
changeActivity(secondActivity.class);
}
//Another class
private void changeActivity(Class activity){
progressBar.setIndeterminateDrawable(doubleBounce);
new Handler().postDelayed(() ->{
Intent intent = new Intent(this,activity);
startActivity(intent);
},1000);
}
This is how you can do it.
Create another activity named anything you want for loading.Eg - LoadingActivity
Make a layout file looking like this or however you want.
Then in the java class add this code in the onCreate
ProgressBar progressBar = (ProgressBar)findViewById(R.id.spin_kit);
Sprite cubeGrid = new CubeGrid();
progressBar.setIndeterminateDrawable(cubeGrid);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(this,OtherActivity.class));
finish();
}
Done!

Why does my app keep crashing when I have 2 setOnclickListener?

My app keeps crashing when I set 2 setOnclickListener. The interesting thing is, when I only have 1 button intent, it doesn't crash. When I have 2 buttons, the new button does not bring me to new screen, and the old one just crashes the whole app.
Here's my code for the "old" button:
SignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intSignUp=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intSignUp);
}
});
If that's the only thing, then everything works. But when I add following, the new one doesn't work and old one crashes:
Support.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent support=new Intent(LoginActivity.this,Support.class);
startActivity(support);
}
});
Here's the Logcat:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference.
at com.hoversfw.notes.LoginActivity.onCreate(LoginActivity.java:96)
And LoginActivity.java Line 96 is the beginning of Support.setOnclickListener.
Also, I have everything declared like:
Support=findViewById(R.id.Support);
SignUp=findViewById(R.id.textView);
Here's my XML file code:
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="Already have an account? Sign in!"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.505"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button" />
<Button
android:id="#+id/Support"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginEnd="29dp"
android:text="Support"
app:layout_constraintEnd_toStartOf="#+id/about"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
I also have all ID matched, Android Studio detects no error. I wonder what's causing the problem.
UPDATE
I have everything matched, like java file and xml, no ID is same, everything declared. And I don't think there is any problem with XML.
SignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intSignUp=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intSignUp);
}
});
/*Support.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent support=new Intent(LoginActivity.this,Support.class);
startActivity(support);
}
});*/
If it's like above, then SignUp it works. If it's like
SignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intSignUp=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intSignUp);
}
});
Support.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent support=new Intent(LoginActivity.this,Support.class);
startActivity(support);
}
});
Both of them doesn't work. (The difference is last one I commented Support.setOnClickListener..... but this one I made it as code.)
I can't confirm directly your error because you don't show your xml, but maybe you can do it.
you need check id view in xml same with you call in activiy with findViewById()
findViewById must above in your listener
If you are using 2 buttons, then you need to link them as button for both of them.
UPDATED
Since, you are using button and textview. I updated the answer.
This code at xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main2Activity">
<TextView
android:id="#+id/txtViewSupport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Support" />
<Button
android:id="#+id/btnSignUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SignUp" />
</LinearLayout>
This code at Java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Button buttonSignUp = findViewById(R.id.btnSignUp);
TextView textViewSupport = findViewById(R.id.txtViewSupport);
buttonSignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
textViewSupport.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}

Launch new activity from fragment in Android Studio [duplicate]

This question already has answers here:
android.content.ActivityNotFoundException:
(24 answers)
Closed 4 years ago.
i just want to open SkipActivity from MondayFragment, and fail in error. The swipe between fragments is work, but when i click on skip button move to another Activity- the app is crushed(closed):
All relevant code is attached, please help:
public class MondayFragment extends Fragment {
final String LOG_TAG="myLogs";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_monday, container, false);
TextView txt=(TextView) v.findViewById(R.id.skip);
txt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
((TextView)getActivity().findViewById(R.id.skip)).setText("Access from Monday Fragment");
//Start your activity here
Intent i = new Intent(getActivity(),SkipActivity.class);
startActivity(i);
}
});
return v;
}
}
This is java Activity class :
public class SkipActivity extends AppCompatActivity {
#Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.after_skip_scr);
}
}
This is MainActivity class - i think there is nothing to change here:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
// Find the view pager that will allow the user to swipe between fragments
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
// Create an adapter that knows which fragment should be shown on each page
SimpleFragmentPagerAdapter adapter = new SimpleFragmentPagerAdapter(getSupportFragmentManager());
// Set the adapter onto the view pager
viewPager.setAdapter(adapter);
}
}
Below is res-layout files: The first one is for monday fragment
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/monday_main"
android:background="#a7cbeb">
<TextView
android:id="#+id/welcome_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_centerInParent="true"
android:textColor="#191970"
android:text="Welcome to Circles"
android:textSize="29sp"
/>
<TextView
android:id="#+id/skip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="54px"
android:textStyle="italic"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginRight="25dp"
android:layout_marginTop="25dp"
android:text="skip"
android:textColor="#ffffff"
android:onClick="openSkipActivity"
/>
</RelativeLayout>
And this one is for main activity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="MainActivity">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
you can call activity by :
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getActivity(), Contact_Developer.class);
getActivity().startActivity(intent);
}
});
why are you take this line
((TextView)getActivity().findViewById(R.id.skip)).setText("Access from Monday Fragment");
and share logcat error log.
or try this
decalare this
Context context;
txt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, SkipActivity.class);
context.startActivity(intent);
}
});
Your app is crashing on click event because your view is null the reason is you are trying to find out the view by using activity.findViewById which will obvious return null.
((TextView)getActivity().findViewById(R.id.skip)).setText("Access from Monday Fragment");
Change above line with this
((TextView)v.findViewById(R.id.skip)).setText("Access from Monday Fragment");
After all you are launching activity from onClick event so doesn't make sense to update the text of skip textview. but if still you want to do that, no problem.
Just replace the above line of code, it should fix your issue.
As a suggestion in case you want to update the skip textview text in on click event or any other place, create a class level instance say TextView skipTextView and initialize in onCreateView then simply use skipTextView.setTex("<text>")" all over the place where ever required.
#Dima , Try this :
public class MondayFragment extends Fragment implements OnClickListener
{
final String LOG_TAG="myLogs";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_monday, container, false);
// Views
TextView txt=(TextView) v.findViewById(R.id.skip);
txt.setOnClickListener(this);
return v;
}
// onClick Method
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.skip:
((TextView)getActivity().findViewById(R.id.skip)).setText("Access from Monday Fragment");
Intent i = new Intent(getActivity(),SkipActivity.class);
startActivity(i);
break;
default:
break;
}
}
}
tnx for answers!
The problem was - i didn't declare the SkipActivity in manifest file.
At the moment i did it- the app is running and i can swipe between fragments and launch another Activity from fragment.
tnx a lot!

Multiple activity on one button click

I have a problem with creating 2 activities for 1 button. I created a button and I managed to connect that button with one activity but I dont know how to add another activity to the same button.. What am I trying to do is that when we press on that button it will start playing animation (I managed to do that) but I also want it to send SMS to a specific number. I have code for that, but I dont know how to include / connect everything together.
Code from a button:
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent" />
Code from a imageview (animation):
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:scaleType="centerInside"
android:src="#drawable/keers" />
Code from main activity, for animation:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
button = (Button) findViewById(R.id.button);
running = false;
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(!running){
((AnimationDrawable) imageView.getDrawable()).start();
running = true;}
}
}
);
}
}
Now what I want to add for the same button is this :
public void sendText(View paramView)
{
Toast.makeText(this, "SENT",Toast.LENGTH_SHORT).show();
SmsManager.getDefault().sendTextMessage("+3564245237",null,"1", null, null);
I'm not that experienced in programming, so it might be pretty easy for you guys.
Both actions seen to be fired by the same Button, so you may add both to the onClickListener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(!running){
((AnimationDrawable) imageView.getDrawable()).start();
running = true;}
Toast.makeText(view.getContext(), "SENT",Toast.LENGTH_SHORT).show(); //Or YourActivity.this
SmsManager.getDefault().sendTextMessage("+3564245237",null,"1", null, null);
}
}

Android start new activity when clicking widget

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.
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.
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));
}
});
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.

Categories