I'm making a gallery in my application. I listed my images successfully and now adding its share buttons. Instead of directing user into another form, I'm trying to show share buttons above my image. For listing and sharing I need to create and add dynamic ImageButtons
. But when I try to add share buttons, they're shown in wrong place. Here is my code:
LinearLayout ll = (LinearLayout) findViewById(R.id.llGallery);
for (int i = 0; i < jsonArray.length(); i++) {
final JSONObject jsonObject = jsonArray.getJSONObject(i);
final String Photo= jsonObject.getString("PhotoLink");
// PhotoDetails into another view
View.OnClickListener cPhotoDetails= new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(GalleryDetailsActivity.this,
GalleryPhotoDetails.class);
intent.putExtra("Image", Photo);
startActivity(intent);
}
};
View.OnClickListener cShare= new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
File myFile = new File(fotograf);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = myFile.getName().substring(myFile.getName().lastIndexOf(".") + 1);
String type = mime.getMimeTypeFromExtension(ext);
Intent sharingIntent = new Intent("android.intent.action.SEND");
sharingIntent.setType(type);
sharingIntent.putExtra("android.intent.extra.STREAM", Uri.fromFile(myFile));
startActivity(Intent.createChooser(sharingIntent, "Share with"));
} catch (Exception e) {
// Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
};
RelativeLayout rel = new RelativeLayout(context);
rel.setGravity(Gravity.CENTER_VERTICAL);
ImageButton img = new ImageButton(context);
img.setClickable(true);
img.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Glide.with(context).load(Photo).into(img);
img.setOnClickListener(cPhotoDetails);
rel.addView(img);
ImageButton share = new ImageButton(context);
share.setClickable(true);
share.setScaleType(ImageView.ScaleType.FIT_XY);
Glide.with(context).load(R.drawable.btn_share).into(share);
share.setOnClickListener(cShare);
rel.addView(share);
ll.addView(rel);
}
RelativeLayout rel = new RelativeLayout(context);
rel.setGravity(Gravity.CENTER_VERTICAL);
ImageView img = new ImageView(context);
img.setClickable(true);
img.setScaleType(ImageView.ScaleType.FIT_XY);
Glide.with(context).load(Photo).into(img);
img.setOnClickListener(cPhotoDetails);
rel.addView(img);
ImageButton share = new ImageButton(context);
share.setClickable(true);
share.setScaleType(ImageView.ScaleType.FIT_XY);
Glide.with(context).load(R.drawable.btn_share).into(share);
share.setOnClickListener(cShare);
rel.addView(share);
ll.addView(rel);