Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7840941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:58:31+00:00 2026-06-02T15:58:31+00:00

So I was trying to wrap a RelativeLayout in a ScrollView programmatically, but I

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:58:33+00:00Added an answer on June 2, 2026 at 3:58 pm

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

    setContentView(rl);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a scroll view that has a nested relativeLayout view.
https://i.stack.imgur.com/lbs7l.png I'm trying to setup admob below the background, but I keep getting admob
I'm trying to write an activity that has a RelativeLayout on top with options,
I'm trying to wrap my head around Object Oriented programming. But I'm having some
I'm trying to wrap my head around custom paging in the ASP.NET Gridview, but
I'm trying to create an android screen that has a TableLayout anchored to the
I am trying to make this ViewGroup programmatically, but I am having trouble doing
I'm trying to make scrollable RelativeLayout, that contains some custom Views. This is the
I'm trying to build a part of a Android View Programmatically, but unfortunately I'm
I'm trying to get a TextView to wrap it's text over multiple lines, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.