how to set android ImageView visibility inside an included layout?

Govan :

We are using android API 17 in our application. I have defined a layout containing two images vies as below:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/image_container_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

<ImageView
        android:id="@+id/image_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image_1_resource"/>

<ImageView
        android:id="@+id/image_2"
        android:layout_marginTop="3dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/image_container_layout"
        android:src="@drawable/image_2_resource"/>

This layout is included inside another layout as below:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    style="@style/wizard_content_style"
    tools:context=".ui.Wizard"
    android:orientation="vertical"
    android:gravity="center_vertical"
    android:layout_gravity="center_vertical"
    >


<include layout="@layout/image_container_layout"
         android:id="@+id/included_view"
            />

<TextView
        style="@style/wizard_content_text_style_medium"
        android:id="@+id/text_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/included_view"
        android:layout_centerHorizontal="true"
        android:gravity="center_horizontal"
        android:layout_gravity="center_horizontal"
        android:text="@string/instruction"
        android:layout_marginBottom="15dp"/>

The reason that the layout is included is that we want to reuse it in two more layouts. Now based on some condition I want to hide or show the image views inside image_container_layout.

The java code looks like this:

  containerLayout = (ViewGroup) ((Activity) getAndroidContext()).getLayoutInflater().inflate(R.layout.image_container_layout, null);
   image1 = (ImageView) containerLayout.findViewById(R.id.image_1);
   image2 = (ImageView) containerLayout.findViewById(R.id.image_2);
  switch (accuracy) {
    case 1:
        log().i(getClass().getSimpleName(), "case 1 chosen");
        image1.setVisibility(View.VISIBLE);
        image2.setVisibility(View.GONE);
        log().i(getClass().getSimpleName(), "image 1 has been shown");
        break;
    case 2:
        image1.setVisibility(View.GONE);
        image2.setVisibility(View.VISIBLE);
        break;
    case 3:
        image1.setVisibility(View.VISIBLE);
        image2.setVisibility(View.VISIBLE);
        break;
}

I am debugging this code and I am sure the code is running. The log messages are printed in Logcat as well, but nothing happens no change in the images. Also, both images are always shown. I wonder if there is something that I have to do when working with the included layout? Thanks for any help in advance. Based on answers I got below, seems that inflating a view will create a new object and because of this, changes in the visibility are not shown on the user interface. Then the question is that if we have a wizard and inside 3 different pages of the wizard I want to have an image and depending on some condition I want to show or hide the image, what is the best solution? I mean I want to reuse the code which is responsible for hiding and showing the image regardless which page of wizard is active.

Mayur Coceptioni :

Why are you complexing with so much code. If you include some layout in your xml then you can use those widgets also same as the xml have. There is no need to inflate.

ImageView image_2 = findViewById(R.id.image_2);
image_2.setVisbility(Visible.GONE);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=125424&siteId=1