Showing posts with label ImageVIew. Show all posts
Showing posts with label ImageVIew. Show all posts

Saturday, June 29, 2013

How to add common OnClick event for ImageView for every activity? - Android

If your layout contains one common header (with an ImageView) across multiple activities.. you may need to write an OnClick event for the imageView in every single activity..

Instead of writing the same code in every activity you can write a class the extends OnClickListener and the OnClick method like below,

public class ImageViewOnclickListener implements OnClickListener {
  private Context context;
  public ImageViewOnclickListener(Context context) {    this.context = context;
  }

  @Override
  public void onClick(View arg0) {
    Intent intent = new Intent(context, SomeActivity.class);
    context.startActivity(intent);
  }
}

In your activities:
protected void onCreate(...) {
  setContentView(...);
  ((ImageView) findViewById(R.id.mybutton)).setOnClickListener(new ImageViewOnclickListener(this));
}
Hope this helps :)

Friday, June 14, 2013

Missing contentDescription attribute on ImageView in XML - Android



I've the below ImageView in my XML file,

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

I get a warning saying that [Accessibility]Missing contentDescription attribute on image in Eclipse IDE. Since it is a warning we used to ignore it.. However to overcome this warning, all you need to add is the below piece of code in ImageView,

android:contentDescription="@string/description" (static or dynamic) 

This is helpful for people using alternative input methods because of their disability (like TalkBack, Tecla Access Shield etc)

You can read more about this here http://android-developers.blogspot.in/2012/04/accessibility-are-you-serving-all-your.html