smartypantstemple
smartypantstemple

Reputation: 29

RelativeLayout in ScrollView Programmatically

So I was trying to wrap a RelativeLayout in a ScrollView programmatically, but I keep getting an error that tells me that the RelativeLayout already has a parent.

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.NumberPicker;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.TimePicker;
/*
 * Ids:
 * 1-first Text box
 * 2-DatePicker
 * 3-TimePicker
 */

public class TwoActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //scrollView
        ScrollView sv = new ScrollView(this);

        Log.v("Ro", "Starts app");
        //create Relative Layout
        RelativeLayout rl = new RelativeLayout(this);
        Log.v("Ro", "Relative Layout: "+rl);

        //From Text View
        RelativeLayout.LayoutParams lptv1 = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        lptv1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        TextView tv1 = new TextView(this);
        tv1.setText("From: 4-24-12 11:59pm");
        tv1.setId(1);
        rl.addView(tv1, lptv1);

        //To Text View
        RelativeLayout.LayoutParams lptv2 = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        lptv2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        TextView tv2 = new TextView(this);
        tv2.setText("To: 4-25-12 12:00am");
        rl.addView(tv2, lptv2);

        //DatePicker
        DatePicker startDate = new DatePicker(this);
        startDate.setId(2);
        RelativeLayout.LayoutParams lptv3 = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        lptv3.addRule(RelativeLayout.BELOW,1);
        rl.addView(startDate, lptv3);

        //TimePicker
        TimePicker startTime = new TimePicker(this);
        startTime.setId(3);
        RelativeLayout.LayoutParams lptv4 = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        lptv4.addRule(RelativeLayout.BELOW,2);
        rl.addView(startTime, lptv4);
        setContentView(rl);

        //NumberPicker
        NumberPicker duration = new NumberPicker(this);
        RelativeLayout.LayoutParams lptv5 = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        lptv5.addRule(RelativeLayout.BELOW,3);
        rl.addView(duration, lptv5);

        sv.addView(rl);

        //setContentView(rl);
        setContentView(sv);
        //setContentView(R.layout.main);
    }
}

Why does it keep on giving me a problem?

Upvotes: 0

Views: 2064

Answers (1)

blessanm86
blessanm86

Reputation: 31779

You are calling setContentView 2 times. Just remove the first setContentView it should work. Its right above the numberpicker

setContentView(rl);

Upvotes: 1

Related Questions