How To Make More Than Two buttons in Fragment to Open Different Activity

3.2k views Asked by At

I have Created 3 Activities and their class, i want to open them when button pressed, And buttons are in Fragment, i have successfully implemented this For A Single Button, and works. But I Want To do same thing for both button, How Can i make both button to open different activity in this class. i have included code..

Profilefragment.class

public class ProfileFragment extends Fragment {
View rootView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.profile, container, false);

    Button newPage = (Button) v.findViewById(R.id.button2);
    newPage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), parseactivity.class);
            startActivity(intent);
        }
    });
    return v;


}

profilefragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical"
android:weightSum="1">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button1"
    android:id="@+id/button1"
    android:layout_gravity="center_horizontal" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button2"
    android:id="@+id/button2"
    android:layout_gravity="center_horizontal" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button3"
    android:id="@+id/button3"
    android:layout_gravity="center_horizontal" />

3

There are 3 answers

0
Ravi On BEST ANSWER
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.profile, container, false);

Button newPage = (Button) v.findViewById(R.id.button2);
newPage.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getActivity(), parseactivity.class);
        startActivity(intent);
    }
});
Button newPage1 = (Button) v.findViewById(R.id.button1);
newPage1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getActivity(), secondActivity.class);
        startActivity(intent);
    }
});
Button newPage2 = (Button) v.findViewById(R.id.button3);
newPage2.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getActivity(), ThirdActivity.class);
        startActivity(intent);
    }
});
return v;

}

0
Cüneyt On

Before this sure to your manifest include the all activities' definitons. repeat same thing which you do for button which id = button2

Button new2Page = (Button) v.findViewById(R.id.button1);

      newPage2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), other.class);
                startActivity(intent);
            }
        });
0
Max77 On
public class ProfileFragment extends Fragment implements View.OnClickListener {
View rootView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.profile, container, false);

    Button button1 = (Button) v.findViewById(R.id.button1);
    button1.setOnClickListener(this);
    Button button2 = (Button) v.findViewById(R.id.button2);
    button2.setOnClickListener(this);
    Button button3 = (Button) v.findViewById(R.id.button3);
    button3.setOnClickListener(this);

    return v;
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button1:
            // launch Activity1
            break;
        case R.id.button2:
            // launch Activity2
            break;
        case R.id.button3:
            // launch Activity3
            break;
    }
}

}