1

I'm adding view dynamically in fragment which works fine but when I'm navigating to another fragment and return back to first fragment all layout is set as initial one (dynamically added view is not present). So here I have to save UI state untill my fragment's parent activity is not closed.

Initial Fragment Class Code

package com.hp.billingapp.ui.billfragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.textfield.TextInputEditText;
import com.hp.billingapp.R;
import com.hp.billingapp.ui.BillingActivity;
public class OrderFragment extends Fragment {
    Button pay, buttonAdd;
    LinearLayout itemLayout;
    Integer item = 1, emptyChk;
    Float total;
    ConstraintLayout constraintLayout;
    Integer layoutId;

    public OrderFragment() {
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_order, container, false);
        pay = root.findViewById(R.id.pay);
        pay.setOnClickListener(v -> {
            datafetch(root);
        });
        itemLayout = root.findViewById(R.id.itemLayout);
        constraintLayout = root.findViewById(R.id.constraintLayout);
        buttonAdd = root.findViewById(R.id.button);
        buttonAdd.setOnClickListener(v -> addView());
        return root;
    }

    private void datafetch(View root) {
        Log.i("heetcount", String.valueOf(itemLayout.getChildCount()));
        emptyChk = 0;
        total = (float) 0;
        for (int i = 0; i < itemLayout.getChildCount(); i++) {
            View itemView = itemLayout.getChildAt(i);
            TextView number = itemView.findViewById(R.id.number);
            AutoCompleteTextView name = itemView.findViewById(R.id.namet);
            TextInputEditText price = itemView.findViewById(R.id.pricet);
            AutoCompleteTextView type = itemView.findViewById(R.id.typet);
            if (name.getText().toString().isEmpty() || price.getText().toString().isEmpty() || 
            type.getText().toString().isEmpty()) {
                Snackbar.make(constraintLayout, "Please fill the required fields", 
            Snackbar.LENGTH_SHORT).show();
                emptyChk += 1;
            } else {
                total = total + Float.parseFloat(price.getText().toString().trim());
            }
        }
        if (emptyChk == 0) {
            Log.i("heettotal", total.toString());
            OrderFragmentDirections.ActionOrderFragmentToBillingFragment action = 
            OrderFragmentDirections.actionOrderFragmentToBillingFragment(total);
            Navigation.findNavController(root).navigate(action);
            
            ((BillingActivity) getActivity()).heading("b");
        }

    }

    private void addView() {
        item = item + 1;
        String text = "Item " + item;
        View itemView = getLayoutInflater().inflate(R.layout.item_layout, null, false);
        TextView number = itemView.findViewById(R.id.number);
        ImageButton remove = itemView.findViewById(R.id.remove);
        AutoCompleteTextView nametiet = itemView.findViewById(R.id.namet);
        AutoCompleteTextView typetiet = itemView.findViewById(R.id.typet);
        TextInputEditText pricetiet = itemView.findViewById(R.id.pricet);
        number.setText(text);
        remove.setOnClickListener(v -> removeView(itemView));
        itemLayout.addView(itemView);
    }

    private void removeView(View itemView) {
        Log.i("heetview", String.valueOf(itemView));
        itemLayout.removeView(itemView);
    }

}

Initial Fragment Layout Code

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/backdropcenter"
    android:theme="@style/Theme.MaterialComponents.NoActionBar"
    tools:context=".ui.billfragment.OrderFragment">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="80dp"
        android:overScrollMode="never">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/itemLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <include layout="@layout/item_layout" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <Button
                    android:id="@+id/button"
                    android:layout_width="300dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="50dp"
                    android:background="@drawable/allcornerround"
                    android:fontFamily="@font/montserrat"
                    android:text="Add Item"
                    android:textAllCaps="false"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/pay"
                    android:layout_width="300dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="20dp"
                    android:background="@drawable/allcornerround"
                    android:fontFamily="@font/montserrat"
                    android:text="Pay"
                    android:textAllCaps="false"
                    android:textSize="18sp"
                    android:textStyle="bold" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

Dynamically Added Layout 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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:theme="@style/Theme.MaterialComponents.NoActionBar">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/number"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:text="Item 1"
            android:textColor="#191b20"
            android:textSize="18sp"
            android:textStyle="bold" />

        <ImageButton
            android:id="@+id/remove"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:background="#00000000"
            android:src="@drawable/ic_close"
            app:tint="#191b20" />
    </LinearLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/nametil"
        style="@style/InputDropDown"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:elevation="5dp"
        android:hint="Item Name"
        app:boxBackgroundMode="filled"
        app:endIconMode="dropdown_menu"
        app:endIconTint="#AA191b20"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/nametil">

        <AutoCompleteTextView
            android:id="@+id/namet"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/montserrat"
            android:gravity="center_vertical"
            android:inputType="none"
            android:textColor="#191b20"
            android:textCursorDrawable="@drawable/cursor"
            android:textSize="18sp" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/typetil"
        style="@style/InputDropDown"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:elevation="5dp"
        android:hint="Type"
        app:boxBackgroundMode="filled"
        app:endIconMode="dropdown_menu"
        app:endIconTint="#AA191b20"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/nametil">

        <AutoCompleteTextView
            android:id="@+id/typet"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/montserrat"
            android:gravity="center_vertical"
            android:inputType="none"
            android:textColor="#191b20"
            android:textCursorDrawable="@drawable/cursor"
            android:textSize="18sp" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/pricetil"
        style="@style/InputText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:elevation="5dp"
        android:hint="Price"
        app:boxBackgroundMode="filled"
        app:endIconMode="clear_text"
        app:endIconTint="#AA191b20">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/pricet"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fontFamily="@font/montserrat"
            android:gravity="center_vertical"
            android:inputType="numberDecimal"
            android:textColor="#191b20"
            android:textCursorDrawable="@drawable/cursor"
            android:textSize="18sp" />
    </com.google.android.material.textfield.TextInputLayout>

</LinearLayout>

Snaps of issue

Initial state when first fragment starts

Dynamically view added and information filled

First fragment after returning from another fragment

HEET PATEL
  • 11
  • 1

0 Answers0