user3291590
user3291590

Reputation: 279

Android setText is not working

SetText is not working for an editText. i change the edittext id, name etc. but its not working. can anyone help me?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_message);

    buttonContact = (Button) findViewById(R.id.imageButton1);
    buttonSend = (Button) findViewById(R.id.imageButton2);
    textPhoneN = (EditText)findViewById(R.id.editText1);
    textSMS = (EditText) findViewById(R.id.editTextSMS);

    /////////// for set message into compose window
    textPhoneN.setText("sdf");
  }

textPhoneN is not set any text in oncreate().

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#6666E0"
android:gravity="end"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:background="#ffffff"
        android:hint="To"
        android:inputType="text|phone" >

        <requestFocus />
    </EditText>




    <Button
        android:id="@+id/imageButton1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="116dp"
        android:layout_height="30dp"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:background="#00004C"
        android:text="Add Contact"
        android:textColor="#ffffff"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/editTextSMS"
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="20dp"
        android:background="#ffffff"
        android:ems="10"
        android:gravity="top"
        android:hint="Message"
        android:inputType="textMultiLine"
        android:lines="5" />

    <Button
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_weight="1"
        android:background="#00004C"
        android:text="Send"
        android:textColor="#ffffff" />

</LinearLayout>

I follow your suggestion but still no result

Upvotes: 0

Views: 1063

Answers (2)

Rishabh Srivastava
Rishabh Srivastava

Reputation: 3745

Remove android:inputType="text|phone" and try.

Upvotes: 0

Firoze Rakib
Firoze Rakib

Reputation: 132

Did you set a android:inputType for the EditText in your XML? For example, if you set android:inputType="number" for your textPhoneN then you will not be able to set "sdf" because it isn't numeric data (it will not throw any error, but the text will not be set)

Instead, try textPhoneN.setText("12345");

If this doesn't work either, maybe your EditText is disabled in XML.

Upvotes: 2

Related Questions