ishhhh
ishhhh

Reputation: 647

How to set a background image in android

Im very new to this android development.I just started to create a hello world application.

I tried to set a background image and it is working fine.When i tried to add a text on the image it is not working.

I tried in the way below.Can anyone please help me where im going wrong.

in

Main.xml

<?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:orientation="vertical" 
    android:background="@drawable/andriod" 
    android:layout_gravity="center_vertical" 
    android:gravity="center_vertical"> 

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

HelloActivity.java

  import android.app.Activity;
  import android.os.Bundle;
  import android.widget.TextView;
  //import android.widget.ImageView;    

    public class HelloActivity extends Activity {
    //    /** Called when the activity is first created. */
    //    @Override
    //    public void onCreate(Bundle savedInstanceState) {
    //        super.onCreate(savedInstanceState);
    //        setContentView(R.layout.main);
    //    }    

    /** Called when the activity is first created. */
       @Override

       public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);

           TextView tv = new TextView(this);              
           tv.setText("This is the first andorid app");          
           setContentView(R.layout.main);         
       }
}

When running it is showing the background image but "This is the first andorid app" is not showing.

Hoping for your help

Upvotes: 7

Views: 18355

Answers (3)

Mudassir
Mudassir

Reputation: 13194

First you need to give the textview an id in XML file, so that it can be accessed in the Java source (note the android:id attribute in following XML);

<?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:orientation="vertical" 
    android:background="@drawable/andriod" 
    android:layout_gravity="center_vertical" 
    android:gravity="center_vertical"> 

    <TextView
        android:id="@+id/textView"

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

Now set the text in this textview;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView textView = (TextView) findViewId(R.id.textView);

    textView.setText("This is the first andorid app");
}

Note, how the textview declared in XML is accessed from here.

It is not

TextView textView = new TextView(this);

this will create a new textview. But what we need here is

TextView textView = (TextView) findViewId(R.id.textView);

this will refer to the textview declared in XML file above.

Upvotes: 5

NotCamelCase
NotCamelCase

Reputation: 1083

You must select the TextView in your layout file with its unique ID and then set it properties. But in ur code, u just create a text view with the text and not place it into the view.

TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("Your text here");

Upvotes: 3

Rajdeep Dua
Rajdeep Dua

Reputation: 11230

You need to fetch the TextView from the current content view .. add these lines after setContentView(..), also add an id to your text view in the layout

TextView tv = (TextView)findViewById(...)

Upvotes: 4

Related Questions