imageview set image from resource (on viewpager's tab)

LaserLaser RUMember ✭✭
edited June 2017 in Xamarin.Android

Hello!
How can I assign a drawable png to ImageView in Fragment?
I declare an ImageView in tab's layout:

<ImageView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content "
          android:layout_rowSpan="2"
          android:id ="@+id/iv_icon"/>````

The tab class is here:
class tabFragment1 : V4Fragment
{

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved)
        {
            var v = inflater.Inflate(Resource.Layout.tabLayout1, container, false);
            return v;
        }

    }

I want to assign an .png from drawable folder , something like this:
ImageView iv_icon = FindViewById(Resource.Id.iv_icon);
iv_icon.SetImageResource(Resource.Drawable.ic_weather_cloudy_white_48dp);

If I put it in the MainActivity OnCreate() method (below the ViewPager initialisation, of course), I get NullRefenceException Error (on the second row).
The question is: in which class should I put these statements?

Thanks!

Best Answer

Answers

  • VIVEKNEGIVIVEKNEGI USMember ✭✭✭

    Include your imageview in fragment xml itself.... or share your fragment xml

  • LaserLaser RUMember ✭✭
    edited June 2017

    @VIVEKNEGI said:
    Include your imageview in fragment xml itself.... or share your fragment xml

    The imageview is already in fragment xml.
    Here is full fragment xml:

    <?xml version="1.0" encoding="utf-8"?>
    <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/grid_general"
        android:rowCount="5"
        android:columnCount="2"
        android:orientation="vertical">
    
        <TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_columnSpan="2"
          android:text="Here comes the rain again"
          android:textSize="@dimen/abc_text_size_medium_material"
          android:id="@+id/tv_field1"
          android:gravity="center" />
    
        <ImageView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_rowSpan="2"
          android:id ="@+id/iv_icon"/>
    
        <TextView
          android:text="-20 C"
          android:textSize="@dimen/abc_text_size_large_material"
          android:id="@+id/tv_temp_current"
          android:gravity="center"
          android:layout_gravity="fill" />
    
        <TextView
          android:text="Realfeel +20 C"
          android:textSize="@dimen/abc_text_size_medium_material"
          android:id="@+id/tv_field2"
          android:gravity="center" />
    
        <TextView
          android:text="Snowy"
          android:textSize="@dimen/abc_text_size_medium_material"
          android:id="@+id/tv_field3"
          android:gravity="center" />
    
        <TextView
            android:text="Min -20 Max +20 C"
            android:textSize="@dimen/abc_text_size_small_material"
            android:id="@+id/tv_field4"
            android:gravity="center" />
    
    </GridLayout>
    
  • LaserLaser RUMember ✭✭
    And what about the second row where a bitmap is assigned to iv_icon? Should I put it in MainActivity?
  • LaserLaser RUMember ✭✭
    Thanks, I got it. The second row goes after the first on fragment class.
  • VIVEKNEGIVIVEKNEGI USMember ✭✭✭

    @Laser superb :smile:

Sign In or Register to comment.