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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:31:54+00:00 2026-05-28T14:31:54+00:00

I have a custom ViewGroup (blue background) that when touched sometime during the app’s

  • 0

I have a custom ViewGroup (blue background) that when touched sometime during the app’s life cycle, I want to add an arbitrary xml defined view layout (whitebox to be in foreground) to that custom view.

Is there any way to do this outside of the activity’s onCreate?

whitebox.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="#FFFFFF"
    android:orientation="vertical" >
</LinearLayout>

TestDynamicLayoutActivity.java

package net.lapasa.testdynamiclayout;

import android.app.Activity;
import android.os.Bundle;

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

        setContentView(new MyCustomViewGroup(this));
    }
}

MyCustomViewGroup.java

package net.lapasa.testdynamiclayout;

import java.lang.annotation.Target;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

public class MyCustomViewGroup extends ViewGroup
{

    private Context context;

    public MyCustomViewGroup(Context context)
{
    super(context);
    this.context = context;
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
    this.setBackgroundColor(Color.BLUE);

}

@Override
public boolean onTouchEvent (MotionEvent event)
{
    if (event.getAction() == MotionEvent.ACTION_DOWN)
    {
        View whiteBoxView= LayoutInflater.from(context).inflate(R.layout.whitebox, this);
        ViewGroup targetParent = (ViewGroup) whiteBoxView.getParent();
        targetParent.removeView(whiteBoxView);
        this.addView(whiteBoxView);
    }
    return true;
}
}

The block of code in the ACTION_DOWN doesn’t do anything except present a black screen =( What is going on?

  • 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-28T14:31:55+00:00Added an answer on May 28, 2026 at 2:31 pm

    Did I get that right – you just want to have some blue view containing a white view that shows up when the blue view is clicked?

    Then it would be better not to implement your own ViewGroup but using one of the existing ones like FrameLayout. Set the layout’s background to blue. Add a child View with it’s background set to white and it’s visibility set to View.INVISIBLE or View.GONE.

    Add a click handler to the ViewGroup that sets the visibility of your white View to View.VISIBLE.

    You could do it like this:

    main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
                 android:background="#00f"
                 android:onClick="onBlueClick"
    
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent">
      <View android:id="@+id/whiteBox"
            android:background="#fff"
            android:visibility="gone"
    
            android:layout_gravity="center"
            android:layout_width="20dp"
            android:layout_height="20dp"/>
    </FrameLayout>
    

    MyActivity.java:

    package com.example;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    
    public class MyActivity extends Activity {
    
      private View whiteBox;
    
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        whiteBox = findViewById(R.id.whiteBox);
      }
    
      @SuppressWarnings({"UnusedDeclaration"}) // wired by layout xml
      public void onBlueClick(final View view) {
        if (whiteBox != null) {
          whiteBox.setVisibility(whiteBox.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have custom classes that I currently instantiate within App.xaml as resources. I want
I have a ListView powered by a custom adapter that dynamically changes the background
I have a custom view that extends ViewGroup . It includes a ProgressBar and
I am having trouble positioning view elements on a custom ViewGroup that I have
I have a few models that need to have custom find conditions placed on
I have a custom CursorAdapter that is taking items from a database and displaying
I have a Custom ListView that contains a vertical LinearLayout of horizontal LinearLayouts for
I have created one gridview and use custom adapter for that. I added two
I have created a custom dialog, the code is below. The problem is that,
I have created one gridview and use custom adapter for that. I added two

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.