How to set background image in android

Im very new to this Android development. I just started creating a welcome application for the world.

I tried setting the background image and it works great. When I tried to add text to the image, it does not work.

I tried the next way. Someone please help me where I am 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); } } 

At startup, a background image is displayed, but "This is the first andorid application" is not displayed.

Hope for your help

+6
source share
3 answers

You must select a TextView in your layout file with its unique identifier, and then set its properties. But in ur code, you just create a text view with text and don't put it in the view.

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

First you need to specify the textview id in the XML file so that it can be accessed in the Java source (note the android: id attribute in the 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 text view;

 @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"); } 

Notice how you access the text view declared in XML.

Is not

 TextView textView = new TextView(this); 

this will create a new text view. But here we need

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

this will refer to the text view specified in the XML file above.

+6
source

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

 TextView tv = (TextView)findViewById(...) 
+4
source

Source: https://habr.com/ru/post/907760/


All Articles