I am trying to add an image to a LinearLayout each time the user selects an item from an AlertDialog modal but once the first item is selected, no other images will appear and the first image doesn't even change to the newly selected item. Ideally, i would like the first image to appear in the center of the layout as it does and when a second choice is selected, I would like it to appear to the right of the first image and then both are centered.
Thanks to anyone looking at this.
Below is the ImageView and LinearLayout portion of my code. This code is in the onClick() method for the AlertDialog
ImageView imageView = new ImageView(getApplicationContext());
LinearLayout l = (LinearLayout) findViewById(R.id.picLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
imageView.setId(which);
imageView.setLayoutParams(params);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
int pic = Pic.getPicID(which);
imageView.setImageResource(pic);
l.addView(imageView, items.size()-1, params);
Related
I'm creating the above popup, the content of which consists of rows of horizontal LinearLayout views within a main vertical LinearLayout. Each horizontal LinearLayout contains one ImageView and one TextView.
I'm creating this within a PopupWindow, and doing so programmatically so that I can change the ImageView source as required.
As you can see the first icon seems to take up a lot of space, despite having the same code generating it as the other icons.
Below is the code:
LinearLayout verticalLayout = new LinearLayout(context);
verticalLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams mainLayoutParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
verticalLayout.setLayoutParams(mainLayoutParams);
LinearLayout.LayoutParams layoutParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams iconParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams textParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//History row
LinearLayout historyLayout = new LinearLayout(context);
historyLayout.setLayoutParams(layoutParams);
historyLayout.setOrientation(LinearLayout.HORIZONTAL);
ImageView historyIcon = new ImageView(context);
historyIcon.setImageResource(R.drawable.small_book_grey);
historyIcon.setAdjustViewBounds(true);
historyIcon.setLayoutParams(iconParams);
historyLayout.addView(historyIcon);
TextView historyText = new TextView(context);
historyText.setLayoutParams(textParams);
historyText.setText("History");
historyLayout.addView(historyText);
verticalLayout.addView(historyLayout);
//Exam row...
//... (duplicate of history row)
I've tried playing around with the layout parameters, even creating a mock xml layout that displays the content as I'd like, to match the parameters to.
If anyone can give some advice on making that book icon the same size as the others, I'd be grateful.
Add a scaleType to ImageView of fitCenter
Write this code under historyIcon.setAdjustViewBounds(true);
historyIcon.setWidth()`
And put width according to your layout.
Although I didn't figure out why the first image was scaling differently to the other images, I did find another solution: Using compound left drawables.
historyText.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Drawable img = m_context.getResources().getDrawable(
R.drawable.small_book_grey);
img.setBounds(0, 0, img.getIntrinsicWidth() * historyText.getMeasuredHeight() / img.getIntrinsicHeight(), historyText.getMeasuredHeight());
historyText.setCompoundDrawables(img, null, null, null);
historyText.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
Manually setting the bounds to match the TextView worked. Seems clunky, but it was the only way I could get it to do what I was aiming for.
My android project connected to server.And i retrieve data from server if it's 5 then create 5 imageview in linearlayout in other words automatically create imageview, depends on retrieved data.
This code Loops over number of images and creates new ImageView for each time, and then it adds them to the Parent Linear Layout. You can also set the LayoutParams of ImageView dynamically as well.
LinearLayout layout = (LinearLayout)findViewById(R.id.parentLayout);
for(int i=0;i<number_of_images;i++) {
ImageView image = new ImageView(context);
layout.addView(image);
}
For Linear Layout:
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
for(int i=0;i<Number_Images;i++){
//ImageView Setup
ImageView i mageView = new ImageView(this);
//setting image resource
imageView.setImageResource(R.drawable.play);
//setting 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);
Hope this Help You.
I want to display image and text at runtime of the activity.
Similarly just like chat happening in WHATSAPP.
I displaying the text in textview and image in imageview.
The problem i am facing is I want to show both of them on the same activity with scroll
For eg:
image
text
text
image
text
image
image
Which layout or control should i use to display like this?
add both like this..
LinearLayout linearLayout=new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams vp=new
LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(this);
tv.settext("test");
linearLayout.addView(imageView,tv);
ImageView imageView= new ImageView(this);
imageView.setImageBitmap(bitMap);
imageView.setVisibility(View.VISIBLE);
imageView.setBackgroundColor(0xFFFF00FF);
//linearLayout=LinearLayout+imageView;
LinearLayout.LayoutParams Iv=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
linearLayout.addView(imageView,Iv);
setContentView(linearLayout,vp);
I am creating a imageview at runtime and now want a add a button on top of the Imageview dynamically at run time and if I pan the image in the imageview the button in it should also pan accordingly. The problem is that Imageview does not allow to add subviews in it.
I am able to add the button on top of the imageview but when I pan the imageview the button remains constant in the screen. Any idea on how to do this?
Here is what I did:
panImageView = new PanView(this); //Custom Imageview class to handle panning
panImageView.setScaleType(ImageView.ScaleType.CENTER);
//panimg.setAdjustViewBounds(true);
FrameLayout mainRL = (FrameLayout) findViewById(R.id.mainRelativeLayout);
mainRL.addView(panImageView);
ImageWorker imgTask = new ImageWorker(panImageView, is);
imgTask.execute(2000, 2000);
//Start button
Button startButton = new Button(this);
int iRndBtn = R.drawable.round_button;
startButton.setBackgroundResource(iRndBtn);
startButton.getBackground().setColorFilter(Color.GREEN,PorterDuff.Mode.MULTIPLY);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(50, 50);
//layoutParams.setMargins(intialPage.mapStartX, intialPage.mapStartY, 0, 0);
layoutParams.setMargins(200, 200, 0, 0);
startButton.setLayoutParams(layoutParams);
//startButton.
mainRL.addView(startButton,layoutParams);
Add Image view first in any Layout and the add Button in that Layout.
Use a RelativeLayout with the ImageView & button. set the button visibility to gone at start. When you need change the visibility.
I have to develop five to six horizontal scrolling view,in this scrolling image add dynamic using adapter.
Feature:
1) Image can be drag and drop one scroll view to another
2) image can be move one horizontal scrolling to another view
3) image can be selected/unselected
4) with with all version
i am using this library, but sometime scrolling is not that much smooth(chopping)
Check this links may be it's useful to you.
1) Insert a view dynamically in a HorizontalScrollView in Android
2) http://android-er.blogspot.in/2012/07/implement-gallery-like.html
3) Horizontal ListView like Google Catalogs
Using this code you can add android control programatically to linear layout and just add Horizontal Scrollview to Linear Layout through xml
You will get horizontally listview.
//My coding here.
String[] name={"PRASHANT","PRASHANT","PRASHANT","PRASHANT","PRASHANT","PRASHANT","PRASHANT"} ;
myLInearLayoutmain =(LinearLayout) findViewById(R.id.linearLayoutmain);
for(int i =0;i<6;i++)
{
LinearLayout li=new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
li.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams paramsnew = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
params1.setMargins(30, 20, 30, 0);
//add textView
valueTV = new TextView(this);
valueTV.setText(""+name[i]);
valueTV.setId(5);
valueTV.setLayoutParams(paramsnew);
valueTV.setGravity(Gravity.CENTER);
// adding Button to linear
valueB = new Button(this);
valueB.setText(""+name[i]);
valueB.setId(i);
valueB.setLayoutParams(params);
valueB.setOnClickListener(this);
valueB.setGravity(Gravity.CENTER);
// adding Imageto linear
img = new ImageView(this);
img.setImageResource(R.drawable.ic_launcher);
img.setLayoutParams(paramsnew);
//add the textView and the Button to LinearLayout
li.addView(valueTV);
li.addView(valueB);
li.addView(img);
li.setLayoutParams(params1);
myLInearLayoutmain.addView(li);
}