Search code examples
androidandroid-layoutandroid-fragmentsandroid-activity

How to start an activity from a header button which is included in a fragment?


I am actually trying to open an activity from header button which is included in a fragment but unable to start. I am a beginner just trying to explore how things happens. I have search and din't found any answer hope someone can answer it. Below is my my code. This is my header layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:background="@color/lightOrange">

    <Button
        android:id="@+id/btnSignInUp"
        android:text="Signin / Signup"
        android:textAllCaps="false"
        android:textColor="@color/orange"
        android:layout_marginTop="55dp"
        android:textSize="20dp"
        android:layout_gravity="center"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:background="@drawable/round_button"/>
</LinearLayout>

This is my fragment

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout  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:background="@color/white"
    tools:context=".AccountFragment">

    <include layout="@layout/header"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_marginTop="45dp"/>
</FrameLayout>

This is my fragment class

public class AccountFragment extends Fragment {

    Button btnSignInUp;

    public AccountFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_account, container, false);

        //binding id for signinup button
        this.btnSignInUp=view.findViewById(R.id.btnSignInUp);

        this.btnSignInUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(),SignInUpActivity.class));
            }
        });
        return view;
    }

}

Actually I am inflating the fragment from BottomNavigationView and from there the header layout button is suppose to open an new activity Here is BottomNavigationVIew code

<?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"
    tools:context=".MainActivity">

  <!--serve as a container for a different fragments-->
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="@color/white"
            app:itemIconTint="@drawable/bottom_navigation_selector"
            app:itemTextColor="@drawable/bottom_navigation_selector"
            app:menu="@menu/bottom_navigation_menu"
            app:labelVisibilityMode="labeled"/>
    </FrameLayout>


</LinearLayout>

When btnSignInUp is clicked all I want to open an activity called SignInUpActivity unfortunately I am unable to open what I am missing here. Guidance will be highly appreciated.


Solution

  • Because you are navigating from a fragment you may want to use Context instead of activity as noted below:

    @Override
    public void onClick(View v) {
        startActivity(new Intent(getContext(), SignInUpActivity.class));
    }
    

    or

    @Override
    public void onClick(View v) {
        startActivity(new Intent(view.getContext(), SignInUpActivity.class));
    }