Thursday, 13 February 2020

How to set Radio Button in recycler view

Simple RecyclerView in Android for show simple listing into Android  App. If you are first time create simple listing screen than this example is best for you & using radio button for select one item from all item from recycler view or list for send one activity to another activity or send data to server.

1. Step => Create simple Activity for example of simple RecyclerView or listing data. Create model class below.

package com.akhari.myapplication;

public class MovieModel {
    private String title, genre, year;
    private Boolean isSelect;
    public MovieModel(String title, String genre, String year,
                Boolean isSelect) {
        this.title = title;
        this.genre = genre;
        this.year = year;
        this.isSelect=isSelect; 
    }
 
    public Boolean getIsSelect() {
        return isSelect;
    }

    public void setIsSelect(Boolean isSelect) {
        this.isSelect= isSelect;
    }

    public String getTitle() {
        return title;
    }

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

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getGenre() {
        return genre;
    }

    public void setGenre(String genre) {
        this.genre = genre;
    }
}
 
2. Step==> Create Simple adapter for set adapter of recycler View. 
With arrayList of MovieList Model class
 
 
package com.akhari.myapplication;

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

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class MoviesListAdapter  extends  
RecyclerView.Adapter<MoviesListAdapter.MyViewHolder> {
    private ArrayList<MovieModel> modelArrayList;
    private Context mContext;

    public MoviesListAdapter(ArrayList<MovieModel> modelArrayList, 
Context mContext) {
        this.modelArrayList = modelArrayList;
        this.mContext = mContext;
    }

    @Override     
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_row, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override 
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        MovieModel movie = modelArrayList.get(position);
        holder.title.setText(movie.getTitle());
        holder.genre.setText(movie.getGenre());
        holder.year.setText(movie.getYear());
        holder.checkItem.setChecked(movie.getIsSelect());
 
        holder.radioItem.setOnCheckedChangeListener(new 
     CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton 
        buttonView, boolean isChecked) {
            for(int i=0;i<modelArrayList.size();i++){
                modelArrayList.get(i).setIsSelect(false); 
              movie.setIsSelect(isChecked);
              notifyDataSetChanged(); 
           }
       }); 
 
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView title, year, genre;
        public RadioButton radioItem;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            genre = itemView.findViewById(R.id.genre);
            year = itemView.findViewById(R.id.year);
            checkItem=itemView.findViewById(R.id.check_item);
       }
    }
}
 
 
3. ==> Create Simple XML layout file for recycler view row layout. 
      Name of layout file list_row.xml
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="top"
    android:gravity="top"
    android:padding="25dp"
    android:orientation="horizontal">

    <RadioButton android:id="@+id/radio_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>  
 
<RelativeLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"     
    android:layout_width="match_parent"     
    android:layout_height="wrap_content" 
    android:background="?android:attr/selectableItemBackground" 
    android:clickable="true" 
    android:focusable="true"     
    android:orientation="vertical"     
    android:paddingBottom="@dimen/row_padding_vertical" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/row_padding_vertical">

    <TextView         
       android:id="@+id/title" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentTop="true" 
       android:textColor="@color/title"         
       android:textSize="16dp" 
       android:textStyle="bold" />

    <TextView 
       android:id="@+id/genre"         
       android:layout_width="match_parent"         
       android:layout_height="wrap_content" 
       android:layout_below="@id/title" />

    <TextView         
       android:id="@+id/year"         
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"         
       android:layout_alignParentRight="true" 
       android:textColor="@color/year" />

</RelativeLayout> 

</LinearLayout>
4. ==> Create MainActivity file for recycler view show Movie list with title,
           subtitle, and year. Using Web API or web services and set data on list
package com.akhari.myapplication;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.akhari.myapplication.AppGlobals;
import com.akhari.myapplication.R;
import com.akhari.myapplication.adapter.NewsListAdapter;
import com.akhari.myapplication.database.DeoliLiveDatabase;
import com.akhari.myapplication.interactor.InteractorImpl;
import com.akhari.myapplication.interactor.ResponsePacket;
import com.akhari.myapplication.interfaces.IntegratorConst;
import com.akhari.myapplication.interfaces.OnResponseListener;
import com.akhari.myapplication.model.NewsInfo;
import com.google.gson.JsonObject;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ProgressDialog progress;
    private ArrayList<MovieModel> movieList = new ArrayList<>();
    private RecyclerView recyclerView;
    private MoviesListAdapter mMoviesListAdapter;

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

        progress=new ProgressDialog(getActivity());

        recyclerView = findViewById(R.id.recycler_view);

        mMoviesListAdapter = new MoviesListAdapter(movieList,this);
        RecyclerView.LayoutManager mLayoutManager =

         new LinearLayoutManager(getApplicationContext());

        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(mMoviesListAdapter);
        prepareMovieList();
    }

    private void prepareMovieList() {

    if(AppGlobals.getInstance().isOnline(getActivity())) {
            getList(true);
        }
    }

    public void getList(boolean isShowProgress){
        if(isShowProgress){
            progress.setMessage("Loading");
            progress.setProgressStyle(ProgressDialog.THEME_DEVICE_DEFAULT_LIGHT);
            progress.setIndeterminate(true);
            progress.setCancelable(false);
            progress.show();
        }
        try {
            JsonObject jsonObject = new JsonObject();
            new InteractorImpl(getActivity(), this, IntegratorConst.Code_URL,
        IntegratorConst.Tag_URL)
                    .makeJsonPostRequest(IntegratorConst.Method_URL, jsonObject, false);

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

    @Override
    public void onSuccess(int requestCode, ResponsePacket responsePacket) {
        if(progress!=null && progress.isShowing()){
            progress.dismiss();
        }
        try {
             if(IntegratorConst.Code_DeoliWall==requestCode){
                if(responsePacket.getErrorCode()==0){
                    liveDatabase.deleteDeoliWalls();
                    for(int i=0;i<responsePacket.getValues().
                getMovieModelInfo().size();i++) {
            MovieModel movie = new MovieModel(
                responsePacket.getValues().
                getMovieModelInfo().get(i).getTitle(),
                               responsePacket.getValues().
                getMovieModelInfo().get(i).getDescription(),
               responsePacket.getValues().getMovieModelInfo().get(i).getYear());
                   movieList.add(movie);
                    }
        mMoviesListAdapter.notifyDataSetChanged();
                }
            }
        }catch (NullPointerException nxe){
            nxe.printStackTrace();
        }
    }

    @Override
    public void onError(int requestCode) {
        if(progress!=null && progress.isShowing()){
            progress.dismiss();
        }
        Toast.makeText(getActivity(),"Please check your internet or please try again",
    Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

5.  activity_main  layout file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="top"
    android:gravity="top"
    android:padding="25dp"
    android:orientation="vertical">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"/>

</LinearLayout>

Thanks

No comments:

Post a Comment