Wednesday, 20 March 2013

Shared Preferences Example in Android

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/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="22dp"
            android:layout_marginTop="30dp"
            android:text="Enter Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/pref_editText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@+id/textView1"
            android:layout_marginTop="20dp"
            android:ems="10" />

        <TextView
            android:id="@+id/pref_textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView2"
            android:layout_marginTop="28dp"
            android:layout_toRightOf="@+id/textView1"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <Button
            android:id="@+id/show_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/pref_editText"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="28dp"
            android:layout_toRightOf="@+id/save_button"
            android:text="Show" />

        <Button
            android:id="@+id/save_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/show_button"
            android:layout_alignBottom="@+id/show_button"
            android:layout_alignLeft="@+id/pref_editText"
            android:text="Save" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/save_button"
            android:layout_below="@+id/show_button"
            android:layout_marginTop="105dp"
            android:text="Preference Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <Button
            android:id="@+id/delete_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/textView2"
            android:layout_marginLeft="18dp"
            android:layout_toRightOf="@+id/textView2"
            android:text="Delete" />

        <Button
            android:id="@+id/clear_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/show_button"
            android:layout_centerVertical="true"
            android:text="Clear All" />

    </RelativeLayout>

MainActivity.java

package com.rajeshvijayakumar.sharedpreferences;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    private EditText mInputEditText;
    private TextView mOutputView;
    private Button mSaveButton;
    private Button mShowButton;
    private Button mDeleteButton;
    private Button mClearButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mInputEditText = (EditText) findViewById(R.id.pref_editText);
        mSaveButton = (Button) findViewById(R.id.save_button);
        mShowButton = (Button) findViewById(R.id.show_button);
        mDeleteButton = (Button) findViewById(R.id.delete_button);
        mOutputView = (TextView) findViewById(R.id.pref_textView);
        mClearButton = (Button) findViewById(R.id.clear_button);
        mSaveButton.setOnClickListener(this);
        mShowButton.setOnClickListener(this);
        mDeleteButton.setOnClickListener(this);
        mClearButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        String message="";
        switch (v.getId()) {
            case R.id.save_button:
                SavePreferences("PrefDemo", mInputEditText.getText().toString());
                message="Text Saved in Preferences";
                break;
            case R.id.delete_button:
                deletePreferences("PrefDemo");
                message = "Text Deleted from Preferences";
                break;
            case R.id.show_button:
                showPreferences("PrefDemo");
                message="Text Displayed from Preferences";
                break;
            case R.id.clear_button:
                clearAllPreferences();
                message="Removed All Text from All Preferences";
                break;
        }
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }

    private void SavePreferences(String key, String value) {
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    private void deletePreferences(String key) {

        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.remove(key);
        editor.commit();
    }
   
    private void clearAllPreferences() {
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.commit();
    }
    private void showPreferences(String key){
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        String savedPref = sharedPreferences.getString(key, "");
        mOutputView.setText(savedPref);
       }
}


output :








Source Code : Download this Example Here


Wednesday, 13 March 2013

Inflating UI Dynammically Example in Android

activity_main.xml

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

        <Button
            android:id="@+id/add_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="29dp"
            android:text="Add" />

        <Button
            android:id="@+id/delete_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="36dp"
                        android:layout_marginTop="10dp"
            android:layout_toRightOf="@id/add_button"
            android:text="Delete" />

        <LinearLayout android:id="@+id/child_linear"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/add_button"
            android:layout_marginTop="45dp"
            android:orientation="vertical" >
        </LinearLayout>

    </RelativeLayout>

custom_layout.xml


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

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />

</LinearLayout>

MainActivity.java

package com.rajeshvijayakumar.inflat;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements OnClickListener {

    private Button mAddButton;
    private Button mDeleteButton;
    private LinearLayout mLinear;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAddButton = (Button) findViewById(R.id.add_button);
        mDeleteButton = (Button) findViewById(R.id.delete_button);
        mLinear = (LinearLayout) findViewById(R.id.child_linear);
        mAddButton.setOnClickListener(this);
        mDeleteButton.setOnClickListener(this);
       
    }

    @Override
    public void onClick(View v) {
       
        switch(v.getId()) {
            case R.id.add_button :
                View childView = getLayoutInflater().inflate(R.layout.custom_layout, null);
                mLinear.addView(childView);
                break;
            case R.id.delete_button :
                int childSize = mLinear.getChildCount();
                if(childSize != 0) {
                    mLinear.removeViewAt(childSize - 1);
                }
                break;
        }
    }  
}

Output :






Source Code :  Download this Example Here

Tuesday, 12 March 2013

Text Watcher Example in Android

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/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="26dp"
            android:layout_marginTop="40dp"
            android:text="Enter Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/input_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@+id/textView1"
            android:layout_marginTop="22dp"
            android:ems="10"  />

        <TextView
            android:id="@+id/output_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/input_text"
            android:layout_below="@+id/input_text"
            android:layout_marginTop="26dp"
            android:text="Your Text Here"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </RelativeLayout>



MainActivity.java


package com.rajeshvijayakumar.textwatcher_example;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements TextWatcher {

    private EditText mInputText;
    private TextView mOutputText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mInputText = (EditText) findViewById(R.id.input_text);
        mOutputText = (TextView) findViewById(R.id.output_text);
        mInputText.addTextChangedListener(this);
    }

    @Override
    public void afterTextChanged(Editable arg0) {
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence txtWatcherStr, int start, int before, int count) {

        String outputedText = txtWatcherStr.toString();
        mOutputText.setText(outputedText);
    }
}


Output :






Source Code :  Download this Example Here