Pages

JSON Example

  • JSON :- JavaScript Object Notation
  • It contains key/value pairs
  • Key-> Strings
  • Values:->JSON
  • {}   :-> JSON Object
  • []    :-> JSON array
Syntax :
{"employees":[
    {"firstName":"John""lastName":"Doe"},
    {"firstName":"Anna""lastName":"Smith"},
    {"firstName":"Peter""lastName":"Jones"}
]}

package csdevbin.json;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

   
public static final String JSON_STRING = "{\"employee\":{\"name\":\"John\",\"salary\":6000}}";

   
@Override
   
public void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);

        TextView textView1 = (TextView) findViewById(R.id.
textView1);

       
try {
            JSONObject emp = (
new JSONObject(JSON_STRING)).getJSONObject("employee");
            String empname = emp.getString(
"name");
           
int empsalary = emp.getInt("salary");

            String str =
"Employee Name:" + empname + "\n" + "Employee Salary:" + empsalary;
            textView1.setText(str);

        }
catch (Exception e) {
            e.printStackTrace();
        }

    }
}

<?xml version="1.0" encoding="utf-8"?>

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="csdevbin.json.MainActivity">

    <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="75dp"
        android:layout_marginTop="46dp"
        android:text="TextView" />

</RelativeLayout>




Grid View

GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid. The grid items are automatically inserted to the layout using a ListAdapter.

Project Structure:

1.MainActivity.java
2.ImageAdapter.java
3.main_activity.xml
4.Save the image files into the project's res/drawable/ directory.  (Download)

main_activity.xml


<?xml version="1.0" encoding="utf-8"?>

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.blogspot.csdevbin.listexample.MainActivity">

    <GridView

        android:id="@+id/gridview"
        android:layout_width="match_parent
        android:layout_height="match_parent"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center">

    </GridView>

</RelativeLayout>


MainActivity.java
package com.blogspot.csdevbin.listexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

        GridView gridview = (GridView) findViewById(R.id.
gridview);
        gridview.setAdapter(
new ImageAdapter(this));

        gridview.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
           
public void onItemClick(AdapterView<?> parent, View v,
                                   
int position, long id) {
                Toast.makeText(MainActivity.
this, "" + position,
                        Toast.
LENGTH_SHORT).show();
            }
        });
    }

}


2.ImageAdapter.java

package com.blogspot.csdevbin.listexample;

  import android.content.Context;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.GridView;
  import android.widget.ImageView;
  public class ImageAdapter extends BaseAdapter {

    private Context mContext;
    public ImageAdapter(Context c) {

        mContext = c;
    }
    public int getCount() {
        return mThumbIds.length;
    }
    public Object getItem(int position) {
        return null;
    }
    public long getItemId(int position) {
        return 0;
    }
    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {

            // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(10, 10, 10, 10);

        } else {

            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }
    // references to our images

    private Integer[] mThumbIds = {

            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7

    };

}




SharedPreferences 2

MainActivity.java
package com.blogspot.csdevbin.sharedpreferences;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    EditText
food,shopping,fuel,telephone;
    TextView
previousExpense;
    SharedPreferences
sharedPreferences;


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

        
food = (EditText)findViewById(R.id.etfood);
        
shopping = (EditText)findViewById(R.id.edShopping);
        
fuel = (EditText)findViewById(R.id.edFuel);
        
telephone = (EditText)findViewById(R.id.etTelephone);
        
previousExpense = (TextView) findViewById(R.id.textView_head);

        Button calculate = (Button)findViewById(R.id.
button_calc);
       
sharedPreferences =getSharedPreferences("EXPENCES", Context.MODE_PRIVATE);
       
previousExpense.setText(String.valueOf(sharedPreferences.getInt("EXP",0)));

        calculate.setOnClickListener(
new View.OnClickListener() {
           
@Override
           
public void onClick(View view) {
               
int expense =Integer.valueOf(food.getText().toString()) +
                        Integer.valueOf(
shopping.getText().toString()) +
                        Integer.valueOf(
fuel.getText().toString()) +
                        Integer.valueOf(
telephone.getText().toString()) ;

                Toast.makeText(MainActivity.
this, "Total Expense is : " +String.valueOf(expense), Toast.LENGTH_SHORT).show();
                
previousExpense.setText("Total Previous  Expense was : " +String.valueOf(expense));

                SharedPreferences.Editor edit =
sharedPreferences.edit();
                edit.putInt(
"EXP",expense);
                edit.apply();

            }
        });
    }
}



activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.blogspot.csdevbin.sharedpreferences.MainActivity">

    <TextView

        android:text="Previous Expense"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:id="@+id/textView_head"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

    <TextView

        android:text="Food"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView_head"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="21dp"
        android:layout_marginStart="21dp"
        android:layout_marginTop="74dp"
        android:id="@+id/textView_food" />

    <EditText

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/etfood"
        android:layout_alignBottom="@+id/textView_food"
        android:layout_alignLeft="@+id/textView_head"
        android:layout_alignStart="@+id/textView_head" />

    <TextView

        android:text="Shopping"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView_food"
        android:layout_alignLeft="@+id/textView_food"
        android:layout_alignStart="@+id/textView_food"
        android:layout_marginTop="51dp"
        android:id="@+id/textView_shopping" />

    <EditText

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:layout_alignBottom="@+id/textView_shopping"
        android:layout_alignLeft="@+id/etfood"
        android:layout_alignStart="@+id/etfood"
        android:id="@+id/edShopping" />

    <TextView

        android:text="Fuel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView_shopping"
        android:layout_alignLeft="@+id/textView_shopping"
        android:layout_alignStart="@+id/textView_shopping"
        android:layout_marginTop="68dp"
        android:id="@+id/textView_fuel" />

    <TextView

        android:text="Telephone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView_tele"
        android:layout_below="@+id/textView_fuel"
        android:layout_alignLeft="@+id/textView_fuel"
        android:layout_alignStart="@+id/textView_fuel"
        android:layout_marginTop="56dp" />

    <EditText

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/etTelephone"
        android:layout_alignBottom="@+id/textView_tele"
        android:layout_alignLeft="@+id/edFuel"
        android:layout_alignStart="@+id/edFuel" />

    <Button

        android:text="Calculate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="28dp"
        android:id="@+id/button_calc"
        android:layout_alignRight="@+id/etTelephone"
        android:layout_alignEnd="@+id/etTelephone"
        android:layout_alignLeft="@+id/textView_tele"
        android:layout_alignStart="@+id/textView_tele" />

    <EditText

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/edFuel"
        android:layout_alignBottom="@+id/textView_fuel"
        android:layout_alignLeft="@+id/edShopping"
        android:layout_alignStart="@+id/edShopping" />

</RelativeLayout>


Button Example 2

MainActivity.java

package com.blogspot.csdevbin.mynewpractice;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        B1();
    }
   
private void B1() {
        Button b1 = (Button) findViewById(R.id.
button1);

        b1.setOnClickListener(
new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {

                Toast.makeText(getApplication(),
"Button 1 clicked",Toast.LENGTH_LONG).show();

            }
        });
    }
}


 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:paddingBottom="@dimen/activity_vertical_margin"
   
android:paddingLeft="@dimen/activity_horizontal_margin"
   
android:paddingRight="@dimen/activity_horizontal_margin"
   
android:paddingTop="@dimen/activity_vertical_margin"
   
tools:context="com.blogspot.csdevbin.mynewpractice.MainActivity"
   
   
<Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="B1"
       
android:clickable="true"
       
android:id="@+id/button1"
       
android:layout_below="@+id/textView"
       
android:layout_marginTop="54dp"
       
android:layout_alignParentRight="true"
       
android:layout_alignParentEnd="true"
       
android:layout_alignParentLeft="true"
       
android:layout_alignParentStart="true" />
</
RelativeLayout>