I would like to set the background of edittext per line as opposed to android's block style of setting up the background i.e when a user writes some text, I should get the text entered by a user, split it into lines and set a background color for each line. Check the attached picture for references.
I've not been able to figure out how to achieve the same, please help. This is what I've done so far, thanks in advance.
Desired picture [1]: https://i.stack.imgur.com/LhpTQ.jpg
String allLines = userInput.getText().toString(); // userInput is an edittext field
String[] lines;
String linelimit = "\n";
lines= allLines.split(linelimit);
//this gives the lines in the user input text][1]][1]
Use 2 or more separate TextView/EditText instead. If number of lines are different during execution then add Textview/EditText dynamically in LinearLayout.
It is not possible in any view (that I know of) because there is only one background for every view, or EditText and cannot be separated for each line.
If you are using an EditText that user can edit, then you will have to calculate width of EditText as text changes and if its width exceeds a limit, you can add another EditText below it.
Related
Is there a way to let a user write in single and multiple lines in EditText, inputType textMultiline is for Multiline only and text for single line only, is there a way to have both?
Multiline attribute normally allows a user to take advantage of multilines. But incase u want to achieve that, the alternative would ve to use teo edittexts. Give one the multiline attribute and the keep singleline for the other. Since single line takes precidence, you might want to hide the multiline edittext onCreate. Set a length (number of characters) at which you the user should now be switched to the multiline edittext. Save the input added at the time, hide the singleline editText then show the multiline after you have set the original text from the singleline edittext.
I hope that helps.
Can I somehow color the text inside EditText without using Spannable?
I will be glad to any link.
Now I use Regex for search keywords and dynamically color the text inside EditText. (I use Kotlin coroutines)
Editable.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
But when I open the text from large Text file 600+ lines, my UI starts to slow down.
I'm trying to achieve the following:
Here, with every input the color of dash changes and the number of dashes is also equal to the maxLength of the EditText.
How do I achieve this in Android?
You can use material-code-input library to achieve a dashed EditText.
What I'm wanting to do is use the user's input to determine the size of a matrix, then create the appropriate amount of edit texts for them to fill out the elements. I think I know how to add in new edit texts dynamically, but I don't know how to address them, because they have no id since they are being made dynamically.
you can set the index of the EditText as id.
for(int i=0;i<max;i++){
EditText et=new ExitText(this);
//.........
et.setId(i);
}
And call findViewById(0); and so on
I started android apps development today and wanted to make a small app that takes some text as input (a color) and returns the text input with the color chosen by the user.
I started by creating a colors.xml in this fashion:
<color name="white">#FFFFFF</color>
<color name="yellow">#FFFF00</color>
...
Then I created an EditText in the main activity, and an activity for displaying the colored text, which included in the onCreate:
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(70);
textView.setText(message);
Which worked fine, but I could not manage a way to retreive the text string and match it with the color in colors.xml. I was thinking something on the lines of parameter substitution in BASH, like:
textView.setBackgroundColor(getResources().getColor(R.color.XXXXXX));
But I do not know what to put instead of "XXXXXX", as putting "message" would not work, but putting the direct color name (e.g. red) works, but only for one color.
I am trying to avoid case statements, also because I would like to make it as general as possible for many colors. Can anyone suggest if this is possible, in this way or in another way. I am sorry if this is trivial, but I do not have much confidence with Java.
Thank you in advance.
Look at the documentation of the Color class here.
Basically, if you want to set the color from the message string, you could do:
textView.setBackgroundColor(Color.parseColor(message));
I haven't tested it but it should work. The colors.xml file isn't needed for this.
Read the documentation of the Color class and don't forget to use a try-catch for the IllegalArgumentException that the parseColor() method can throw.
this R.color.XXXXXX is a integer. You need to grab the whole parameter instead of just XXX, basically you can store the colors in a static integer variables and then use them depending on your condition. Hope this helps.