Search code examples
androidandroid-fragmentsandroid-activityandroid-fragmentactivity

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


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


Solution

  • @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;
    

    }