Android App Development With AndroidCource: August 2017

Tuesday, 29 August 2017

Glide Download Image Example In Android

Add Permission in AndroidMainFest.xml File

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

Add Library Glide in build.gradle File

    compile 'com.github.bumptech.glide:glide:3.7.0'
MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.bhaumik.networkingtask.MainActivity">

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
MainActivity.java

package com.example.bhaumik.networkingtask;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

public class MainActivity extends AppCompatActivity {

    ImageView imageView;

    String url = "https://static.pexels.com/photos/53966/rabbit-palm-hand-snatch-53966.jpeg";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.image_view);

        Glide.with(MainActivity.this).load(url).into(imageView);

    }
}

Weather Data OK http And Json Example In Android

Add Permission in AndroidMainFest.xml File
    <uses-permission android:name="android.permission.INTERNET" />
MainActivity.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.example.bhaumik.simplejsonparshing.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>
MainActivity.java
package com.example.bhaumik.simplejsonparshing;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

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

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {


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

      String url = "http://samples.openweathermap.org/data/2.5/weather?id=2172797&appid=b1b15e88fa797225412429c1c50c122a1";

        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();

        Call call = client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                parseData(response.body().string());
            }
        });
    }

    private void parseData(String string) {

        try {
            JSONObject object = new JSONObject(string);
            JSONObject coord = object.getJSONObject("coord");
            double longitude = coord.getDouble("lon");
            double latitude = coord.getDouble("lat");

            Log.d("LogX","Values :-> " + longitude + "   " + latitude);

            JSONArray jsonArray =object.getJSONArray("weather");

            for(int i=0; i<jsonArray.length(); i++)
            {
                JSONObject objects = jsonArray.getJSONObject(i);
                int id = objects.getInt("id");
                String main = objects.getString("main");
                String desc = objects.getString("description");
                String icon = objects.getString("icon");

                Log.d("LogX","Values :-> " + id + "   " + main + "   " + desc + "   " + icon);

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

RecyclerView Crud(Insert,Update,Delete,View) Example in Android

Add Library into build.gradle Using app folder
    compile 'com.android.support:recyclerview-v7:26+'
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.bhaumik.recyclerviewcrudexample.MainActivity">


    <TextView
        android:id="@+id/tv_name_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:textColor="#000"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:text="Name : " />

    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/tv_name_label"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="20dp"
        android:padding="10dp"
        android:hint="Enter Name"
        android:layout_marginTop="10dp"/>

    <TextView
        android:id="@+id/tv_email_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:textColor="#000"
        android:layout_below="@+id/tv_name_label"
        android:layout_marginTop="40dp"
        android:layout_marginLeft="20dp"
        android:text="Email : " />

    <EditText
        android:id="@+id/et_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/tv_name_label"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="20dp"
        android:padding="10dp"
        android:layout_below="@id/et_name"
        android:hint="Enter Email"
        android:layout_marginTop="20dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_email_label"
        android:id="@+id/layout_btn"
        android:orientation="horizontal"
        android:weightSum="1"
        android:layout_marginTop="20dp">

        <Button
            android:id="@+id/btn_add"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Add"
            android:layout_gravity="center_horizontal"
            android:textAllCaps="false"/>

    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/layout_btn"
        android:scrollbars="vertical"/>
</RelativeLayout>
item_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:orientation="horizontal"
        android:id="@+id/layout_item"
        android:weightSum="2.50">

        <TextView
            android:id="@+id/tv_name_item"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Name"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:layout_marginLeft="20dp"/>

        <TextView
            android:id="@+id/tv_email_item"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Email"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:layout_marginLeft="20dp"/>

        <TextView
            android:id="@+id/tv_delete_item"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.50"
            android:layout_gravity="center"
            android:layout_marginRight="5dp"
            android:drawableEnd="@drawable/ic_delete"
            android:layout_marginLeft="20dp"/>

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000"/>
</LinearLayout>
Create Alert Dialog User Input Using Update User When User to Click Recycler Item And Open Dialog
dialog_update.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/et_update_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:padding="15dp"
        android:hint="Enter Name"/>
    <EditText
        android:id="@+id/et_update_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:padding="15dp"
        android:hint="Enter Email"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">

        <Button
            android:id="@+id/btn_update_user"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:textAllCaps="false"
            android:layout_margin="20dp"
            android:text="Update User"/>

        <Button
            android:id="@+id/btn_update_cancel"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:textAllCaps="false"
            android:text="Cancel"/>

    </LinearLayout>
</LinearLayout>
UserData.java
package com.example.bhaumik.recyclerviewcrudexample;

/**
 * Created by Bhaumik on 8/29/2017.
 */

public class UserData {

    String name,email;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}
MainActivity.java
package com.example.bhaumik.recyclerviewcrudexample;

import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {

    EditText et_name,et_email,et_update_name,et_update_email;
    Button add,btn_update,btn_cancel;
    RecyclerView recyclerView;
    MyAdapter adapter;

    List<UserData> list = new ArrayList<>();

    AlertDialog.Builder builder;

    AlertDialog dialog;

    String name,email;

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

        et_name = (EditText) findViewById(R.id.et_name);
        et_email = (EditText) findViewById(R.id.et_email);

        add = (Button) findViewById(R.id.btn_add);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        adapter = new MyAdapter(list);
        recyclerView.setAdapter(adapter);


        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                name = et_name.getText().toString();
                email = et_email.getText().toString();

                    UserData userData = new UserData();

                    userData.setName(name);
                    userData.setEmail(email);

                   list.add(userData);
                    adapter.notifyDataSetChanged();
                    Toast.makeText(MainActivity.this,"User Add Success...",Toast.LENGTH_SHORT).show();

                    et_name.setText("");
                    et_email.setText("");

            }
        });
        adapter.setOnItemClickListener(new ItemClickListener() {
            @Override
            public void OnItemClick(int position, UserData userData) {

                builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Update User Info");
                builder.setCancelable(false);
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_update,null,false);
                InitUpdateDialog(position,view);
                builder.setView(view);
                dialog = builder.create();
                dialog.show();
            }
        });


    }

    private void InitUpdateDialog(final int position, View view) {

        et_update_name = view.findViewById(R.id.et_update_name);
        et_update_email = view.findViewById(R.id.et_update_email);
        btn_update = view.findViewById(R.id.btn_update_user);
        btn_cancel = view.findViewById(R.id.btn_update_cancel);

        et_update_name.setText(name);
        et_update_email.setText(email);

        btn_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                name = et_update_name.getText().toString();
                email = et_update_email.getText().toString();

                UserData userData = new UserData();

                userData.setName(name);
                userData.setEmail(email);

                adapter.UpdateData(position,userData);
                Toast.makeText(MainActivity.this,"User Updated..",Toast.LENGTH_SHORT).show();

            }
        });
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
    }
}
MyAdapter.java
package com.example.bhaumik.recyclerviewcrudexample;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

/**
 * Created by Bhaumik on 8/29/2017.
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder>{

    List<UserData> list = new ArrayList<>();
    ItemClickListener itemClickListener;

    public MyAdapter(List<UserData> list) {
        this.list = list;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row,parent,false);

        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {

        final UserData userData = list.get(position);

        holder.tv_name.setText(userData.getName());
        holder.tv_email.setText(userData.getEmail());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itemClickListener.OnItemClick(position,userData);
            }
        });

        holder.tv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.remove(position);
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class MyHolder extends RecyclerView.ViewHolder{

        TextView tv_name,tv_email,tv_delete;

        public MyHolder(View itemView) {
            super(itemView);

            tv_name = itemView.findViewById(R.id.tv_name_item);
            tv_email = itemView.findViewById(R.id.tv_email_item);
            tv_delete = itemView.findViewById(R.id.tv_delete_item);
        }
    }

    public void setOnItemClickListener(ItemClickListener itemClickListener){
        this.itemClickListener = itemClickListener;
    }

    public void UpdateData(int position,UserData userData){

        list.remove(position);
        list.add(userData);
        notifyItemChanged(position);
        notifyDataSetChanged();
    }
}
Create an Interface Using Custom Create ItemClickListener
ItemClickListener.java
package com.example.bhaumik.recyclerviewcrudexample;

/**
 * Created by Bhaumik on 8/29/2017.
 */

public interface ItemClickListener {

    void OnItemClick(int position,UserData userData);
}


Output









RecyclerView Staggered Layout Example in Android

Add Library into build.gradle Using app folder

    compile 'com.android.support:recyclerview-v7:26+'
MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.bhaumik.recyclerviewstaggeredlayout.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

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

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.java

package com.example.bhaumik.recyclerviewstaggeredlayout;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

public class MainActivity extends AppCompatActivity {

    MyAdapter adapter;

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

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,
                StaggeredGridLayoutManager.VERTICAL));

        adapter = new MyAdapter();
        recyclerView.setAdapter(adapter);
    }
}
MyAdapter.java

package com.example.bhaumik.recyclerviewstaggeredlayout;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

/**
 * Created by Bhaumik on 8/29/2017.
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder>{

    int img[] = {R.drawable.m1,R.drawable.m2,R.drawable.m3,R.drawable.m4,R.drawable.m5};


    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row,parent,false);

        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {

        holder.imageView.setImageResource(img[position]);

    }

    @Override
    public int getItemCount() {
        return img.length;
    }

    public static class MyHolder extends RecyclerView.ViewHolder{

        ImageView imageView;

        public MyHolder(View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.image_view);
        }
    }
}