how to set visibility for imagepager and linear layout

时间:2016-10-20 12:36:40

标签: android android-viewpager

i have this pager in the layout and i tried to setVisibility for the pager and the linear layout based on condition , but its not working i search in the internet and all have the same answer, any idea ?

this is the xml

<LinearLayout
    android:orientation="vertical"
    android:id="@+id/error_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1"
    android:visibility="visible">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:id="@+id/error_image_view"
        android:layout_weight="0.59" />
    <TextView
        android:text="This Screen Is Not Active"
        android:textSize="30sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/error_text_view"
        android:layout_weight="0.59" />
</LinearLayout>

<android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible"></android.support.v4.view.ViewPager>

and this is the main activity

ViewPager imagePager;
LinearLayout errorLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_pager);

    //views
    errorLayout = (LinearLayout) findViewById(R.id.error_linear_layout);
    imagePager = (ViewPager) findViewById(R.id.pager);
    IsActive();
}
public void IsActive()
{
    String x = "x";

    if (x=="x")
    {
        imagePager.setVisibility(View.VISIBLE);
        errorLayout.setVisibility(View.INVISIBLE);
    }else
    {
        imagePager.setVisibility(View.INVISIBLE);
        errorLayout.setVisibility(View.VISIBLE);
    }
}

1 个答案:

答案 0 :(得分:1)

Maybe the error isnt that the view isnt INVISIBLE, but that:

x can never equal "x". variable X is a reference, not the actual representation of it.

Use:

if(x.equals("x")) instead.