How to Set Text in an Edittext

How To Set Text In An EditText

If you check the docs for EditText, you'll find a setText() method. It takes in a String and a TextView.BufferType. For example:

EditText editText = (EditText)findViewById(R.id.edit_text);
editText.setText("This sets the text.", TextView.BufferType.EDITABLE);

It also inherits TextView's setText(CharSequence) and setText(int) methods, so you can set it just like a regular TextView:

editText.setText("Hello world!");
editText.setText(R.string.hello_world);

Setting text in EditText Kotlin

Use setText(String), since editText.text expects an Editable, not a String.

How to set text in edittext which should not editable

My Question is how to put TextView in EditText?

Not Possible, because This is not Built in behavior of EditText

How is it possible?

You can use a TextView and EditText in a LinearLayout with android:orientation="horizontal"

SAMPLE CODE

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/colorBackgroundFloating"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:orientation="horizontal">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp"
android:text="Enter Name" />

<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="123"
android:padding="5dp" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:orientation="horizontal">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp"
android:text="Enter Name" />

<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="123"
android:padding="5dp" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:orientation="horizontal">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp"
android:text="Enter Name" />

<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="123"
android:padding="5dp" />

</LinearLayout>

</LinearLayout>

OUTPUT

enter image description here

How to change text in EditText

Simply call the setText() method. Here is an example:

EditText e = (EditText) findViewById(R.id.editText);
e.setText("New Text");

setText doesn't set text to EditText

onActivityResult() is not the last method called when returning to an Activity. You can refresh your memory of the Life Cycle in the docs. :)

As we discussed in the comments, if you call setText() again in methods like onResume() this will override any text set in onActivityResult().

The same goes for Fragments, you need to make updates in onViewStateRestored() method (which was added in API 17).

Setting text in EditText with input type number

setText(balance.toString()) is correct. Your error is in the code you posted in your comment.

The java.lang.ClassCastException is because you are casting something that is not a Long to a Long. I think maybe you need clarification on the difference between converting something and casting something. Casting (using the as keyword) does not convert one type of object into another. Casting is you telling the compiler that the object already is that other type of object.

When you cast, the compiler takes your word for it that you know better than the compiler, and it allows you to treat it like that other type of object. Then at runtime, if your declaration to the compiler was incorrect, it will crash with a ClassCastException.

For those reasons, you should rarely be using as at all. It is for special cases, like if you had a List<Any> and you want to pull something out of it and treat it as a more specific class. Or when you are writing a class that works with generics and run into a situation where the compiler cannot figure out a type. Or when the object implements multiple interfaces and there is ambiguity about which function overload you want to pass it to.

When you get text from a TextView, it is some kind of CharSequence object, specifically a SpannableStringBuilder. If you want to use it as a Long, you must convert to a Long, not cast to a Long. You should also account for the fact that the existing content passed to your listener may not be a non-null text that can truly be converted to a Long. For that, you can do something like using the Elvis operator to set a default value if the text cannot be converted.

textAmount.addTextChangedListener { text ->
val input : Long = text?.toString()?.toLongOrNull() ?: 0L
if(input > cash){
textAmount.setText(cash.toString())
}
}

Unable to set text in EditText Kotlin

You are passing an int to setText method.
setText method is overloaded and it calls the public final void setText(@StringRes int resid) since this method expects an int and that too a string resource but the int you are passing to it is not a valid StringRes so it causes ResourceNotFoundException.

The solution is to convert the int to String and then pass it to the setText method.

yourTextView.setText((curVal - 1).toString())

or

yourTextView.setText("${curVal - 1}")

Whichever way you prefer

Is it possible to setText for an Edittext for a long data type element in android

Why not you make it to String first:

yourEditText.setText(String.valueOf(yourLongVariable));


Related Topics



Leave a reply



Submit