-2

I can create an Android AlertDialog as like this.

AlertDialog.Builder MyDialog = new AlertDialog.Builder(this);

But what matters to me is how can I set a layout for this. When I use a Dialog I can initialize and set a layout as like this.

dialog = new Dialog(Login.this,android.R.style.Theme_Holo_Dialog);
dialog.setContentView(R.layout.journey_details);

can I do something like that to set a layout. Or is in AlertDialog such a functionality not there.

I first created a custom dialog and when I'm setting the time for a text view it gave a null pointer. That's why I tried a AlertDialog. This is my first code. I was unable to figure out the error.

public void ShowJourneyDetails() {
    dialog = new Dialog(Login.this,android.R.style.Theme_Holo_Dialog);

    crnt_time=(TextView)findViewById(R.id.jd_txt_str_tim);
    date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    String crnt_date =date.format(new Date()).toString(); 
    crnt_time.setText(crnt_date);

    }

This gave me an error. Thats why I tried a AlertDialog.

But my AlterDialog also gives me a NullPointer and the error comes from the button. My code is below

public void ShowJourneyDetails() {

    // dialog = new Dialog(Login.this,android.R.style.Theme_Holo_Dialog);
    final AlertDialog.Builder MyDialog = new AlertDialog.Builder(this);
    LayoutInflater factory = LayoutInflater.from(Login.this);
    final View DialogView = factory.inflate(R.layout.journey_details, null);
    MyDialog.setView(DialogView);

    LoginIntent = new Intent(Login.this, DropPickUp_List.class);
    check_fuel = (CheckBox) findViewById(R.id.jd_chk_ful);
    check_e_oil = (CheckBox) findViewById(R.id.jd_chk_eoil);
    check_wtr = (CheckBox) findViewById(R.id.jd_chk_wtr);
    check_b_oil = (CheckBox) findViewById(R.id.jd_chk_boil);
    check_lic = (CheckBox) findViewById(R.id.jd_chk_lc_inc);
    check_gt_pass = (CheckBox) findViewById(R.id.jd_chk_gt_ps);
    check_bills = (CheckBox) findViewById(R.id.jd_chk_bil);
    crnt_time = (TextView) findViewById(R.id.jd_txt_str_tim);
    dialogButton = (Button) dialog.findViewById(R.id.jd_btn_strt);

    MyDialog.setView(check_fuel);
    MyDialog.setView(check_e_oil);
    MyDialog.setView(check_wtr);
    MyDialog.setView(check_b_oil);
    MyDialog.setView(check_lic);
    MyDialog.setView(check_gt_pass);
    MyDialog.setView(check_bills);
    MyDialog.setView(dialogButton);

    MyDialog.setView(crnt_time);

    SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String crnt_date = date.format(new Date()).toString();
    crnt_time.setText(crnt_date);

    final Stack<String> ItemStack=new Stack();

    dialogButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(Login.this, msg, Toast.LENGTH_LONG).show();

        }
    });

    MyDialog.show();

}

I can't figure out the error.

Samantha Withanage
  • 3,471
  • 10
  • 25
  • 56

3 Answers3

2

If you want to create Custom AlertDialog then you need to Inflate Layout Like below:

LayoutInflater factory = LayoutInflater.from(this);
final View dialogView = factory.inflate(
            R.layout.custom_progress_layout, null);

and set this Layout to your Dialog like below:

    Dialog main_dialog = new Dialog(this, R.style.Theme_Dialog);
    main_dialog.setContentView(dialogView);

That's it.

user149408
  • 4,098
  • 1
  • 25
  • 46
M D
  • 46,860
  • 8
  • 87
  • 108
1

Yes. You can do it this way:

LayoutInflater mLayoutInflator = LayoutInflater.from(this);
final View customDialogView = mLayoutInflator.inflate(
        R.layout.journey_details, null);
final AlertDialog customDialog= new AlertDialog.Builder(this).create();
customDialog.setView(customDialogView);
customDialog.show();
Chintan Soni
  • 22,543
  • 24
  • 96
  • 158
0

You can call

http://developer.android.com/reference/android/app/AlertDialog.html#setView(android.view.View)

on the AlertDialog.

JoxTraex
  • 13,105
  • 5
  • 30
  • 45