Sunday, 11 August 2013

StartActivityForResult with Dialog Activity Example in Android

In activity_main.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/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="95dp"
        android:layout_marginTop="62dp"
        android:textSize="19sp"
        android:text="**********" />

    <Button
        android:id="@+id/set"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/name"
        android:layout_below="@+id/name"
        android:layout_marginTop="24dp"
        android:text="Set Name" />

</RelativeLayout>


In dialog_box.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" >

    <EditText
        android:id="@+id/name_edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:ems="10"
        android:inputType="textPersonName" />

    <Button
        android:id="@+id/ok"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name_edit_text"
        android:layout_marginTop="50dp"
        android:layout_toLeftOf="@+id/cancel"
        android:text="Ok" />

    <Button
        android:id="@+id/cancel"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/ok"
        android:layout_alignBottom="@+id/ok"
        android:layout_alignParentRight="true"
        android:layout_marginRight="14dp"
        android:text="Cancel" />

</RelativeLayout>


MainActivity.java

package com.rajeshvijayakumar.safr;

public class MainActivity extends Activity implements OnClickListener {

    private TextView mNameTextView;
    private Button mSetButton;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mNameTextView = (TextView) findViewById(R.id.name);
        mSetButton = (Button) findViewById(R.id.set);
        mSetButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.set:
            Intent intent = new Intent(this, NameDialogActivity.class);
            startActivityForResult(intent, 1);
            break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       
        if(resultCode == RESULT_OK && data.getExtras().containsKey("name")) {
            String name = data.getExtras().getString("name");
            mNameTextView.setText(name);
        }
    }   
}


NameDialogActivity.java

package com.rajeshvijayakumar.safr;

public class NameDialogActivity extends Activity implements OnClickListener {

    private EditText mNameEditText;
    private Button mOk;
    private Button mCancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_box);
        mNameEditText = (EditText) findViewById(R.id.name_edit_text);
        mOk = (Button) findViewById(R.id.ok);
        mCancel = (Button) findViewById(R.id.cancel);
        mOk.setOnClickListener(this);
        mCancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        Intent intent = getIntent();
        switch (v.getId()) {
        case R.id.ok:
            String name = mNameEditText.getText().toString();
            intent.putExtra("name", name);
            setResult(RESULT_OK, intent);
            break;
        case R.id.cancel:
            setResult(RESULT_CANCELED, intent);
            break;
        }
        finish();
    }
}


In manifest.xml add theme as @android:style/Theme.Dialog for NameDialogActivity

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.rajeshvijayakumar.safr.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>
        <activity
            android:name=".NameDialogActivity"
            android:theme="@android:style/Theme.Dialog" />
    </application>

</manifest>


Output :






















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...........