0

i have android app which have main activity and three fragment 1-home 2-visa 3-trans,i want to click button in home fragment to start visa fragment ,I've tried more things in home fragment which have the button and here is my code for Home after i tried to add onclicklistner in onCreatView :

package com.example.ghoniem.myfrag.Fragments;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

    import com.example.ghoniem.myfrag.R;


    public class Home extends Fragment {


        private static final String ARG_PARAM1 = "param1";
        private static final String ARG_PARAM2 = "param2";


        // TODO: Rename and change types of parameters
        private String mParam1;
        private String mParam2;

        private OnFragmentInteractionListener mListener;

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


        public static Home newInstance(String param1, String param2) {
            Home fragment = new Home();
            Bundle args = new Bundle();
            args.putString(ARG_PARAM1, param1);
            args.putString(ARG_PARAM2, param2);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);



            if (getArguments() != null) {
                mParam1 = getArguments().getString(ARG_PARAM1);
                mParam2 = getArguments().getString(ARG_PARAM2);
            }
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_home, container, false);
             Button button=rootView.findViewById(R.id.text_View88);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Visa visa = new Visa();
                    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.visaaa, visa);
                    fragmentTransaction.commit();
                }
            });


            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_home, container, false);
        }




        // TODO: Rename method, update argument and hook method into UI event
        public void onButtonPressed(Uri uri) {
            if (mListener != null) {
                mListener.onFragmentInteraction(uri);
            }
        }

        @Override
        public void onAttach(Context context) {
            super.onAttach(context);

        }

        @Override
        public void onDetach() {
            super.onDetach();
            mListener = null;
        }


        public interface OnFragmentInteractionListener {
            // TODO: Update argument type and name
            void onFragmentInteraction(Uri uri);
        }


    }

code for visa fragment :

package com.example.ghoniem.mfrag.Fragments;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.ghoniem.myfrag.R;


public class Visa extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public Visa() {

    }


    public static Visa newInstance(String param1, String param2) {
        Visa fragment = new Visa();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_visa, container, false);
    }


    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);


    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }


    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

and this xml for fragment_visa

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:id="@+id/visaaa"
    android:layout_height="match_parent"
    tools:context="com.example.ghoniem.myfuckenfrag.Fragments.Visa">

    <TextView
        android:textColor="@color/colorPrimary"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text 2"/>

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:alpha="0.3"
        android:src="@drawable/ic_photo_library_black_24dp" />

</FrameLayout>
Henry
  • 42,982
  • 7
  • 68
  • 84

4 Answers4

0
replace ur onCreateView() in Home Fragment from it
@Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_home, container, false);
             Button button=rootView.findViewById(R.id.text_View88);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Visa visa = new Visa();
                    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.visaaa, visa);
                    fragmentTransaction.commit();
                }
            });


            // Inflate the layout for this fragment
            return rootView;
        }
0

the best practice is to define a call back in the activity that is called from fragment 1 when the button is clicked, this call back should replace the fragment

ammcom
  • 992
  • 1
  • 7
  • 24
0

Try This:

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Visa fragment = new Visa();
               replaceFragment(fragment);
            }
        });

public void replaceFragment(Fragment someFragment) {
    FragmentTransaction transaction = getActivity().getFragmentManager().beginTransaction();
    transaction.replace(R.id.visaaa, someFragment);
    transaction.addToBackStack(null);
    transaction.commit();
}
Salim Lachdhaf
  • 1,073
  • 1
  • 9
  • 21
  • i fixed "Visa fragment = new Visa();" , try it now , otherwise please let me know the error. Note that "replaceFragment" is a separate function , so put it outside the "onCreateView()" function. – Salim Lachdhaf Sep 30 '17 at 20:32
  • i tried that and i found that error : Error:(75, 94) error: incompatible types: android.app.FragmentTransaction cannot be converted to android.support.v4.app.FragmentTransaction – Mohamed Ghoniem Sep 30 '17 at 21:57
  • check the solution here for this error : https://stackoverflow.com/questions/33822508/error-incompatible-types-android-app-fragmentmanager-cannot-be-converted-to-and – Salim Lachdhaf Oct 01 '17 at 08:42
0

i tried this and worked for me

Button button=rootView.findViewById(R.id.text_View88);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               FragmentManager fragmentManager=getFragmentManager();
                FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
                Visa visa=new Visa();
                fragmentTransaction.replace(R.id.frame,visa);
                fragmentTransaction.commit();}
    });