Reputation: 53
Hi I am using recyclerview, I have checkbox in my recyclerview, User can check the checkbox in recyclerview. Now lets say user will check two items from recyclerview, and click on share button , now in one alert dialog I am loading recyclerview data with different layout in another horizontal recyclerview. now in second layout I will have two items which i am scrolling horizontally. now from dialog if i am sharing to whatsapp or another apps, it is taking only one layout as image of horizontal recyclerview instead of two images.
customAdapter = new CustomAdapter(this,modelArrayList);
recyclerView.setAdapter(customAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
btndeselect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Batch_Share_Layout();
}
});
}
public static class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
private LayoutInflater inflater;
public static ArrayList<Model> imageModelArrayList;
private Context ctx;
public CustomAdapter(Context ctx, ArrayList<Model> imageModelArrayList) {
inflater = LayoutInflater.from(ctx);
this.imageModelArrayList = imageModelArrayList;
this.ctx = ctx;
}
@Override
public CustomAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.rv_item, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final CustomAdapter.MyViewHolder holder, int position) {
// holder.checkBox.setText("Checkbox " + position);
holder.checkBox.setChecked(imageModelArrayList.get(position).getSelected());
holder.tvAnimal.setText(imageModelArrayList.get(position).getAnimal());
alertviewlist =new ArrayList<Model>();
// holder.checkBox.setTag(R.integer.btnplusview, convertView);
holder.checkBox.setTag(position);
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Integer pos = (Integer) holder.checkBox.getTag();
Toast.makeText(ctx, imageModelArrayList.get(pos).getAnimal() + " clicked! ", Toast.LENGTH_SHORT).show();
alertviewlist.add(imageModelArrayList.get(pos));
System.out.println(alertviewlist.size());
if (imageModelArrayList.get(pos).getSelected()) {
imageModelArrayList.get(pos).setSelected(false);
} else {
imageModelArrayList.get(pos).setSelected(true);
}
}
});
}
@Override
public int getItemCount() {
return imageModelArrayList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
protected CheckBox checkBox;
private TextView tvAnimal;
public MyViewHolder(View itemView) {
super(itemView);
checkBox = (CheckBox) itemView.findViewById(R.id.cb);
tvAnimal = (TextView) itemView.findViewById(R.id.animal);
}
}
}
public static class CustomAdapterShare extends RecyclerView.Adapter<CustomAdapterShare.MyViewHolder> {
private LayoutInflater inflater;
public static ArrayList<Model> imageModelArrayListshare;
private Context ctx;
public CustomAdapterShare(Context ctx, ArrayList<Model> imageModelArrayListshare) {
inflater = LayoutInflater.from(ctx);
this.imageModelArrayListshare = imageModelArrayListshare;
this.ctx = ctx;
}
@Override
public CustomAdapterShare.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.share_layout_data, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final CustomAdapterShare.MyViewHolder holder, int position) {
holder.product_name.setText(imageModelArrayListshare.get(position).getAnimal());
// holder.product_name.setText(imageModelArrayList.get(position).getAnimal());
/* for (int i = 0; i < CustomAdapterShare.imageModelArrayList.size(); i++){
if(CustomAdapterShare.imageModelArrayList.get(i).getSelected()) {
holder.product_name.setText(CustomAdapterShare.imageModelArrayList.get(i).getAnimal());
}
}*/
// holder.checkBox.setTag(R.integer.btnplusview, convertView);
}
@Override
public int getItemCount() {
return imageModelArrayListshare.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
private TextView product_name;
public MyViewHolder(View itemView) {
super(itemView);
product_name = (TextView) itemView.findViewById(R.id.product_name);
}
}
}
public void Batch_Share_Layout()
{
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = getLayoutInflater();
View convertView = inflater.inflate(R.layout.main_layout_recyclers, null);
list = (RecyclerView) convertView.findViewById(R.id.list);
list.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
list.setHasFixedSize(true);
CustomAdapterShare adapter = new CustomAdapterShare(MainActivity.this,alertviewlist);
list.setAdapter(adapter);
alertDialog.setView(convertView);
share_main_btn = (Button) convertView.findViewById(R.id.main_share_btn);
share_main_linear = (LinearLayout) convertView.findViewById(R.id.main_linear_layout);
share_main_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
OnClickShare(view);
}
});
AlertDialog dialog = alertDialog.create();
dialog.getWindow().setLayout(600, 400);
dialog.show();
adapter.notifyDataSetChanged();
}
public void OnClickShare(View view){
Bitmap bitmap =getBitmapFromView(share_main_linear);
try {
/*Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "Text caption message!!");
intent.setType("text/plain");
intent.setType("image/jpeg");
intent.setPackage("com.whatsapp");
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(intent);*/
File file = new File(MainActivity.this.getExternalCacheDir(),"logicchip.png");
FileOutputStream fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
file.setReadable(true, false);
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setType("image/png");
MainActivity.this.startActivity(Intent.createChooser(intent, "Share image via"));
} catch (Exception e) {
e.printStackTrace();
}
}
this is what i am trying to create arraylist of uri
uris = new ArrayList<>();
for(int i=0;i<alertviewlist.size();i++)
{
uris.add(alertviewlist.get(i));
}
Upvotes: 0
Views: 2573
Reputation: 2564
You can use view's drawing cache to create a view holding all the views you want to share and convert it to bitmap.
First, you need to enable drawing cache on the view (which you want to convert to bitmap)
view.setDrawingCacheEnabled(true)
Then you can build a bitmap from the drawing cache using -
public static Bitmap getViewBitmap(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null) {
bgDrawable.draw(canvas);
} else {
canvas.drawColor(Color.TRANSPARENT);
}
view.draw(canvas);
return returnedBitmap;
}
Now you have the bitmap. You can write it to a file in the File System and then can share it on other platforms.
Upvotes: 1