Reputation: 1558
I am able to set image in Non-Custom ImageView
but unable to set Image in Custom ImageView
, app loads successfully but unable to see the Image
, below is the code.
public class MainActivity : AppCompatActivity
{
public ImageTestView imageView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
imageView = FindViewById<ImageTestView>(Resource.Id.imageView1);
imageView.SetImageResource(Resource.Mipmap.line_indent);
}
}
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageViewTestProject.ImageTestView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView1" />
</RelativeLayout>
public class ImageTestView : ImageView
{
public ImageTestView(Context context,IAttributeSet attr) :
base(context,attr)
{
}
}
Upvotes: 0
Views: 357
Reputation: 16449
Update
In the end, adding the image background through XML ended up solving the issue.
android:background="@mipmap/imagename"
Well I think the reason it's not working is that of the unavailable constructors and improper initialization
public class ImageTestView : ImageView
{
Context mContext;
public ImageTestView (Context context) : base(context)
{
init(context, null);
}
public ImageTestView (Context context, Android.Util.IAttributeSet attrs) : base(context, attrs)
{
init(context, attrs);
}
public ImageTestView (Context context, Android.Util.IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
{
init(context, attrs);
}
public ImageTestView (Context context, Android.Util.IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
{
init(context, attrs);
}
private void init(Context ctx, Android.Util.IAttributeSet attrs)
{
mContext = ctx;
}
}
Then use this custom imageview in your app like:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageViewTestProject.ImageTestView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView1" />
</RelativeLayout>
And then you can set the image resource to it like this:
var imageView = FindViewById<ImageTestView>(Resource.Id.imageView1);
imageView?.SetImageResource(Resource.Mipmap.line_indent);
Revert in case this does not work or in case of queries
Upvotes: 1
Reputation: 500
1.First thing you not doing anything in custom ImageTestView class.
2.And setting the image Resource is wrong.either you have to taking from Drawable folder it should be.
imageView.SetImageResource(Resource.Drawable.line_indent);
I don't know in Xamarin Android.But if you know Android please refer this Example.it's bit similar to xamarin Android
http://www.java2s.com/Code/Android/UI/extendsImageView.htm
Upvotes: 0