0

I'm working on my app, and came to a huge problem. I need to implement buttons in my fragments and can't find a normal solution that works. this is the code:

package com.example.konjicdiscover10;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class SettingsFragment extends Fragment {

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, 
@Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_settings, container, false);


    }
}

and XML

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="60dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="14dp"
android:background="@color/white">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <Button
        android:id="@+id/button"
        android:layout_width="350sp"
        android:layout_height="50sp"
        android:layout_centerHorizontal="true"
        android:textSize="11sp"
        android:layout_marginVertical="20dp"
        android:text="dark mode(in preparation)" />

    <Button
        android:id="@+id/button2"
        android:layout_width="350sp"
        android:layout_height="50sp"
        android:layout_centerHorizontal="true"
        android:layout_marginVertical="80dp"
        android:text="Languages(in preparation)"
        android:textSize="11sp"/>
</RelativeLayout>

</ScrollView>

basically what I'm looking for is that a buttons can lead to a new fragments or activities where content will be.

Deba
  • 1
  • 1

2 Answers2

0

you need to add declare a View :

package com.example.konjicdiscover10;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

View view;

public class SettingsFragment extends Fragment {

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable       ViewGroup container, 
@Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_settings, container, false);

Button myButton = view.findViewById(R.id.button);
myButton..setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
          //write what you want the button to do
      }
});
return view;

}
}

add View :

View view;

And set the :

view = inflater.inflate(R.layout.fragment_settings, container, false);

and when you link your xml code with java you will use the 'view' like that :

myButton = view.findViewById(R.id.button);

finally you return the view

return view;
0

It is probably best practice to implement your buttons in the onViewCreated method of the Fragment class.

This seems to be the most logical place since you want to implement the buttons as soon as the view is created. The benefit of using this method is that you don't have to bother about storing the View as variable. The parent class will handle this for you.

Example

package com.example.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.app.R;

public class SettingsFragment extends Fragment {
    private Button mOpenButton;
    private Button mCloseButton;

    // constructor
    public SettingsFragment(){
        // require a empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

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

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        this.mOpenButton = (Button) view.findViewById(R.id.button);
        this.mCloseButton = (Button) view.findViewById(R.id.button2);

        this.mOpenButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SettingsFragment.this.openSomething();
                // in your case: SettingsFragment.this.activateDarkMode();
            }
        });

        mCloseButton.setOnClickListener(view -> {
            @Override
            public void onClick(View view) {
                closeSomething();
                // in your case: selectLanguage();
            }
        });
    }

    private void openSomething() {
        // in your case: activate dark mode
    }

    private void closeSomething() {
        // in your case: select language
    }
}
devTM
  • 1
  • 4