Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a background image in android

Tags:

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

like image 963
ishhhh Avatar asked Feb 06 '12 11:02

ishhhh


People also ask

How do I set a picture as a background?

Choose a background wallpaperRight-click your desktop. Select Set wallpaper & style. Select Wallpaper. Select one of the images to set as your wallpaper.


2 Answers

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.

like image 157
Mudassir Avatar answered Sep 20 '22 21:09

Mudassir


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(...)
like image 37
Rajdeep Dua Avatar answered Sep 19 '22 21:09

Rajdeep Dua