1

Currently I understand I can create a XML Layout and pass that to setContentView(...), or I can pass a Customized view to setContentView(...).

But what if I want to combine the elements of both? Is it possible to use a layout first, then to programmatically add to the UI via java code?

For example: How could I create a view that uses an Asset background picture with an added loading widget on top?

ADDED INQUIRY: Right now and I think of a View and a Layout as two things for setContentView to display. But can a View hold a layout within it to be displayed?

3
  • 1
    Is there a particular reason the image is in your assets folder instead of your drawable folder? Commented May 30, 2012 at 22:33
  • not at all, i didn't even know there's a different folder. What should go in assets and should go in drawable? Commented May 30, 2012 at 22:46
  • 1
    You would typically put images in the res/drawable folder so you can reference them by ID and pull them into your activity. Commented May 31, 2012 at 17:20

2 Answers 2

1

Yes its possible to set an XML layout with setContentView(), and programmatically add more views/widget to that layout. Here's a short example.

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/background_image">

    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Some text"/>

    <LinearLayout 
        android:id="@+id/custom_content_root"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
            android:orientation="vertical">

        <!-- This is where we will add views programmatically -->
    </LinearLayout>
</LinearLayout>

TestActivity.java

public class TestActivity extends Activity {
private LinearLayout mRoot;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set the layout
    setContentView(R.layout.main);

    // Get the Linearlayout we want to add new content to..
    mRoot = (LinearLayout) findViewById(R.id.custom_content_root);

    // Create a TextView for example
    TextView moreText = new TextView(this);

    // Set the layout parameters of the new textview.
    moreText.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,  LayoutParams.WRAP_CONTENT));

    moreText.setText("More text :)");

    // Add the new textview to our existing layout
    mRoot.addView(moreText);
}
}

The result is an activity with background_image.png as background, and two textviews with text ;)

You can add any type of view (TextView, EditText, ImageView, Buttons etc) this way.

Sign up to request clarification or add additional context in comments.

7 Comments

Should I create a XML Layout for each one of my screens in my application? How do I associate a screen with its XML layout?
When you say screen, you probably mean Activity. You usually create one XML layout for each Activity, and set it via setContentView().
When I say screen I mean for example: load screen, main screen, personal screen. How does one XML layout generalize for the entire app?
One XML layout does not usually generalize for the entire app, and what you call screens are in reality Activities. Lets use your example: That app would have 3 activities, one for each "screen". Lets call them LoadingActivity.java, MainActivity.java and PersonalActivity.java. Each of the activities would have its own layout, loading_layout.xml, main_layout.xml and personal_layout.xml. In the onCreate() method of each Activity, you would specify the correct layout for that activity with setContentView().
This is the usual paradigm for Android apps! You navigate between Activities using Intents, and the Activity method startActivity(Intent i). Check out developer.android.com/guide/topics/fundamentals/activities.html There's a subsection explaining how to start a new Activity. You can give me points by checking the checkbox to the left of my answer.
|
0

Yes, it is possible to add widgets after using setContentView(). It's also possible to inflate XML layouts yourself using LayoutInflater.

You could add the loading widget to a layout that was defined inside your XML by getting it using findViewById and then using a method from ViewGroup.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.