Ad

How To Handle TextInputLayout Through Java

- 1 answer

I want to make an Interface for route-planning purposes that has 2 states 1 - Inputting start and target addresses 2 - Doing the same thing through GPS location services

I have made the Content layout through the XML file but since I need to switch between them dynamically I tried to set up the same thing via Java button events. But whenever I try to setup the TextInputLayout with an EditText inside through Java and try to compile and launch it on my Android emulator, which is a Pixel 2 API lvl 28 it gives me a java.lang.IllegalArgumentError with the message 'The style on this component requires your app theme to be Theme.AppCompat (or a descendant)'.

    inputTop = new android.support.design.widget.TextInputLayout(getApplicationContext());
    inputTop.setLayoutParams(new ConstraintLayout.LayoutParams(Constraints.LayoutParams.FILL_PARENT - 120, (int) convertDpToPx(getApplicationContext(), 45)));
    inputTop.setX(convertDpToPx(getApplicationContext(), 174));
    inputTop.setY(convertDpToPx(getApplicationContext(), 60));

    inputTopInner = new EditText(getApplicationContext());
    inputTopInner.setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    inputTopInner.setGravity(Gravity.CENTER);
    inputTopInner.setInputType(InputType.TYPE_CLASS_TEXT);
    inputTopInner.setTextColor(Color.rgb(151,151,151));
    inputTopInner.setTextSize(12);
    inputTopInner.setHint("Standort");
    inputTopInner.setEms(10);
    inputTop.addView(inputTopInner);

    layout.addView(viewTop);
    layout.addView(inputTop);

Anybody know how to make this run as it does when inputted into the XML file?

Ad

Answer

This is how we did for our project:

We kept Parent as LinearLayout and add views in that layout

 LiearLayout parentView = findViewById(R.id.parentView);

            TextInputLayout emailTextInputLayout = new TextInputLayout(this, null, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);

            emailTextInputLayout.setHint("Please Enter Email Address");
            emailTextInputLayout.setBoxBackgroundMode(TextInputLayout.BOX_BACKGROUND_OUTLINE);
            emailTextInputLayout.setBoxCornerRadii(5, 5, 5, 5);
            TextInputEditText edtEmail = new TextInputEditText(emailTextInputLayout.getContext());
            emailTextInputLayout.addView(edtEmail);

            parentView.addView(emailTextInputLayout);


            TextInputLayout passTextInputLayout = new TextInputLayout(this, null, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);

            passTextInputLayout.setHint("Please Enter Password");
            passTextInputLayout.setBoxBackgroundMode(TextInputLayout.BOX_BACKGROUND_OUTLINE);
            passTextInputLayout.setBoxCornerRadii(5, 5, 5, 5);
            TextInputEditText edtPass = new TextInputEditText(passTextInputLayout.getContext());
            passTextInputLayout.addView(edtPass);

            parentView.addView(passTextInputLayout);
Ad
source: stackoverflow.com
Ad