Thursday, 1 November 2012

Shared Preference Example which stores data locally and displays in other activity

package com.shared;





import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class SharedprefActivity extends Activity {
    private static final String TAG = SharedprefActivity.class.getSimpleName();
    private EditText id,name;
    private Button bt1;
    private SharedPreferences shared;
   
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample1);
       
        id = (EditText)findViewById(R.id.ET1);
        name = (EditText)findViewById(R.id.ET2);
        bt1 = (Button) findViewById(R.id.backbutton);
       shared = getSharedPreferences("shared", 0);
      
        id.setText(shared.getString("id", "novalue"));
        name.setText(shared.getString("name", "noname"));
        bt1.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.i(TAG,"button called" );
                String f1,f2;
                f1 = id.getText().toString();
                f2 = name.getText().toString();
                Log.i(TAG,"c log example" +f1+" "+f2);
                SharedPreferences.Editor editor = shared.edit();
                editor.putString("id", f1);
                editor.putString("name",f2 );
                editor.commit();
                Intent  act = new Intent(v.getContext(),act.class);
                startActivity(act);
               
            }
        });
    }
  
}



package com.shared;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;

public class act extends Activity{
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act);
         SharedPreferences shared;
        EditText name = (EditText) findViewById(R.id.ET1);
        EditText id = (EditText) findViewById(R.id.ET2);
        shared = getSharedPreferences("shared", 0);
        String idshared = shared.getString("name", "null");
        String nameshared = shared.getString("id", "null");
        name.setText(nameshared);
        id.setText(idshared);
       
    }

}

Sample1.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
     <Button
        android:id="@+id/backbutton"
        android:text="Back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:text="First Name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <EditText
            android:id="@+id/ET1"
            android:width="100px"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />   
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">   
        <TextView
            android:text="Last Name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <EditText
            android:id="@+id/ET2"
            android:width="100px"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>


act.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
   
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:text="First Name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <EditText
            android:id="@+id/ET1"
            android:width="100px"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />   
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">   
        <TextView
            android:text="Last Name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <EditText
            android:id="@+id/ET2"
            android:width="100px"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>


Sample output of first screen

 

 Sample output of second screen 

Displays List of Name and Id in other activity which are entered in previous activity on click of button

package com.success;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TableLayout;

public class SuccessActivity extends Activity {
   
    /** Called when the activity is first created. */
   
     private TableLayout tab1;
     private LinearLayout lv1;
    //private LinearLayout lv1;
    private LinearLayout lv2;
    private ListView listv;
    private static final String TAG = SuccessActivity.class
            .getSimpleName();
    private static List<DisplayTo> displaylist= new ArrayList<DisplayTo>();
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         lv1 = (LinearLayout)findViewById(R.id.linear1);
         lv2 = (LinearLayout)findViewById(R.id.linear2);
         tab1 = (TableLayout)findViewById(R.id.table1);
          listv = (ListView)findViewById(R.id.listView1);
       // listv.setAdapter(new OrderAdapter(this ,displaylist));
       
        Button button1 =(Button)findViewById(R.id.button1);
        Button button2 = (Button) findViewById(R.id.button2);
        Button button3 = (Button) findViewById(R.id.button3);
       // ListView list = (ListView) findViewById(R.id.listView1);
        button1.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                tab1.setVisibility(View.VISIBLE);
                lv2.setVisibility(View.GONE);
               
            }
        });
        button2.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //Log.i(TAG,"button2");
                tab1.setVisibility(View.GONE);
                lv2.setVisibility(View.VISIBLE);
                for(DisplayTo displayTo:displaylist)
                {
                    Log.i(TAG,displayTo.getName());
                }
                listv.setAdapter(new OrderAdapter(SuccessActivity.this,displaylist));
               
               
            }
        });
        button3.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
           
                EditText et1 = (EditText)findViewById(R.id.editText1);
                EditText et2 = (EditText) findViewById(R.id.editText2);
                int id = Integer.parseInt(et1.getText().toString());
                String name = et2.getText().toString();
                System.out.println("text in ed1" + id);
                System.out.println("text in ed2" + name);
                displaylist.add(new DisplayTo(id,name));
               
               
               
               
               
            }
        });
       
       
       
       
    }
}



package com.success;

public class DisplayTo {
    private int id;
    private String name;
    public DisplayTo() {
        super();
    }
    public DisplayTo(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}


package com.success;

import java.util.List;

import android.R.layout;
import android.app.Activity;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class OrderAdapter extends BaseAdapter {
    private Activity activity;
    private List<DisplayTo> items;

    public OrderAdapter(Activity activity, List<DisplayTo> items) {
        // TODO Auto-generated constructor stubi
        this.activity = activity;
        this.items = items;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return items.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return items.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder;
        if (convertView == null) {
            LayoutInflater inflater = activity.getLayoutInflater();
            convertView = inflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.textView1);
            holder.id = (TextView) convertView.findViewById(R.id.textView2);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (position < items.size()) {
            final DisplayTo producName = items.get(position);

            holder.name.setText("" + producName.getId());
            holder.id.setText(producName.getName());
        }
        return convertView;
    }

    private static class ViewHolder {
        TextView name, id;
    }
}


Main.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" >
<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <LinearLayout
        android:id="@+id/linear1"
        android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal">

           <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button1" />
     
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button2" />
       
    </LinearLayout>
    <TableLayout
        android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:id="@+id/table1">
        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ID" />
           
           
           
           
            <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType="textPersonName" >

            <requestFocus />
        </EditText>
       
           
           
           
        </TableRow>
        <TableRow android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

           
            <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name" />
           
            <EditText
            android:id="@+id/editText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType="textPersonName" >

            <requestFocus />
        </EditText>
           
                     
        </TableRow>
  
<TableRow android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
   
       
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="submit" />
       
   </TableRow>
    </TableLayout>
    <LinearLayout
        android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:id="@+id/linear2" >
            <TextView
            android:id="@+id/android:empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hi" />
   
    <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
        </ListView>
   
     </LinearLayout>
</LinearLayout>


List_item

<?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"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="88dp"
        android:text="TextView" />

    <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="50dp"
        android:text="TextView" />
   
</RelativeLayout>


Sample output of first screen

 

 

 

Sample output of second screen 

 

 

 

 

 

Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...