Search code examples
javaandroidandroid-studioandroid-fragmentsandroid-toast

Need help getting a Toast to appear on a Tab Fragment


I'm trying to get a Toast to appear in a tabbed fragment when an EditText is left empty.

Here is my MainActivity's onCreate:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
        ViewPager viewPager = findViewById(R.id.view_pager);
        viewPager.setAdapter(sectionsPagerAdapter);
        TabLayout tabs = findViewById(R.id.tabs);
        tabs.setupWithViewPager(viewPager);        
    }
}

My Tabbed Fragment works fine with this code:

public class SignupFragment extends Fragment {

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

But it doesn't work when creating the Toast like this:

public class SignupFragment extends Fragment {

    EditText textFillCheck;
    Button submitCheck;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        submitCheck = (Button) submitCheck.findViewById(R.id.signupBtn);
        textFillCheck = (EditText) textFillCheck.findViewById(R.id.signupFirstName);
        submitCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (TextUtils.isEmpty(textFillCheck.getText().toString())) {
                    Intent intent = new Intent(getActivity(), SignupFragment.class);
                    startActivity(intent);
                    Toast.makeText(getActivity(), "Please fill in all fields", Toast.LENGTH_SHORT).show();
                }
                else{Toast.makeText(getActivity(), textFillCheck.getText().toString(), Toast.LENGTH_LONG).show();
                }
            }
        });

        return inflater.inflate(R.layout.fragment_signup, container, false);
    }
}

Solution

  • You're not accessing the views of your Fragment's layout so the layout you're returning in onCreateView() doesn't have your logic attached to it. You should set up your Fragment to look something like this:

    public class SignupFragment extends Fragment {
    
        EditText textFillCheck;
        Button submitCheck;
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
    
            // Store a reference to your Fragment's inflated layout
            View root = inflater.inflate(R.layout.fragment_signup, container, false);
            
            // Use the reference to access your Fragment's views
            submitCheck = (Button) root.findViewById(R.id.signupBtn);
            textFillCheck = (EditText) root.findViewById(R.id.signupFirstName);
            
            submitCheck.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    if(TextUtils.isEmpty(textFillCheck.getText().toString())) {
                        Toast.makeText(getActivity(), "Please fill in all fields", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(getActivity(), textFillCheck.getText().toString(), Toast.LENGTH_LONG).show();
                    }
                }
            });
    
            // Return the inflated layout
            return root;
        }
    }