• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to make a button open a new layout xml

SnakeKoRn

Lurker
Hey there,

I'm trying to make a button for my app, which will bring the screen to another page. However, I'm not successful in doing so.

I've tried many things, without a reliefing answer.

My project doesn't accept "Intent" in my program.
My button I need to open a new layout is called "OptionButton"

Here's what I've got:

in MainActivity.java
In the beginning I got this
public class MainActivity extends Activity {

private Button startButton;
private Button pauseButton;
private Button resetButton;
public Button OptionButton;

MainActivity.java
further I got this
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
I'm just mentioning this because I use savedInstanceState here too

MainActivity.java
And my code for my button is this
OptionButton = (Button) findViewById(R.id.Button1);
OptionButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
myClick(v); /* my method to call new intent or activity */
}
public void myClick(View v) {
Intent intent = new Intent(this, Background2.class);
startActivity(intent);// for calling the activity
}
});



I added this in AndroidManifest:
<activity android:name=".Background2"></activity>

and this in the 2nd class (java file in src map)
(package & imports, then this:
public class Background2 extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
_
I got 2 classes in src map:
-Background2.java
-MainActivity.java


Also 2 layout xml's:

- activity_main.xml
- activity main2.xml


In Activity_main, I got this for the button:
<Button
android:id="@+id/Button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="@string/OptionButtonLabel"/>


Still it's not working. What am I missing?


Ty all so much!
 
Is this part in your OnCreate method in MainAcitvity.java?
(note the change of params at setOnClickListener)

button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(this);
 
Ty for your answer :) !

yes, it's indeed in MainActivity.java

I've tried modifying that line, and that results in underlining
setOnClickListener, the ) of (this), onClick and myClick (both)

:-/
 
Back
Top Bottom