How to Select Single RadioButton in RecyclerView in Android ?

 

 How to Select Single Radio Button in Recycler View in Android ? 




First Create A new project and then go to Main Activity  ... and i rename it Select plan  ...


this is Xml code...
<?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=".activities.activities.SelectPlan">


<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="180dp"
android:fontFamily="@font/roboto_bold"
android:text=" Select Your Plan"
android:textColor="@color/black"
android:textSize="24sp">
</TextView>


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

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnPayNow"
android:layout_marginTop="20dp"
android:layout_width="248dp"
android:layout_height="56dp"
android:textAllCaps="false"
android:layout_marginHorizontal="70dp"
android:background="@drawable/bg_btn"
android:text="Pay Now !"
android:textColor="@color/white"
>
</androidx.appcompat.widget.AppCompatButton>

</LinearLayout>
</ScrollView>


</LinearLayout>

Then Create A custom layout For Select Plan...

<?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="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<androidx.cardview.widget.CardView
android:id="@+id/cardRadioButton"
android:layout_marginVertical="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="10dp"
app:cardElevation="5dp"
android:layout_marginHorizontal="15dp"
>

<LinearLayout
android:paddingVertical="15dp"
android:id="@+id/LnPlane"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="5dp"
android:weightSum="2"
android:orientation="horizontal"
>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.1"
android:paddingHorizontal="8dp"

>
<RadioButton
android:id="@+id/rdPlan"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:button="@drawable/radiodraw"
android:buttonTint="@color/black"
android:textColor="@color/black"
android:textSize="14sp"
tools:ignore="TouchTargetSizeCheck" />

</LinearLayout>

<LinearLayout
android:layout_marginHorizontal="5dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1.9"
android:gravity="center_vertical"
>
<TextView
android:id="@+id/txtPlan"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="16sp"
android:fontFamily="@font/roboto_bold"
android:textColor="@color/black"
android:text="Free For 1 Month"
>

</TextView>

<TextView
android:layout_marginTop="5dp"
android:id="@+id/txtDesPlan"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="15sp"
android:textColor="@color/halfBlack"
android:text="Lorem Ipsum is simply dummy text of the."
>

</TextView>

</LinearLayout>


</LinearLayout>


</androidx.cardview.widget.CardView>


</LinearLayout>


Then Create A Model Class .

package com.daizzyinfo.vidhiksahayata.activities.models;

public class SelectPlanModel {

String PlanHeader, PlanDescription;

public SelectPlanModel(String planHeader, String planDescription) {
PlanHeader = planHeader;
PlanDescription = planDescription;
}

public String getPlanHeader() {
return PlanHeader;
}

public void setPlanHeader(String planHeader) {
PlanHeader = planHeader;
}

public String getPlanDescription() {
return PlanDescription;
}

public void setPlanDescription(String planDescription) {
PlanDescription = planDescription;
}
}

Then Create A Adapter Class .

package com.daizzyinfo.vidhiksahayata.activities.adapter;


import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.core.content.ContextCompat;
import androidx.core.widget.CompoundButtonCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.daizzyinfo.vidhiksahayata.R;
import com.daizzyinfo.vidhiksahayata.activities.models.SelectPlanModel;

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

public class SelectPlanAdapter extends RecyclerView.Adapter<SelectPlanAdapter.SelectPlanVH> {

Context context;
List<SelectPlanModel> model = new ArrayList<>();
int mSelectedPosition = -1;

public SelectPlanAdapter(Context context, List<SelectPlanModel> model) {
this.context = context;
this.model = model;
}

@NonNull
@Override
public SelectPlanVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.custom_select_plan, parent, false);
return new SelectPlanVH(view);
}

@Override
public void onBindViewHolder(@NonNull SelectPlanVH holder, int position) {
holder.txtPlan.setText(model.get(position).getPlanHeader());
holder.txtDesPlan.setText(model.get(position).getPlanDescription());
holder.rdPlan.setChecked(position == mSelectedPosition);


if (position == mSelectedPosition) {
holder.txtPlan.setTextColor(ContextCompat.getColor(context, R.color.white));
holder.txtDesPlan.setTextColor(ContextCompat.getColor(context, R.color.white));
holder.cardRadioButton.setCardBackgroundColor(ContextCompat.getColor(context, R.color.Green));
CompoundButtonCompat.setButtonTintList(holder.rdPlan, ContextCompat.getColorStateList(context, R.color.white));
} else {
holder.txtPlan.setTextColor(ContextCompat.getColor(context, R.color.black));
holder.txtDesPlan.setTextColor(ContextCompat.getColor(context, R.color.black));
holder.cardRadioButton.setCardBackgroundColor(ContextCompat.getColor(context, R.color.white));
CompoundButtonCompat.setButtonTintList(holder.rdPlan, ContextCompat.getColorStateList(context, R.color.black));
}


}

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

public class SelectPlanVH extends RecyclerView.ViewHolder implements View.OnClickListener {
CardView cardRadioButton;
TextView txtPlan, txtDesPlan;
RadioButton rdPlan;

public SelectPlanVH(@NonNull View itemView) {
super(itemView);
txtDesPlan = itemView.findViewById(R.id.txtDesPlan);
cardRadioButton = itemView.findViewById(R.id.cardRadioButton);
rdPlan = itemView.findViewById(R.id.rdPlan);
txtPlan = itemView.findViewById(R.id.txtPlan);

itemView.setOnClickListener(this);
rdPlan.setOnClickListener(this);
}

@Override
public void onClick(View v) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
if (mSelectedPosition != position) {
int previousSelectedPosition = mSelectedPosition;
mSelectedPosition = position;
notifyItemChanged(previousSelectedPosition);
notifyItemChanged(mSelectedPosition);
}
}
}
}
}


Then Go Main Class Main Activity ( Select Plan).

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import com.daizzyinfo.vidhiksahayata.R;
import com.daizzyinfo.vidhiksahayata.activities.adapter.SelectPlanAdapter;
import com.daizzyinfo.vidhiksahayata.activities.models.SelectPlanModel;
import java.util.ArrayList;
import java.util.List;

public class SelectPlan extends AppCompatActivity {

RecyclerView rvSelectPlan;
protected AppCompatButton btnPayNow;

SelectPlanAdapter adapter;

List<SelectPlanModel> model;


@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_plan);

rvSelectPlan=findViewById(R.id.rvSelectPlan);
btnPayNow=findViewById(R.id.btnPayNow);

model = new ArrayList<>();

adapter = new SelectPlanAdapter(this,model);
rvSelectPlan.setAdapter(adapter);

LinearLayoutManager manager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
rvSelectPlan.setLayoutManager(manager);


model.add(new SelectPlanModel("Free For 1 Month","Lorem Ipsum is simply dummy text of the."));
model.add(new SelectPlanModel("INR 200 for 6 Month","Lorem Ipsum is simply dummy text of the."));
model.add(new SelectPlanModel("INR 300 for 1 Year","Lorem Ipsum is simply dummy text of the."));





}

I Hope This Code is Very Helpful For You .



Comments

Popular posts from this blog

API

How to get latitude longitude simply in Android using Address

Manifest