Skippy
Skippy

Reputation: 1590

Android programmatically added views not showing in UI

I am very new to Java/Android, and probably trying to run before I can walk, but I have had some success starting on a version of the old 'Mastermind' code guessing game. My current stumbling block is that having entered my first guess of the code and generated the score for this guess, I want to present a new row of 'holes' ready to accept my next guess (the first set of holes is static in my activity_main.xml file). I have a LinearLayout (vertical orientation) called guesses to hold the rows of holes, and each row of holes is a LinearLayout (horizontal orientation) containing a TextView, to indicate which guess number this is, and a number of ImageViews for the empty guess holes and score holes.

I have written code to get the guesses Layout as the parent layout, create a new horizontal LinearLayout and use addView to add this into the guesses layout. I then switch my 'parent' layout to point to the new horizontal LinearLayout I've just created and create the various other TextView and ImageView items and add these to this new horizontal LinearLayout.

This code all runs without any errors. I just don't see any of my new layouts in the UI. What have I missed?

Extract of the code so far:

parentLayout = (LinearLayout)findViewById(R.id.guesses);
LinearLayout guessLayout = new LinearLayout(MainActivity.this);
guessLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                        getResources().getIdentifier("@dimens/guessLayoutHeight", null, getPackageName()));
int pxMargin = (int) TypedValue.applyDimension(
                        TypedValue.COMPLEX_UNIT_DIP,
                        getResources().getIdentifier("@dimens/guessLayoutMargin", null, getPackageName()),
                        getResources().getDisplayMetrics());
layoutParams.setMargins(pxMargin, 0, pxMargin, 0);
guessLayout.setLayoutParams(layoutParams);
guessLayout.setId(guessNumber*10);
parentLayout.addView(guessLayout);

// Add the Guess# label
parentLayout = (LinearLayout)findViewById(guessNumber*10);
TextView guessLabel = new TextView(MainActivity.this);
layoutParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 11);
layoutParams.gravity = Gravity.CENTER_VERTICAL;
guessLabel.setText("Guess " + guessNumber);
guessLabel.setLayoutParams(layoutParams);
guessLabel.setId(guessNumber*100);
parentLayout.addView(guessLabel);

// Add the guess positions
ImageView guessPosition;
pxMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics());
for (int i = 1 ; i <=4 ; i++ ) {
    guessPosition = new ImageView(MainActivity.this);
    layoutParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 6);
    if (i == 1) {
        layoutParams.setMarginStart(pxMargin);
    } else {
        layoutParams.setMarginStart(0);
    }
    guessPosition.setLayoutParams(layoutParams);
    guessPosition.setId(guessNumber*10 + i);

    guessPosition.setImageResource(getResources().getIdentifier("@drawable/blank_guess", null, getPackageName()));
    guessPosition.setTag("blank_guess");
    guessPosition.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    parentLayout.addView(guessPosition);
}

[Edit - the following additional question can be ignored. It's been answered] This code is in the onClickListener for my Score Button. The other thing I want to do, having created the new ImageView items for my guess holes, is call setOnDragListener to use a listener that I've defined in the enclosing onCreate method of MainActivity.java. Is it possible to reference this listener from within another listener's code? I have the following declaration at the Class level:

ImageView guess1 = null;

...and what I was hoping to do was something like

guess1 = findViewById(guessNumber*10 + 1);
guess1.setOnDragListener(MainActivity.this.guessDragListener);

but it's telling me it can't resolve symbol guessDragListener.

Upvotes: 0

Views: 753

Answers (2)

cutiko
cutiko

Reputation: 10497

You are adding the same view 4 times. The variable is not recreated but is changing its reference

for (int i = 1 ; i <=4 ; i++ )
ImageView guessPosition = new ImageView(MainActivity.this)
...

And the whole thing for the margins can be reduced to

int margin = getResources().getDimens(R.dimens.name)

That value is on the needed DPs

Upvotes: 0

einUsername
einUsername

Reputation: 1619

Regarding your second question: I'm not sure i understood it properly. Do you want to use the same OnDragListener for multiple Views? If so, try this:

View view = findViewById(R.id.view);
View view2 = findViewById(R.id.view2);
View.OnDragListener onDragListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     onDragListener = new View.OnDragListener() {
        @Override
        public boolean onDrag(View v, DragEvent event) {
            return false;
        }
    };

    view.setOnDragListener(onDragListener);
    doStuff();
}

void doStuff(){
    view2.setOnDragListener(onDragListener);
}

Upvotes: 1

Related Questions