Remove large gap between images in Linear Layout Android Studio - java

I have a program that is adding imageViews to a linear layout within a horizontal scrollview, but each image ends up with a very large gap between each one. This looks terrible, and I want to reduce the gap between them, just so that they are closer together. This is how big the gap between each image is right now
Here is the code where I am adding them into the layout. I have tried changing the scaletype of the imageviews but I couldn't find a way to fix it. Any help would be appreciated.
public void addPicturesToView(){
LinearLayout pictureLayout = findViewById(R.id.qrImageLinearLayout);
docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
#Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
List<String> imageList = (List<String>) documentSnapshot.get("Images");
for(String imageString : imageList){
StorageReference storRef = storage.getReferenceFromUrl(imageString);
Task<byte[]> task = storRef.getBytes(1000000).addOnSuccessListener(new OnSuccessListener<byte[]>() {
#Override
public void onSuccess(byte[] imageBytes) {
Bitmap imageBitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
ImageView qrImage = new ImageView(QRcodeViewerActivity.this);
qrImage.setImageBitmap(imageBitmap);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
qrImage.setLayoutParams(params);
pictureLayout.addView(qrImage);
}
});
}
return;
}
});
return;
}

I have solved this issue, by setting AdjustViewBounds to true and adding padding I was able to make it look proper.
Bitmap imageBitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
ImageView qrImage = new ImageView(QRcodeViewerActivity.this);
qrImage.setImageBitmap(imageBitmap);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
qrImage.setLayoutParams(params);
qrImage.setAdjustViewBounds(true);
qrImage.setPadding(5,0,5, 0);
pictureLayout.addView(qrImage);

Related

How to display label using Slider of Material component?

I am using the Slider of Material component:
https://github.com/material-components/material-components-android/blob/master/docs/components/Slider.md
I am trying to display a label when the thumb is moving, which seem to be supported with the labelFormatter attribute.
here is what my code looks like:
Slider s = new Slider(context);
s.setLabelFormatter(new Slider.LabelFormatter() {
#NonNull
#Override
public String getFormattedValue(float value) {
return "MY STRING";
}
});
When I go line by line with debugger it goes through this function:
private void drawLabelText(#NonNull Canvas canvas, int width, int top) {
labelTextPaint.getTextBounds(labelText, 0, labelText.length(), labelTextBounds);
int left = trackSidePadding + (int) (thumbPosition * width) - labelTextBounds.width() / 2;
canvas.drawText(labelText, left, top - labelTextTopOffset - thumbRadius, labelTextPaint); }
but no text is displayed only the slider...
I am kind of new in androïd and I am surely missing something.
Thanks for help :)
EDIT 1:
Here is the whole code simplified :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CoordinatorLayout layout = new CoordinatorLayout(this);
setContentView(layout);
// I am Using Coordinator Layout for current activity so...
CoordinatorLayout.LayoutParams layoutParams = new CoordinatorLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//Setting gravity to CENTER
layoutParams.gravity = Gravity.CENTER;
Slider slider = new Slider(this);
slider.setLabelFormatter(new Slider.LabelFormatter() {
#NonNull
#Override
public String getFormattedValue(float value) {
return "MY STRING";
}
});
layout.addView(slider, layoutParams);
}
But still not working ...
output result
https://youtu.be/obV4K-Nxu-0
EDIT 2:
Updating Material component from version: '1.2.0-alpha02' to version: '1.2.0-alpha05' fixed the issue.
Actually i wanted to comment but because of Reputation Constraints i couldn't,
Looks like you are adding a view programatically in your activity. Here i have implemented new MaterialComponent Slider with LabelFormatter
ViewGroup group = findViewById("YOUR_ACTIVITY_LAYOUT");
// I am Using Coordinator Layout for current activity so...
CoordinatorLayout.LayoutParams layoutParams = new CoordinatorLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//Setting gravity to CENTER
layoutParams.gravity = Gravity.CENTER;
Slider slider = new Slider(this);
slider.setLabelFormatter(new Slider.LabelFormatter() {
#NonNull
#Override
public String getFormattedValue(float value) {
return "MY STRING";
}
});
group.addView(slider, layoutParams);
}
And then there you go...

How to use Layout Params in RecyclerView Item

I have app about surveys. I list all active surveys in RecyclerView.
In the bottom of RecyclerView custom item, I add profile pictures of users who attendant to that survey (like in facebook - seen by bla bla)
I decided to implement imageview to that layout horizontally with params, somehow i couldn't and images are not displaying on item.
Here is my OnBindViewHolder(Other parts work well)
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
Picasso.with(context)
.load(surveyList.get(position).getItem_image())
.into(holder.item_survey_image);
String imageStringList = surveyList.get(position).getItem_survey_users_image();
List<String> splitList = Arrays.asList(imageStringList.split(","));
for(int i=0; i<splitList.size(); i++){
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 20);
Glide.with(context)
.load(splitList.get(i))
.into(holder.item_survey_users_image);
if (holder.item_survey_users_image.getParent() != null)
((ViewGroup) holder.item_survey_users_image.getParent()).removeView(holder.item_survey_users_image);
holder.item_layout_users.addView(holder.item_survey_users_image, layoutParams);
}
holder.item_survey_head.setText(surveyList.get(position).getItem_survey_head());
holder.item_survey_desc.setText(surveyList.get(position).getItem_survey_desc());
holder.item_survey_amount.setText(surveyList.get(position).getItem_survey_amount());
holder.item_survey_time.setText(surveyList.get(position).getItem_survey_time());
holder.item_survey_users.setText(surveyList.get(position).getItem_survey_users());
holder.item_cardview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context, ""+position, Toast.LENGTH_SHORT).show();
}
});
}
how you can tell the picture absence is caused by layoutparams ? however here is how you implement a layoutParams in your OnBindViewHolder, say you have RelativeLayout
RelativeLayout.LayoutParams params
=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
params.setMargins(0, 0, 0, 0); //if you need margins
holder.item_survey_image.setLayoutParams(params);

how to load image with Universal ImageLoader without displaying

I'm trying to do something like this:
Android Map api v2 Custom marker with ImageView
But I'm stuck on using image loader.
Trying:
Bitmap bmImg = imageLoader.loadImageSync(url);
LogCat gives me
04-13 14:11:44.953: E/ImageLoader(18542): android.os.NetworkOnMainThreadException
Here's is my code. I have an ArrayList camera with all information needed (titrle, position, url, etc).
public void drawPicsOnMap()
{
String title = null;
String place = null;
for (int i = 0; i<camera.size(); i++)
{
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp = Bitmap.createBitmap(80, 80, conf);
Canvas canvas = new Canvas(bmp);
// paint defines the text color,
// stroke width, size
Paint color = new Paint();
color.setTextSize(35);
color.setColor(Color.BLACK);
Camera cam = camera.get(i);
LatLng coord = new LatLng(cam.lat, cam.lon);
title = Integer.toString(cam.id);
place = cam.place;
String url = cam.img;
Bitmap bmImg = imageLoader.loadImageSync(url);
//modify canvas
canvas.drawBitmap(bmImg, 0,0, color);
canvas.drawText(title, 30, 40, color);
Marker mark = map.addMarker(new MarkerOptions()
.position(coord)
.title(title)
.snippet(place)
.icon(BitmapDescriptorFactory
.fromBitmap(bmp)));
markers.put(mark.getId(), url);
}
}
Try this:
imageLoader.loadImage(url, new SimpleImageLoadingListener(){
#Override
public void onLoadingComplete(String imageUri, View view,
Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
//write your code here to use loadedImage
}
});
Here
onLoadingComplete will be called on UI thread which makes it thread safe
You can not do any networking on the main thread. See for description. UIL usually creates the thread and stuff for you when you call displayImage, but loadImageSync does not. Either create a thread and load it or use an AsynTask.

Create ImageViews dynamically inside a loop

I have written this code that loads an image to in ImageView widget:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
i = (ImageView)findViewById(R.id.imageView1);
new get_image("https://www.google.com/images/srpr/logo4w.png") {
ImageView imageView1 = new ImageView(GalleryActivity.this);
ProgressDialog dialog = ProgressDialog.show(GalleryActivity.this, "", "Loading. Please wait...", true);
protected void onPreExecute(){
super.onPreExecute();
}
protected void onPostExecute(Boolean result) {
i.setImageBitmap(bitmap);
dialog.dismiss();
}
}.execute();
}
bu now, I want to load several images. for this I need create image views dynamically but i don't know how...
I want run my code inside a for loop:
for(int i;i<range;i++){
//LOAD SEVERAL IMAGES. READ URL FROM AN ARRAY
}
my main problem is creating several ImageViews inside a loop dynamically
you can modify the layout , image resource and no of images (may be dynamic as well) according to your requirement...
LinearLayout layout = (LinearLayout)findViewById(R.id.imageLayout);
for(int i=0;i<10;i++)
{
ImageView image = new ImageView(this);
image.setLayoutParams(new android.view.ViewGroup.LayoutParams(80,60));
image.setMaxHeight(20);
image.setMaxWidth(20);
// Adds the view to the layout
layout.addView(image);
}
You can use this code to create ImageViews
ImageView image = new ImageView(this);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
image.setLayoutParams(vp);
image.setMaxHeight(50);
image.setMaxWidth(50);
// other image settings
image.setImageDrawable(drawable);
theLayout.addView(image);
where theLayout is the layout you want to add your image views to.
For more customization check out the dev page, where all the possible options are listed.
If your requirement is show in List or Gridview then you should go for Lazy Loading List or grid.
Please go through the below link.
https://github.com/thest1/LazyList
https://github.com/abscondment/lazy-gallery
Try this
rootLayout = (LinearLayout) view1.findViewById(R.id.linearLayout1);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER_VERTICAL;
params.setMargins(0, 0, 60, 0);
for(int x=0;x<2;x++) {
ImageView image = new ImageView(getActivity());
image.setBackgroundResource(R.drawable.ic_swimming);
rootLayout.addView(image);
}

Why addView() reset my layout in Android Studio?

I have a thread that call a function in my main activity that should add an image to my layout at a certain position.. When i call my function all works properly but my layout's parameter (margin in my case) resets when i call:
myLayout.addView(newImage);
That's my function that my thread calls:
public void createImageAt(Point location) {
final ImageView newImage = new ImageView(this);
final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(90, 90);
params.leftMargin = location.x;
params.topMargin = location.y;
newImage.setImageResource(R.drawable.image);
newImage.setLayoutParams(params);
final RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.gameLayout);
myLayout.post(new Runnable() {
#Override
public void run() {
myLayout.addView(newImage);
}
});
}
My problem is that i have to slide my layout by moving the leftMargin, and when i call the function wrote above the margin jump back to 0 even if it was at -200px
If the problem, that the state does not saves, make newImage global variable and reuse it. Just use myLayout.addView(newImage); and myLayout.remove(newImage);

Categories

Resources