DEV'S

ShoppingList - Cloud App - WebService POST - Final

LoginActivity.java

package app.mycompany.dev.myshoppingapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class LoginActivity extends AppCompatActivity {

    private EditText password;
    private EditText userName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("Login");
        userName = (EditText)findViewById(R.id.username);
        password = (EditText)findViewById(R.id.password);

    }

    public void onLoginClicked(View view){

        if (!userName.getText().toString().equals(Shopping.DEF_VALUE) && !password.getText().toString().equals(Shopping.DEF_VALUE)){
            HashMap<String,String> query = new HashMap<String, String>();
            query.put("username", userName.getText().toString());
            query.put("password", password.getText().toString());
            ServerConnect serverConnect = new ServerConnect(LoginActivity.this,query) {
                @Override
                public void getResult(String s) {

                    try {
                        JSONObject jsonObject = new JSONObject(s);
                        String result = jsonObject.getString("result");
                        if (result.equals("OK")){
                            Shopping.setStringValue(LoginActivity.this,Shopping.USER_NAME,userName.getText().toString());
                            Shopping.setStringValue(LoginActivity.this,Shopping.PASSWORD,password.getText().toString());
                            Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                            startActivity(intent);
                            Shopping.displayToast(LoginActivity.this, "Login Successful");
                            finish();

                        }else {
                            Shopping.displayToast(LoginActivity.this, "Login failed, Retry");
                        }

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

                }
            };
            serverConnect.execute("http://devsonline.in/app/login");
        }else{
            Shopping.displayToast(this,"Empty fields not allowed");
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {

        }else if (id == R.id.action_register){
            Intent intent = new Intent(this,RegistrationActivity.class);
            startActivityForResult(intent,Shopping.REG_REQ_CODE);
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == Shopping.REG_REQ_CODE){
            if (resultCode == Shopping.REG_RESULT_CODE){
                Bundle extras = data.getExtras();
                userName.setText(extras.getString(Shopping.USER_NAME));
                password.setText(extras.getString(Shopping.PASSWORD));
            }
        }

    }

}

RegistrationActivity.java

package app.mycompany.dev.myshoppingapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class RegistrationActivity extends AppCompatActivity {

    private EditText firstName;
    private EditText lastName;
    private EditText userName;
    private EditText password;
    private RadioButton isFemale;
    private RadioButton isMale;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registration_activity);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("Register");
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);

        firstName = (EditText)findViewById(R.id.fname);
        lastName = (EditText)findViewById(R.id.lname);
        userName = (EditText)findViewById(R.id.uname);
        password = (EditText)findViewById(R.id.pwd);

        isMale = (RadioButton)findViewById(R.id.ismale);
        isFemale = (RadioButton)findViewById(R.id.isfemale);

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    public void onSave(View view){
        if (!firstName.getText().toString().equals(Shopping.DEF_VALUE)){

            HashMap<String,String> query = new HashMap<String, String>();
            query.put("username", userName.getText().toString());
            query.put("password", password.getText().toString());
            query.put("firstname",firstName.getText().toString());
            query.put("lastname", lastName.getText().toString());
            if (isMale.isChecked()){
                query.put("gender", "1");
            }else if (isFemale.isChecked()){
                query.put("gender", "2");
            }

            ServerConnect serverConnect = new ServerConnect(RegistrationActivity.this,query) {
                @Override
                public void getResult(String s) {
                    try {
                        JSONObject jsonObject = new JSONObject(s);
                        String result = jsonObject.getString("result");
                        if (result.equals("OK")){
                            Intent intent = new Intent();
                            intent.putExtra(Shopping.USER_NAME,userName.getText().toString());
                            intent.putExtra(Shopping.PASSWORD,password.getText().toString());
                            setResult(Shopping.REG_RESULT_CODE, intent);
                            Shopping.displayToast(RegistrationActivity.this, "Registration Successful");
                            finish();

                        }else {
                            Shopping.displayToast(RegistrationActivity.this, "Something gone wrong, Retry");
                        }

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

                }
            };

            serverConnect.execute("http://devsonline.in/app/registration");

        }else{
            Shopping.displayToast(this,"Empty Fields not allowed");
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == android.R.id.home){
            finish();
        }

        return super.onOptionsItemSelected(item);
    }

}

MainActivity.java

package app.mycompany.dev.myshoppingapp;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity{

    private ListView itemListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("Shopping List");

        itemListView = (ListView)findViewById(R.id.item_list);

        itemListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                JSONObject itemObject = (JSONObject) parent.getAdapter().getItem(position);
                Intent intent = new Intent(MainActivity.this,AddEditActivity.class);
                try {
                    intent.putExtra(Shopping.ITEM_ID,itemObject.getString("item_id"));
                    intent.putExtra(Shopping.ITEM_NAME,itemObject.getString("item_name"));
                    intent.putExtra(Shopping.ITEM_QTY,itemObject.getString("item_qty"));
                    intent.putExtra(Shopping.ITEM_PRICE,itemObject.getString("item_price"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                startActivityForResult(intent, Shopping.REG_REQ_CODE);
            }
        });

        itemListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                displayDialog(id);

                return true;
            }
        });

        reloadAdapter();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    private void displayDialog(final long selected){

        AlertDialog.Builder  builder = new AlertDialog.Builder(this);
        builder.setTitle("Alert");
        builder.setMessage("Do you really want to delete this entry?");
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // proceed delete
                reloadAdapter();
                Shopping.displayToast(MainActivity.this, "Successfully deleted");
            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //
            }
        });

        builder.setCancelable(false);

        AlertDialog alertDialog = builder.create();
        alertDialog.show();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == android.R.id.home){
            finish();
        }else if (item.getItemId() == R.id.action_add){
            Intent intent = new Intent(this,AddEditActivity.class);
            startActivityForResult(intent,Shopping.REG_REQ_CODE);
        }
        return super.onOptionsItemSelected(item);
    }

    private void reloadAdapter(){
        HashMap<String,String> query = new HashMap<String, String>();
        query.put("username",Shopping.getStringValue(this,Shopping.USER_NAME));
        query.put("password",Shopping.getStringValue(this,Shopping.PASSWORD));

        ServerConnect serverConnect = new ServerConnect(this,query) {
            @Override
            public void getResult(String result) {
                Log.w("RESULT",result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String resultObj = jsonObject.getString("result");
                    if (resultObj.equals("OK")){
                        ItemsAdapter itemsAdapter = new ItemsAdapter(MainActivity.this,jsonObject.getJSONArray("items"));
                        itemListView.setAdapter(itemsAdapter);
                        //Shopping.displayToast(MainActivity.this, "Item added Successfully");
                    }else {
                        Shopping.displayToast(MainActivity.this, "Something went wrong on Fetch, Retry");
                    }

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

        serverConnect.execute("http://devsonline.in/app/getallitems");

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Shopping.REG_REQ_CODE && resultCode == Shopping.REG_RESULT_CODE){
            reloadAdapter();
        }
    }

}

AddEditActivity.java

package app.mycompany.dev.myshoppingapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class AddEditActivity extends AppCompatActivity {

    private EditText itemPrice;
    private EditText itemQty;
    private EditText itemName;
    private String itemId = "-1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_edit);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        itemName = (EditText)findViewById(R.id.item_name);
        itemQty = (EditText)findViewById(R.id.item_qty);
        itemPrice = (EditText)findViewById(R.id.item_price);
        Button addEditButton = (Button)findViewById(R.id.addedit);
        Button delteButton = (Button)findViewById(R.id.delete);
        Bundle extras = getIntent().getExtras();
        if (extras != null){
            actionBar.setTitle("Edit/Delete Item");
            itemName.setText(extras.getString(Shopping.ITEM_NAME));
            itemQty.setText(extras.getString(Shopping.ITEM_QTY));
            itemPrice.setText(extras.getString(Shopping.ITEM_PRICE));
            itemId = extras.getString(Shopping.ITEM_ID);
            addEditButton.setText("Update");
        }else{
            actionBar.setTitle("Add");
            delteButton.setVisibility(Button.GONE);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    public void addEditClicked(View view){
        HashMap<String,String> query = new HashMap<String, String>();
        query.put("username",Shopping.getStringValue(this,Shopping.USER_NAME));
        query.put("password",Shopping.getStringValue(this,Shopping.PASSWORD));
        query.put("itemname",itemName.getText().toString());
        query.put("itemqty",itemQty.getText().toString());
        query.put("itemprice",itemPrice.getText().toString());
        query.put("itemid",itemId);
        ServerConnect serverConnect = new ServerConnect(this,query) {
            @Override
            public void getResult(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String resultObj = jsonObject.getString("result");
                    if (resultObj.equals("OK")){
                        Intent intent = new Intent();
                        setResult(Shopping.REG_RESULT_CODE,intent);
                        finish();
                    }else {
                        Shopping.displayToast(AddEditActivity.this, "Something went wrong, Retry");
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        };
        if (itemId.equals("-1")) {
            serverConnect.execute("http://devsonline.in/app/additem");
        }else {
            serverConnect.execute("http://devsonline.in/app/updateitem");
        }
    }

    public void deleteClicked(View view){
        HashMap<String,String> query = new HashMap<String, String>();
        query.put("username", Shopping.getStringValue(this,Shopping.USER_NAME));
        query.put("password", Shopping.getStringValue(this,Shopping.PASSWORD));
        query.put("itemid",itemId);
        ServerConnect serverConnect = new ServerConnect(this,query) {
            @Override
            public void getResult(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String resultObj = jsonObject.getString("result");
                    if (resultObj.equals("OK")){
                        Intent intent = new Intent();
                        setResult(Shopping.REG_RESULT_CODE,intent);
                        finish();
                    }else {
                        Shopping.displayToast(AddEditActivity.this, "Something went wrong, Retry");
                    }

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

        serverConnect.execute("http://devsonline.in/app/deleteitem");
    }
}

ItemsAdapter.java

package app.mycompany.dev.myshoppingapp;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by megha on 18/11/15.
 */
public class ItemsAdapter extends BaseAdapter {

    private JSONArray jsonArray;
    private LayoutInflater layoutInflater;

    public ItemsAdapter(Context context, JSONArray jsonArray){
        layoutInflater = LayoutInflater.from(context);
        this.jsonArray = jsonArray;
    }

    @Override
    public int getCount() {
        return jsonArray.length();
    }

    @Override
    public Object getItem(int position) {
        try {
            return jsonArray.getJSONObject(position);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return new JSONObject();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if (convertView == null){
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_box,parent,false);
            viewHolder.itemName = (TextView)convertView.findViewById(R.id.item_name_list);
            viewHolder.itemQty = (TextView)convertView.findViewById(R.id.item_qty_list);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder)convertView.getTag();
        }

        try {
            viewHolder.itemName.setText(jsonArray.getJSONObject(position).getString("item_name"));
            viewHolder.itemQty.setText(jsonArray.getJSONObject(position).getString("item_qty"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return convertView;
    }

    private class ViewHolder{
        private TextView itemName;
        private TextView itemQty;
    }
}

Shopping.java

package app.mycompany.dev.myshoppingapp;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.widget.Toast;

/**
 * Created by megha on 10/11/15.
 */
public class Shopping {

    public static final String DEF_VALUE = "";
    public static final int DEF_VALUE_INT = -1;
    public static final String FIRST_NAME = "_firstname";
    public static final String LAST_NAME = "_lastname";
    public static final String USER_NAME = "_user_name";
    public static final String PASSWORD = "_password";
    public static final String GENDER = "_gender";
    public static final int MALE = 2;
    public static final int FEMALE = 1;

    public static final int REG_RESULT_CODE = 222;
    public static final int REG_REQ_CODE = 232;

    public static final String ITEM_ID = "_item_id";
    public static final String ITEM_QTY = "_item_qty";
    public static final String ITEM_NAME = "_item_name";
    public static final String ITEM_PRICE = "_item_price";

    public static void setStringValue(Context context, String key, String value){
        SharedPreferences.Editor ed = PreferenceManager.getDefaultSharedPreferences(context).edit();
        ed.putString(key,value);
        ed.commit();
    }

    public static String getStringValue(Context context, String key){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return sp.getString(key,DEF_VALUE);
    }

    public static void setIntValue(Context context, String key, int value){
        SharedPreferences.Editor ed = PreferenceManager.getDefaultSharedPreferences(context).edit();
        ed.putInt(key,value);
        ed.commit();
    }

    public static int getIntValue(Context context, String key){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return sp.getInt(key, DEF_VALUE_INT);
    }

    public static void displayToast(Context context, String msg){
        Toast.makeText(context,msg,Toast.LENGTH_LONG).show();
    }
    

}

ServerConnect.java

package app.mycompany.dev.myshoppingapp;

import android.app.ProgressDialog;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.HashMap;

/**
 * Created by megha on 18/11/15.
 */
public abstract class ServerConnect {
    private GetFromServer getFromServer;
    public ServerConnect(Context context,HashMap<String,String> payloads){
        getFromServer = new GetFromServer(context,getUrlEncoder(payloads));
    }

    public void execute(String url){
        getFromServer.execute(url);
    }

    private String getUrlEncoder(HashMap<String,String> payloads){
        Uri.Builder builder = new Uri.Builder();

        for (HashMap.Entry<String,String> temp: payloads.entrySet()){
            Log.w("HM", temp.getKey() + "," + temp.getValue());
            builder.appendQueryParameter(temp.getKey(),temp.getValue());
        }

        return builder.build().getEncodedQuery();

    }

    public abstract void getResult(String result);

    private class GetFromServer extends AsyncTask<String,Void,String>{

        private ProgressDialog progressDialog;
        private String urlEncodedPayLoads;
        public GetFromServer(Context context,String urlEncodedPayLoads){
            progressDialog = ProgressDialog.show(context,"Wait","Connecting Server");
            this.urlEncodedPayLoads = urlEncodedPayLoads;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String… params) {
            StringBuilder stringBuilder = new StringBuilder();
            progressDialog.setMessage("Connected, Please wait");
            try {
                URL url = new URL(params[0]);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setConnectTimeout(10000);
                httpURLConnection.setReadTimeout(15000);
                httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                httpURLConnection.setDoInput(true);
                httpURLConnection.setDoOutput(true);

                OutputStream outputStream = httpURLConnection.getOutputStream();

                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTf-8"));
                bufferedWriter.write(urlEncodedPayLoads);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();

                httpURLConnection.connect();

                InputStream inputStream = httpURLConnection.getInputStream();

                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));

                String line = "";

                while ((line = bufferedReader.readLine()) != null){
                    stringBuilder.append(line).append("\n");
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return stringBuilder.toString();

        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            progressDialog.dismiss();
            getResult(s);
        }
    }

}

login_activity.xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="10dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Username" />

        <EditText
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter Username"
            android:imeOptions="actionNext"
            android:inputType="text" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Password" />

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter Password"
            android:imeOptions="actionDone"
            android:inputType="numberPassword" />

        <Button
            android:id="@+id/login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onLoginClicked"
            android:text="Login" />

    </LinearLayout>

</ScrollView>

registration_activity.xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="10dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/firstname" />

        <EditText
            android:id="@+id/fname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/enter_firstname"
            android:imeOptions="actionNext"
            android:inputType="textPersonName" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/lastname" />

        <EditText
            android:id="@+id/lname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/enter_lastname"
            android:imeOptions="actionNext"
            android:inputType="textPersonName" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/username" />

        <EditText
            android:id="@+id/uname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/enter_username"
            android:imeOptions="actionNext"
            android:inputType="text" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/password" />

        <EditText
            android:id="@+id/pwd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/enter_pass"
            android:imeOptions="actionDone"
            android:inputType="numberDecimal" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/gender" />

        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <RadioButton
                android:id="@+id/ismale"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/male" />

            <RadioButton
                android:id="@+id/isfemale"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/female" />

        </RadioGroup>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/interest_area" />

        <CheckBox
            android:id="@+id/istravel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/travel" />

        <CheckBox
            android:id="@+id/isbusiness"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/business" />

        <CheckBox
            android:id="@+id/iseducation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/education" />

        <Button
            android:id="@+id/save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onSave"
            android:text="@string/save" />

    </LinearLayout>

</ScrollView>

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

activity_add_edit.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:fillViewport="true"
    tools:context="app.mycompany.dev.myshoppingapp.AddEditActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="10dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/item_name" />

        <EditText
            android:id="@+id/item_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/enter_item_name"
            android:imeOptions="actionNext"
            android:inputType="text" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/item_qty" />

        <EditText
            android:id="@+id/item_qty"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/enter_item_qty"
            android:imeOptions="actionNext"
            android:inputType="numberDecimal" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/item_price" />

        <EditText
            android:id="@+id/item_price"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/enter_item_price"
            android:imeOptions="actionDone"
            android:inputType="numberDecimal" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal">

            <Button
                android:id="@+id/addedit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="addEditClicked"
                android:text="@string/add" />

            <Button
                android:id="@+id/delete"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="deleteClicked"
                android:text="@string/delete" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

item_box.xml

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

    <TextView
        android:id="@+id/item_name_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Item name here"
        android:textSize="24sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/item_qty_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="Qty"
        android:textSize="18sp"
        android:textStyle="italic" />

</RelativeLayout>

menu/menu_login.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".LoginActivity">

    <item
        android:id="@+id/action_register"
        android:orderInCategory="100"
        android:title="Register"
        app:showAsAction="always" />
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="101"
        android:title="@string/action_settings"
        app:showAsAction="never" />
</menu>

menu/menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".LoginActivity">

    <item
        android:id="@+id/action_add"
        android:orderInCategory="100"
        android:title="@string/add"
        app:showAsAction="always" />
</menu>

values/strings.xml

<resources>
    <string name="app_name">MyShoppingApp</string>
    <string name="action_settings">Settings</string>
    <string name="title_activity_registration">RegistrationActivity</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="enter_item_name">Enter Item Name</string>
    <string name="item_name">Item Name</string>
    <string name="enter_item_qty">Enter Item Qty</string>
    <string name="item_qty">Item Qty</string>
    <string name="item_price">Item Price</string>
    <string name="enter_item_price">Enter Item Price</string>
    <string name="add">Add</string>
    <string name="delete">Delete</string>
    <string name="save">Save</string>
    <string name="education">Education</string>
    <string name="business">Business</string>
    <string name="travel">Travel</string>
    <string name="interest_area">Interest Area</string>
    <string name="female">Female</string>
    <string name="male">Male</string>
    <string name="gender">Gender</string>
    <string name="enter_pass">Enter a Password</string>
    <string name="password">Password</string>
    <string name="enter_username">Enter Username</string>
    <string name="username">Username</string>
    <string name="enter_lastname">Enter Lastname</string>
    <string name="lastname">Last name</string>
    <string name="enter_firstname">Enter Firstname</string>
    <string name="firstname">First name</string>
</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="app.mycompany.dev.myshoppingapp">

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".LoginActivity"
            android:label="@string/app_name"
            android:windowSoftInputMode="stateHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".RegistrationActivity"
            android:label="@string/title_activity_registration"
            android:theme="@style/AppTheme"
            android:windowSoftInputMode="stateHidden"></activity>
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main"
            android:theme="@style/AppTheme"
            ></activity>
        <activity
            android:name=".AddEditActivity"
            android:theme="@style/AppTheme"
            android:windowSoftInputMode="stateHidden"></activity>
    </application>

</manifest>

Android

2018-11-27 21:26:06
1) Android Videos
2018-07-09 06:40:27
2) 09 Jul - Classroom Videos
2018-07-06 21:15:16
3) Recommended Videos
2018-07-06 21:00:45
4) Classroom Videos
2018-06-21 23:15:31
5) ViewPager TabStrip - TabLayout
2018-06-21 22:19:12
6) AsyncTask Filedownload
2018-06-21 19:54:27
7) Service Connection - Bind Service
2018-06-21 03:08:05
8) Instrumentation Test
2018-06-20 22:10:24
9) ApiDoc - Shopping List App
2018-06-20 22:08:56
10) AsyncTask - PreferenceManager - Example
2018-06-20 20:02:07
11) Fragments - RecyclerView Examples UnitTesting- Part 4
2018-06-20 19:59:57
12) Fragments - RecyclerView Examples -Database - Part 3
2018-06-20 19:59:00
13) Fragments - RecyclerView Examples - Part 2
2018-06-20 19:52:33
14) Fragments - RecyclerView Examples - Part 1
2018-06-19 23:52:14
15) Customize Adapter Example
2018-06-19 22:58:07
16) SimpleAdapter - HashMap Data Type
2018-06-19 03:39:01
17) ListView Add-Edit Example
2018-06-19 02:25:02
18) OptionMenu - Background - Shape
2018-06-18 23:53:27
19) SetActivityForResult Example
2018-06-18 21:33:01
20) Android Button Event
2018-06-18 03:19:58
21) Android Calculator View - Activity LifeCycle
2018-06-15 02:59:14
22) ViewPager - TabLayout Example
2018-06-15 00:14:08
23) Broadcast Receiver SMSManager
2018-06-14 22:58:22
24) Android Location Aware Apps - OnScreen Permission
2018-06-14 05:13:06
25) Sqlite Example - SimpleCursor Adapter
2018-06-14 03:03:36
26) Fragment - NavigationDrawer Example
2018-06-13 22:51:12
27) WebService POST
2018-06-13 05:07:48
28) WebService GET Example
2018-06-13 03:26:20
29) Service - Broadcast Receiver - SimpleAdapter
2018-06-12 23:00:54
30) ListView Add Edit Delete
2018-06-12 04:40:31
31) Background - SharedPreferences
2018-06-12 03:15:15
32) ActionBar - Option Menu
2018-06-12 01:05:30
33) RelativeLayout - ScrollView - Intent -Bundle
2018-06-11 03:38:21
34) Calculator App - Part 2
2018-06-11 00:19:59
35) Calculator Layout - Part 1
2018-05-15 03:07:50
36) Kotlin - Functional Programming Best Practices
2018-05-15 01:10:02
37) Shopping App - Kotlin - Companion Object
2018-05-14 23:51:41
38) Shopping App - Kotlin - Java SAM Interface To Lambda
2018-05-14 20:58:14
39) ShoppingList App - Api Doc
2018-05-14 01:03:11
40) Kotlin - Activities - UI Building - WebService - AsyncTask
2018-05-13 23:53:26
41) Kotlin Classes - Inheritance
2018-01-24 01:18:09
42) IOS - Performance Tuning - Best Practices
2018-01-24 00:07:33
43) Google API Pricing Plan
2018-01-24 00:03:21
44) Firebase Integration - App Analytics
2018-01-23 03:39:46
45) Profiling - Best Practices - Service - Broadcast Receiver - Document -PDF
2018-01-23 03:24:36
46) Broadcast Receiver - Profiling - Unregister - Reduce Memory Leak
2018-01-23 00:20:06
47) Background Service - Use Of LocationListener - Run Time Permission Check - Best Practice
2018-01-22 22:39:07
48) Android Memory Profiling - Performance Improvement - Handler - Runnable
2018-01-22 21:24:46
49) Android Performance Improvement Techniques - PDF Document
2018-01-22 04:50:44
50) Best Practice To Add Smooth View Pan And Communicate Between Activities
2018-01-22 02:50:35
51) Layout Technique - Activity LifeCycle
2017-12-11 05:23:14
52) ShoppingList App - Fragment - Part 5
2017-12-11 05:08:05
53) Feedback
2017-12-11 03:25:52
54) ShoppingList App - RecyclerView - Part 4
2017-12-11 00:27:38
55) ShoppingList App - DrawerLayout - Part 3 - Drawables - Menus - Gradle Configs
2017-12-11 00:23:30
56) ShoppingList App - DrawerLayout - Part 2 - Layouts
2017-12-11 00:20:50
57) ShoppingList App - DrawerLayout - Part 1
2017-12-10 21:17:56
58) ShoppingList App - Api Calls
2017-12-08 03:32:57
59) App - AsyncTast Part 2
2017-12-08 01:32:57
60) App - AsyncTast Part 1
2017-12-07 23:06:12
61) WebService GET - JSON Parsing
2017-12-07 04:51:25
62) Service And Broadcast Receiver
2017-12-06 22:49:06
63) Sqlite DB Example - ListView - Complex Form
2017-12-06 00:23:16
64) ListView - Adapters
2017-12-05 22:44:14
65) ActionBar Option Menu
2017-12-05 03:22:37
66) Multiple Activities
2015-11-19 02:45:01
67) Generate APK
2015-11-18 09:47:41
68) ShoppingList - Cloud App - WebService POST - Final
2015-11-17 13:29:54
69) Creating Swipe Views With Tabs Using ViewPager And TabLayout
2015-11-17 08:00:24
70) TabHost Using Fragment
2015-11-17 03:41:10
71) ShoppingList - Cloud App - WebService POST Example
2015-11-16 03:02:07
72) Camera - Gallery - Intent Calling - Customised Adapter
2015-11-13 08:36:20
73) Android Broadcast Receiver - SMS And Call
2015-11-12 13:17:25
74) WebService Example - HttpURLConnection Example
2015-11-12 13:11:57
75) Android Location Services - Foreground And Background Example
2015-11-11 23:04:58
76) ShoppingList App - Sqlite
2015-11-09 23:23:00
77) ShoppingList App - Continued
2015-11-09 04:09:50
78) ListView - ShoppingList App
2015-11-08 22:23:23
79) Android Layouts - Revisit
2015-03-25 04:05:46
80) Overriding Theme - ActionBar And Tab
2015-03-14 02:45:58
81) SMSReplier - V1.0 - Service - Intent Share - Phone - WhiteList - ReplyLog Addon - Part4
2015-03-14 02:44:07
82) SMSReplier - V1.0 - Service - Intent Share - Phone - WhiteList - ReplyLog Addon - Part3
2015-03-14 02:34:48
83) SMSReplier - V1.0 - Service - Intent Share - Phone - WhiteList - ReplyLog Addon - Part2
2015-03-14 02:30:01
84) SMSReplier - V1.0 - Service - Intent Share - Phone - WhiteList - ReplyLog Addon - Part1
2015-03-12 02:04:35
85) SMSReplier - V1.0 - Broadcast Receiver - Foreground Notification - Persistency Add-on - Part 2
2015-03-12 01:59:08
86) SMSReplier - V1.0 - Broadcast Receiver - Foreground Notification - Persistency Add-on - Part 1
2015-03-11 01:24:43
87) WebService -HTTP GET - AsyncTask - XML Parsing
2015-03-10 01:22:20
88) GoogleMap V2 - LocationListener - GPS
2015-03-09 01:51:33
89) SMS And Call Broadcast Receiver
2015-03-06 01:25:52
90) WebService - HTTP POST - AsyncTask - JSON Parsing
2015-03-05 01:18:38
91) WebService Introduction - GET - AsyncTask - JSON Parsing
2015-03-04 00:39:34
92) AsyncTask Example - Loading Image From Server
2015-03-03 00:15:21
93) SqliteDB Integration - SMSReplier V1.0 - Continued
2015-03-02 01:39:20
94) SMSList - Compose Fragment - FragmentBackStack - SMSReplier V1.0 - Continued...
2015-02-28 00:45:24
95) Post Login - DrawerLayout - Menu System - SMSReplier V1.0 Continued
2015-02-27 01:26:30
96) ViewPager - Tab - PreferenceManager Add On - Moving Everything To Fragment -SMSReplier V1.0 Continued
2015-02-25 02:56:09
97) Camera - Bitmap Add On - SMSReplier V1.0 - Continued
2015-02-25 01:29:05
98) SMSReplier - V1.0 - Registration Form
2015-02-24 03:56:38
99) Introduction To Service And BroadcastReceiver - IntentFilter
2015-02-23 00:57:08
100) Communicating Fragments - Bundle - Interface
2015-02-20 02:01:03
101) Navigation Drawer - Fragment Indroduction
2015-02-18 00:40:37
102) ListView - CustomAdapter With ViewHolder
2015-02-18 00:37:09
103) ListView - SimpleCursorAdapter Example
2015-02-16 04:13:55
104) ListView With SqliteDB - Add -Edit - Delete - Retrive
2015-02-13 23:07:36
105) OptionsMenu And ContextMenu With ListView
2015-02-13 01:40:46
106) ArrayAdapter And SimpleAdapter - ListView Example
2015-02-11 04:22:29
107) Forward And Reverse Bundling Objects Between Activities
2015-02-11 00:16:14
108) Bundle - Parceling Objects Between Activities
2015-02-10 00:34:05
109) ScrollView - PreferenceManager
2015-02-09 03:49:48
110) MultiActivity - AndroidManifest - Intent Introduction
2015-02-09 00:33:52
111) ClickListeners - Object Initialisation - Toast
2015-02-08 23:35:56
112) Bitmap From SD Card
2015-02-07 03:26:37
113) Android Layout Tricks - RelativeLayout
2015-02-06 23:04:29
114) Android Layout Tricks - LinearLayout
2015-02-05 20:47:58
115) UI Stack And Activity LifeCycle
2015-02-04 21:04:55
116) Working With Android Studio
2015-02-04 20:52:49
117) Java - OOP Concept - A Revisit
2014-12-08 22:11:38
118) Maintaining Login State - A Simple Approach
2014-12-07 21:03:53
119) 9 Patch Image Background
2014-12-05 04:25:48
120) WebService - JSON Example From 10.0.2.2 - Localhost
2014-12-03 03:30:10
121) VideoView - Media Controller
2014-12-03 02:52:15
122) Google Map V2 - Location Listener - Display Current Location - Check GPS Status
2014-12-02 23:12:16
123) Google Map V2 Integration
2014-12-02 04:22:51
124) Implicit Intent MIME Type - Camera - Gallery
2014-12-01 23:41:14
125) Camera - Implicit Intent - External Storage Example
2014-12-01 03:01:45
126) WebService - Google Map API WebService Example
2014-11-27 04:22:52
127) Google API Key, KeyStore, Certificate SHA1 Fingerprints
2014-11-27 03:59:46
128) Navigation Drawer - ActionBarDrawerToggle Example
2014-11-26 03:39:20
129) Notification From Service
2014-11-25 03:33:52
130) WebView Example
2014-11-24 00:37:52
131) Saving Item State - Fragment Master Detail Flow Final
2014-11-23 11:43:21
132) Package - Apk Suppress Lint Error
2014-11-21 22:49:09
133) AsyncTask Introduction - Load Image from URL
2014-11-21 05:15:40
134) GridView Example
2014-11-21 03:44:34
135) Location Aware Apps - GPS_PROVIDER - NETWORK_PROVIDER - Introduction
2014-11-20 03:20:22
136) SMSReplier Ver1.3 With Broadcast Receiver
2014-11-19 03:25:18
137) SMSReplier Ver1.2 Using Fragments And SQLite
2014-11-18 05:10:34
138) SMSReplier Ver1.1 Using Fragments - A Modular Approach
2014-11-17 05:21:15
139) ActionBar Tab - Fragment - ViewPager - Lisview - Compose Message
2014-11-15 04:43:26
140) APK - Android Application Package Process
2014-11-15 03:43:55
141) View Fragment And Pager - An Image Gallery Example
2014-11-13 04:07:42
142) Button,Checkbox With Image Background
2014-11-12 04:49:54
143) SMSReplier Ver1.0
2014-11-12 03:10:49
144) BroadcastReceiver With Permission
2014-11-11 03:06:20
145) BroadcastReceiver - Service - Example
2014-11-10 05:21:10
146) Background Service Example
2014-11-10 03:47:35
147) Runtime UI View Generation
2014-11-10 03:30:14
148) Shopping List App Version 3.1 - Shopping Object - Shopping Adapter - Custom Data View Binding
2014-11-10 00:01:53
149) Spinner Example - Dropdown - ArrayAdapter
2014-11-08 01:20:13
150) Shopping List App Version 3.0 - SQlite - CRUD
2014-11-07 03:16:33
151) Shopping List App Version 2.9 - SQlite - SimpleCursorAdapter
2014-11-06 03:08:08
152) Shopping List App Version 2.2 - JSONAdapter (custom) Addon
2014-11-05 04:12:28
153) Customising Adapter From BaseAdapter
2014-11-04 04:46:19
154) Shopping List App Version 2.1 - Alert Dialog Addon
2014-11-04 01:25:04
155) Shopping List App Version 2.1 - View-Edit-Delete Addon
2014-11-03 03:16:51
156) Json Revisit
2014-11-02 23:07:09
157) PreferenceManager Revisit
2014-10-31 03:19:16
158) Shopping List App Version 2.0 - PreferenceManager Add On - Save List
2014-10-30 03:42:19
159) Shopping List App Version 1.0 - Menu-onActivityResult-ListActivity
2014-10-30 02:18:46
160) SharedPreference Example
2014-10-29 22:17:27
161) Option Menu And Context Menu Example
2014-10-29 02:57:14
162) MultiActivity With OnActivityResult Example
2014-10-28 03:09:56
163) Bundle Example
2014-10-27 03:06:53
164) ListView - SimpleAdapter Example
2014-10-27 01:35:15
165) CoreJava Questions & Answers
2014-10-26 22:37:58
166) ListView - ArrayAdapter Example - Continued
2014-10-25 03:32:33
167) ListView - ArrayAdapter Example
2014-10-25 00:18:15
168) Calculator App
2014-10-23 03:14:37
169) Calculator Layout
2014-10-23 00:01:20
170) Event-listeners Example
2014-10-21 03:45:26
171) Android MultiActivity Example
2014-10-20 23:05:09
172) Activity Life Cycle Example
2014-10-20 23:03:17
173) Inheritance Scenario Wrt To Activity
2014-10-20 05:17:13
174) Android HelloWorld From Base
2014-10-20 01:49:59
175) Java Array And ArrayList
2014-10-19 23:06:25
176) Java Basics 1
2014-10-18 05:03:46
177) Hello World Java

© Vasudev.H / Email: tech3in@live.com