简体   繁体   中英

How to change LinearLayout programmaticaly to fit numbers entered to EditText without moving other parts of layout?

Im trying to create LinearLayout programmaticaly and I would want to allow user to put number range. Now it looks like that:

在此处输入图像描述

But when I try to enter more digits eg. 100, 101 or 3,50 it dissapears.

在此处输入图像描述

I guess there is not enough space for it to be shown, but I can't figure out what is wrong. Generally I don't want to move + and - buttons when the user enters some values so I guess it should be hardcoded. There would be up to 5-6 digits only, so I need space just for it, but as I said, I can't find the place, where I can change it as my changes eiter move entire layout or doesn't do anything.

Below is my code:

LinearLayout horizontalLayout = new LinearLayout(mContext);
            LinearLayout titleLayout = new LinearLayout(mContext);
            LinearLayout countLayout = new LinearLayout(mContext);
            ImageButton buttonAdd = new ImageButton(mContext);
            ImageButton buttonSub = new ImageButton(mContext);
            TextView titleTextView = new TextView(mContext);
            EditText countEditText = new EditText(mContext);

            final int[] currentCount = {defaultValue};

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            LinearLayout.LayoutParams linearLayout = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1f);
            LinearLayout.LayoutParams utilParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

            horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
            horizontalLayout.setLayoutParams(params);

            utilParams.gravity = Gravity.CENTER_VERTICAL;
            titleLayout.setOrientation(LinearLayout.HORIZONTAL);
            titleLayout.setPadding(0, pxFromDp(mContext, 16),0, pxFromDp(mContext, 16));
            titleLayout.setLayoutParams(linearLayout);

            countLayout.setOrientation(LinearLayout.HORIZONTAL);
            countLayout.setPadding(0, pxFromDp(mContext, 16),0, pxFromDp(mContext, 16));
            countLayout.setLayoutParams(linearLayout);

            utilParams.setMargins(0,0,pxFromDp(mContext, 16f),0);
            titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP,16);
            titleTextView.setText(title);
            titleTextView.setLayoutParams(utilParams);
            titleLayout.addView(titleTextView);

            utilParams.setMargins(pxFromDp(mContext, 16f),0,pxFromDp(mContext, 16f),0);
            buttonSub.setImageResource(R.drawable.ic_remove);
            buttonSub.setLayoutParams(utilParams);
            buttonSub.setBackgroundColor(mContext.getResources().getColor(R.color.fsm_survey_btn));
            buttonSub.setColorFilter(ContextCompat.getColor(mContext, R.color.fsm_white), android.graphics.PorterDuff.Mode.SRC_IN);
            countLayout.addView(buttonSub);

            countEditText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
            countEditText.setText(String.valueOf(defaultValue));
            countEditText.setLayoutParams(linearLayout);
            countEditText.setGravity(Gravity.CENTER);
            countLayout.addView(countEditText);

            buttonAdd.setImageResource(R.drawable.ic_add_24);
            buttonAdd.setLayoutParams(utilParams);
            buttonAdd.setBackgroundColor(mContext.getResources().getColor(R.color.fsm_survey_btn));
            buttonAdd.setColorFilter(ContextCompat.getColor(mContext, R.color.fsm_white), android.graphics.PorterDuff.Mode.SRC_IN);
            countLayout.addView(buttonAdd);

            horizontalLayout.addView(titleLayout);
            horizontalLayout.addView(countLayout);

What you can do is to create separate LinearLayout.LayoutParams for the edittext and add addTextChangedListener to that edit text and on onTextChanged write switch statement to increase the weight of the edittext on increase of length of the input number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM