i am Programically trying to add imageViews into a Linear Layout as follows:
LinearLayout.LayoutParams layoutParamsTV = new LinearLayout.LayoutParams(100, 100);
for( i = 0; i < 5; i++) {
LinearLayout llv = new LinearLayout(this);
llv.setOrientation(LinearLayout.VERTICAL);
ImageView row1 = new ImageView(this);
row1.getLayoutParams().height=50; // getting error here
}
How to set the height and width of the ImageView and i want to add one more image view right over this imageView , how to do that
Try this:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(640, 480); // width , height
row1.setLayoutParams(params); // row1 is your ImageView
Related
i m using 4 imageview in relativelayout and that images size equal to each other in relative layout but want to make dynamically but i m not getting
Heading
final ImageView profile_img1 = (ImageView) view.findViewById(R.id.profile_img1);
final ImageView profile_img2 = view.findViewById(R.id.profile_img2);
Display display = activity.getWindowManager().getDefaultDisplay();
Point point = new Point();
display.getSize(point);
int width = point.x;
final double margin_15 = width * 0.15;
final RelativeLayout relativeLayout=view.findViewById(R.id.relative_profile);
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
parms.setMargins((int) margin_15, 0, (int) margin_15, 0);
relativeLayout.setLayoutParams(parms);
// Toast.makeText(activity,width+"",Toast.LENGTH_SHORT).show();
relativeLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
relativeLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
width_relativelayout = relativeLayout.getMeasuredWidth();
h_relativelayout = relativeLayout.getMeasuredHeight();
// Toast.makeText(activity,width_linearlayout+"===",Toast.LENGTH_SHORT).show();
RelativeLayout.LayoutParams parms_img = new RelativeLayout.LayoutParams( width_relativelayout/2,350);
parms_img.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
profile_img1.setLayoutParams(parms_img);
RelativeLayout.LayoutParams parms_img2 = new RelativeLayout.LayoutParams(width_relativelayout/2,300);
parms_img.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
profile_img1.setLayoutParams(parms_img2);
profile_img2.setLayoutParams(parms_img2);
}
});
You can use RecyclerView and Adapter for same.
and Set GridLayoutManager with number count to show, like 2 for two image view horizontally.
Or you can ArrayList of ImageView.
I add two Checkboxes dynamically to a Linearlayout. Then those Linearlayouts are added one after another in a Relativelayout. The weights of the checkboxes are set so that each take 50% of the Linearlayout width. Now, if their heights do not match, the bottom of the checkbox with bigger height disappears. How to solve this? Here's a screenshot:
And the code:
LinearLayout ll;
LinearLayout.LayoutParams lp;
CheckBox ch;
int id = 1200, i, j;
for (i = 0, j = 0; i < selections.size() - 1; i += 2, j += 2) {
ll = new LinearLayout(NotificationSettings.this);
lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ch = new CheckBox(NotificationSettings.this);
lp.weight = 1.0f;
ch.setLayoutParams(lp);
ch.setText(selections.get(i));
ch.setChecked(isSelected);
ch.setTextColor(color);
ch.setId(j);
ll.addView(ch);
ch = new CheckBox(NotificationSettings.this);
ch.setLayoutParams(lp);
ch.setText(selections.get(i + 1));
ch.setChecked(isSelected);
ch.setTextColor(color);
ch.setId(j + 1);
ll.addView(ch);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
if (id == 1200)
p.addRule(RelativeLayout.BELOW, addBelow);
else
p.addRule(RelativeLayout.BELOW, id);
ll.setLayoutParams(p);
ll.setId(++id);
rl.addView(ll);
}
Edit:
When both checkboxes have multiple lines:
Can you make sure that the Linear Layout's height below it is not too large that it covers the above Linear Layout?
Or try changing your Relative Layout Params' height to WRAP_CONTENT
Change
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
To
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
A HorizontalScrollView is placed so that it is not taking all the horizon space.
When scrolling, the children are visible even when outside of the HorizontalScrollView boundaries, on top of the padding, on top of the margin, outside the card that contains the HorizontalScrollView, on top of that margin and padding.
The children are added programmatically, an ImageView and TextView inside a vertical LinearLayout.
I expected views to be masked when outside the boundaries of their ScrollView parent, but this is not happening.
This is the code that creates the children.
int pad = getResources().getDimensionPixelSize(R.dimen.element_margin);
int imageSize = Misc.dpToPx(30, this);
for(int i = 0; i < 6 ; i++){
LinearLayout layout = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( 3 * imageSize, LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setGravity(Gravity.CENTER_HORIZONTAL);
layout.setPadding(pad, 0, pad,0);
TintedIcon iv = new TintedIcon(layout.getContext());
TextView tv = new TextView(layout.getContext());
tv.setText("ארוחת בוקר");
tv.setTextAppearance(this, android.R.style.TextAppearance_Small);
tv.setTextColor(getResources().getColor(R.color.FontDark));
//init image
Drawable drawable = getResources().getDrawable(R.mipmap.breakfast);
iv.setTintedDrawable(drawable, getResources().getColor(R.color.iconDark));
iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
iv.setAdjustViewBounds(true);
layout.addView(iv, imageSize, imageSize);
layout.addView(tv, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
features.addView(layout, lp);
}
TintedIcon is a view that extends ImageView with a function that gives tint to an image using mutate() on its drawable.
I'm trying to create, resize and add an imageView and I've been looking for a solution to this all over but nothing works properly, every time I set any kind of Param the image doesn't appear anymore or the app crashes, I'm using Android Studio 1.0.1 and this are the methods I've tried:
This one the image disapear
TableRow tableInfoCont = new TableRow(MainActivity.this);
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(R.drawable.simpleAvatar);
LinearLayout.LayoutParams imgPar = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
imgPar.width = 50;
imgPar.height = 50;
imgPar.setMargins(5,5,5,5);
imageView.setLayoutParams(imgPar);
tableInfoCont.addView(imageView);
This one the image disapear
TableRow tableInfoCont = new TableRow(MainActivity.this);
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(R.drawable.simpleAvatar);
FrameLayout.LayoutParams imgPar = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT);
imgPar.width = 50;
imgPar.height = 50;
imgPar.setMargins(5,5,5,5);
imageView.setLayoutParams(imgPar);
tableInfoCont.addView(imageView);
This one the app crashes
TableRow tableInfoCont = new TableRow(MainActivity.this);
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(R.drawable.simpleAvatar);
ViewGroup.LayoutParams imgPar = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
imgPar.width = 50;
imgPar.height = 50;
//imgPar.setMargins(5,5,5,5); //not possible to set margin
imageView.setLayoutParams(imgPar);
tableInfoCont.addView(imageView);
This one the app crashes
TableRow tableInfoCont = new TableRow(MainActivity.this);
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(R.drawable.simpleAvatar);
imageView.getLayoutParams().width = 50;
imageView.getLayoutParams().height = 50;
tableInfoCont.addView(imageView);
That is it, tryed all this methods... Any idea what am I missing here?
Keep in mind that is discouraged to create Layout in code.
Also try to let a simple ImageView show up first before you go with a TableView.
Try this code :
TableLayout tableLayout = new TableLayout(this);
TableRow tableRow = new TableRow(this);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.simpleAvatar);
tableRow.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
tableRow.addView(imageView);
tableLayout.addView(tableRow);
setContentView(tableLayout);
Take into consideration that in such expressions you specify height and width in pixels, not in dp. Your picture may disappear just because it's wider or higher than your tablerow is (check your tablerow's layout parameters). Set your image's width and height to match_parent and check up how it feels.
Try using imageView.requestLayout() after you commit some changes in LayoutParams. This makes the View re-read the layout for changes.
I have the following code:
LinearLayout lyt = (LinearLayout)findViewById(R.id.mylayout);
for(i=0;i<=3 ;i++) {
ImageButton ib= new ImageButton(this);
BitmapDrawable imagebd;
ib.setClickable(true);
imageid = getResources().getIdentifier("drawable/" + image,null,getPackageName());
ib.setBackgroundResource(imageid);
lyt.addView(ib);
}
all the images are show vertically i want it to be horizontal
Try this code:
LinearLayout lyt = (LinearLayout) findViewById(R.id.mylayout);
lyt.setOrientation(LinearLayout.HORIZONTAL);
for(i=0;i<=3 ;i++) {
ImageButton ib= new ImageButton(this);
BitmapDrawable imagebd;
ib.setClickable(true);
imageid = getResources().getIdentifier("drawable/" + image,null,getPackageName());
ib.setBackgroundResource(imageid);
lyt.addView(ib);
}
Avoid setting attributes in your Java code when it can be done via XML. Try setting the orientation attribute in your ListView:
<LinearLayout
...
android:orientation="horizontal"
...>
</LinearLayout>
Try it like that:
LinearLayout lyt = (LinearLayout)findViewById(R.id.mylayout);
LinearLayout buttonsLinearLayout = new LinearLayout(context);
buttonsLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
for(i=0;i<=3 ;i++)
{
ImageButton ib= new ImageButton(this);
BitmapDrawable imagebd;
ib.setClickable(true);
imageid = getResources().getIdentifier("drawable/" + image,null,getPackageName());
ib.setBackgroundResource(imageid);
buttonsLinearLayout.addView(ib);
}
lyt.addView(buttonsLinearLayout);
That way, you still have your main vertical linearlayout, so you can put stuff under the buttons.
EDIT:
to use it for more than 1 row, i did a simple math calc...
LinearLayout lyt = (LinearLayout) findViewById(R.id.mylayout);
// calculate the number of rows needed
int numOfButtons = something you already have i guess;
// row layout
LinearLayout buttonsLinearLayout = new LinearLayout(context);
;
for (i = 0; i <= numOfButtons; i++) {
// for every 3 rows, create a new layout
// and add it to the main linear layout
if (i % 3 == 0) {
// create layout for a 3 button row
buttonsLinearLayout = new LinearLayout(context);
buttonsLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
// add the new row with 3 buttons to the main lineal layout
lyt.addView(buttonsLinearLayout);
}
ImageButton ib = new ImageButton(this);
BitmapDrawable imagebd;
ib.setClickable(true);
imageid = getResources().getIdentifier("drawable/" + image, null, getPackageName());
ib.setBackgroundResource(imageid);
buttonsLinearLayout.addView(ib);
}
}
not tested, let me know if that worked for you...