skip to Main Content

I’m making an android app and i want to put a settings button on every layout in the app. When i press the settings button, a custom dialog pops up and i can access the app settings.
The problem i’m having is that i want to refer to 1 method in some class (doesn’t matter to me which one). I’m already using the include in my XML of my layouts like this:

<include android:id="@+id/settingsButton"
    layout="@layout/settingsbuttonlayout"/>

The settingsbuttonlayout.xml file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/root_vg">
    <ImageButton
        android:layout_width="50dp"
        android:layout_height="50dp" app:srcCompat="@drawable/settingsicon" 
        android:id="@+id/settings_dialog"
        android:cropToPadding="true"
        android:adjustViewBounds="false" android:scaleType="fitCenter"
        app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintHorizontal_bias="0.133"
        app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintVertical_bias="0.123"
        android:clickable="true"
        android:focusable="true"
        android:background="@drawable/customdialog" android:onClick= "showSettings"
        app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

You can see that there is an onclick defined in this layout. However (for as far as i know) this means i need the same "showSettings" method in every layout class. How can i work around this so i should only write the "showSettings" method once and can refer to it?
This is the showSettings method:

public void showSettings(View v){
    Dialog dialog = new Dialog(this, R.style.DialogStyle);
    dialog.setContentView(R.layout.settings_dialog);
    dialog.getWindow().setBackgroundDrawableResource(R.drawable.dialogbackground);

    Button btnClose = dialog.findViewById(R.id.close_settings);
    btnClose.setOnClickListener(view -> dialog.dismiss());

    dialog.show();}

PS: I’m pretty new into making apps and GUI’s. I didn’t learn it yet in school and i’m just figuring out everything myself so sorry if this is some straightforward or stupid question 🙂

2

Answers


  1. Chosen as BEST ANSWER

    After searching some more I found the following: From whichever class i wanted to open the dialog i have to write this:

    SettingsDialog.showSettings(this);
    

    In my SettingsDialog class i have the following:

    public class SettingsDialog {
        static void showSettings(Context context) {
            Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.settings_dialog);
            
    Dialog.getWindow().setBackgroundDrawableResource(R.drawable.dialogbackground);
        }
      dialog.show();}
    }
    

  2. you can remove the onClick attribute from your setttings_dialog which calls the showSettings, next create a Utility.java file in which you can make your function as static

    public static void showSettings(View v){
    Dialog dialog = new Dialog(this, R.style.DialogStyle);
    dialog.setContentView(R.layout.settings_dialog);
    dialog.getWindow().setBackgroundDrawableResource(R.drawable.dialogbackground);
    
    Button btnClose = dialog.findViewById(R.id.close_settings);
    btnClose.setOnClickListener(view -> dialog.dismiss());
    
    dialog.show();}
    

    now in whichever class you want to call this method just write

    Button settingsButton = (Button) findViewById(R.id.settingsButton);
    settingsButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Utility.showSettings(v);
        }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search