Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare EditText as TextView in Android

i have a xml layout main.xml like this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="fill_parent"
 android:gravity="center" >
 <EditText      
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="text"
 android:textColor="#000"
 android:textSize="16dp"
 android:id="@+id/edtxt"
 android:gravity="center|top|left"/>     
 </RelativeLayout>

and i declare this in my activity like this

public class MyActivity extends Activity {
private TextView txt = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    txt= (TextView)findViewById(R.id.edtxt);
 }

}

but i have no problem in my activity. why?

like image 958
Jhon Methew Avatar asked Oct 05 '12 07:10

Jhon Methew


2 Answers

see this link http://developer.android.com/reference/android/widget/EditText.html

and look here enter image description here

you will find your answer

like image 74
Deepak Swami Avatar answered Sep 20 '22 14:09

Deepak Swami


That would be because EditText is a subclass of TextView. You're casting an EditText to a TextView, which works because an EditText IS a TextView.

Check out the EditText's docs: http://developer.android.com/reference/android/widget/EditText.html

And here's a little something so that you can learn about inheritance, which is what is happening here: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

like image 38
Michael Avatar answered Sep 18 '22 14:09

Michael