Search code examples
androidtextviewandroid-edittexttextcolor

Android application crashes because of TextColor in main.xml


I have developed a basic UI activity page in android with a TextView and EditText and a Button. I have set the TextView textColor to some hexadecimal value and my application crashes because of that, If I remove that textColor means the application runs perfectly. Target sdk = 2.3 here is the code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical|center_horizontal"
    android:orientation="vertical"
    android:padding="15dp">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:text="Enter search Key"
        android:textColor="#ff3300"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/searchkey"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Search Key ..." />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button" />
</LinearLayout>

Can we set textColor for TextView or is there something else is wrong with the code.

Thanks in advance...


Solution

  • You need to define color in the resources file in res/values folder

    <resources>
    <color name="mycolor">#33b5e5</color>
    </resources>
    

    And specify using @color/mycolor in Layout file

    <TextView
        android:id="@+id/textView"
        android:layout_gravity="left"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Enter search Key"
        android:textColor="@color/mycolor"
        android:textStyle="bold" />