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

Thursday, June 13, 2013

How to change ImageView Source - Android?

Consider you have set the source for the ImageView in your xml as below,

<ImageView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:id="@+id/myImgView"
            android:src="@drawable/something"/>
To change the image source programatically,

myImgView.setImageResource(R.drawable.ball);
or,

myImgView.setImageDrawable(getResources().getDrawable(R.drawable.ball));

GridView scroll - Android


I've a list of images in my app that needs to be shown in a Grid layout.. I've tried many ways to set height in Gridview layout, but all went in vain.. 

After too much of research, I stumbled on the excellent answer of Neil Traft.  I just copy-pasted his code in my app and it worked like a charm :)

ExpandableHeightGridView.java:

public class ExpandableHeightGridView extends GridView
{

    boolean expanded = false;

    public ExpandableHeightGridView(Context context)
    {
        super(context);
    }

    public ExpandableHeightGridView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public ExpandableHeightGridView(Context context, AttributeSet attrs,
            int defStyle)
    {
        super(context, attrs, defStyle);
    }

    public boolean isExpanded()
    {
        return expanded;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        // HACK! TAKE THAT ANDROID!
        if (isExpanded())
        {
            // Calculate entire height by providing a very large height hint.
            // But do not use the highest 2 bits of this integer; those are
            // reserved for the MeasureSpec mode.
            int expandSpec = MeasureSpec.makeMeasureSpec(
                    Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);

            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();
        }
        else
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public void setExpanded(boolean expanded)
    {
        this.expanded = expanded;
    }
}

embed the GridView inside ScrollView as below,

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <RelativeLayout
        android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <com.droid.gview.ExpandableHeightGridView
       android:id="@+id/dashboard_grid"
       android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:horizontalSpacing="2dp"
    android:isScrollContainer="false"
    android:numColumns="2"
    android:stretchMode="columnWidth"
    android:verticalSpacing="20dp" />
</RelativeLayout>
</ScrollView>    

and lastly set the following,


ExpandableHeightGridView gridview = (ExpandableHeightGridView)  findViewById(R.id.dashboard_grid);
gridview.setExpanded(true);

Monday, April 8, 2013

Capitalize every letter in EditText - Android

I had an interesting usecase in my application, where I need to make EditText uppercase by default.

After googling it for a while found the solution,


EditText someText = (EditText) findViewById(R.id.someText);
someText.setInputType(0x00001001);



This will solve the problem :) Let me know if there are any alternate ways

Wednesday, February 13, 2013

Subdirectories in drawable folder - Android?


In Android SDK documentation, all of the examples shown used @drawable/someimage directly address images that are stored in res/drawable directory..

I wondered will it  be possible to create subfolders under drawable folder.. something like


res/drawable
-- fruits
  -- apple.png
  -- orange.png
-- vegetables
  -- cabbage.png
  -- carrot.png

Later I found that it is not possible to create subfolders under drawable directory.. we need to keep the hierarchy flat. Having subfolders under drawable directory will cause the compiler to fail - preventing R.java from being generated correctly..

So what is the workaround??

We can create subfolders under assets directory and load images from there.


Sample code to load images from Assets directory
InputStream is = null;
try {
    is = this.getResources().getAssets().open("fruits/apple.png");
} catch (IOException e) {
    //something
}

image = BitmapFactory.decodeStream(is);



Wednesday, January 30, 2013

Layouts in Android!!


Layouts in Android:

The UI in Android is a hierarchy of viewgroups and views. The viewgroups will be intermediate nodes in the hierarchy, and the views will be terminal nodes.
For example, in the above main.xml file, the LinearLayout is a viewgroup and the TextView is a view.
Android provides the following standard layouts (viewgroups) that can be used in you Android application.
  • LinearLayout
  • RelativeLayout
  • AbsoluteLayout
  • FrameLayout
  • TableLayout


LinearLayout:

LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute.
All children of a LinearLayout are stacked one after the other, so a vertical list will only have one child per row, no matter how wide they are, and a horizontal list will only be one row high

RelativeLayout:

In a Relative layout every element arranges itself relative to other elements or a parent element.

In order to specify the position with relation to its parent container, we use android:layout_alignParentTop=“true” and android:layout_alignParentLeft=“true” to align elements to the top-left of the parent container. Then, to align with respect to another element we can use the properties android:layout_alignLeft=“@+id/otherelement” and android:layout_below=“@+id/otherelement”.

AbsoluteLayout:

In absolute layout, we can specify the exact coordinates of each control that we want to place. In absolute layout, we will give the exact X and Y coordinates of each control. You can move them around by changing the android:layout_x and android:layout_y properties within the control.
AbsoluteLayout has been deprecated and is not recommended , as it gets tied to a particular screen size.

FrameLayout:

Frame layout is used when you want to show one item on each screen. Using frame layout, we can have multiple items, but they will be overlapping and only only displaying themselves one at a time.

TableLayout:

A layout that arranges its children into rows and columns. A TableLayout consists of a number of TableRow objects, each defining a row.


Tuesday, December 11, 2012

How to make an Activity fullscreen??

There are two ways to make an activity fullscreen,

In your OnCreate method in the activity class,


getWindow().addFlags(LayoutParams.FLAG_FULLSCREEN);

From AndroidManifest.xml:

<activity android:name=" "

---

----

android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />