How to handle TextInputLayout through Java - java

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?

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

Related

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.

ClickableSpan onClick not working android java

I am developing an android SMS application using java and I want to check if the received message contains a phone number. If yes, I want to make the phone number bold and underlined and when user clicks on it, I want to open a popup for options like Call, Add To Contacts etc. I have done everything except for the part where I want to show the popup when user clicks on the phone number. I tried using ClickableSpan but the onClick method never triggers. Here is what I have done so far:
String finalMsg;
String ifNumberExists = Util.extractNumber(message.getMessageBody());
String boldNum = "<b><u>" + ifNumberExists + "</u></b>";
if (message.getMessageBody().contains(ifNumberExists)) {
finalMsg = message.getMessageBody().replace(ifNumberExists, boldNum);
} else {
finalMsg = message.getMessageBody();
}
SpannableString spannableString = new SpannableString(boldNum);
spannableString.setSpan(new ClickableSpan() {
#Override
public void onClick(#NonNull View widget) {
Toast.makeText(viewMessageThreadActivity, "Text: ", Toast.LENGTH_SHORT).show();
}
}, 0, boldNum.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView messageBodyLabel = rowView.findViewById(R.id.message_body_label);
messageBodyLabel.setText(Html.fromHtml(finalMsg));
messageBodyLabel.setClickable(true);
messageBodyLabel.setMovementMethod(LinkMovementMethod.getInstance());
you are creating spannableString, but enver uses that, TextView have set Html.fromHtml(finalMsg) as a content. set this spannableString as text to get desired result (and onClick working)
messageBodyLabel.setText(spannableString.toString());

How can I add a sup number to a TextView programmatically?

How can I put that little number in the same column (textView) or how can I add two of them .
I have tried linearlayout horizantal but it did not worked How I want that text view to be look like;
ı want to add number like this image
//this my textView for adding to rows
txtColumnArray[i] = new TextView(this);
txtColumnArray[i].setTextColor(Color.BLUE);
txtColumnArray[i].setAllCaps(true);
txtColumnArray[i].setGravity(Gravity.CENTER);
txtColumnArray[i].setTypeface(Typeface.DEFAULT.DEFAULT_BOLD);
txtColumnArray[i].setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f);
// my last try is spanable but it is not working , I thought it may be
// coz of textView properties but couldnt find it.
spanableForQuestionNumbers[questionNumbersCount] = new SpannableString(questionNumbersCount + " " + LetterFormat[count]);
spanableForQuestionNumbers[questionNumbersCount].setSpan(new SuperscriptSpan(), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
txtColumnArray[i].setText(spanableForQuestionNumbers[questionNumbersCount]);
trCrosswordRow.addView(txtColumnArray[i]);

User input too long for alertdialog to handle

is there a way to handle long user inputs in alert dialogs? I have a small alert dialog that requires a few inputs and whenever the string input is too long, the other inputs and textviews get all squished up. Is there a way to put the string input on a different line or a way to wrap the other textfields? Im creating the alertdialog straight from code rather than XML.
LinearLayout layout = new LinearLayout(this);
EditText weight = new EditText(this);
EditText mark = new EditText(this);
TextView marktext = new TextView(this);
marktext.setText("Mark");
TextView weighttext = new TextView(this);
weighttext.setText("Weight");
mark.setInputType(InputType.TYPE_CLASS_NUMBER);
weight.setInputType(InputType.TYPE_CLASS_NUMBER);
//Assignment name input
EditText workType = new EditText(this);
workType.setInputType(InputType.TYPE_CLASS_TEXT);
TextView workTypeText = new TextView(this);
workTypeText.setText("Name of the work: ");
weight.setId(99);
mark.setId(100);
workType.setId(9999);
/*Spinner addworkspinner = new Spinner(this);
ArrayAdapter<String> addworkadapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, ClassManager.possiblework);
addworkspinner.setAdapter(addworkadapter); */
layout.addView(workTypeText);
layout.addView(workType);
layout.addView(marktext);
layout.addView(mark);
layout.addView(weighttext);
layout.addView(weight);
AlertDialog.Builder addwork = new AlertDialog.Builder(this);
addwork.setTitle("Add a piece of work");
addwork.setView(layout);
You may want to code a limit for your input text characters, wherein if it is over limit, it changes the nuumber of lines. Use (%) modulus to divide the number of characters in one certain line.
This information about
Android: Vertical alignment for multi line EditText (Text area) may also help in identifying what you need to change in EditText.

how to align text to top of a text view dynamically

I have hit a problem when trying to align my text to the top of a text view dynamically. I have a method that creates textviews dynamically. I have set the gravity of the text view to Gravity.TOP. Which a believe should place the text at the very top of the textView object. However when I run the project there is a gap of about 2 lines of text from the top of the object. I can work around this by changing the height of the textView but I would rather it loaded in an set the size depending on how much text there is too load into the textView.
Here is my function, it works well and does what it is supposed to do:
public void addItems(LinearLayout page,int id,String text,int row){
LinearLayout item = new LinearLayout(this);
item.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
TextView itemNum = new TextView(this);
//itemNum.setLayoutParams(new ViewGroup.LayoutParams(50,50));
itemNum.setWidth(50);
itemNum.setTextSize(40);
itemNum.setTextAppearance(getApplicationContext(), R.style.large_text);
itemNum.setLeft(0);
itemNum.setText(row+":");
LayoutParams numParams = new LinearLayout.LayoutParams(70, 70);
numParams.setMargins(25, 0, 0, 0);
TextView itemDetails = new TextView(this);
itemDetails.setLayoutParams(new ViewGroup.LayoutParams(150,LayoutParams.WRAP_CONTENT));
itemDetails.setText(text);
itemDetails.setWidth(100);
itemDetails.setLeft(5);
itemDetails.setRight(5);
itemDetails.setTop(0);
itemDetails.setVisibility(View.VISIBLE);
itemDetails.setGravity(itemDetails.getGravity() | Gravity.TOP);
itemDetails.setPadding(0,0,0,0);
Button delete = new Button(this);
//delete.setLayoutParams(new ViewGroup.LayoutParams(50,50));
delete.setText("Delete");
LayoutParams btnParams = new LinearLayout.LayoutParams(70, 70);
btnParams.setMargins(0, 0, 15, 0);
item.addView(itemNum,numParams);
item.addView(itemDetails);
item.addView(delete,btnParams);
page.addView(item);
Log.d("Loop","Loop is adding function in");
}
If anybody can see where I have gone wrong I would really appreciate it. The method above may seem very long winded but I am new to Java.
Not sure if it will help but here is my string to show in the textView:
String formatText = lat + ", " + lng + " /n" + result;

Categories