Recycle listview is the listview better than a simple list view now here is the code to create Recycle listview with dynamic listview raw.
First need to add Recycle listview library in build.gradle(Module:app) file
}
Then in activity_main.xml file add below code
Then make new xml file recycleview_row.xml and add below code
Then make new xml file customdialog.xml and add below code
And then make one Adapter for recycle list view
MyRecycleViewAdapter.java and add below code
And final code in MainActivity.java
First need to add Recycle listview library in build.gradle(Module:app) file
dependencies {
implementation 'androidx.recyclerview:recyclerview:1.1.0'}
Then in activity_main.xml file add below code
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvalphabets"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Then make new xml file recycleview_row.xml and add below code
<?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"
android:orientation="horizontal"
android:padding="10dp"
android:layout_gravity="center"
android:gravity="center">
<TextView android:id="@+id/tvandroidversionname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text=""
android:textSize="35sp"
android:textStyle="bold"
android:textColor="@color/colorPrimary"/>
<ImageView android:id="@+id/imgview"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:gravity="center"
android:visibility="gone"/>
</LinearLayout>
Then make new xml file customdialog.xml and add below code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:padding="10dp"
android:background="@android:color/white">
<FrameLayout android:id="@+id/frmcustom"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@color/colorPrimary">
<ImageButton android:id="@+id/closebtn"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="top|right"
android:layout_margin="10dp"
android:background="@android:drawable/ic_menu_close_clear_cancel"/>
<TextView android:id="@+id/tvdialogName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_below="@+id/closebtn"
android:text=""
android:textSize="25sp"
android:padding="10dp"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:textColor="@android:color/white"/>
</FrameLayout>
</RelativeLayout>
And then make one Adapter for recycle list view
MyRecycleViewAdapter.java and add below code
import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class MyRecyclerViewAdapter extends
RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
private List<String> mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
Context mContext;
// data is passed into the constructor
MyRecyclerViewAdapter(Context context, List<String> data) {
this.mInflater = LayoutInflater.from(context);
this.mData = data;
mContext = context;
}
// inflates the row layout from xml when needed
@Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.recyclerview_row, parent, false);
return new ViewHolder(view);
}
// binds the data to the TextView in each row
@Override public void onBindViewHolder(ViewHolder holder, int position) {
String alphabet = mData.get(position);
holder.myTextView.setText(alphabet);
}
// total number of rows @Override public int getItemCount() {
return mData.size();
}
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
TextView myTextView;
ViewHolder(View itemView) {
super(itemView);
myTextView = itemView.findViewById(R.id.tvAlphabetsName);
itemView.setOnClickListener(this);
}
@Override public void onClick(View view) {
if (mClickListener != null)
mClickListener.onItemClick(view, getAdapterPosition());
}
}
// convenience method for getting data at click position
String getItem(int id) {
return mData.get(id);
}
// allows clicks events to be caught
void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}
And final code in MainActivity.java
import android.app.Dialog;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements
MyRecyclerViewAdapter.ItemClickListener{
Context mcontext;
MyRecyclerViewAdapter adapter;
String gettext;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mcontext = MainActivity.this;
// data to populate the RecyclerView with
ArrayList<String> alphabetsNames = new ArrayList<>();
alphabetsNames.add("Android Cupcake.");
alphabetsNames.add("Android Donut.");
alphabetsNames.add("Android Eclair.");
alphabetsNames.add("Android Froyo.");
alphabetsNames.add("Android Gingerbread.");
alphabetsNames.add("Android Honeycomb.");
alphabetsNames.add("Android Ice Cream Sandwich.");
alphabetsNames.add("Android Jelly Bean.");
alphabetsNames.add("Android KitKat");
alphabetsNames.add("Android Lollipop");
alphabetsNames.add("Android Marshmallow");
alphabetsNames.add("Android Nougat");
alphabetsNames.add("Android Oreo");
alphabetsNames.add("Android Pie");
// set up the RecyclerView
RecyclerView recyclerView = findViewById(R.id.tvandroidversionname);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new MyRecyclerViewAdapter(this, alphabetsNames);
adapter.setClickListener(this);
recyclerView.setAdapter(adapter);
}
@Override public void onItemClick(View view, int position) {
gettext = adapter.getItem(position);
// custom dialog final Dialog dialog = new Dialog(mcontext);
dialog.setContentView(R.layout.customdialog);
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.tvdialogName);
ImageButton closebtn = (ImageButton)dialog.findViewById(R.id.closebtn);
text.setText(gettext);
closebtn.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
No comments:
Post a Comment
Comments