Saturday, 25 August 2012

GridView with Images in Android


GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid. The grid items are automatically inserted to the layout using a Adapter.

Here The below Example Project shows GridView With  Images Elements.
UI Layout(grid_view_example.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <GridView
        android:id="@+id/gridview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="90dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" />

</LinearLayout>
Android Activity Class(GridViewExample.java)

package com.androidsurya.androidgridviewexample;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class GridViewExample extends Activity {

    GridView gridView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_view_example);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));
    }

    public class ImageAdapter extends BaseAdapter {
        private Context mContext;

        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return mThumbIds.length;
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }
            imageView.setImageResource(R.drawable.ic_launcher);
            return imageView;
        }

        private Integer[] mThumbIds = { R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher };
    }

}
Register Activity in AndroidManifest file

<activity
            android:name="com.androidsurya.androidgridviewexample.GridViewExample"
            android:label="@string/app_name" >

Output Screenshot


 





For more information about GridView :- Android Developers site

Thursday, 23 August 2012

GridView Example in Android

GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid. The grid items are automatically inserted to the layout using a Adapter.


Here The below Example Project shows GridView With A to Z TextView Elements.
UI Layout(grid_view_example.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".GridViewExample" >

    <GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:numColumns="3" >
    </GridView>

</RelativeLayout>

Android Activity Class(GridViewExample.java)

package com.androidsurya.androidgridviewexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

public class GridViewExample extends Activity {

    static final String[] numbers = new String[] { "A", "B", "C", "D", "E",
            "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
            "S", "T", "U", "V", "W", "X", "Y", "Z" };
    GridView gridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_view_example);
        gridView = (GridView) findViewById(R.id.gridView);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, numbers);

        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                // TODO Auto-generated method stub

                Toast.makeText(getApplicationContext(),
                        "Selected: " + ((TextView) v).getText(),
                        Toast.LENGTH_SHORT).show();

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.grid_view_example, menu);
        return true;
    }

}

Register Activity in AndroidManifest file

<activity
            android:name="com.androidsurya.androidgridviewexample.GridViewExample"
            android:label="@string/app_name" >

Output Screenshot








For more information about GridView :- Android Developers site

Thursday, 16 August 2012

Context Menu Example in Android

Context Menu is a floating list of menu items that appears when a user touches
and holds a particular item displayed in the view,which has a menu associated with it.

Here below example shows ContextMenu when user Long_press on one Button.

UI Layout(android_context_menu_example.xml)


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AndroidContextMenuExample" >

    <Button
        android:id="@+id/click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="Long-press on me i will show Context Menu.." />

</RelativeLayout>

Android Activity (AndroidContextMenuExample.java)

package com.androidsurya.androidcontextmenu;

import android.os.Bundle;
import android.app.Activity;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.Toast;

public class AndroidContextMenuExample extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.android_context_menu_example);

        Button click = (Button) findViewById(R.id.click);
        registerForContextMenu(click);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("Context Menu Title");
        menu.add(0, v.getId(), 0, "Item 1");
        menu.add(0, v.getId(), 0, "Item 2");
        menu.add(0, v.getId(), 0, "Item 3");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // when ever user Click on any item Toast message will fired..for Demo
        // purpose i added Toast message now you can add your functionality
        if (item.getTitle() == "Item 1") {
            Toast.makeText(getApplicationContext(), "Item 1 clicked....",
                    Toast.LENGTH_LONG).show();
        } else if (item.getTitle() == "Item 2") {
            Toast.makeText(getApplicationContext(), "Item 2 clicked....",
                    Toast.LENGTH_LONG).show();
        } else if (item.getTitle() == "Item 3") {
            Toast.makeText(getApplicationContext(), "Item 3 clicked....",
                    Toast.LENGTH_LONG).show();
        } else {
            return false;
        }
        return true;
    }

}

Register Activity in AndroidManifest file

 <activity
            android:name="com.androidsurya.androidcontextmenu.AndroidContextMenuExample"
            android:label="@string/app_name" >

Output Screenshot











For More information about Context Menu : Android Developers site

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...