addView(View) not adding objects to the contentView

523 Views Asked by At

In the following code, you will see I have created my layouts all programmatically. I have set the contentView and added a view inside of that contentView, which contains 2 buttons.

The contentView is being set and working fine, however all involving .addView(View) and not.

All help is appreciated. Thank you.

My code:

package com.idleappsinc.ampup;

import android.content.res.Resources;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.transition.AutoTransition;
import android.transition.TransitionManager;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;


public class MainActivity extends AppCompatActivity {

    int rootWidth = 411;
    int rootHeight = 731;

    RelativeLayout rootView;
    RelativeLayout container;
    Button createParty;
    Button joinParty;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        rootView = new RelativeLayout(this);
        RelativeLayout.LayoutParams rootViewLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

        rootView.setId(R.id.rootView);
        rootView.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorBackground));
        rootView.setLayoutParams(rootViewLayoutParams);

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        container = new RelativeLayout(this);
        RelativeLayout.LayoutParams containerLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, (getWidth() / rootHeight) * 220);
        container.setId(R.id.relativeLayoutContainer);
        container.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.white));
        containerLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        container.setLayoutParams(containerLayoutParams);

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        createParty = new Button(new ContextThemeWrapper(getApplicationContext(), android.R.style.Widget_Material_Button_Borderless));
        RelativeLayout.LayoutParams createPartyButtonParams = new RelativeLayout.LayoutParams((getWidth() / rootWidth) * 150, (getHeight() / rootHeight) * 50);
        createParty.setId(R.id.createPartyButton);
        createParty.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorCreatePartyButton));
        createParty.setText(R.string.create_party);
        createParty.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        createParty.setAllCaps(false);
        createParty.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.white));
        createPartyButtonParams.width = (getWidth() / rootWidth) * 150;
        createPartyButtonParams.height = (getHeight() / rootHeight) * 50;
        createPartyButtonParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        createPartyButtonParams.topMargin = (getHeight() / rootHeight) + 50;
        createParty.setLayoutParams(createPartyButtonParams);

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        joinParty = new Button(new ContextThemeWrapper(getApplicationContext(), android.R.style.Widget_Material_Button_Borderless));
        RelativeLayout.LayoutParams joinPartyButtonParams = new RelativeLayout.LayoutParams((getWidth() / rootWidth) * 150, (getHeight() / rootHeight) * 50);
        joinParty.setId(R.id.joinPartyButton);
        joinParty.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorJoinPartyButton));
        joinParty.setText(R.string.join_party);
        joinParty.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        joinParty.setAllCaps(false);
        joinParty.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.white));
        joinPartyButtonParams.width = (getWidth() / rootWidth) * 150;
        joinPartyButtonParams.height = (getHeight() / rootHeight) * 50;
        joinPartyButtonParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        joinPartyButtonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        joinPartyButtonParams.bottomMargin = (getHeight() / rootHeight) + 50;
        joinParty.setLayoutParams(joinPartyButtonParams);

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        rootView.addView(container);
        container.addView(createParty);
        container.addView(joinParty);
        setContentView(rootView);

        createParty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                expandCreatePartyButton();
            }
        });

        joinParty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

    }

    private void expandCreatePartyButton() {

        // Hide join party button
        beginTransition(250);
        joinParty.setVisibility(View.INVISIBLE);

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) createParty.getLayoutParams();
        RelativeLayout.LayoutParams containerParams = (RelativeLayout.LayoutParams) container.getLayoutParams();

        // Expand and set the relative layout height
        containerParams.height = containerParams.height + (int) dpToPx(50);
        container.setLayoutParams(containerParams);

        // Expand and set the button to the correct width and height
        params.topMargin = 0;
        params.width = containerParams.width;
        params.height = containerParams.height;
        createParty.setLayoutParams(params);


    }

    public static float dpToPx(int dp) {
        return (dp * Resources.getSystem().getDisplayMetrics().density);
    }

    public static float pxToDp(int px) {
        return (px / Resources.getSystem().getDisplayMetrics().density);
    }

    public void beginTransition(int delay) {
        // Create a delay based on given parameters
        AutoTransition delayTime = new AutoTransition();
        delayTime.setDuration(delay);

        // Create transition
        //TransitionManager.beginDelayedTransition(container, delayTime);
    }

    public int getWidth() {
        return  Math.round(Resources.getSystem().getDisplayMetrics().widthPixels / Resources.getSystem().getDisplayMetrics().density);
    }

    public int getHeight() {
        return Math.round(Resources.getSystem().getDisplayMetrics().heightPixels / Resources.getSystem().getDisplayMetrics().density);
    }

}
3

There are 3 best solutions below

3
Roberto Martucci On

Every time you perform

getWidth() / rootWidth

or

getHeight() / rootHeight

You're dividing a small value for a bigger one and it is an integer division, so the result is always 0.

You're assigning to all of your children 0 as width and height. So you simply don't see them even if they are correctly added.

Find alternatives values for your layout params or at least perform a float division.

For example:

joinPartyButtonParams.width = (int) (((float) getWidth() / rootWidth) * 150);
joinPartyButtonParams.height = (int) (((float) getHeight() / rootHeight) * 50);

and so on...

0
ams73 On

I believe it is a matter of reordering your code, I would try it like this and hope it works let us see:

    setContentView(rootView);
    rootView.addView(container);
    container.addView(createParty);
    container.addView(joinParty);

if this do not work, try attaching one view at a time to catch the problematic one if exists.

0
AudioBubble On

I have figured out the problem! Before, the width was always as it should be, however it was the height that was returning 0 every time. After debugging it, I have realized the height the code was returning was returning the height minus the UIDepth (In my case 48dp) Because I had set the height to include that, it was returning 0 every time. Thanks for all of the help everybody!