3

I have requirement where I need to combine a programmatically generate layout and a xml. What approach should I take? Please let me know. Thank you.

Generate layout :

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    LinearLayout audio = new LinearLayout(this);

    audio.setGravity(Gravity.BOTTOM | Gravity.CENTER);

    RecordButton = new RecordButton(this);
    audio.addView(RecordButton,
                new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.CENTER | Gravity.BOTTOM));

    PlayButton = new PlayButton(this);
    audio.addView(PlayButton,
                new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.CENTER | Gravity.BOTTOM));

    setContentView(audio);
}

xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:gravity="top">

<Button
android:id="@+id/next"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="130dp"
android:background="#54C571"
android:text="Next"
android:textColor="#FFFFFF"
android:textSize="23sp"
android:textStyle="bold" />
4
  • How about some_view_in_xml.addView(generated_layout);? Commented Sep 17, 2016 at 16:10
  • @KNeerajLal what about the linear layout? How should I include them? Is it like this audio.xml.addView(generated)? I tried and it said that the xml cannot be resolve Commented Sep 17, 2016 at 16:30
  • Show your xml layout. Commented Sep 17, 2016 at 16:32
  • @KNeerajLal Updated the xml. The xml has 1 button at the center of the page and the generated layout include 2 buttons at the bottom of the page. Commented Sep 17, 2016 at 16:44

1 Answer 1

3

Create a container view to hold your view and add the generated view to that,

This is the xml,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:gravity="top">

    <Button
        android:id="@+id/next"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="130dp"
        android:background="#54C571"
        android:text="Next"
        android:textColor="#FFFFFF"
        android:textSize="23sp"
        android:textStyle="bold" />

    <!-- create a container view to hold your view -->
    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/next" />

</RelativeLayout>

In your code,

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.the_above_layout);

    LinearLayout container = (LinearLayout) findViewById(R.id.container);


    LinearLayout audio = new LinearLayout(this);

    audio.setGravity(Gravity.BOTTOM | Gravity.CENTER);

    RecordButton = new RecordButton(this);
    audio.addView(RecordButton,
                new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.CENTER | Gravity.BOTTOM));

    PlayButton = new PlayButton(this);
    audio.addView(PlayButton,
                new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.CENTER | Gravity.BOTTOM));


    // add the view to your container
    container.addView(audio);
}
Sign up to request clarification or add additional context in comments.

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.