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 6906161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:16:26+00:00 2026-05-27T08:16:26+00:00

I have some code below that creates a RelativeLayout and adds a button to

  • 0

I have some code below that creates a RelativeLayout and adds a button to it. It draws fine when created from onCreate(), shows a blue button on a red background.

But when creating a new ReltativeLayout when clicking on the first the new button shows on a black background, so my RelativeLayout doesn’t show.

The funny thing is that it works if I comment out the line that adds the button, so somehow the button is impacting the relative layout.

Thanks for any help.

package com.android.mikeviewtester;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;

public class ViewTesterActivity extends Activity {

    void createNewView( int bgColor, int btnColor ) {

        // create a new relative layout
        android.widget.RelativeLayout newView = new android.widget.RelativeLayout( this );

        // create a button
        Button btn = new Button( this );

        // set the background color
        btn.setBackgroundColor( btnColor );

        // create a layoutParams struct for adding the button to the relative layout view
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams( 100, 100 );

        // add the button to the relative layout
        newView.addView( btn, params );

        // set the relative layout background color
        newView.setBackgroundColor( bgColor );

        // set the ontouch listener for the relativeLayout
        newView.setOnTouchListener( (android.view.View.OnTouchListener) mOnTouchListener );

        // create the layout to fill the activity
        RelativeLayout.LayoutParams viewParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);

        // set the relative layout as the view
        setContentView( newView, viewParams );
    }

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

        // create and set the initial view
        createNewView( Color.RED, Color.BLUE );
    }

    // ios - (void)buttonWasPressed:(id)whichButton {
    private android.view.View.OnTouchListener mOnTouchListener = new android.view.View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {

            if ( v != null )
                v.onTouchEvent( event );

            if ( event.getAction() == MotionEvent.ACTION_UP ) {

                // create and set a new view
                createNewView( Color.GREEN, Color.MAGENTA );
            }

            return true;
        }
    };
}
  • 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-05-27T08:16:27+00:00Added an answer on May 27, 2026 at 8:16 am

    Make a change in your code, i.e. add v.setVisibility(View.GONE); before calling createNewView(Color.GREEN, Color.MAGENTA);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bit of code below that creates a button on a html
I have some really funky code. As you can see from the code below
I have been writing some code that creates a generic blog. Its features are
I have some code in umbraco that is run and creates a javascript file
I have some code that creates a CSV file. The order of the rows
Below, I have written simple code that re-creates problems that have emerged as my
I have some problem with my cfml website. I have used the below code
I have some code which collects points (consed integers) from a loop which looks
I have some code in a javascript file that needs to send queries back
I have some code that gives a user id to a utility that then

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.