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