1

So I am building a really basic app. I'm using an alert dialog to show the user whether they've won or not. What I have done is that I created this class :

public class WinLoseDialog {
  Activity activity;
  AlertDialog dialog;
  TextView text;

  public WinLoseDialog(Activity activity) {
        this.activity = activity;
  }

  public void startDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        LayoutInflater inflater = activity.getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.win_lose_layout, null));
        builder.setCancelable(false);
        
        dialog = builder.create();
        dialog.show();
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  }

  public void dismissDialog() {
        dialog.dismiss();
  }

}

What I wanted to do was to make this class as reusable as possible. I want to be able to use this for an alert dialog in case the user wins as well as in case they lose, for all of my game fragments.

To do this I need to able to change the text on the textView defined as follows (in the win_lose_layout) :

<TextView
            android:id="@+id/alertText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/alertDialogText"
            tools:text="hi" />

and the Raw res for my lottie View defined as (in the win_lose_layout):

<com.airbnb.lottie.LottieAnimationView
            android:id="@+id/lottie"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center_horizontal"
            android:layout_margin="10dp"
            app:lottie_autoPlay="true" />

And I need to be able to change the above two from inside the fragment where I am using this WinLoseDialog class.

The thing I'm stuck on is how do I use something like findViewById in this so that I'm able to attach the TextView object in my class to that in the win_lose_layout, so that i have the ability to create a setter for the text (TextView) and use that inside my fragment. And similarly for the lottie View

Arnav Mangla
  • 82
  • 1
  • 7

1 Answers1

1

You have to make the method to change those 2 views like this.

public class WinLoseDialog {
  Activity activity;
  AlertDialog dialog;
  TextView text;
  LottieAnimationView lottieView;

  public WinLoseDialog(Activity activity) {
        this.activity = activity;
  }

  public WinLoseDialog startDialog(String textStr, String lottieFilePath) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        LayoutInflater inflater = activity.getLayoutInflater();
        View view = inflater.inflate(R.layout.win_lose_layout, null);
        builder.setView(view);
        builder.setCancelable(false);

        text = view.findViewById(R.id.alertText);
        lottieView = view.findViewById(R.id.lottie);
        
        //You can update your text & Lottie animation view here with the new string and new Lottie path you got from arguments.

        if (!TextUtils.isEmpty(textStr))
            text.settext(textStr);
        if (!TextUtils.isEmpty(lottieFilePath))
            //Set your animation file path here.

        dialog = builder.create();
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

    return this;
  }

  public void dismissDialog() {
        dialog.dismiss();
  }
}

Now, when you want to sue the dialog without the changes, then simply :

WinLoseDialog dialog = new WinLoseDialog(activity);
dialog.startDialog(null, null);
dialog.show()

With the changes :

WinLoseDialog dialog = new WinLoseDialog(activity);
dialog.startDialog("This is new Text", lottiePath);
dialog.show()
Arnav Mangla
  • 82
  • 1
  • 7
Dev4Life
  • 2,328
  • 8
  • 22