0

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);
    }

}
  • Do you have any special reasons why you use `Views` programmatically and not using XML? It is kinda hard to read! – Xenolion Dec 19 '17 at 22:00
  • I prefer to hard-code everything because although it takes up more lines of code, I can easily change variables and I guess it's just personal preference for unknown reasons too. I personally can read it rather easily, but I can see where many could find it difficult to understand. –  Dec 19 '17 at 22:04
  • Okay I have got you, let me try to see! First I would like you to change all occurences of `getApplicationContext` to simply `MainActivity.this` try that and give me a feedback! There are some parts the Application Context is unnecessary just an Activity is enough! – Xenolion Dec 19 '17 at 22:10
  • So are you saying swap the getApplixationContext and replace it with MainActivity.this? Or the other way round? –  Dec 19 '17 at 22:13
  • Try removing the `getApplicationContext` to `MainActivity.this`. There are a number of parts of your code you are using the very big context of the application while the activity is enough. So put `MainActivity.this`. – Xenolion Dec 19 '17 at 22:17
  • Okay. I've gone out so will have to test it tomorrow. Thanks anyways –  Dec 19 '17 at 22:41
  • All the best there! **Happy Coding!**. – Xenolion Dec 19 '17 at 22:43

3 Answers3

0

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...

Roberto Martucci
  • 1,237
  • 1
  • 13
  • 21
0

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.

ams73
  • 146
  • 5
0

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!