2

So what I've done in Eclipse, in layouts I have: activity_main.xml and activity_main2.xml. What I tried is to create a button in activity_main.xml and on click to go on screen of activity_main2.xml

so in com.example.myfirstapp I have

MainActivity.Java:

package com.example.myfirstapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @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_main, menu);
        return true;
    }

    public void click1(View v) {
        Log.i("clicks","You Clicked B1");
        Intent i=new Intent(
                     MainActivity.this,
                     MainActivity2.class);
        startActivity(i);
    }
}

MainActivity2.java

package com.example.myfirstapp;

import android.os.Bundle;
import android.view.Menu;
import android.app.Activity;

public class MainActivity2 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    }

    @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_main, menu);
        return true;
    }
}
Jonas
  • 121,568
  • 97
  • 310
  • 388
Dmitry
  • 79
  • 1
  • 2
  • 9

3 Answers3

6

Write below code in your MainActivity.java file instead of your code.

public class MainActivity extends Activity implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button mBtn1 = (Button) findViewById(R.id.mBtn1);
        mBtn1.setOnClickListener(this);
    }

    @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_main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        Log.i("clicks","You Clicked B1");
        Intent i=new Intent(MainActivity.this, MainActivity2.class);
        startActivity(i);
    }
}

And Declare MainActivity2 into your Androidmanifest.xml file using below code.

<activity
    android:name=".MainActivity2"
    android:label="@string/title_activity_main">
</activity>
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
  • in which Androidmanifest.xml do i need to add – Dmitry Jan 08 '13 at 14:45
  • There is only 1 Androidmanifest.xml file in your application, please see this link for detail about Androidmanifest.xml file http://developer.android.com/guide/topics/manifest/manifest-intro.html – Dipak Keshariya Jan 09 '13 at 04:44
2

Change your FirstyActivity to:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn_go=(Button)findViewById(R.id.YOUR_BUTTON_ID);
            btn_go.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                  Log.i("clicks","You Clicked B1");
              Intent i=new Intent(
                     MainActivity.this,
                     MainActivity2.class);
              startActivity(i);
            }
        }
    });

}

Hope it will help you.

Bhavesh Patadiya
  • 25,740
  • 15
  • 81
  • 107
0

There is more than one way to do this.

Here is a good resource straight from Google: http://developer.android.com/training/basics/firstapp/starting-activity.html

At developer.android.com, they have numerous tutorials explaining just about everything you need to know about android. They even provide detailed API for each class.

If that doesn't help, there are NUMEROUS different resources that can help you with this question and other android questions.

Rob Avery IV
  • 3,562
  • 10
  • 48
  • 72
  • Right! learning the whole topic is better than finding just an answer –  Aug 03 '17 at 14:51