Question

Here's an image of my problem: enter image description here The two small images in the Green and Red rectangles are the images I want centered. They should fall directly in the middle of both of thier parent's respectively.

Here's why it's not working (As far as I can tell): I've set the width of the wrapper to 0 in order to avoid conflicts with the weight set for each wrapper. This lets me divide the screen right down the middle and divide content on either side. Unfourtunately, I think it also means that each image is trying to center itself on the 0dp portion of the wrapper rather than the dynamic width generated by the weight.

Here's the code, see for yourself:

public void mergeHotels(Hotel keepHotel, Hotel absorbHotel, ArrayList<Hotel> absorbArray){
    Context context = getApplicationContext();

    //get the current player and let him choose what to do with stock first
    Player thisPlayer = players[getPlayerIndexByPlayOrder(CURRENT_TURN)];

    //create the dialog for exchanging stocks       
    ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.AppTheme);
    AlertDialog.Builder stockExchangeDialog = new AlertDialog.Builder(ctw);
    stockExchangeDialog.setTitle(getResources().getString(R.string.stock_exchange_dialog_title));

    LinearLayout dialogLayout = new LinearLayout(context);
    dialogLayout.setOrientation(LinearLayout.VERTICAL);

        LinearLayout dialogHeader = new LinearLayout(context);
        dialogHeader.setOrientation(LinearLayout.HORIZONTAL);
        dialogHeader.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 50, 1.0f));

            View blankSpace = new View(context);
            blankSpace.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 2.0f));

            LinearLayout tileOneWrapper = new LinearLayout(context);
            tileOneWrapper.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f));
            tileOneWrapper.setBackgroundColor(Color.GREEN);

                ImageView tileOne = new ImageView(context);
                String tileOneResourceName = "tile_" + keepHotel.getName().toLowerCase();
                int tileOneResourceId = getResources().getIdentifier(tileOneResourceName, "drawable", getPackageName());
                tileOne.setBackgroundResource(tileOneResourceId);
                LinearLayout.LayoutParams tileOneParams = new LinearLayout.LayoutParams(40, 40);
                tileOneParams.gravity = Gravity.CENTER;
                tileOne.setLayoutParams(tileOneParams);



            tileOneWrapper.addView(tileOne);

            LinearLayout tileTwoWrapper = new LinearLayout(context);
            tileTwoWrapper.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f));
            tileTwoWrapper.setBackgroundColor(Color.RED);

                ImageView tileTwo = new ImageView(context);
                String tileTwoResourceName = "tile_" + absorbHotel.getName().toLowerCase();
                int tileTwoResourceId = getResources().getIdentifier(tileTwoResourceName, "drawable", getPackageName());
                tileTwo.setBackgroundResource(tileTwoResourceId);
                LinearLayout.LayoutParams tileTwoParams = new LinearLayout.LayoutParams(40, 40);
                tileTwoParams.gravity = Gravity.CENTER;
                tileTwo.setLayoutParams(tileTwoParams);

            tileTwoWrapper.addView(tileTwo);

        dialogHeader.addView(blankSpace);
        dialogHeader.addView(tileOneWrapper);
        dialogHeader.addView(tileTwoWrapper);

        LinearLayout dialogBody = new LinearLayout(context);
        dialogBody.setOrientation(LinearLayout.VERTICAL);   

        for (int i = 0; i < players.length; i++) {
            LinearLayout playerStockDetails = new LinearLayout(context);
            playerStockDetails.setOrientation(LinearLayout.HORIZONTAL);

                TextView playerName = new TextView(context);
                playerName.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));

                TextView playerMoney = new TextView(context);
                playerMoney.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));

                TextView playerTileOneQuantity = new TextView(context);
                playerTileOneQuantity.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
                playerTileOneQuantity.setBackgroundColor(Color.LTGRAY);

                TextView playerTileTwoQuantity = new TextView(context);
                playerTileTwoQuantity.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
                playerTileTwoQuantity.setBackgroundColor(Color.CYAN);

                playerName.setText(players[i].getName());
                playerMoney.setText(String.valueOf(players[i].getMoney()));
                playerTileOneQuantity.setText(String.valueOf(players[i].getStockQuantityByCode(getHotelCodeByName(keepHotel.getName()))));
                playerTileTwoQuantity.setText(String.valueOf(players[i].getStockQuantityByCode(getHotelCodeByName(absorbHotel.getName()))));

            playerStockDetails.addView(playerName);
            playerStockDetails.addView(playerMoney);
            playerStockDetails.addView(playerTileOneQuantity);
            playerStockDetails.addView(playerTileTwoQuantity);
            playerStockDetails.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));

            dialogBody.addView(playerStockDetails);
        }

    dialogLayout.addView(dialogHeader);
    dialogLayout.addView(dialogBody);       

    stockExchangeDialog.setView(dialogLayout);
    stockExchangeDialog.show();
}

So how do I get it to center the images based on the dynamic-width (based on weight), rather than the static width I declared in LayoutParams?

Thanks in advance for your help!

JRadTheBad

Was it helpful?

Solution

Try this..

Add this line in your LinearLayout LayoutParams

tileOneWrapper.gravity = Gravity.CENTER;

and also

tileTwoWrapper.gravity = Gravity.CENTER;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top