How to Enable/ Disable button from another fragment in android? - android

i have an activity with 4 fragments from fragment number 1 I want to enable an existing button (that is disable) on fragment 3, when i click in my button in fragment1. this is my attempt:
fragment 1:
public class FragmentEvolucion extends Fragment {
//btnGuardar is in fragment1, the others are in fragment 3 and 4
Button btnGuardar, btnHabilitarMed, btnHabilitarImc;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_evolucion, container, false);
btnGuardar=(Button)rootView.findViewById(R.id.btnGuardarEvolucion);
btnHabilitarMed=(Button)rootView.findViewById(R.id.btnGuardarMedicacion);
btnHabilitarImc=(Button)rootView.findViewById(R.id.btnGuardarDiagnostico);
btnGuardar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btnHabilitarMed.setEnabled(true);
btnHabilitarImc.setEnabled(true);
}
});
this give me an error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setEnabled(boolean)' on a null object reference
How can i access the button and change it status enabled correctly?

First of all, create a interface.
UpdateButtonListener.java
public interface UpdateButtonListener {
void onUpdate(boolean status);
}
Now in fragment 3, implement interface to class
public class Fragment3 extends Fragment implements UpdateButtonListener{
public static UpdateButtonListener updateButton;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment3, container, false);
updateButton = this;
return view;
}
#Override
public void onUpdate(boolean status) {
// here set the functionality of button whether to disable or not .
if(status){
btnHabilitarMed.setEnabled(true);
btnHabilitarImc.setEnabled(true);
}
}
}
Now in first fragment.
btnGuardar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Fragment3.updateButton.onUpdate(true);
}
Like-wise do for other's.

Related

Fragment Interactions

I am trying to add a fragment to an existing layout from within the onClick method of a button in another Fragment class..
In my MainActivity I add two fragments to the layout..
public class MainActivity extends AppCompatActivity {
private Collapsebutton_Fragment cbut_frag;
private ColourView_Fragment cvw_frag;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cbut_frag = new Collapsebutton_Fragment();
cvw_frag = new ColourView_Fragment();
//ColourView Fragment ready
//CollapseButton Fragment ready
getSupportFragmentManager().beginTransaction()
.add(R.id.container_mainactivity, cbut_frag)
.add(R.id.container_mainactivity, cvw_frag)
.commit();
}
}
One of the added fragments contains a button. When clicking that button - accessing the onClick method of the button - I want to add another fragment..
My code is the following:
public class ColourView_Fragment extends Fragment {
FragmentManager fragmentManager;
ThreeButton_Fragment tbt_fragment;
public ColourView_Fragment(){}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.colourview_fragment, container, false);
FrameLayout frameLayout = (FrameLayout)rootView.findViewById(R.id.ColourView);
frameLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getFragmentManager().beginTransaction()
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.add(R.id.container_mainactivity, tbt_fragment)
.commit();
}
});
return rootView;
}
I get the following error:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
There is an error in line
.add(R.id.container_mainactivity, tbt_fragment)
How can I correct?
You haven't initialized tbt_fragment in ColourView_Fragment

which method when display dialog show in fragment

I opened dialog in a fragment and when come back to fragment, my fragment's view is null Object.
when the dialog is displaying which lifecycle method fragment is calling?
public class MyFragment extends Fragment{
TextView textView;
View view;
FloatingActionButton actionButton;
#Override
public View onCreateView(final LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_read, container, false);
textView = view.findViewById(R.id.txtSaved);
actionButton = view.findViewById(R.id.floatingActionButton);
actionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DialogSearch search = new DialogSearch();
search.show(getActivity().getSupportFragmentManager(),"MyDialog");
}
});
return view;
}
}
The problem is the dialog is being showned in the onCreateView life cycle, that is why is the Fragment view is null. You have to do it inside the onViewCreated method.
#Override
public View onViewCreated(View view, Bundle savedInstanceState) {
//Work here, the view argument in the method is the view inflated in the onCreateView method
});
Make a small change,
public class MyFragment extends Fragment{
TextView textView;
View view;
FloatingActionButton actionButton;
#Override
public View onCreateView(final LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_read, container, false);
textView = view.findViewById(R.id.txtSaved);
actionButton = view.findViewById(R.id.floatingActionButton);
actionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DialogSearch search = new DialogSearch();
search.show(getActivity().getSupportFragmentManager(),"MyDialog");
}
});
return view; / Add this line -------
}
}
You need to return view
when the dialog is displaying which lifecycle method fragment is calling?
onPause() of fragment gets called.

EventBus in viewPager fragment issue

I have thee fragments in viewpager, Fragment1, Fragment2, and Fragment3.
fragment1 has a button and when onclick it setEnable(true) a button in fragment2 and the button in fragment 2 setEnable(true) a button3 in fragment3
All seem to work fine, but when I navigate through the fragments when passing from fragment 3 to fragment 1, fragment3 lose the information and the button comes back to its initial state -setEnable(false) as in the beginning, but fragment1 and fragment2 still work fine and don't lose the information when I navigate through all the fragments in viewpager.
How can I keep the information in fragment3 while navigating?
Here is my code:
fragment1
public class FragmentUno extends Fragment {
Button btnClick;
private EventBus bus = EventBus.getDefault();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_uno, container, false);
btnClick = (Button)rootView.findViewById(R.id.button1);
btnClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
bus.post(new EventoBotton());
System.out.println("saliendoooo");
}
});
return rootView;
}
}
fragment2
public class FragmentDos extends Fragment {
private EventBus bus = EventBus.getDefault();
private EventBus bus2 = EventBus.getDefault();
Button btnNext;
#Override
public void onResume() {
super.onResume();
bus.register(this);
}
#Override
public void onPause() {
super.onPause();
bus.unregister(this);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_dos, container, false);
btnNext = (Button)rootView.findViewById(R.id.btnF2);
btnNext.setEnabled(false);
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//HERE I POST EVENT TO SEND TO FRAG 3
bus2.post(new EventoDos());
}
});
return rootView;
}
#Subscribe
public void reenvio(EventoBotton ev) {
btnNext.setEnabled(true);//HERE RECEIVE EVENT FORM FRAG 1
}
}
Fragment3
public class FragmentTres extends Fragment {
TextView txtDos;
Button btnDis;
private EventBus bus2 = EventBus.getDefault();
#Override
public void onResume() {
super.onResume();
bus2.register(this);
}
#Override
public void onPause() {
super.onPause();
bus2.unregister(this);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tres, container, false);
txtDos = (TextView)rootView.findViewById(R.id.txtCambio);
btnDis = (Button)rootView.findViewById(R.id.buttonF2);
btnDis.setEnabled(false);
return rootView;
}
#Subscribe
public void actualizar(EventoDos eventoDos) {
System.out.println("estamos aquiiii"); // Here I receive the event from frag 2
txtDos.setText("hola mundo");
btnDis.setEnabled(true);
}
}
These are my POJO events
public class EventoBotton {
public EventoBotton() {}
}
The other event
public class EventoDos {
public EventoDos() {}
}
I'm using eventbus 3.0 with my SDK 21.
All works fine but when I slip through the view pager, if I slide from 3 to 2, the information is still there in frag3. But when I slide from 3 to 2 and reach 1 when I come back to 3, the information is lost. That is to say, the button in frag 3 come back to its original state (enabled false) Why?? How can I persist the changes in all the navigation in the viewpager?
You can try using:
setOffscreenPageLimit(int limit) // In your case, limit = 2
on the ViewPager object which sets the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state.

Null pointer exception in imageview when using callback between two fragments?

i have two fragments, i want use callback using interface to change imageview
in first fragment from the second fragment but when callback is fired its get imageview null and i get the below error
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.ImageView.setImageResource(int)' on a null object
reference
first fragment:
public class firstfragment extends Fragment implements MyProfileCallback
{
Imageview myprofile_image;
public firstfragment()
{
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootview = inflater.inflate(R.layout.fisrt, container, false);
myprofile_image=(ImageView) rootview.(find.id.myprofile_image);
/
...
/
}
#Override
public void callbackCall()
{
myprofile_image.setImageResource(R.drawable.profile_friends);
}
}
second fragment:
public class secondfragment extends Fragment
{
MyProfileCallback mcallback;
public secondfragment()
{
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootview = inflater.inflate(R.layout.second, container, false);
mcallback.callbackCall();
return rootview;
}
interface
public interface MyProfileCallback
{
void callbackCall();
}
Your question is kind of hard to understand so is your code, BUT, I think you should load the resource file in this case R.drawable.profile_friends
when the fragment view has being created
You can:
EDIT:
You are calling your callback from the onCreateView method meaning your mypfile_image view does not have a reference to your element yet
and you get a null pointer exception
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mcallback.callbackCall();
}

How to set onClick listener for a Fragment?

I am working with Android Studio. I have a fragment on my Activty and I want to hide it when the user clicks on it.
For some reason I canĀ“t just override a function for onclick like in the activties. And everytime I ask google all I can find are questions about how to
Implement onclick listeners for buttons or other elements in a fragment but that is not what I want. I want the onclick listener for the fragment itself.
Can anyone please tell me how to do that!?
You can do this by set ClickListener on the view inflating in a onCreateView of fragment like this :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.layout_your, container, false);
v.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do your operation here
// this will be called whenever user click anywhere in Fragment
}
});
return v;
}
It goes like below
public class fragmentOne extends Fragment implements OnClickListener {
Button myButton;
#Override
public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState) {
View myView = inflater.inflate(R.layout.fragment_1, container, false);
myButton = (Button) myView.findViewById(R.id.myButton);
myButton.setOnClickListener(this);
return myView;
}
#Override
public void onClick(View v) {
// implements your things
}
}

Categories

Resources