Alert Single choice Dialogs in Fragments

52 Views Asked by At

So I'm trying to add a Singlechoice Alert Dialog that would pop up and have three separate themes changing the entire theme of the app. I know how to do this using activities, but i'm unsure on how to complete this using fragments. I am also using fragments as i am using bottom nav.

The errors are in fragment_settings.xml ('Builder(android.content.Context)' in 'androidx.appcompat.app.AlertDialog.Builder' cannot be applied to '(com.example.smart_stick.SettingsFragment)'

Cannot resolve method 'makeText(SettingsFragment, String, int)'

SettingsFragment.java

package com.example.smart_stick;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Toast;


public class SettingsFragment extends Fragment  {

    String[] Themes={"Light", "Dark", "Black and Yellow"};
    String themesSelected;

    private Button getapptheme;


    // TODO: Rename and change types of parameters


    public SettingsFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_settings, container, false);


        Button button = getapptheme.findViewById(R.id.Apptheme);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });
    }

    public void showDialog(){
        themesSelected = Themes[0];
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
         builder.setTitle("Choose a theme");
         builder.setSingleChoiceItems(Themes, 0, new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
                themesSelected = Themes[which];
             }
         });

         builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
                 Toast.makeText(SettingsFragment.this, themesSelected+" selected.", Toast.LENGTH_SHORT).show();
             }
         });

         builder.setNegativeButton("CANCEL", null);
         builder.show();
    }
}

fragment_settings.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SettingsFragment">

    <RelativeLayout
        android:id="@+id/text_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="10dp"
            android:layout_marginBottom="10dp"
            android:gravity="center"
            android:text="Settings"
            android:textAlignment="center"
            android:textColor="#000000"
            android:textSize="30sp"
            android:textStyle="bold"
            tools:ignore="HardcodedText" />

    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Space
            android:layout_width="1dp"
            android:layout_height="65dp"
            tools:ignore="ExtraText" />

        <Button
            android:id="@+id/Apptheme"
            style="@style/Widget.MaterialComponents.Button.TextButton.Icon"
            android:layout_width="match_parent"
            android:layout_height="54dp"
            android:text="App Theme"
            android:textAppearance="@android:style/TextAppearance.Material.Headline"
            android:textColor="#000000"
            android:textSize="20sp"
            app:icon="@drawable/ic_art_pal_foreground"
            app:iconSize="35dp"
            app:iconPadding="-200dp"
            tools:ignore="HardcodedText" />

        <Button
            android:id="@+id/TextSize"
            style="@style/Widget.MaterialComponents.Button.TextButton.Icon"
            android:layout_width="match_parent"
            android:layout_height="54dp"
            android:text="Text Size"
            android:textAppearance="@android:style/TextAppearance.Material.Headline"
            android:textColor="#000000"
            android:textSize="20sp"
            app:icon="@drawable/baseline_text_fields_24"
            app:iconSize="35dp"
            app:iconPadding="-220dp"
            tools:ignore="HardcodedText" />


    </LinearLayout>

</FrameLayout>

ExmpDialog.java

package com.example.smart_stick;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatDialogFragment;
import androidx.fragment.app.DialogFragment;

import java.util.AbstractList;
import java.util.ArrayList;

public class ExmpDialog extends DialogFragment {
    public ExmpDialog(SettingsFragment settingsFragment) {

    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Choose a theme")
                .setMultiChoiceItems(R.array.appthemes, null,
                        new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which,
                                    boolean isChecked) {
                                AbstractList<Object> selectedItems = null;
                                if (isChecked) {
                                    //if user checks item
                                    selectedItems.add(which);
                                } else if (selectedItems.contains(which)) {
                                    //else, if the item is already in the array remove it
                                    selectedItems.remove(which);
                                    
                                }

                            }
                        })
                .setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // START THE GAME!
                    }
                });

        // Create the AlertDialog object and return it
        return builder.create();
    }
}

I was expecting to create a singlechoice item that would change the themes however the dialog does pop up but it only shows the title and the cancel, nothing else.

1

There are 1 best solutions below

0
Enowneb On

The first parameter of Toast.makeText() takes Context. So you cannot pass Fragment into it, instead you can pass an Activity to it:

Toast.makeText(getActivity(), themesSelected+" selected.", Toast.LENGTH_SHORT).show();

For ExmpDialog, you need to make sure you have values in R.array.appthemes. So you should have something like this in strings.xml resources file:

<resources>
    ...
    <string-array name="appthemes">
        <item>Light</item>
        <item>Dark</item>
        <item>Black and Yellow</item>
    </string-array>
</resources>