-1

i want to hide my layout which is in activity class and its visibilty handle in fragmetn class below is my code

Myfragment.java

Tags.mStringVarientSoldOut="yes";
ActivityDetailTest site = new ActivityDetailTest();
site.isSoldOut();

ActivityDetailTest.java

mLinearLayoutBottom is already declared in my onCreate() method of activity class

public void isSoldOut()
{


    if (Tags.mStringVarientSoldOut.equalsIgnoreCase("yes"))
    {
        mLinearLayoutBottom.setVisibility(View.GONE);
    }
    else
    {
        mLinearLayoutBottom.setVisibility(View.VISIBLE);
    }
}

when I run above code i m getting nullpointer error at line mLinearLayoutBottom.setVisibility(View.GONE);
any idea how can i solve this? your all suggestions are appreciable

SripadRaj
  • 1,687
  • 2
  • 22
  • 33
  • mLinearLayoutBottom is defined in activity class so how can you call it in fragment ? where are you getting reference of it in fragment ? – Khizar Hayat Jun 14 '16 at 04:49

3 Answers3

0

If mLinearLayoutBottom is declared in ActivityDetailTest with public visibility then use it as in Fragment :

ActivityDetailTest objActivity = (ActivityDetailTest) getActivity();
objActivity.mLinearLayoutBottom.setVisibility(View.GONE);
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
0

The perfect way of activity and fragment communication via Interface.

You should declare a interface and implement it in your activity. Then from your fragment you can send command via that interface.

Here is code example, Your interface is:

public interface IActivityController{
void hideOrVisibleView(boolean action);
}

Now, implement this interface in your activity. After implementing this interface, your activity code will like this:

@Override
public void hideOrVisibleView(boolean isHide) {

    if(isHide){
      mLinearLayoutBottom.setVisibility(View.GONE); 
      }else{
        mLinearLayoutBottom.setVisibility(View.VISIBLE); 
       }
}

Now, in your fragment based on your code logic just use this line to hide/visible your View in activity.

((IActivityController)getActivity()).hideOrVisibleView(true/false);

here, if you send true, view will hide and if false then view will visible.

I think, this will the best solution. Hope this will help you.

My related answer is here and here

Community
  • 1
  • 1
Md. Sajedul Karim
  • 6,749
  • 3
  • 61
  • 87
0

Without interface you can also handle by this way

pass Linearlayout object in fragment class constructor from your activity class

MyFragment mMyframge=new MyFragment(mlinearLayoutBottom); 

MyFragment.java

create constructor

@SuppressLint("ValidFragment")
public MyFragment (LinearLayout mLinearLayout){
    this.mLinearLayout=mLinearLayout;
}

and now handle your condition in fragment class

if (Tags.mStringVarientSoldOut.equalsIgnoreCase("yes"))
{
    mLinearLayout.setVisibility(View.GONE);
}
else
{
    mLinearLayout.setVisibility(View.VISIBLE);
}
Harshal Kalavadiya
  • 2,412
  • 4
  • 38
  • 71
  • Harshal Kalavadiya : ya working!! –  Jun 14 '16 at 06:08
  • Thanks for your answer. But here has some problem: To pass value via Constructor in fragment, it will show error message when generate release APK. If there has another efficient way to do this things, why you pass a View to another class? – Md. Sajedul Karim Jun 14 '16 at 06:15
  • Md. Sajedul Karim: i also release APK with same code in my other project and in my case it is working fine – Harshal Kalavadiya Jun 14 '16 at 07:49