Search code examples
javaandroidbackground-imagetextviewsetcontentview

Multiple views in Android


I have a background image:

android:background="@drawable/bg">

and a TextView:

final TextView theTimer = new TextView(this);

If I use setContentView I can only place one of them on the screen. How can I fit them both in one activity?


Solution

  • Do something like this -

    my_layout.xml file

    <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/ll"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" 
            android:background="@drawable/bg">
        </LinearLayout>
    

    Activity code

    setContentView(R.layout.my_layout);
    final TextView theTimer = new TextView(this);
    LinearLayout ll = (LinearLayout)findViewByID(R.id.ll);
    ll.addView(theTimer);