0

enter image description hereI have created 3 fragments for one of my project ..this is one of them. So basically I want When ever the admin(aAccountFragment) will click on the update quote button the quote should be updated and should be reflected in AccountFragment.xml file in the user end.

For this in the admin(aAccountFragment) side I have provided and edit text where the admin can update the quote every 24hrs.and in the user end(AccountFragment) the user can only see the updated quote when ever the admin will update the quote because at user end textview is provided and in the admin end EditText is provided.

For this I have attached my aAccountFragment.xml, AccountFragment.xml,AccountFragment.java,aAccountFragment.java code.

This is my aAccountFragment.xml code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".aAccountFragment"
    android:background="@drawable/ic_launcher_background">

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


    <TextView
        android:layout_width="339dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="41dp"
        android:layout_marginBottom="465dp"
        android:text="    QUOTE OF THE DAY"
        android:textColor="@color/black"
        android:textSize="30sp" />

    <EditText
        android:layout_width="323dp"
        android:layout_height="111dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="21dp"
        android:layout_marginBottom="273dp"
        android:text="-She decided to start living the life she imagined-"
        android:textColor="@color/DarkRed"
        android:textSize="25sp" />

    <Button
        android:layout_width="166dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="107dp"
        android:layout_marginBottom="177dp"
        android:background="@color/LightBlue"
        android:text="UPDATE QUOTE"
        android:textColor="@color/black"
        android:textSize="20sp" />

    <Button
        android:id="@+id/logout1"
        android:layout_width="166dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="43dp"
        android:layout_marginBottom="75dp"
        android:background="@color/Pink"
        android:text="LOG OUT"
        android:textColor="@color/black"
        android:textSize="20sp" />

</RelativeLayout>

this is my aAccountFragment.java code.

public class aAccountFragment extends Fragment {
    Button logout1;

    public aAccountFragment() {
        // 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.afragment_account, container, false);

        logout1 = view.findViewById(R.id.logout1);
        logout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), aloginpage.class));
                Toast.makeText(getActivity().getApplicationContext(), "SUCCESSFULLY LOGOUT", Toast.LENGTH_SHORT).show();

            }


        });

        return view;
    }

}

this is my AccountFragment.xml code.

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".AccountFragment"
    android:background="@drawable/ic_launcher_background">

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


    <TextView
        android:layout_width="339dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="41dp"
        android:layout_marginBottom="465dp"
        android:text="    QUOTE OF THE DAY"
        android:textColor="@color/black"
        android:textSize="30sp" />

    <TextView
        android:layout_width="296dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="49dp"
        android:layout_marginBottom="272dp"
        android:text="-She decided to start living
                               the life she imagined-"
        android:textColor="@color/DarkRed"
        android:textSize="25sp" />

    <Button

        android:id="@+id/logout"
        android:layout_width="166dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="28dp"
        android:layout_marginBottom="75dp"
        android:background="@color/Pink"
        android:text="LOG OUT"
        android:textColor="@color/black"
        android:textSize="20sp" />

</RelativeLayout>
    

this is my AccountFragment.java code.

public class AccountFragment extends Fragment {

    Button logout;

    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);

        logout = view.findViewById(R.id.logout);
        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), loginpage.class));
                Toast.makeText(getActivity().getApplicationContext(), "SUCCESSFULLY LOGOUT", Toast.LENGTH_SHORT).show();

            }


        });

        return view;
    }
}
Suzzi
  • 53
  • 5
  • Admin is a device and user is another device, am I right ? How are they connected ? – NRUSINGHA MOHARANA Apr 21 '21 at 09:12
  • Actually In my app there are 2 buttons if the user is using it he can click on user button and of admin is using it he can click on admin button.No they are not 2 diifrerent devices just there xml pages are different. – Suzzi Apr 21 '21 at 13:21
  • What I am trying to ask is , quote will be passed just between fragments or via network ? – NRUSINGHA MOHARANA Apr 21 '21 at 13:23
  • just between fragments – Suzzi Apr 21 '21 at 13:23
  • Can u please help me how can I do that. – Suzzi Apr 21 '21 at 13:28
  • Sure, but not today its already 7pm. Sorry .... – NRUSINGHA MOHARANA Apr 21 '21 at 13:32
  • Then when ..Can u specify the time – Suzzi Apr 21 '21 at 13:34
  • So that I can be ready – Suzzi Apr 21 '21 at 13:35
  • Tomorrow after 10 AM – NRUSINGHA MOHARANA Apr 21 '21 at 13:37
  • Ya sure ..I will be ready.Thank you – Suzzi Apr 21 '21 at 13:38
  • So basically, you are trying to pass data between two fragments? – Priyansh Kedia Apr 21 '21 at 13:49
  • yes...I ant to pass data betwween fragments – Suzzi Apr 21 '21 at 14:09
  • This has been already answered [here](https://stackoverflow.com/questions/16036572/how-to-pass-values-between-fragments) – Priyansh Kedia Apr 21 '21 at 18:00
  • yes I tried that too still its not working:( – Suzzi Apr 21 '21 at 21:15
  • @NRUSINGHAMOHARANA u there?? – Suzzi Apr 22 '21 at 09:32
  • When user opens the app and logged in as Admin and entered the quote in edit text and clicked on button, then what will happen ? – NRUSINGHA MOHARANA Apr 22 '21 at 09:35
  • The admin will update the quote every 24 hour...so when admin will change the quote in edit text and click on update quote button its should get updated in AccounFragment.java code or else it should reflect in Textview of AccountFragment.xml code – Suzzi Apr 22 '21 at 10:02
  • @Suzzi Sorry man , really I am confused. In AdminFragment update button is clicked, but right there do you want to switch the fragment ? If you just want passing data between fragments then please don't confuse others by putting technical words like Admin and User. Ideally Admin and User are just like client/server principles. So sorry man because I might have misunderstood. – NRUSINGHA MOHARANA Apr 22 '21 at 10:39
  • I could not see any relation between aAccountFragment and AccountFragment in the posted code. If you have latest updated code then post it and make question clear what exactly you want. Thanks – NRUSINGHA MOHARANA Apr 22 '21 at 10:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/231452/discussion-between-suzzi-and-nrusingha-moharana). – Suzzi Apr 22 '21 at 11:04
  • see https://github.com/moharananrusingha/hello_repository. You need to look at only AdminFragment.java(this has edit text), AccountFragment.java( this shows quote text). I have used LiveData to get the quoted text inside AccountFragment.java. Look at BottomNavigationViewModel.java. This ViewModel is used in BottomNavigationActivity.java as well. BottomNavigationActivity holds 3 fragments as told by you. – NRUSINGHA MOHARANA Apr 22 '21 at 13:42
  • Passing entered quote from AdminFragment.java to BottomNavigationActivity first. Then passing quote as LiveData and selecting third fragment i.e AccountFragment and shows the quote there. – NRUSINGHA MOHARANA Apr 22 '21 at 13:46
  • OK SURE I WILL CHECK THANK U SO MUCH !!!!!CHEERS – Suzzi Apr 22 '21 at 16:33
  • @NRUSINGHAMOHARANA no suitable constructor found for ViewModelProvider(FragmentActivity) bottomNavigationViewModel = new ViewModelProvider(requireActivity()).get(BottomNavigationViewModel.class); ^ constructor ViewModelProvider.ViewModelProvider(ViewModelStoreOwner,Factory) is not applicable (actual and formal argument lists differ in length) constructor ViewModelProvider.ViewModelProvider(ViewModelStore,Factory) is not applicable (actual and formal argument lists differ in length) this is the error which I am getting – Suzzi Apr 22 '21 at 17:01
  • I am getting this error – Suzzi Apr 22 '21 at 17:01
  • Check out my answer. – NRUSINGHA MOHARANA Apr 23 '21 at 04:47

1 Answers1

1

STEP-1: To take quote input from edittext inside AdminFragment.java. I am not posting it's xml file as nothing important there except an EditText. You can put it inside any Activity.

AdminFragment.java

    public class AdminFragment extends Fragment {

    public static AdminFragment newInstance() {
        return new AdminFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.main_fragment, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState){
        EditText quoteEditText = view.findViewById(R.id.quote_et);
        Button quoteBtn = view.findViewById(R.id.quote_btn);
        quoteBtn.setOnClickListener(v -> {
            String quote = quoteEditText.getText().toString();
            if (quote.isEmpty()){
                Toast.makeText(getContext(),"Please Enter Quote!",Toast.LENGTH_SHORT).show();
            }else {
                Intent intent = new Intent(getActivity(), BottomNavigationActivity.class);
                intent.putExtra("quote",quote);
                startActivity(intent);
            }
        });
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }
}

STEP-2: As you would have declared BottomNavigationActivity which would have 3 fragments namely SnapFragment.java , HomeFragment.java and AccountFragment.java. Quote text will be received from AdminFragment.java while click on updateQuote button inside BottomNavigationActivity. Once quote is received inside BottomNavigationActivity, I am opening 3rd Fragment i.e AccountFragment to show quoted text.

BottomNavigationActivity.java

    public class BottomNavigationActivity extends AppCompatActivity {

    String updatedQuote = "";
    BottomNavigationViewModel bottomNavigationViewModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottom_navigation);
        bottomNavigationViewModel = new ViewModelProvider(this).get(BottomNavigationViewModel.class);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_snap, R.id.navigation_home, R.id.navigation_account)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);

        updatedQuote = getIntent().getStringExtra("quote");
        Log.d("tisha==>>","Quote = "+updatedQuote);
        bottomNavigationViewModel.setQuoteLiveData(updatedQuote);

       // Below I am selecting 3rd Fragment to show Quote
        navView.setSelectedItemId(R.id.navigation_account);
    }

}

activity_bottom_navigation.xml

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

I am using ViewModel here, because it will be easier to pass quote data from current activity to AccountFragment.java as AccountFragment is not instantiated by it's constructor here. BottomNavigationViewModel is responsible for storing quote and provide wherever it is required.

BottomNavigationViewModel.java

public class BottomNavigationViewModel extends ViewModel {
    private final MutableLiveData<String> quoteLiveData;
    public String getQuoteLiveData(){
        return quoteLiveData.getValue();
    }
    public void setQuoteLiveData(String quote){
        quoteLiveData.setValue(quote);
    }
    public BottomNavigationViewModel(){
        quoteLiveData = new MutableLiveData<>();
    }
}

STEP-3: Now inside AccountFragment, I just get the view model which is created inside BottomNavigationActivity and collecting quote from that and displaying it in TextView.

AccountFragment.java

public class AccountFragment extends Fragment {


    private String mQuote;
    private BottomNavigationViewModel bottomNavigationViewModel;
    public AccountFragment() {
        // Required empty public constructor
    }

    public static AccountFragment newInstance(String quote) {
        return new AccountFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bottomNavigationViewModel = new ViewModelProvider(requireActivity()).get(BottomNavigationViewModel.class);
        mQuote = bottomNavigationViewModel.getQuoteLiveData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_account, container, false);
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState){
        TextView quoteView = view.findViewById(R.id.quote_tv);

        quoteView.setText(mQuote);
    }
}
NRUSINGHA MOHARANA
  • 1,489
  • 8
  • 13
  • The code under BottomNavigationActivity.java should I merge that code in my bottom_nav.java file. when I copied that code in my code ..that gave me n no.of errors – Suzzi Apr 23 '21 at 06:24
  • bottomNavigationViewModel = new ViewModelProvider(requireActivity()).get(BottomNavigationViewModel.class); also under requireActivity() its giving me an error. – Suzzi Apr 23 '21 at 06:24
  • What is the error? Try using getActivity() in place of requireActivity() though both are same. – NRUSINGHA MOHARANA Apr 23 '21 at 06:28
  • no suitable constructor found for ViewModelProvider(FragmentActivity) bottomNavigationViewModel = new ViewModelProvider(requireActivity()).get(BottomNavigationViewModel.class); ^ constructor ViewModelProvider.ViewModelProvider(ViewModelStoreOwner,Factory) is not applicable (actual and formal argument lists differ in length) constructor ViewModelProvider.ViewModelProvider(ViewModelStore,Factory) is not applicable (actual and formal argument lists differ in length) this is the error for requireActivity – Suzzi Apr 23 '21 at 06:30
  • getActivity() by using this also the error is there – Suzzi Apr 23 '21 at 06:33
  • no suitable constructor found for ViewModelProvider(FragmentActivity) bottomNavigationViewModel = new ViewModelProvider(getActivity()).get(BottomNavigationViewModel.class); – Suzzi Apr 23 '21 at 06:34
  • Are you using import androidx.lifecycle.ViewModelProvider; ? Check import – NRUSINGHA MOHARANA Apr 23 '21 at 06:35
  • Also check in your app level build.gradle file correct version of ViewModel library implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1' – NRUSINGHA MOHARANA Apr 23 '21 at 06:38
  • implementation'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1' should I add this dependency – Suzzi Apr 23 '21 at 06:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/231495/discussion-between-nrusingha-moharana-and-suzzi). – NRUSINGHA MOHARANA Apr 23 '21 at 06:41