Unable to enter text in edit text view

1.4k views Asked by At

I was following a toolbar tutorial and when I tried to add an edit text below it and then ran it on my physical device, I was unable to enter any text into it. I don't know what I did wrong. I applied a custom text color to the edit text in the apptheme, but I doubt it that the problem is because of this.

This is my xml code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">
<include
    android:id="@+id/toolbar"
    layout="@layout/tool_bar"/>

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:layout_below="@+id/toolbar"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textSize="30sp"
    android:hint="enter text"/>

and this is what I have in my style.xml file:

sources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/ColorPrimary</item>
    <item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
    <item name="android:editTextStyle">@style/CustomEditText</item>
</style>
<style name="CustomEditText">
    <item name="android:textColor">#ab42</item>
</style>

This is my main activity:

public class MainActivity extends AppCompatActivity {
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main,menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if(id == R.id.action_search){
        Toast.makeText(this,"SEARCH",Toast.LENGTH_LONG).show();
    }else if(id == R.id.action_user){
        Toast.makeText(this,"User",Toast.LENGTH_LONG).show();
    }else{
        Toast.makeText(this,"Overflow",Toast.LENGTH_LONG).show();
    }

    return super.onOptionsItemSelected(item);
}

enter image description here

2

There are 2 answers

0
Georgi Koemdzhiev On BEST ANSWER

I found the problem, myself. I forgot to inherit from some of the theme's base edit texts like that:

<style name="CustomEditText" parent="Widget.AppCompat.EditText">
    <item name="android:textColor">#ab42</item>
</style>
0
Piotr Wittchen On

Your code seems to be OK.

I am not sure, if it will help, but you can try to update your layout code as follows:

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:layout_below="@+id/toolbar"
    android:textSize="30sp"
    android:hint="enter text"/>

I've removed layout_alignParentLeft and layout_alignParentStart attributes and changed value of the layout_width from wrap_content to match_parent.

It's only my guess.

Moreover, you can explicitly define EditText as editable.

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:layout_below="@+id/toolbar"
    android:textSize="30sp"        
    android:hint="enter text"
    android:editable="true"/>