Android App Development With AndroidCource: List
Showing posts with label List. Show all posts
Showing posts with label List. Show all posts

Thursday, 7 September 2017

Okhttp With ListView Json Example in Android

Add Library
compile 'com.squareup.okhttp3:okhttp:3.8.1'
compile 'com.google.code.gson:gson:2.6.2'
OkhttpListViewActivity.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"
    tools:context="com.example.bhaumik.networkingtask.OKhttpListViewActivity">


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

</RelativeLayout>
okhttp_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_marginTop="10dp"
    app:cardBackgroundColor="@android:color/holo_blue_light">
        <TextView
            android:id="@+id/text_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:layout_marginTop="20dp"
            android:textColor="@android:color/background_light"
            android:textSize="20dp"
            android:maxLength="5"
            android:text="Hello World"/>

</android.support.v7.widget.CardView>
OkhttpListViewActivity.java
package com.example.bhaumik.networkingtask;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import com.example.bhaumik.networkingtask.adapter.OkhttpListAdapter;
import com.example.bhaumik.networkingtask.pojo.Image;
import com.example.bhaumik.networkingtask.pojo.Network;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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

public class OKhttpListViewActivity extends AppCompatActivity {

    OkhttpListAdapter adapter;
    List<Image> list = new ArrayList<>();
    ListView listView;

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

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

        adapter = new OkhttpListAdapter(list,OKhttpListViewActivity.this);
        listView.setAdapter(adapter);

        new MyTask().execute();
    }

    public class MyTask extends AsyncTask<Void,Void,String>{

        String url = "http://bing.com/HPImageArchive.aspx?format=js&idx=0&n=20&mkt=en-US";

        @Override
        protected String doInBackground(Void... voids) {

            try {

                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url(url).build();
                Response response = client.newCall(request).execute();

                Gson gson = new Gson();

                Network network = gson.fromJson(response.body().string(),Network.class);
                List<Image> list_data = network.getImages();

                for (Image image : list_data){
                        list.add(image);
                }

                return "Size -> " + list.size();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String aVoid) {
            super.onPostExecute(aVoid);
            adapter.notifyDataSetChanged();
        }
    }
}
OkhttpListAdapter.java
package com.example.bhaumik.networkingtask.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.bhaumik.networkingtask.R;
import com.example.bhaumik.networkingtask.pojo.Image;

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

/**
 * Created by Bhaumik on 9/3/2017.
 */

public class OkhttpListAdapter extends BaseAdapter{

    List<Image> list = new ArrayList<>();
    Context context;

    public OkhttpListAdapter(List<Image> list, Context context) {
        this.list = list;
        this.context = context;
    }

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

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

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

    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        ViewHolder viewHolder;

        if (convertView == null){

            View view = LayoutInflater.from(context).inflate(R.layout.okhttp_item_list,parent,false);

            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Image image = list.get(position);

        viewHolder.textView.setText(image.getCopyright());

        return viewHolder.itemView;
    }

    public static class ViewHolder{

        TextView textView;
        View itemView;

        public ViewHolder(View itemView) {
            this.itemView = itemView;
            textView = itemView.findViewById(R.id.text_view);
        }

    }
}

Tuesday, 29 August 2017

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









Monday, 21 August 2017

Spinner Example in Android

SpinnerActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:id="@+id/activity_spinner"
              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"
              android:orientation="vertical"
              tools:context="com.bhaumik.programmingstudy.SpinnerActivity">

    <Spinner
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:layout_gravity="center_horizontal"
            android:id="@+id/spinner" />

</LinearLayout>
Add This Resource String In Value String.xml File
<string-array name="str_language">
    < item>Android< /item>
    < item>Java< /item>
    < item>Asp.Net< /item>
    < item>C++< /item>
    < item>Python< /item>
</string-array>
SpinnerActivity.java
package com.bhaumik.programmingstudy;

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

public class ServicesActivity extends AppCompatActivity {

    Button start,stop;

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

        start = (Button) findViewById(R.id.btn_start);
        stop = (Button) findViewById(R.id.btn_stop);

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ServicesActivity.this,MyServices.class);
                startService(intent);
            }
        });
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ServicesActivity.this,MyServices.class);
                stopService(intent);
            }
        });
    }
}

Ads