0

So I'm making a game where people can buy stock in hotel chains as they grow. I needed a special dialog box who's content changes dynamically as the game progresses so I'm creating a custom dialog from scratch using AlertDialog as the foundation.

My questions are these:

  1. I've tried adjusting the LinearLayout.LayoutParams for a larger width using the dialogContainer layout. For some reason, is has 0 effect on the size of the dialog box. Kinda strange. Any ideas what's causing that? I need a way to keep the height and width fixed because I want the content to eventually scroll.

  2. (Removed for other question)

Here's what I've got for code so far:

public void buyStock(View view){

    Player thisPlayer = players[getPlayerIndexByPlayOrder(CURRENT_TURN)];
    Context context = getApplicationContext();

    //generate content for dialog       
    LinearLayout dialogContainer = new LinearLayout(context);
    dialogContainer.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(400, LinearLayout.LayoutParams.MATCH_PARENT, 1);
    dialogContainer.setLayoutParams(params);

    //title
    TextView dialogTitle = new TextView(context);
    dialogTitle.setText("Buy Stock:");
    dialogTitle.setHeight(40);
    dialogTitle.setWidth(600);

    //each hotel stock options

    //Text Content
    Hotel testHotel = new Hotel("Tower", 0);
    testHotel.setPrice(200);
    View stockPicker = getStockPicker(testHotel);


    dialogContainer.addView(dialogTitle);
    dialogContainer.addView(stockPicker);

    AlertDialog.Builder buyStockDialog = new AlertDialog.Builder(this);
    buyStockDialog.setView(dialogContainer);

    buyStockDialog.show();

}

public View getStockPicker(Hotel hotel){
    Context context = getApplicationContext();


    LinearLayout container = new LinearLayout(context);
    container.setOrientation(LinearLayout.HORIZONTAL);

    //Example color tile : image
    ImageView tileBlock = new ImageView(context);
    tileBlock.setBackgroundResource(tileResourceByHotel(hotel));


    //Hotel Name
    TextView hotelName = new TextView(context);
    hotelName.setText(hotel.getName());


    //Hotel Price
    TextView hotelPrice = new TextView(context);
    hotelPrice.setText("$" + hotel.getPrice());


    //"Number-Picker"
    LinearLayout numPicker = new LinearLayout(context);
    numPicker.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, 400);
    numPicker.setLayoutParams(params);
    int textValue = 0;

        //Up button
        ImageView upArrow = new ImageView(context);
        upArrow.setBackgroundResource(R.drawable.arrow_up);

        //text
        TextView pickerNum = new TextView(context);
        pickerNum.setText(String.valueOf(textValue));           

        //down
        ImageView downArrow = new ImageView(context);
        upArrow.setBackgroundResource(R.drawable.arrow_down);

    numPicker.addView(upArrow);
    numPicker.addView(pickerNum);
    numPicker.addView(downArrow);

    container.addView(tileBlock);
    container.addView(hotelName);
    container.addView(hotelPrice);
    container.addView(numPicker);

    return container;
}
enter code here

PLEASE NOTE: I has every intention of using Linear Layouts wherever possible simply because I come from a web development background and am very proficient using div tags. (Essentially a linear layout).

JRad the Bad
  • 511
  • 5
  • 25

1 Answers1

0

To set exact size of dialog, You should use WindowManager.LayoutParams for dialog window, because framework itself defines default sizes for it. There's lot of the similar questions like this one or this.

Basically, You can add the following call after show():

AlertDialog dialog = buyStockDialog.show();

dialog.getWindow().setLayout(600, 400);
Community
  • 1
  • 1
sandrstar
  • 12,503
  • 8
  • 58
  • 65
  • There appears to be something wrong here. getWindow() is not a valid method for the AlertDialog.Builder class. It's expecting me to TypeCast buyStockDialog which it can't. Any other ideas? – JRad the Bad Mar 03 '14 at 20:56