androidandroid-viewgroup

getChildCount() always returning 1 even after using addView


I am designing a simple calculator with history feature. All the previous results are stored in form of text view and are added to linear layout using addView(childview). The problem is that even after adding textviews to linear layout, linearlayout.getChildCount() is always returning 1. For simplicity I will only add relevent code here.

Calculator.java:

public class Calculator extends Fragment {
TextView resultTextView;
LinearLayout linearLayout;
ScrollView scrollView;
TextView et_temp, sign;

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

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated (view, savedInstanceState);
    initControl(view);
    initControlListener();
    vibrator=(Vibrator) getActivity ().getSystemService (Context.VIBRATOR_SERVICE);
}

  private void initControlListener() {
    button0.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onNumberButtonClicked("0");
        }
    });
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onNumberButtonClicked("1");
        }
    });
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onNumberButtonClicked("2");
        }
    });
    button3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onNumberButtonClicked("3");
        }
    });
    button4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onNumberButtonClicked("4");
        }
    });
    button5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onNumberButtonClicked("5");
        }
    });
    button6.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onNumberButtonClicked("6");
        }
    });
    button7.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onNumberButtonClicked("7");
        }
    });
    button8.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onNumberButtonClicked("8");
        }
    });
    button9.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onNumberButtonClicked("9");
        }
    });

    buttonClear.setOnLongClickListener (new View.OnLongClickListener () {
        @Override
        public boolean onLongClick(View view) {
            onClearHold ();
            return false;
        }
    });

    buttonClear.setOnClickListener (new View.OnClickListener () {
        @Override
        public void onClick(View view) {
            onClearButtonClicked ();

        }
    });

    buttonSubstract.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            onOperatorButtonClicked("-");
        }
    });
    buttonAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onOperatorButtonClicked("+");
        }
    });
    buttonMul.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onOperatorButtonClicked("X");
        }
    });
    buttonDiv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onOperatorButtonClicked("/");
        }
    });
    buttonEqual.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onEqualButtonClicked();
        }
    });
    buttonDeci.setOnClickListener (new View.OnClickListener () {
        @Override
        public void onClick(View view) {
            onNumberButtonClicked (".");
        }
    });
    buttonReciprocal.setOnClickListener (new View.OnClickListener () {
        @Override
        public void onClick(View view) {
            onSpecialFunctionClick("Reci");
        }
    });
    buttonSqr.setOnClickListener (new View.OnClickListener () {
        @Override
        public void onClick(View view) {
            onSpecialFunctionClick("Sqr");
        }
    });
    buttonSqrRoot.setOnClickListener (new View.OnClickListener () {
        @Override
        public void onClick(View view) {
            onSpecialFunctionClick("SqrRoot");
        }
    });

    linearLayout.setOnLongClickListener (new View.OnLongClickListener () {
        @Override
        public boolean onLongClick(View view) {

            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity ());
            builder.setMessage("Clear History?");
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                        scrollView.removeAllViews();
                        scrollView.setFillViewport (true);
                        scrollView.setClickable (true);
                }
            });
            builder.setNegativeButton ("Cancel", new DialogInterface.OnClickListener () {@Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
            });
            AlertDialog alert = builder.create();
            alert.show();

            return false;
        }
    });

}
//For adding history/views to linear layout
    public void createHistory(String str){
    if(!str.equals ("")) {
        et_temp = new TextView (getActivity ());
        et_temp.setGravity (Gravity.RIGHT);
        et_temp.setPadding (0, 5, 10, 5);
        et_temp.setTextColor (Color.rgb (50, 50, 50));
        et_temp.setTextSize (20);
        et_temp.setHovered (true);
        String x= sign.getText ().toString () + "   " + str;
        et_temp.setText (x);
        Log.e("dsfsd", (String.valueOf (scrollView.getChildCount ())));
        linearLayout.addView (et_temp);
    }

}

related xml file fragment_calculator:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:background="?android:windowBackground"
android:orientation="vertical"
android:weightSum="1.0">

<ScrollView
    android:id="@+id/scroll_view"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="bottom"
    android:layout_weight="1"
    android:clickable="true"
    android:fillViewport="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="false">

        <LinearLayout
            android:id="@+id/linear_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:clickable="true"
            android:gravity="bottom"
            android:orientation="vertical">

        </LinearLayout>

    </RelativeLayout>
</ScrollView>
</LinearLayout>

Solution

  • You called scrollView.getChildCount() instead of linearLayout.getChildCount().