Sunday, 11 August 2013

Instant Adapter View Handler Example in Android

Get Instant Adapter Library from the following Url : https://github.com/ragunathjawahar/instant-adapter

Import and Add Instant library Project to your Project as shown below :





Then use the following code :

In activity_main.xml


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

    <ListView
        android:id="@+id/name_list_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>


In list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

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

        <TextView
            android:id="@+id/person_name"
            android:layout_width="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_height="wrap_content"
            android:text="TextView" />
      
    </LinearLayout>

</RelativeLayout>

Create Person.java Model

package com.rajeshvijayakumar.insta;

public class Person {

    private int photoResId;
    private String name;

    public Person(int photoResId, String name) {

        this.photoResId = photoResId;
        this.name = name;
    }

    public int getPhotoResId() {
        return photoResId;
    }

    public void setPhotoResId(int photoResId) {
        this.photoResId = photoResId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

MainActivity.java

package com.rajeshvijayakumar.insta;
public class MainActivity extends Activity implements Evaluator<Person>,
        OnItemClickListener {

    private ListView mPersonListView;
    private InstantAdapter<Person> mAdapter;
    private List<Person> mPersons;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPersonListView = (ListView) findViewById(R.id.name_list_view);
        mPersons = getPersons();
        mAdapter = new InstantAdapter<Person>(MainActivity.this,
                R.layout.list_item, Person.class, mPersons);
        mAdapter.setViewHandler(R.layout.list_item, MainActivity.this);
        mAdapter.setViewHandler(R.id.person_image, MainActivity.this);
        mAdapter.setViewHandler(R.id.person_name, MainActivity.this);
        mPersonListView.setAdapter(mAdapter);
        mPersonListView.setOnItemClickListener(this);
    }

    private List<Person> getPersons() {
        List<Person> personLst = new ArrayList<Person>();
        personLst.add(new Person(R.drawable.ic_fav, "Rajesh"));
        personLst.add(new Person(R.drawable.ic_rate, "Mahesh"));
        personLst.add(new Person(R.drawable.ic_person, "Akshay"));
        personLst.add(new Person(R.drawable.ic_group, "Aakash"));
        return personLst;
    }

    @Override
    public void handleView(ListAdapter adapter, View parent, View view,
            Person person, int position) {

        switch (view.getId()) {
        case R.id.person_image:
            ((ImageView) view).setImageResource(person.getPhotoResId());
            break;
        case R.id.person_name:
            ((TextView) view).setText(person.getName());
            break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adpaView, View v, int position,
            long id) {
        Toast.makeText(this, mPersons.get(position).getName().toString(),
                Toast.LENGTH_SHORT).show();
    }
}

Output :


Source Code :  Coming soon to Github...........

Wednesday, 31 July 2013

Converting a Layout view to Image in Android Example

First Add permission for external storage in Manifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.rajeshvijayakumar.view2imagedemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="org.rajeshvijayakumar.view2imagedemo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

img_view.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffffff" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="25dp"
        android:text="Android Jelly Bean 4.3"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="21sp" />

</RelativeLayout>

home.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Convert" />
  
    <include android:id="@+id/f_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        layout="@layout/img_view"
        android:layout_above="@id/button1"/>

</RelativeLayout>

MainActivity.java


package org.rajeshvijayakumar.view2imagedemo;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

    private Button mButton;
    private View mView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        mView = findViewById(R.id.f_view);
        mButton = (Button) findViewById(R.id.button1);
        mButton.setOnClickListener(this);

        mView.setDrawingCacheEnabled(true);
        mView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        mView.layout(0, 0, mView.getMeasuredWidth(), mView.getMeasuredHeight());
        mView.buildDrawingCache(true);
    }

    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.button1) {
            Bitmap b = Bitmap.createBitmap(mView.getDrawingCache());
            mView.setDrawingCacheEnabled(false);
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

            File f = new File(Environment.getExternalStorageDirectory() + File.separator + "v2i.jpg");
            try {
                f.createNewFile();
                FileOutputStream fo = new FileOutputStream(f);
                fo.write(bytes.toByteArray());
                fo.close();
            } catch (Exception e) {
            }
            finish();
        }
    }
}

Output :





Source Code : Coming Soon to Github..........

Sunday, 28 July 2013

Listing Files from SDCard in Android Example


First Add permission for external storage in Manifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.rajeshvijayakumar.fldemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="org.rajeshvijayakumar.fldemo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

flist.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>

fl_list_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/fname"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

MainActivity.java

package org.rajeshvijayakumar.fldemo;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnItemLongClickListener {

    private ListView mListView;
    private List<String> fileNameList;
    private FlAdapter mAdapter;
    private File file;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.flist);
        mListView = (ListView) findViewById(R.id.listView1);
        file = Environment.getExternalStorageDirectory();
        fileNameList = getFileListfromSDCard();
        mAdapter = new FlAdapter(this, R.layout.fl_list_item, fileNameList);
        mListView.setAdapter(mAdapter);
    }

    private List<String> getFileListfromSDCard() {
        String state = Environment.getExternalStorageState();
        List<String> flLst = new ArrayList<String>();
        if (Environment.MEDIA_MOUNTED.equals(state) && file.isDirectory()) {
            File[] fileArr = file.listFiles();
            int length = fileArr.length;
            for (int i = 0; i < length; i++) {
                File f = fileArr[i];
                flLst.add(f.getName());
            }
        }

        return flLst;
    }

    public class FlAdapter extends ArrayAdapter<String> {

        private List<String> fLst;
        private Context adapContext;

        public FlAdapter(Context context, int textViewResourceId,
                List<String> fLst) {
            super(context, textViewResourceId, fLst);
            this.fLst = fLst;
            adapContext = context;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View view = convertView;
            FHolder fHolder = null;

            if (convertView == null) {
                view = View.inflate(adapContext, R.layout.fl_list_item, null);

                fHolder = new FHolder();
                fHolder.fNameView = (TextView) view.findViewById(R.id.fname);

                view.setTag(fHolder);
            } else {
                fHolder = (FHolder) view.getTag();
            }
            String fileName = fLst.get(position);
            fHolder.fNameView.setText(fileName);

            return view;
        }
    }

    static class FHolder {
        public TextView fNameView;
    }
}

Output :

  

Source : Coming Soon to Github.............