-1

I am very new to java and android development. So, even after reading many tutorials(e.g. this, this), I can't figure out how to call a fragment from a bottomnevigationmenu item.

Currently, I have:

MainActivity.java

package com.example.myapplication;

import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private void loadCalFragment() {
        String string1="Hello",string2 = "Hii";

        CalFragment fragment = CalFragment.newInstance(string1,string2);
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.fragment_frame, fragment);
        ft.commit();
    }

    private TextView mTextMessage;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {



        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Context context = getApplicationContext();
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    Toast toasth = Toast.makeText(context , R.string.title_home, Toast.LENGTH_LONG);
                    toasth.show();
                    return true;
                case R.id.navigation_dashboard:
                    mTextMessage.setText(R.string.title_dashboard);
                    Toast toastd = Toast.makeText(context , R.string.title_dashboard, Toast.LENGTH_LONG);
                    toastd.show();
                    loadCalFragment();
                    return true;
                case R.id.navigation_notifications:
                    mTextMessage.setText(R.string.title_location);
                    Toast toastn = Toast.makeText(context , R.string.title_location, Toast.LENGTH_LONG);
                    toastn.show();
                    return true;
            }
            return false;
        }
    };

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

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

}

activaty_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.myapplication.MainActivity">

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:text="@string/title_home"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

Now, when I am adding a fragment using android-studio, it has created the xml file. My target is to create a datepicker in this fragment and show it. But, I don't know how to do that.

fragment_cal.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.myapplication.CalFragment">

    <!-- TODO: Update blank fragment layout -->

    <DatePicker
        android:id="@+id/datePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:calendarViewShown="false"
        android:datePickerMode="spinner" />

</FrameLayout>

and a corresponding java file:

calFragment.java

package com.example.myapplication;

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;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link CalFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link CalFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class CalFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    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 CalFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment CalFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static CalFragment newInstance(String param1, String param2) {
        CalFragment fragment = new CalFragment();
        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) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_cal, 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);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

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

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

My AndroidManifest.xml is:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

When I am trying to attach the CalFragment() call to the nevigation menu as:

case R.id.navigation_dashboard:
                    mTextMessage.setText(R.string.title_dashboard);
                    Toast toastd = Toast.makeText(context , R.string.title_dashboard, Toast.LENGTH_LONG);
                    toastd.show();
                    CalFragment();
                    return true;

its giving error: Error:(32, 21) error: cannot find symbol method CalFragment()

I will be grateful if somebody kindly help me in it.

BaRud
  • 3,055
  • 7
  • 41
  • 89

2 Answers2

1

You are using same name for fragment CalFragment() and method CalFragment()

change to below:

case R.id.navigation_dashboard:
                mTextMessage.setText(R.string.title_dashboard);
                Toast toastd = Toast.makeText(context , R.string.title_dashboard, Toast.LENGTH_LONG);
                toastd.show();
                loadCalFragment();
                return true;

When your are calling fragment, create loadCalFragment(); method

Create method like below in your Activity class , you are not passing two string values to your fragment newInstance pass using CalFragment fragment = CalFragment.newInstance(string1,string2); pass your actual data here i used demo data:

private void loadCalFragment() {
   String string1,string2 = "Hii"

    CalFragment fragment = CalFragment.newInstance(string1,string2);
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.fragment_frame, fragment);
    ft.commit();
}

Update your code from my Code (Activity):

     package com.example.myapplication;

import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {



    private TextView mTextMessage;

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

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Context context = getApplicationContext();
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    Toast toasth = Toast.makeText(context , R.string.title_home, Toast.LENGTH_LONG);
                    toasth.show();
                    return true;
                case R.id.navigation_dashboard:
                    mTextMessage.setText(R.string.title_dashboard);
                    Toast toastd = Toast.makeText(context , R.string.title_dashboard, Toast.LENGTH_LONG);
                    toastd.show();
                    loadCalFragment();
                    return true;
                case R.id.navigation_notifications:
                    mTextMessage.setText(R.string.title_location);
                    Toast toastn = Toast.makeText(context , R.string.title_location, Toast.LENGTH_LONG);
                    toastn.show();
                    return true;
            }
            return false;
        }
    };


    private void loadCalFragment() {
        CalFragment fragment = CalFragment();
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.fragment_frame, fragment);
        ft.commit();
    }

    }

And CalFragment Code Here :

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class CalFragment extends Fragment {


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


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

}
Abhishek kumar
  • 4,347
  • 8
  • 29
  • 44
0

Here is the full answer.

activity_main.xml

<android.support.design.widget.CoordinatorLayout 
    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"
    tools:showIn="@layout/app_tab_bar">

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

app_tab_bar.xml

<android.support.design.widget.CoordinatorLayout             
    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">

    <include layout="@layout/activity_main" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:itemBackground="@android:color/white"
        android:foreground="?attr/selectableItemBackground"
        app:itemIconTint="@color/colorAccent"
        app:itemTextColor="@color/colorAccent"
        app:menu="@menu/navigation" />

</android.support.design.widget.CoordinatorLayout>

MainActivity.java

    protected void onCreate(Bundle savedInstanceState) {
    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    // load the first fragment by default
    loadFragment(new Frag_one());
}

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment fragment;
        switch (item.getItemId()) {
            case R.id.navigation_explore:
                fragment = new Frag_one();
                loadFragment(fragment);
                return true;
            case R.id.navigation_saved:
                fragment = new Frag_two();
                loadFragment(fragment);
                return true;
            case R.id.navigation_inbox:
                fragment = new Frag_three();
                loadFragment(fragment);
                return true;
            case R.id.navigation_profile:
                fragment = new Frag_four();
                loadFragment(fragment);
                return true;
        }
        return false;
    }
};

private void loadFragment(Fragment fragment) {
    // load fragment
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.frame_container, fragment);
    transaction.addToBackStack(null);
    transaction.commit();
}

In order to display a fragment you have to define a container in xml file. Framelayout serves as placeholder for your fragments. So in your mainactivity.xml you need to define the container as I did in my code.

Zankrut Parmar
  • 1,872
  • 1
  • 13
  • 28
  • Hi, I am getting error on incompatible types: Error:(37, 32) error: incompatible types: CalFragment cannot be converted to Fragment Error:(58, 22) error: incompatible types: CalFragment cannot be converted to Fragment Error:(63, 87) error: incompatible types: android.support.v4.app.FragmentTransaction cannot be converted to android.app.FragmentTransaction Can you kindly check? – BaRud Feb 09 '18 at 09:12
  • In my code you just need to replace the fragment classes only remove your calfragment method and use my load fragment method. Also As @Abhishek kumar said you need to use different names for your method and your fragments. – Zankrut Parmar Feb 09 '18 at 09:21