Search code examples
javaandroidandroid-fragmentsandroid-fragmentactivity

Where to add methods of a fragment?


I have a fragment named fragment_login.xml and a corresponding class named LoginPageFragment.java. In my fragment there are 2 EditTexts and a button.

I want to write an onClick method for my button but I want it in my class so that i can use it where ever I use my fragment ( dynamically ) . i wrote the body of the method in my LoginPageFragment class but the program does not recognize it. Nor sure what is the problem here?

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;



public class LoginPageFragment extends Fragment {

    private Button btnLogin ;
    private EditText inputUsername , inputPassword ;

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

        inputUsername = (EditText) fragmentView.findViewById(R.id.input_username);
        inputPassword = (EditText) fragmentView.findViewById(R.id.input_password);
        btnLogin = (Button) fragmentView.findViewById(R.id.btn_login);

        return fragmentView ;
    }

    public void onLoginClick(View v){

        String usernameString = inputUsername.getText().toString() ;
        String passwordString = inputPassword.getText().toString() ;

        if(Utility.areEditTextsEmpty(inputUsername , inputPassword)){
            Toast.makeText(LoginActivity.fa , "Some Fields are EMPTY",Toast.LENGTH_SHORT).show();
        }
        User tempUser =  Utility.doesTheUserExist(usernameString , passwordString);
        if(tempUser != null){

            Utility.sendUserInfoToAnotherActivity(tempUser);

        }
        else{
            Toast.makeText(LoginActivity.fa , "User Not Defined" , Toast.LENGTH_SHORT);
        }
    }
}



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginActivity"
    android:padding="40dp"
    android:orientation="vertical"
    >


    <EditText
        android:id="@+id/input_username"
        android:inputType="number"
        style="@style/LoginElements"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableRight="@mipmap/username_icon2" />

    <EditText
        android:id="@+id/input_password"
        style="@style/LoginElements"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableRight="@android:drawable/ic_secure" />

    <Button
        android:id="@+id/btn_login"
        style="@style/LoginElements"
        android:text="@string/loginButtonText"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onLoginClick"
        />

</LinearLayout>

Solution

  • onClick tags in xml need to be handled by the activity. In a fragment, the easiest thing to do is just add the on click listerner programaticly. The alternative would be to have the activity handle it then call a function on the fragment, but that's a pain in the neck and can be problematic if you change out the fragment.