Error while converting textview to edittext in android studio. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Error while converting textview to edittext in android studio.

This is the code of the activity_main.xml and MainActivity.java /////////////////////////////////**************************File activity_main.xml*************///////////////////// <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="176dp" android:layout_marginLeft="176dp" android:layout_marginTop="80dp" android:layout_marginEnd="176dp" android:layout_marginRight="176dp" android:hint="Enter your name" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.511" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.306" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="161dp" android:layout_marginLeft="161dp" android:layout_marginTop="92dp" android:layout_marginEnd="162dp" android:layout_marginRight="162dp" android:layout_marginBottom="342dp" android:text="Button" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView" app:layout_constraintVertical_bias="1.0" /> <

25th May 2020, 9:46 AM
Aman Kumar
Aman Kumar - avatar
6 Answers
+ 2
Edittext extends Textview so you can only cast an Edittext to a Textview but not a Textview to an Edittext. But you can change the attributes of the Textview so it acts like an Edittext.
25th May 2020, 3:47 PM
Jnn
Jnn - avatar
+ 1
This is only the xml code. What error do you get exactly?
25th May 2020, 12:31 PM
Jnn
Jnn - avatar
+ 1
/androidx.constraintlayout.widget.ConstraintLayout> ///////////////////////**********************/*MainActivity.java//////////////////**********************//// package com.example.interactiveelements; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class MainActivity extends AppCompatActivity { public void ClickFunction(View view){ EditText textview = (EditText) findViewById(R.id.textView); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } Error Unexpected cast to EditText: layout tag was TextView (Id bound to a text view in activtiy_main.xml)
25th May 2020, 3:06 PM
Aman Kumar
Aman Kumar - avatar
+ 1
But than you still need to change it to Textview in the java file, if you dont change the Textview in the XML to an Edittext
25th May 2020, 3:58 PM
Jnn
Jnn - avatar
0
So what code I had to write in order to get value from the text box. Like Log.i(tag:"Value", textview.getText().toString();
25th May 2020, 3:52 PM
Aman Kumar
Aman Kumar - avatar
0
You could add android: editable ="true" in the xml file.
25th May 2020, 3:57 PM
Jnn
Jnn - avatar