2

Good afternoon everyone, I have succesfully created a working alertdialog, which is shown in following link: https://i.stack.imgur.com/XR1Pm.png

Later I thought that it would be better if I moved the information of computer in right of information of first player (shown here: https://i.stack.imgur.com/rj3M6.png ). It would increase readability of message and generally look better.

I have searched over the internet the way of doing it, but I couldn't find the solution. Could anyone tell me how I could efficiently reach this effect shown in picture 2? Thanks in advice.

Here is my code of dialogalert (it won't be compiled but it mostly uses data from SharedPreferences, strings from strings.xml and it should be understood):

private void showMatchResults(int[] p1results, int[] p2results) {
    AlertDialog.Builder aldb = new AlertDialog.Builder(this);
    aldb.setTitle(getString(R.string.match_summary));
    aldb
            .setMessage(matchResults_message(p1results, p2results))
            .setCancelable(false)
            .setNeutralButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }

            });
    AlertDialog alertDialog = aldb.create();
    alertDialog.show();
}

private String matchResults_message(int[] p1results, int[] p2results) {
    StringBuilder sb = new StringBuilder();
    sb.append(data.getString("player1", getApplicationContext().getString(R.string.player1)));
    int[] results = new int[6];
    System.arraycopy(p1results, 0, results, 0, p1results.length);
    System.arraycopy(p2results, 0, results, p1results.length, p2results.length);
    sb.append(System.getProperty("line.separator"));
    for (int i = 0; i < results.length; i++) {
        if (i == 3) {
            sb.append(System.getProperty("line.separator"));
            if (mode.equals("Two"))
                sb.append(data.getString("player2", getApplicationContext().getString(R.string.player2)));
            else sb.append(getString(R.string.computer) + " " + ComputerAI.getDifficulty(this));
            sb.append(System.getProperty("line.separator"));
        }
        if (i == 0 || i == 3) sb.append(getString(R.string.wins));
        else if (i == 1 || i == 4) sb.append(getString(R.string.draws));
        else sb.append(getString(R.string.loses));
        sb.append(" "+results[i]);
        sb.append(System.getProperty("line.separator"));
    }
    return sb.toString();
}
mikele
  • 106
  • 1
  • 8

2 Answers2

1

I think that the easiest way for you would be to create custom XML layout and use:

setView(getLayoutInflater().inflate(R.layout.custom_two_columns_layout, null));

How to create this layout is easy I suppose.

Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
1

You will have to create a custom layout with two LinearLayouts, set main layout weight sum to 2, for others set layout_weight's to 1. Put a TextView in each of the LinearLayout's(total 2 TextViews). Here is how you can use that custom layout in AlertDialog:

How to implement a custom AlertDialog View

Community
  • 1
  • 1
Gintas_
  • 4,940
  • 12
  • 44
  • 87