0

I am very new at android development, and I am trying to make an app which has 4 buttons in its main activity and when I click on one of its button it takes me to another activity and displays its xml file, what should I write in the 2nd activity? Here is my code so far.

main xml file

 <?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">

    <TextView
        android:id="@+id/txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:background="@color/colorAccent"
        android:text="Overview"
        android:textAppearance="@style/TextAppearance.AppCompat.Headline"
        android:textStyle="bold" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <Button
            android:id="@+id/one"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/background_light"
            android:text="Information" />
        <TableRow
            android:id="@+id/hr1"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#444">

        </TableRow>

        <Button
            android:id="@+id/two"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/background_light"
            android:text="Education" />
        <TableRow
            android:id="@+id/hr2"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#444">

        </TableRow>
        <Button
            android:id="@+id/three"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/background_light"
            android:text="Work Experience" />
        <TableRow
            android:id="@+id/hr3"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#444">

        </TableRow>
        <Button
            android:id="@+id/four"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/background_light"
            android:text="Education" />
        <TableRow
            android:id="@+id/hr4"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#444">

        </TableRow>


    </LinearLayout>

    </LinearLayout>

........................

 main activity
    package com.lakshay.display.piechart;


    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;

    public class MainActivity extends AppCompatActivity implements 
    View.OnClickListener {
    Button btn1 , btn2 ;


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

    }


     @Override
     public void onClick(View v) {
        Intent intent = new Intent();
        String nextAct = null ;
        String shield = "com.lakshay.display.piechart";
        Integer flag= -1;
        switch (v.getId())
        {
            case (R.id.one ):
            nextAct = shield + "ContactActicity";
            break;

            default:
                Toast.makeText(MainActivity.this , "Item Currently Unavailable" 
    , Toast.LENGTH_SHORT).show();

        }
        try {
            if (nextAct!=null)
            {
                intent = new Intent(MainActivity.this , Class.forName(nextAct));
                flag = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT;
                if (flag  != -1 ){
                    intent.setFlags(flag);
                } startActivity(intent);
            }
        } catch (ClassNotFoundException e){
            e.printStackTrace();
        }
    }
    }

................... xml file for 2nd activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:background="@color/colorAccent"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    tools:context="com.lakshay.display.piechart.ContactActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Your Name"
        android:textStyle="bold"
        android:paddingLeft="20dp"
        android:textSize="22dp"/>


    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/background_light"
        android:inputType="text"
        android:paddingBottom="20dp"


        android:paddingLeft="20dp" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Your Address"
        android:textStyle="bold"
        android:paddingLeft="20dp"
        android:textSize="22dp"/>


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPostalAddress"
        android:id="@+id/address"
        android:paddingBottom="20dp"
        android:paddingLeft="20dp"
        android:background="@android:color/background_light"



        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Phone Number"
        android:textStyle="bold"
        android:paddingLeft="20dp"
        android:textSize="22dp"/>

    <EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="phone"
    android:id="@+id/number"
        android:paddingBottom="20dp"
        android:background="@android:color/background_light"

        android:paddingLeft="20dp"

    />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Email"
        android:textStyle="bold"
        android:paddingLeft="20dp"
        android:textSize="22dp"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:id="@+id/email"
        android:paddingBottom="20dp"
        android:paddingLeft="20dp"
        android:background="@android:color/background_light"


        />



</LinearLayout>

...................

api55
  • 11,070
  • 4
  • 41
  • 57

4 Answers4

0

First of all, you must get your Button id from your xml. Or you will get NullPointerException so change your onCreate like this.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn1 = (Button)findViewById(R.id.one); //This line
    btn2 = (Button)findViewById(R.id.two); //and this line
    btn1.setOnClickListener(this);
    btn2.setOnClickListener(this);
}

And if you want to call intent with Class, you can see this solution


OR

You can simply call another activity like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn1 = (Button)findViewById(R.id.one); //This line
    btn2 = (Button)findViewById(R.id.two); //and this line
    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(firstActivity.this, secondActivity.class));
        }
    });
}
Cagri Yalcin
  • 402
  • 4
  • 13
0

Let's say you have two activities : Activity1.java and Activity2.java.

to start Activity2 from Activity1 just do :

final Intent intent = new Intent(this, Activity2.class);>
startActivity(intent)

If you want to start activity on button click you have to write this codein an onClickListener. To do that, write the following attribute in your button definition in Activity1 xml file.

android:onCLick="onButtonClick"

Then in your activity write the following listener :

public void onButtonClick(final View v) {
// put your intent here
}

adc
  • 76
  • 8
0
This code help you ....   

 public class MainActivity extends AppCompatActivity {
         Button btn1 ;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button = (Button)findViewById(R.id.btn1);
            final String name = editText.getText().toString();
            button.setOnClickListener(new View.OnClickListener() {//when your btn1 button press
                public void onClick(View v) {
                 Intent intent = new Intent(this, Activity2.class);//this is call your second activity
                 startActivity(intent);//start activity
                }
            });
        }
    }
0

My answer as a checklist:

1.- If you are using android studio you should create 2nd activity with the assistant so you dont get into more complications. The 2nd activity must have an xml file and a class file.

2.- You should add android:onClick propertie for your button in the xml file on the activity.

3.- Your 2nd activity must have an onCreate method in the class file to fill the contents of the 2nd activity.

3b.- You can leave onCreate with default content but your xml file must then have al the info of your textviews. I can elaborate further if needed.

Albert
  • 58
  • 7