Friday, August 8, 2014

Create JSON Array and JSON Object




private String writeJSON() {

             JSONObject student1Detail = new JSONObject();
             try {
                    student1Detail.put("id", "1");
                    student1Detail.put("name", "abc");
             } catch (JSONException e) {
                    e.printStackTrace();
             }

             JSONObject student2Detail = new JSONObject();
             try {
                    student2Detail.put("id", "2");
                    student2Detail.put("name", "xyz");
             } catch (JSONException e) {
                    e.printStackTrace();
             }

             JSONArray jsonArray = new JSONArray();

             jsonArray.put(student1Detail);
             jsonArray.put(student2Detail);

             JSONObject studentsObj = new JSONObject();
             try {
                    studentsObj.put("Students", jsonArray);
             } catch (JSONException e) {
                    e.printStackTrace();
             }

             String jsonStr = studentsObj.toString();
             System.out.println("jsonString: " + jsonStr);

             return jsonStr;
       }


Output :

{"Students":[{"id":"1","name":"abc"},{"id":"2","name":"xyz"}]}

Wednesday, August 6, 2014

ListView with SimpleAdapter

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

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="@android:color/black"
        android:dividerHeight="1dp" />


</LinearLayout>


listrow.xml

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

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:contentDescription="@string/app_name" >
    </ImageView>

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/icon"
        android:textSize="15sp" >
    </TextView>

    <TextView
        android:id="@+id/desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name"
        android:layout_marginTop="4dp"
        android:layout_toRightOf="@+id/icon"
        android:textSize="13sp" >
    </TextView>

</RelativeLayout>


MainActivity.xml

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends Activity {

       private ListView list;

       @SuppressWarnings({ "unchecked", "rawtypes" })
       @Override
       protected void onCreate(Bundle savedInstanceState) {

             super.onCreate(savedInstanceState);
             setContentView(R.layout.main);
             list = (ListView) findViewById(R.id.list);

             final List<Map> data = addSampleData();

             SimpleAdapter adapter = new SimpleAdapter(this,
                           (List<? extends Map<String, ?>>) data, R.layout.listrow,
                           new String[] { "key_icon", "key_name", "key_desc" }, new int[] {
                                        R.id.icon, R.id.name, R.id.desc });

             list.setAdapter(adapter);
             list.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                 int position, long id) {

                           Toast.makeText(getApplicationContext(),
                                        data.get(position).get("key_name").toString(),
                                        Toast.LENGTH_LONG).show();
                    }
             });
       }

       @SuppressWarnings({ "unchecked", "rawtypes" })
       List<Map> addSampleData() {
             List<Map> list = new ArrayList<Map>();

             Map map = new HashMap();
             map.put("key_icon", R.drawable.ic_launcher);
             map.put("key_name", "Student 1");
             map.put("key_desc", "This is Student 1");
             list.add(map);

             map = new HashMap();
             map.put("key_icon", R.drawable.ic_launcher);
             map.put("key_name", "Student 2");
             map.put("key_desc", "This is Student 2");
             list.add(map);

             map = new HashMap();
             map.put("key_icon", R.drawable.ic_launcher);
             map.put("key_name", "Student 3");
             map.put("key_desc", "This is Student 3");
             list.add(map);

             map = new HashMap();
             map.put("key_icon", R.drawable.ic_launcher);
             map.put("key_name", "Student 4");
             map.put("key_desc", "This is Student 4");
             list.add(map);

             map = new HashMap();
             map.put("key_icon", R.drawable.ic_launcher);
             map.put("key_name", "Student 5");
             map.put("key_desc", "This is Student 5");
             list.add(map);

             return list;
       }
}





Friday, August 1, 2014

XML Strings and Special Characters

String
Special Characters
…
&#8230;
< 
&lt;
> 
&gt;
&
&amp;
"
&quot;
'
&#39;

Tuesday, July 29, 2014

Simple ListView with ArrayAdapter

main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>


</LinearLayout>


MainActivity.xml

public class MainActivity extends Activity {
       private ListView listView;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.main);

             listView = (ListView) findViewById(R.id.list);

             // Defined Array values to show in ListView
             String[] values = new String[] { "Item 1", "Item 2", "Item 3",
                           "Item 4", "Item 5", "Item 6", "Item 7", "Item 8", "Item 9",
                           "Item 10" };

             // Define a new Adapter
             // First parameter - Context
             // Second parameter - Layout for the row
             // Third parameter - ID of the TextView to which the data is written
             // Forth - the Array of data

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

             // Assign adapter to ListView
             listView.setAdapter(adapter);

             // ListView Item Click Listener
             listView.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                 int position, long id) {

                           // ListView Clicked item value
                           String item = (String) listView.getItemAtPosition(position);

                           Toast.makeText(getApplicationContext(),
                                        "Position :" + position + "  ListItem : " + item,
                                        Toast.LENGTH_LONG).show();
                    }
             });
       }
}






Tuesday, April 29, 2014

Hide Field in DatePicker

public class Activity1 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

DatePicker datepicker = (DatePicker) findViewById(R.id.datePicker1);

findAndHideField(datepicker, "mDayPicker");
// findAndHideField(datepicker, "mMonthPicker");

// Honeycomb(+) fields:
findAndHideField(datepicker, "mDaySpinner");
// findAndHideField(datepicker, "mMonthSpinner");
}

/** find a member field by given name and hide it */
private void findAndHideField(DatePicker datepicker, String name) {
try {
Field field = DatePicker.class.getDeclaredField(name);
field.setAccessible(true);
View fieldInstance = (View) field.get(datepicker);
fieldInstance.setVisibility(View.GONE);
} catch (Exception e) {
e.printStackTrace();
}
}
}