how to avoid characters stacking in EditText in android studio - java

I created a EditText and set maxLenght to 200. But when I type more than 200 characters and try to erase the text, first it erases the characters that are not shown. How do I avoid this?

If you are ok with using multiple lines for your EditText add android:inputType="text|textMultiLine", then all 200 characters will fit into the EditText.

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="#+id/fieldDescription"
android:hint="Descrição(até 200 caracteres)"
android:maxLength="200"
android:layout_marginBottom="15dp" />

Related

Problem with aligning textView to the end of LinearLayout

I am making chatting app and want to style messages as they are in messenger app.
And this is my code to style them:
textView.setTextColor(Color.parseColor("#FFFFFFFF"));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(400,0,10,10);
params.gravity = Gravity.END;
textView.setLayoutParams(params);
linearLayout.addView(textView);
This is my XML code:
<ScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="bottom"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="12dp"
android:background="#drawable/background"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toTopOf="#+id/editTextTextPersonName2"
app:layout_constraintTop_toBottomOf="#+id/name">
<LinearLayout
android:id="#+id/chatMessages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
And my TextViews are added to this ^ linear layout.
And this is what I am receiving in app: (don't translate messages they are only for test purposes and they are stupid)
And I do not why those gaps are made, someone please help me.
It is the normal behavior of textView. You can test on the SIGNAL app by typing a long word and it will move it to the next line and on the upper line, you will get to see the same space left as you are getting in yours.
If you mean to change the alignment of text in the textview then you can define the textAlignment property of textView according to your requirement.
That's happening because of the long length of the last word of your messages, if Android sees that he can't put a sequence of chars (word) into the current line, then it types it down on the next one, so to solve this problem you have 2 different solutions (or more):
1- But it may live a gap from the other side
textView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
2- It centers the text in its container which may live a gap from both sides
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
Good luck.

Cannot get editText to do a line break from enter (android)

So i have multiple edittext's in one constraint layout and i cannot get my multiline edittext to do a linebreak from enter, i have tried settings imeOptions to normal, actionUnspecified, actionNone and actionDone and no luck, it does not do a line break. Could not find anyone with this issue, only opposite =)
What am i doing wrong? here is the code for my edittext
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text|textImeMultiLine|textCapSentences"
android:ems="10"
android:singleLine="false"/>
have you tried android:maxLines, like android:maxLines="10"?

Dynamically made Layout with EditText - Android

This isn't much of a question, more of an explanation, I like to know whats happening and not treat things like a black box. I'm making an android app, and I created a form where you can dynamically make a new field to add to the form, and the field has 2 edit text next to each other.
formLayout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<EditText
android:id="#+id/team_form_player_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginRight="7dp"
android:layout_weight="1"
android:background="#color/white"
android:hint="#string/form_hint_team_player_name"
android:inputType="textCapWords"
android:padding="15dp"/>
<EditText
android:id="#+id/team_form_player_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#color/white"
android:hint="#string/form_hint_team_player_number"
android:inputType="number"
android:padding="15dp"/>
</LinearLayout>
And this is inside a button listener when clicked, it'll create the new field
View layout = getLayoutInflater().inflate(R.layout.layout_add_new_field_team, null);
EditText name = (EditText)layout.findViewById(R.id.team_form_player_name);
EditText number = (EditText)layout.findViewById(R.id.team_form_player_number);
playerName.add(name); //arraylist
playerNumber.add(number); //arraylist
containerLayout.addView(layout);
being that its a different layout i had to inflate the view. anyway, my question is this. how does this even work? by code, I'm adding an edit text, each time to its respectful array. im adding the same layout each time, im not even using "new" so its not instantiating anything new. and if I create 10 fields at once, then fill in the fields. and run this method
for(EditText edit : playerNumber) {
String test = edit.getText().toString();
System.out.println(test);
}
it gives me all the correct values in each field. How does that happen? Because on add field click, it instantly adds the edit text to the array with the fields EMPTY. and theres no code inputting the values into the array. I'm just boggled that this works and would like to know how it does. anyone have any idea?
.inflate(layoutResId) == "create new instance of this layout"
So basically every time you use inflate you are creating instances of all the Views in the layout, in your case a LinearLayout and two EditText Views inside it.

How do I make a counter display a variable?

So I have been trying to make an app that sort of counts things. Basically its a standard counter app. But, I'm having trouble actually displaying the number. I have the setup for the button:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button android:id="#+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"/>
<Button android:id="#+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:layout_below="#+id/button_1" />
</RelativeLayout>
However, I don't know how to actually create the display of the number increasing or decreasing in java. I've read on some places on how to change text with EditText and TextView, but how would I be able to do that with integers? As in, if the number displayed is 5, how can it be so that I also have an int or float with the same value?
TL;DR: How do I make a counter app? Additionally, How can I make the number displayed be a variable rather than text/a string?
you have to use String.valueOf("Your content") which will convert following values to string : boolean, char, char[], long, float, double, integer Object;

Layout issue for messaging app Android XML

I'm trying to create a messaging app in android and have created a custom list adapter that displays the message text and then the date as well. I have got them both displaying, now want them to be able to be displayed in a certain way. I want to be able to have sent messages on one side of the screen but received messages on the other, with the date appearing below/to the side of the message text
This is the custom xml I have used for each item on the list, any ideas on how to make them wrap appropriately and move to sides of the screen would be appreciated!
(Note: I've tried using LinearLayouts with weights and RelativeLayouts with android:layout_alignParentRight and things, but couldn't get it exactly how I wanted it so thought I'd ask here!)
(Don't think other code snippets are needed but can provide if wanted!)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/msgTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:paddingRight="2dp"
android:textStyle="bold"
android:textSize="15sp" />
<TextView android:id="#+id/msgDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Cheers
I've tried using LinearLayouts with weights
Whats wrong with that? android:layout_weight="0.4"
try to use not 'fill_parent', use 'match_parent';
also, replace this
android:textSize="15sp"
to this:
android:textAppearance="?android:attr/textAppearanceMedium"

Categories