Friday, August 8, 2014

Hardcoded string "TextView", should use @string resource

It's a very simple problem in android, just two steps. It's happen when you do not create/update your string file and do not reference in your layout. Please follow the bellow steps:

Step 1:
Please go to your string.xml file (it's located in res > values > string.xml) and update this file.

Step 2:
Now go to your layout file and reference to your string file like : @string/... 

Example:
Suppose your TextView like this in your layout and you show that warring massage: 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_marginBottom="60dp" 
        android:text="TextView" /> 

now you have to go your string file and add this TextView like:

<resources>

    <string name="TextView">Hello World</string>
<resources>

and now update your layout file like :

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_marginBottom="60dp" 
        android:text="@string/TextView" /> 

Now it's ok. It's very good practice to write this way because this allows you to update every occurrence of the words "Hello World" in all layouts at the same time by just editing your strings.xml file. It's also useful for supporting multiple languages as a separate strings.xml file.


Thank you.
Allah Hafez.

Friday, August 1, 2014

r cannot be resolved to a variable android - error

It's a very simple problem in android, just two steps. Please follow the bellow steps:

Step 1:
Go to the properties of your project and click Java Build Path with Order and Export like:




Step 2:
Then select Android option and ok like:



Now Build your project then R.java file will be generated in your gen package and problem will be solved.


Thank you.
Allah Hafez.