vendredi 8 mai 2015

Dynamic creation of gridView with varying row-column size, where grid Items are rounded corner TextView

This is the gridview I'm trying to make. All the letter tiles must be distinct textviews but with corners rounded.

http://ift.tt/1RjnpqR

And this is how far I have come.

public class MainActivity extends Activity {
GridView grid;
static final String[] letters = new String[] {"B","A","T","t","t"};
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    grid = (GridView) findViewById(R.id.myGrid);
    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, letters);
    grid.setAdapter(adapter);


    grid.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id)
        {
            Toast.makeText(getApplicationContext(),
                    ((TextView) v).getText(), Toast.LENGTH_SHORT).show();

            v.setBackgroundColor(Color.CYAN);
        }
    });
}

}

My xml file,

<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"  android:layout_width="match_parent"
android:layout_height="match_parent"  android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"  tools:context=".MainActivity">

<GridView xmlns:android="http://ift.tt/nIICcg"
    android:id="@+id/myGrid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnWidth="50dp"
    android:gravity="center"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:background="@drawable/rounded">
</GridView>

</RelativeLayout>

Now the board I'm trying to make is like,

                         F I
                         R E

and for 5 digit letter it'll show as,

                     E I G
                      H T

I'm trying to set the layout from my java file dynamically. Please provide some suggestion or example code. I don't think I understand the part how can I get individual items of the ArrayAdapter, so that I can set it's color yellow or rounded corners. So if you will please explain a little it'll be a great help. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire