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);
}
Related
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);
I want to add an ImageView within the onClick() of the Button. But the ImageView should be aligned to the centre of the button.
How can I do this pragmatically?
I'm creating a new instance of the ImageView to be added on top of the View that called it and it's position is equivalent of the View that called it.
How can I get the X and Y coordinates?
EDIT:
#Override
public void onClick(final View v) {
if(v.getWidth() > v.getHeight()) size = v.getHeight();
if(v.getWidth() < v.getHeight()) size = v.getWidth();
RelativeLayout.LayoutParams button = new RelativeLayout.LayoutParams(size,size);
buttonc = new ImageView(getApplicationContext());
buttonc.setBackgroundResource(R.drawable.round);
layout.addView(buttonc,button);
I'm using the above code to attempt to pragmatically set the size of the ImageView to that of the button, and it's working.
As for the coordinates, I still haven't figured it out.
To add clarity: I'm attempting to create a rounded ripple effect over the view that calls it.
I suggest a better solution create an image view before hand place it in the xml as you see fit,at first set its visibility to false .
On click switch its visibilty to true, and the image will appear
This solution is easier than what you are searching.
Feel free to ask questions
to add Imageview
#Override
public void onClick(final View v) {
//LinearLayout
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//ImageView
ImageView imageView = new ImageView(this);
//image resource
imageView.setImageResource(R.drawable.specs);
//image position
imageView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
//adding view to layout
linearLayout.addView(imageView);
//make visible to program
setContentView(linearLayout);
}
I have a RelativeLayout completely created within java, not using xml layout.
I have a few buttons on the left if my screen. I want to click on one button and show a default image from my res/drawable next to the button and make it disappear again on second click.
What I have tried was toggling the visibility but my onClick() raises a FATAL EXCEPTION, NullPointer Exception.
This is my code so far. Hardcoded the image shows right when I set picview.setVisibility(View.INVISIBLE); by hand. What I am doing wrong in the onClick() ?
private ImageView picview;
//*snip* loads of other code
//Show Image Button
ImageButton show_pic = new ImageButton(this);
show_pic.setBackgroundColor(Color.WHITE);
show_pic.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
if(picview.getVisibility() == View.INVISIBLE)
{
picview.setVisibility(View.VISIBLE);
}
else if (picview.getVisibility() == View.VISIBLE)
{
picview.setVisibility(View.INVISIBLE);
}
}
});
params = new RelativeLayout.LayoutParams(40, 40);
params.topMargin = 10;
params.leftMargin = 10;
params.addRule(RelativeLayout.BELOW, button2_id);
rl.addView(show_pic, params);
//Imageview loaded from drawable
ImageView picview = new ImageView(this);
params = new RelativeLayout.LayoutParams(200, 400);
params.topMargin = 0;
params.leftMargin = 30;
params.addRule(RelativeLayout.RIGHT_OF, button2_id);
picview.setImageResource(R.drawable.my_image);
picview.setVisibility(View.INVISIBLE);
rl.addView(picview, params);
this.setContentView(rl);
You are accidentally creating two copies of picview. Shorten this line:
ImageView picview = new ImageView(this);
To:
picview = new ImageView(this);
(Your field variable private ImageView picview; never changed from null, so when you clicked your Button you see the NullPointerException...)
change
show_pic.setOnClickListener(new OnClickListener() to
show_pic.setOnClickListener(new View.OnClickListener()
Hi guys i am stuck with image caching. i need to populate Linear Layout with images from web.
I was searching examples how to cache images in android and found these examples:
http://developer.android.com/resources/samples/XmlAdapters/src/com/example/android/xmladapters/ImageDownloader.html
Lazy load of images in ListView
I tried it implement for my code.
String picPath = "https://www.google.lt/logos/classicplus.png";
try {
/*
View v = null;//new View(this);
ImageLoader loader = new ImageLoader(this);
ImageView im = (ImageView)this.findViewById(R.id.image);
loader.DisplayImage(picPath, im);
addImage( im);*/
LayoutInflater inflater =(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View convertView = inflater.inflate(R.layout.item, null);
ImageView image = (ImageView)convertView.findViewById(R.id.image);
ImageDownloader down = new ImageDownloader();
down.download(picPath, image);
addImage(image);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
public void addImage( View view){
LinearLayout pubLayout = (LinearLayout)findViewById( R.id.scrollerLinearlayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//setting layout_margin
params.setMargins(15, 30, 15, 30);
pubLayout.addView(view,params);
}
However this didn't worked. I believe because i add view to linear layout and when image is downloaded it can't repaint it ?
There are examples how to do image caching using ListView. But what to do if i am not using List View.
I would write my own caching but how to make callback to image downloaded as i want first add image to layout and on callback update it ?
Thanks.
you have to call invalidate on parent view
pubLayout.getParent().invalidate();
I'm creating a version of the popular Minesweeper game for Android. I'm trying to programmatically create a button and add it to a RelativeLayout. I've found something very similar here: How do I programmatically add buttons into layout one by one in several lines?
When I try to run it I get a NullPointerException at:
RelativeLayout layout1 = (RelativeLayout) findViewById(R.layout.game);
Here's the whole block of code:
public void create() {
RelativeLayout layout1 = (RelativeLayout) findViewById(R.layout.game);
for(int i = 0; i < gridSize; i++) {
if(grid[i] == 0) { //if grid pos. indicates an empty cell
Button empty = new Button(this);
empty.setBackgroundResource(R.drawable.emptybutton); //set background to empty
empty.setId(i); //set id to value of i
empty.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layout1.addView(empty); //add the button to the relativeLayout view
//((Button) findViewById(i)).setOnClickListener(emptyListener);
}
Thanks in advance for any responses
have set the layout xml of the Activity by setContentView(R.layout.xxxx)?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
...
this
RelativeLayout layout1 = (RelativeLayout) findViewById(R.layout.game);
should be
RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.relative_id);
R.id... used for mapping control and RelativeLayout is a control.
I think you're getting a blank screen because you haven't set the content view.
What i mean is that the codes does what it's supposed to do however, you should remove the "setContentView()" method at the top and place it at the end, then you should set it to the RelativeLayout before you close the onCreate() method! Something like this:
public void create() {
RelativeLayout layout1 = new RelativeLayout(this);
for(int i = 0; i < gridSize; i++) {
if(grid[i] == 0) { //if grid pos. indicates an empty cell
Button empty = new Button(this);
empty.setBackgroundResource(R.drawable.emptybutton); //set background to empty
empty.setId(i); //set id to value of i
empty.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layout1.addView(empty); //add the button to the relativeLayout view
//((Button) findViewById(i)).setOnClickListener(emptyListener);
}
}
setContentView(layout1);
}
Also notice that I've changed the declaration of the Relativelayout a little bit.
I hope this helps. :) !
You must enter the ID of the RelativeLayout, not the xml fil name.
try with
RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.yourRelativeLayoutViewID);