1

I would like to open a custom dialog after the click of a button. The code of button in XML is:

<Button
            android:id="@+id/Button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/BorderMargin"
            android:layout_marginRight="@dimen/BorderMargin"
            android:background="#D2D2D2"
            android:onClick="openDialog1"
            android:padding="17dip"
            android:text="@string/ButtonAdd" />

After click, the button open the method "openDialog1":

public void openDialog1(View view) {

    final Dialog dialog = new Dialog(this.getApplicationContext());
    dialog.setContentView(R.layout.dialogbrand_layout);
    dialog.setTitle("Hello");

    TextView textViewUser = new TextView(getApplicationContext());
    textViewUser = (TextView) findViewById(R.id.textBrand);
    textViewUser.setText("Hi");

    dialog.show();
}

I tried to execute this cose but the application crash on the textViewUser.setText

Any ideas?

hasmet
  • 758
  • 3
  • 13
  • 32

4 Answers4

4

You can findViewById of the current view hierarchy set to the activity.

In your case you should use activity context and also use dialog object to initialize textview.

You can also remove the final modifier.

public void openDialog1(View view) {
Dialog dialog = new Dialog(ActivityName.this);
dialog.setContentView(R.layout.dialogbrand_layout);
dialog.setTitle("Hello");
TextView textViewUser = (TextView) dialog.findViewById(R.id.textBrand);
textViewUser.setText("Hi");
dialog.show();
}

When to call activity context OR application context?

Check the above link and the answer by commomsware to know when to use activity context or application context.

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

You should not use Application's Context for creating Dialogs/Views.

Change:

final Dialog dialog = new Dialog(this.getApplicationContext());

To this:

final Dialog dialog = new Dialog(view.getContext());
//or
final Dialog dialog = new Dialog(this);   //'this' refers to your Activity
waqaslam
  • 67,549
  • 16
  • 165
  • 178
0

Waqas is right but moreover you override the textViewUser you have created programmatically: "textViewUser = new TextView(getApplicationContext());" with an instance found into your view hierarchy: "textViewUser = (TextView) findViewById(R.id.textBrand);".

It seems that textViewUser is null. Did you try to get this TextView from the view dialogbrand_layout? If yes it is sure that your textViewUser is null because this TextView is not in your curent view hierarchy.

Donkey
  • 1,176
  • 11
  • 19
0

Are you sure that textBrand textview is inside the contentView you have set for activity.

 textViewUser = (TextView) findViewById(R.id.textBrand);

findViewById call is returning null. If R.layout.dialogbrand_layout has textBrand textview then replace above line with

 textViewUser = (TextView)dialog.findViewById(R.id.textBrand);
nandeesh
  • 24,740
  • 6
  • 69
  • 79