user1481122
user1481122

Reputation: 21

How do I add a RelativeLayout (from XML) to a LinearLayout that I define programmatically?

I've looked around at other questions and haven't found anything that quite matches this scenario. I have defined a RelativeLayout in XML. I want to put this entire RelativeLayout below a GraphView object (from a class that I made separately). I figured the best way to put the RelativeLayout below the GraphView is to just stick both of these inside a LinearLayout, which I have tried to define programmatically.

This is what I have so far - there's something wrong with it, but I'm not quite sure what.

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class Grapher extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        LinearLayout ll = new LinearLayout(this); 
        GraphView gv = new GraphView(this); 
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.mainRelativeLayout);
        ll.addView(gv); 
        ll.addView(rl); 
        setContentView(ll); 

And the xml....

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/mainRelativeLayout"
    android:orientation="vertical">
    <Button
        android:id="@+id/second"
        android:clickable="true"
        android:background="@drawable/button_chooser"
        android:layout_width="70dp"
        android:layout_height="40dp"
        android:text="@string/second" 
        android:textColor="@color/white"
        android:textSize="15sp"/>
    <Button
        android:id="@+id/alpha"
        android:clickable="true"
        android:background="@drawable/button_chooser"
        android:layout_width="70dp"
        android:layout_height="40dp"
        android:layout_below="@id/second"
        android:layout_alignParentLeft="true"
        android:text="@string/alpha"
        android:textColor="@color/white"
        android:textSize="15sp" />
    <Button
        android:id="@+id/mode"
        android:clickable="true"
        android:background="@drawable/button_chooser"
        android:layout_width="70dp"
        android:layout_height="40dp"
        android:layout_toRightOf="@id/second"
        android:text="@string/mode"
        android:textColor="@color/white"
        android:textSize="15sp" />
</RelativeLayout>

Thanks for your help. I have a hunch that this is just something very simple that I'm looking over.

Upvotes: 1

Views: 2008

Answers (1)

Chris
Chris

Reputation: 23171

Activity.findViewById searches the view bound to the activity (usually as a result of calling setLayout(R.layout.yourlayout);). Since you are not calling this, there are no views currently active so calling findViewById will return null.

What it looks like you're trying to do is inflate a layout from xml and then add it to a view programatically. What you should do is something like this:

Inflater inflater = LayoutInflater.from(context);
//this will inflate the mainRelativeLayout into the linear layout you called "ll"
inflater.inflate(R.id.mainRelativeLayout, ll);

Upvotes: 3

Related Questions