How to add a button to a PreferenceScreen?

后端 未结 10 1293
抹茶落季
抹茶落季 2020-11-28 20:48

I\'m quite new to Android Development and just came across Preferences. I found PreferenceScreen and wanted to create a login functionality with it. The only pr

10条回答
  •  情歌与酒
    2020-11-28 21:19

    Create a custom layout for the SettingsFragment.java i.e layout_settings.xml

    
    
    
        
    
            
        
    
        
    
        
    
    
        
    
    
    

    Then, refer to the layout file in your styles.xml:

    
    
    
    

    And then, in the onViewCreated() method of your SettingsFragment.java, you can use the button like this:

        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle 
                                 savedInstanceState) {
    
            MaterialButton btnSample = view.findViewById(R.id.btnSample);
            btnSample.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(requireContext(), "Sample Button 
                       clicked!!", Toast.LENGTH_LONG).show();
                }
            });
        }
    

提交回复
热议问题