Android when my text exceeded the width horizontal programatically [duplicate] - java

This question already has answers here:
How to adjust text font size to fit textview
(23 answers)
Closed 7 years ago.
I need to know how I can get the condition in the onclick, my edittext is a singleline and I want to know when the text is exceeded, for example my edittext has 6 characters "HOLAHI" I add a new character and this show "HOL...".
I want to know until to set the 7th character to change the size font.

Look here: How to adjust text font size to fit textview
Overview:
Create a CustomTextView
Extend TextView
override method onMeasure()
Do your calculations on the text width [Hint: get width by calling this.getWidth()]
Set calculated text size [Hint: use this.setTextSize()]

You can check the content of your EditText should ellipsize by the method provided in TextUtils, the following code is a simple example.
EditText editText = new EditText(this);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
String ellipseString = TextUtils.ellipsize(s, editText.getPaint(), editText.getWidth(), TextUtils.TruncateAt.END, false, new TextUtils.EllipsizeCallback() {
#Override
public void ellipsized(int start, int end) {
}
}).toString();
if (ellipseString.contains("\u2026")) {
Log.d(TAG, "ellipse");
}
}
});

Related

How to get the selected cursor position in EditText?

There is an EditText, what I want is when I select it ,I should get the cursor position. The maximum length of the edittext is 13.
Also in my edittext, the first three character's type is text and the other character's type is number. If the first three characters are selected, the input type of keyboard should show text. And if any other characters are selected ,then the input type should be number. How to do this?
I think this code can help you ❤ :
Class Code :
edittext = (EditText) findViewById(R.id.edtxt);
edittext.setInputType(InputType.TYPE_CLASS_TEXT);
edittext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(final CharSequence s, int start, int before, int count) {
if (edittext.getText().length() < 3 ) {
edittext.setInputType(InputType.TYPE_CLASS_TEXT);
}
else if (edittext.getText().length() == 3 ) {
edittext.setInputType(InputType.TYPE_CLASS_NUMBER);
}}
#Override
public void afterTextChanged(Editable s) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
});}}
Xml Code :
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:maxLength="13"
android:id="#+id/edtxt"
android:ems="10"/>

Calculation with textWatcher android

In my project I have 7 EditText field.
User will only need to fill their own value in first two EditText.
I need a series calculation like this...
(after user input) subtract ed1-ed2 the result automatically show in ed3 field.
multiply ed3*2 = result show in ed4 field automatically.
multiply ed3*3= result will show in ed5 field.
multiply ed3*4=result will show in ed6 field.
Finally sum of ed4+ed5+ed6= result will show in ed7 field automatically.
So how to do it any one please help.
Thank you in Advance.
All EditText use a same TextWatcher. A simple solution like this.
EditText et1;
EditText et2;
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int num1 = Integer.valueOf(et1.getText().toString()); // Make sure you get a number
int num2 = Integer.valueOf(et2.getText().toString());
// do your calculation
}
#Override
public void afterTextChanged(Editable s) {
}
};
et1.addTextChangedListener(textWatcher);
et2.addTextChangedListener(textWatcher);

How do I create multiple panes on Android and show them one by one?

I'm trying to create a Payment Gateway, which will look a little different from the tradition Gateway looks.
Here I want a simple form in which I'll have the following EditText fields:
Card Number
Expiry Date
CVV
Name on Card
Now I want it to work the following way:
When the user hasn't typed his card number in the Card EditText field, the rest of the EditText fields won't be visible.
When the user starts typing in his card number in the Card EditText, a new pane should slide out right below the Card EditText field & it should contain the Expiry Date & CVV EditText fields.
Once the user starts tying in the CVV field, a new pane should slide out right below the CVV field & should show the EditText field for Name on Card.
How do I achieve this on Android?
Set the Visibility Gone of Other in cardedittext textchange listener. Do for other too. In the Same way. Hide and Visible them on the Require text or character.
cardedittext.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() >= 16) {
expiry.setVisibility(View.VISIBLE);
} else {
expiry.setVisibility(View.GONE);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});

Changing textview on edittext ( when possible without a button)

I'm kinda new to java programming for android, so if i make stupid mistakes, i'm sorry.
So basically, what i wanna make is an app where if you type in the answer correctly, the next textview is gonna be displayed. And when the next textView is displayed, you're needing to give a answer to that textView, when the answer is given correctly. The textview changes again. And so on.
Does anybody have an idea how to do this?
If you don't undarstand what im saying, here is a example:
public class Game extends AppCompatActivity {
public static EditText editText_ans;
public static TextView textView_1;
String enteredText = editText.getText().toString();
If(enteredText = 3 && textView_1 = #string/1+2){
setText.textView_1(#string/3+4)
}
If(enteredText = 7 && textView_1 = #string/3+4){
setText.textView_1("100 - 23")
I'm really stuck and i hope that you guys wanna help me.
If you wanted to change the view without button you can use method addTextChangeListner() which will notify you when when the text hasbeen change for particular edittext.
edittext.addTextChangedListener(textWatcher);
private final TextWatcher textWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
textView.setVisibility(View.VISIBLE);
}
public void afterTextChanged(Editable s) {
if (s.length() == 0) {
textView.setVisibility(View.GONE);
} else{
textView.setText("You have entered : " + editText.getText());
}
}
};
int x=0; //to keep track of qustions
private List<String> mQuestionList=new ArrayList<>(); //array of question
private List<String> mAnswerList=new ArrayList<>(); //array of question answer
displayquestion.settext(mQuestionList.get(x);//displayquestion is textview
//nextquestion is the button when user click it will first check answer and than move to next question if answer is correct
nextquestion.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String answer=editText.getText().toString();
if(answer.equal(mAnswerList.get(x)){
x=x+1;
displayquestion.settext(mQuestionList.get(x); //answer is correct display next quesion
}else{
//wrong answer
}
}
});

Getting character count of EditText

I'm trying to get a character count of an EditText. I've looked into different properties of the EditText and TextView classes, but there doesn't seem to be a function that returns the amount of characters. I have tried to use the TextWatcher, but that is not ideal since sometimes I load a saved message into the EditText from the preferences, and TextWatcher does not count characters not typed right then.
Any help would be great!
Cheers!
Just grab the text in the EditText as a string and check its length:
int length = editText.getText().length();
EditText edittext;
private final TextWatcher mTextEditorWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//This sets a textview to the current length
textview.setText(String.valueOf(s.length());
}
public void afterTextChanged(Editable s) {
}
};
editText.addTextChangedListener(mTextEditorWatcher);
In Kotlin it's very easy
<EditText_name>.Text.length

Categories

Resources