Setting a button enabled - android

I have a main page that has a 2 buttons, button1 is enabled and button2 is disabled. button1 will open 2nd page that has 1 button named button3. button3 will intent BACK to the main page and it should make the button2 enabled. the problem is button2 will become enabled for a short period then back to being disabled(shaded).
Main page.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Enable extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enable);
Button page1 = (Button) findViewById(R.id.button1);
page1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), p2.class);
startActivityForResult(myIntent, 0);
}
});
Button page2 = (Button) findViewById(R.id.button2);
page1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), p2.class);
startActivityForResult(myIntent, 0);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_enable, menu);
return true;
}
}
2nd page
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class p2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.p2);
Button page1 = (Button) findViewById(R.id.button3);
page1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Enable.class);
setContentView(R.layout.activity_enable);
Button a = (Button) findViewById(R.id.button2);
a.setEnabled(true);
startActivityForResult(myIntent, 0);
}
});
}
}
XML of main page
<RelativeLayout 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"
tools:context=".Enable" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="104dp"
android:layout_marginTop="32dp"
android:text="Button" />
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="46dp"
android:enabled="false"
android:text="Button" />
XML of page2
<?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"
android:orientation="vertical" >
<Button
android:id="#+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

Instead of controlling the behaviour of button via the XML, control the enable and disable feature in the activity code. Hope this will help

Your problem is with how Intents work. What your second activity is doing is accessing the layout of the first activity (which is bad practice as they're now tightly coupled), but the intent is recreating the Main activity - which means the onCreate method gets called again, and the button is recreated - with the enabled property set to false.
To fix your issue, either take a look at this article, which covers Intents, intent extras (to pass data - which will allow you to set a property which your Main activity can use to determine whether to enable the button), and intent flags (to manipulate how instances of your classes operate - one option is to use FLAG_ACTIVITY_REORDER_TO_FRONT which takes any existing instance and puts it at the top of the stack), or just search for information on flags and extras, they should show you what you need to know.

The Intent starts the activity again, and the activity calls the onCreate method, which sets the Button 2 to disabled.

Related

Unfortunately (project name) has stopped working on button click

An area which will present a predefined question
A button which will cause the answer to the question to be shown while leaving the question on the screen
An area which will present the answer
A button which will cause a transition to a screen that is formatted the same as this one, with the next question shown on it
A button which will cause the app to end (a transition to (3))
Only trying to get the transition from screen 1 to screen 2 to work with a different question/answer pair in its place with a button click. If there is anyway to do this other than switching the screens and activities which is the error, please let me know.
package com.example.androidassignment2;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
public class AndroidAssignment2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_assignment2_1);
Button next = (Button) findViewById(R.id.QButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.android_assignment2_1, menu);
return true;
}
}
layout file
<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" >
<TextView android:id="#+id/Questions"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:text="#string/Q2" />
<Button android:id="#+id/QButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_question" />
<Button android:id="#+id/AButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
<TextView android:id="#+id/Answers"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:hint="#string/A2" />
<Button android:id="#+id/QuitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_quit" />
</LinearLayout>
activity 1 file (incase needed)
package com.example.androidassignment2;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.QButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), AndroidAssignment2_1.class);
startActivityForResult(myIntent, 0);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Intent myIntent = new Intent(view.getContext(), AndroidAssignment2_1.class);
startActivityForResult(myIntent, 0);
You are starting a new activity, the most likely cause is that this is has not been declared in the Manifest.
In your AndroidManifest.xml`, you will need to add something like this:
<activity
android:name="your.package.name.AndroidAssignment2_1" />
This will reside inside of the <application> tag.
You are using startActivityForResult() but you don't override the onActivityForResult() method in your first Activity. You could change that to just use startActivity() and remove setResult() from your second Activity.
Button next = (Button) findViewById(R.id.QButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), AndroidAssignment2_1.class);
startActivity(myIntent);
}
});
If this isn't your problem then please post logcat (always when you have a crash) and make sure the Activity is decalred.
As far as your other inquiry
If there is anyway to do this other than switching the screens and activities which is the error, please let me know.
If you are simply changing the text then you can stay in the same Activity and change the text of your TextViews. You could store questions and answers in something like Arrays or in a DB if you wish and keep a counter to where you are and change the text with each click.
Another option, which may go along with the first if you want, is to use a ViewPager

How to navigate from one page to another on android?(Total:3 pages)

I am new to Android development and I have a question regarding how to navigate from one page to another.
Actually,what I want to do is this:
Open app,first page appears,it says "Hello I am activity 1".Then there is a button which says "Next",you press next then you navigate to second page where it says "Hello I am activity 2".In this page there are two buttons,first says "Previous" which takes you back to page 1 and second says "Next" which takes you to page 3.
Basically,this is where I am stuck,page 1 and 2 works fine,both next and previous buttons but I can't navigate to 3rd page when I press the "next" button from page 2.
I have uploaded my source code here so you guys can download it and import it to Eclipse in order to see exactly what I have done.
Click here to download.
Would be glad if someone could help mates,
Thanks in advance.
OK,code is shown here,I have created 3 activities which I also registered at Manifest and I also have created 3 layouts for these 3 activities.
Activity 1
import android.app.Activity;<br>
import android.content.Intent;<br>
import android.os.Bundle;<br>
import android.view.View;<br>
import android.widget.Button;<br>
public class Activity1 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Activity2.class);
startActivityForResult(myIntent, 0);
}
});
}
}
Activity2
import android.app.Activity;<br>
import android.content.Intent;<br>
import android.os.Bundle;<br>
import android.view.View;<br>
import android.widget.Button;<br>
public class Activity2 extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Button next = (Button) findViewById(R.id.Button02);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Button next = (Button) findViewById(R.id.Button04);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent1 = new Intent(view.getContext(), Activity3.class);
startActivityForResult(myIntent1, 0);
}
});
}
}
Activity3
import android.app.Activity;<br>
import android.content.Intent;<br>
import android.os.Bundle;<br>
import android.view.View;<br>
import android.widget.Button;<br>
public class Activity3 extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
Button next = (Button) findViewById(R.id.Button04);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}
main.xml
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="This is Activity 1" />
<Button android:text="Next"
android:id="#+id/Button01"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px">
</Button>
</LinearLayout>
main2.xml
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="This is Activity 2" />
<Button android:text="Previous"
android:id="#+id/Button02"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px">
</Button>
<Button
android:layout_width="162dp"
android:layout_height="34dp"
android:text="Next"
android:id="#+id/Button04"
android:textSize="18px" />
</LinearLayout>
main3.xml
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="This is Activity 3" />
I have also registered my activities in Manifest.
Step #1: Replace startActivityForResult() with startActivity() in Activity1
Step #2: Remove your current code in the onClick() from Activity2 and replace it with a call to startActivity() to start Activity3
Step #3: Completely rewrite Activity3, as either it will not compile or it will crash at runtime, as you are referring to a widget that does not exist (Button04)
I'm not sure if it't too late but if you still need a clear answer then:
You will need to start activities. There isn't a DIRECT page navigation code, but instead there is a start Activity. By starting an activity you are able to go to another page because the page is related to the activity.
Intent myIntent = new Intent(Enter_Your_Current_Activity.this, Enter_The_Activity_You_Want_To_Navigate_To.class);
startActivity(myIntent);
Hope this helps!
p.s What I use in my code.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Activity1 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Activity2.class);
startActivityForResult(myIntent, 0);
}
});
}
}
a simple way to navigate one page to another is startActivity new Intent(firstpagename.this,secondpagename.class);

can't go back button

I have a problem with my Android app. I have three Activities that all connect with one another with a Image button.
My problem is I can't go back on any of the pages, just forwards. I have been reading about the back button and how to keep its state and not kill (End) it, so the user can click back if needed too.
I am new to Android and I know there's a lot to learn. So thank you for your help.
Java code for first Activity:
package my.hope;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.content.Intent;
public class NewhopeActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView myImage = (ImageView) findViewById(R.id.imagebutton1);
myImage.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(NewhopeActivity.this, Act2.class);
startActivity(intent);
}
});
}
}
XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" android:background="#drawable/mint">
<ImageButton
android:id="#+id/imagebutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="250dp"
android:background="#drawable/ic_launcher"
android:onClick="Act2"
android:src="#drawable/ic_launcher" />
</LinearLayout>
What happens when you click the back button on your device? Unless specified, it kills the current activity and brings you back to the previous one.
However, if you wish to specify exactly what would clicking the back button do, you can override the onBackPressed() method:
#Override
public void onBackPressed() {
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(intent);
finish();
}

Back button in dialog

Am trying to get the back button in my dialog to go back to the original screen. I don't know if I have all the imports that I need. Can someone tell me where I am going wrong?
Java code:
package my.dlog;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
public class DlogActivity extends Activity {
/** Called when the activity is first created. */
Dialog dialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dialog = new Dialog(this);
dialog.setContentView(R.layout.main2);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onBackPressed() {
Intent intent = new Intent(DlogActivity.this, DlogActivity.class);
startActivity(intent);
finish();
}
public void onClick(View v) {
dialog.show();
}
});
}
}
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:minHeight="400dp"
android:minWidth="300dp" android:background="#drawable/mint1">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<ImageView
android:layout_width="236dp"
android:layout_height="220dp"
android:layout_marginRight="100dp" android:background="#drawable/carsee"/>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</RelativeLayout>
</LinearLayout>
b.setOnClickListener(new OnClickListener() {
public void onBackPressed() {
dialog.cancel();
// Simply Dismiss the dialog to make it close and return to back..
/*What you are using is not a valid construct */
}
Also make sure that button1 in in main layout as you have used findViewById(R.id.button1) directly for set content view
Well normally the back button works just without any help from us. If you take the
public void onBackPressed() {
Intent intent = new Intent(DlogActivity.this, DlogActivity.class);
startActivity(intent);
finish();
}
out, what happens when you press 'back'? If this is not what you want, then what do you wnat to happen? If there are no errors, I would think you have the required imports.
Cliff

Android Null Pointer Exception Button

Ok this may seem like a pointless example but if I can figure this out then the program I am trying to make will work. So I have two activities test and test two each with one button.
Test 1:
package thompson.cameron.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class Test extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View button = findViewById(R.id.testButton);
button.setOnClickListener(this);
}
public void onClick(View v){
Intent i = new Intent(this, Test2.class);
startActivity(i);
}
}
and test2
package thompson.cameron.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class Test2 extends Activity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
View test = findViewById(R.id.testButton);
test.setOnClickListener(this);
}
public void onClick(View v){
switch (v.getId()){
case R.id.testButton:
System.exit(1);
}
}
}
When I click the button on Test it is supposed to launch test2 however it is at this point I get an null pointer exception that I have narrowed down to test.setOnClickListener(this); line of code. Below are my two xml files for the layout. I can get the button to work when I only have one activity but as soon as I add a second activity with a different layout file it all falls apart
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/testButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST TEST TEST"/>
</LinearLayout>
main2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/testButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST2 TEST2 TEST2"/>
</LinearLayout>
I'm still new at Android programming so thanks for all your help.
Open the Debug perspective in Eclipse
Choose the 'Breakpoints' view tab.
Select the 'Add Java Exception Breakpoint' and choose
NullPointerException.
Launch your activity, either by using 'Debug As...' or attaching
the debugger to a running instance via DDMS.
Execute the offending workflow. It will break on the line that
caused the NullPointerException.
In your test.java file give:
implements View.OnClickListener
Initialize your button as:
Button testButton = (Button) findViewById(R.id.testButton);
and inside your onClick method, check whether you are clicking button:
if(v == testButton) {
//give ur intent code
}
There are different ways to perform onClick functionality.
One is the above method which I have mentioned.
Another one is what ankit has mentioned.
Third way is through your layout.
Inside your layout for your button tag, you may give as:
<Button android:id="#+id/testButton" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Click" android:onClick="onTestButtonClick" />
And inside your class just mention the below details for button:
public void onTestButtonClick(View view) {
//give your intent code
}
You may refer to the link also:
http://android-developers.blogspot.com/2009/10/ui-framework-changes-in-android-16.html
Make sure that both activities are register at the application's manifest file.
As a side note never call System.exit in your code. You can call finish() to close an Activity and this will bring at the front the previous Activity on the stack.
The issue here is that you haven't typecasted your views to buttons.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; // Needed to add this import for the button casting below
public class Test extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// I have changed View to Button and then typecasted
// with the "(Button)" the return of findViewById
Button button = (Button) findViewById(R.id.testButton);
button.setOnClickListener(this);
}
public void onClick(View v){
Intent i = new Intent(this, Test2.class);
startActivity(i);
}
}
Let me know if you have any issues with this. I just completed my first experiment through using the onClickListener implementation through the main class instead of individual anonymous listeners.
Andrew
I had the same problem, you may put the same content view that the button,
setContentView(R.layout.main); if the button is in that content view, in other case, you will put:
setContentView(R.layout.buttoncontentview);
View button = findViewById(R.id.testButton);
button.setOnClickListener(this);
public void onClick(View v){
Intent i = new Intent(this, Test2.class);
startActivity(i);
}
setContentView(R.layout.main);
sorry for my bad english, but i'm spanish
Implement OnClickListener interface
and set button.setOnClickListener(this);
and override
public void onClick(View v) {
}
I think your buttons IDs need to be different in different activities. R.id.testButton would refer to only one button.
The final solution is that you may modify the AndroidManifest.xml file, i finally solved my error in this link How to register a new activity in AndroidManifest.xml?
You can try this.it may work.
package thompson.cameron.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class Test extends Activity{
private Button button;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = findViewById(R.id.testButton);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent i=new Intent().setClass(Test.this,Test2.class);
startActivity(i);
}
});
}
}
First of all in main.xml and main2.xml chage the button's ids like in below code.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/testButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST TEST TEST"/>
</LinearLayout>
main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/testButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST2 TEST2 TEST2"/>
</LinearLayout>
It throws nullpointerexception because of id confict with each other so in your java file use following code to find button.
In Activity 1
Button button = findViewById(R.id.testButton);
button.setOnClickListener(this);
and
In Activity 2
Button button = findViewById(R.id.testButton1);
button.setOnClickListener(this);

Categories

Resources