Im building an android app which needs to create max of 1-4 imageviews depending on the number of images i need to display on the screen. I have written a small code but it doesn't seem to divide the screen equally on to 4 imageviews though. The top 2 are a little larger than the bottom two in my case. Following is my code i really cant think what the issue is
private void CreateScreen()
{
int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
int matrixN = 2;
int maxWForEachScreen = screenWidth / matrixN;
int maxHForEachScreen = screenHeight / matrixN;
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayoutFullScreen);
ImageView imgView1 = new ImageView(LiveViewer.this);
ImageView imgView2 = new ImageView(LiveViewer.this);
ImageView imgView3 = new ImageView(LiveViewer.this);
ImageView imgView4 = new ImageView(LiveViewer.this);
int size = 3;
if(size >= 3 && size <= 4)
{
imgView1.setImageResource(R.drawable.icon);
imgView2.setImageResource(R.drawable.icon);
imgView3.setImageResource(R.drawable.icon);
imgView4.setImageResource(R.drawable.icon);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(100, 100);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(100, 100);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(100, 100);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(100, 100);
params1.height = maxHForEachScreen/2;
params1.width = maxWForEachScreen/2;
params2.height = maxHForEachScreen/2;
params2.width = maxWForEachScreen/2;
params3.height = maxHForEachScreen/2;
params3.width = maxWForEachScreen/2;
params4.height = maxHForEachScreen/2;
params4.width = maxWForEachScreen/2;
params1.setMargins(0, 0, 0, 0);
params2.setMargins(maxWForEachScreen/2, 0, 0, 0);
params3.setMargins(0, maxHForEachScreen/2, 0, 0);
params4.setMargins(maxWForEachScreen/2, maxHForEachScreen/2, 0, 0);
relativeLayout.addView(imgView1, params1);
relativeLayout.addView(imgView2, params2);
relativeLayout.addView(imgView3, params3);
relativeLayout.addView(imgView4, params4);
}
}
You can set the weightsum of the parentLayout equal to the number of views.. And set 1 as weight of every view...This will make each views occupy equal space..
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.
Hi i am trying to add TextView with drawableLeft to GridLayout.
I am adding this TextView in an Loop. The TextView are getting added properly but the are not aligned properly. Each textview should take equal width in one horizontal row which is not happening.
Following is the code i am using
GridLayout gridLayout = new GridLayout(getContext());
gridLayout.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
gridLayout.setColumnCount(2);
gridLayout.setRowCount(3);
TextView titleText;
for (int i = 0; facilities != null && i < facilities.size(); i++) {
titleText = new TextView(getContext());
titleText.setText(facilities.get(i));
gridLayout.addView(titleText, i);
titleText.setCompoundDrawablesWithIntrinsicBounds(rightIc, 0, 0, 0);
}
For this you have to dynamically set the column width for the views. This will finally align each view properly with equal amount of space.
GridLayout gridLayout = new GridLayout(getContext());
gridLayout.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
gridLayout.setColumnCount(2);
gridLayout.setRowCount(3);
TextView titleText;
for (int i = 0; facilities != null && i < facilities.size(); i++) {
titleText = new TextView(getContext());
titleText.setText(facilities.get(i));
gridLayout.addView(titleText, i);
titleText.setCompoundDrawablesWithIntrinsicBounds(rightIc, 0, 0, 0);
GridLayout.LayoutParams param =new GridLayout.LayoutParams();
param.height = LayoutParams.WRAP_CONTENT;
param.width = LayoutParams.WRAP_CONTENT;
param.rightMargin = 5;
param.topMargin = 5;
param.setGravity(Gravity.CENTER);
param.columnSpec = GridLayout.spec(c);
param.rowSpec = GridLayout.spec(r);
titleText.setLayoutParams (param);
}
The following code sample should give each text view equal height and width, and order the TextViews left-to-right and then top-to-bottom.
The critical part is explicitly providing the GridLayout.LayoutParams, setting height/width to 0 and defining the row/column specs with weights set to 1 so that the height and width will be automatically calculated based on the weights.
Also notice I set the number of rows as a function of the number of facilities, so that if your list grows you'll have more rows.
if (facilities == null) {
// In this case there is nothing to display. You can adjust this part to your needs.
return;
}
GridLayout gridLayout = new GridLayout(getContext());
gridLayout.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
gridLayout.setColumnCount(2);
gridLayout.setRowCount(facilities.size() / 2);
for (int i = 0; i < facilities.size(); i++) {
TextView titleText = new TextView(getContext());
titleText.setText(facilities.get(i));
titleText.setCompoundDrawablesWithIntrinsicBounds(rightIc, 0, 0, 0);
GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
layoutParams.height = 0;
layoutParams.width = 0;
int currentCol = i % 2;
int currentRow = i / 2;
// The last parameter in the specs is the weight, which gives equal size to the cells
layoutParams.columnSpec = GridLayout.spec(currentCol, 1, 1);
layoutParams.rowSpec = GridLayout.spec(currentRow, 1, 1);
// Optional, if you want the text to be centered within the cell
layoutParams.setGravity(Gravity.CENTER);
gridLayout.addView(titleText, layoutParams);
}
Basically that is the column and row count. I have re wrotten the complete logic
GridLayout gridLayout = new GridLayout(getContext());
int total = facilities.size();
int column = 2;
int row = total / column;
gridLayout.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
gridLayout.setColumnCount(column);
gridLayout.setRowCount(row + 1);
TextView titleText;
for(int i =0, c = 0, r = 0; i < total; i++, c++)
{
if(c == column)
{
c = 0;
r++;
}
titleText = new TextView(getContext());
titleText.setText(facilities.get(i));
gridLayout.addView(titleText, i);
titleText.setCompoundDrawablesWithIntrinsicBounds(rightIc, 0, 0, 0);
GridLayout.LayoutParams param =new GridLayout.LayoutParams();
param.height = LayoutParams.WRAP_CONTENT;
param.width = LayoutParams.WRAP_CONTENT;
param.rightMargin = 5;
param.topMargin = 5;
param.setGravity(Gravity.CENTER);
param.columnSpec = GridLayout.spec(c);
param.rowSpec = GridLayout.spec(r);
titleText.setLayoutParams (param);
}
That's may be because of your dynamic text length is not fixed so each textview not take same space Check this
int height=getContext().getResources().getDimension(R.dimen.activity_horizontal_margin); //set size of dimen in required resolution
titleText .setLayoutParams(new LinearLayout.LayoutParams(0, height, height));
My solution:
GridLayout gl = findViewById( R.id.grid_layout );
TextView tv = new TextView( this );
tv.setText( ""+cursor.getInt( column ) ); //for example
GridLayout.LayoutParams lp = new GridLayout.LayoutParams();
lp.columnSpec = GridLayout.spec( GridLayout.UNDEFINED, 1, GridLayout.FILL ); //for stretch a child to column use GridLayout.FILL
gl.addView( tv, lp );
//DO NOT USE lp.setGravity( ... );
//FOR ALIGN TEXT USE tv.setTextAlignment( ... );
I'm trying to dynamically build relative layouts consisting of an image and a textview at the moment. I have tried building a loop to place the next relative layout below the former one, but I can't really make it work. The end result should be something like this, but I guess that if i figure out how to align below the former one, I can also figure out how to place it right_of the former relative layout.
Any suggestions? This is my code so far:
RelativeLayout container1 = (RelativeLayout) rootView
.findViewById(R.id.main_layout);
for (int i = 0; i < pictures.size(); i++) {
RelativeLayout tile = new RelativeLayout(getActivity());
tile.setId(i);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
params.height = height / 3;
params.width = width / 2;
tile.setLayoutParams(params);
ImageButton ibGood = new ImageButton(getActivity());
ibGood.setId(1);
ibGood.setImageResource(pictures.get(0));
ibGood.setAdjustViewBounds(true);
ibGood.setMaxHeight(height / 3 / 5 * 4);
ibGood.setMaxWidth(width);
ibGood.setScaleType(ScaleType.CENTER_CROP);
ibGood.setPadding(5, 5, 5, 5);
tile.addView(ibGood);
RelativeLayout.LayoutParams tvPriceParam = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
tvPriceParam.addRule(RelativeLayout.BELOW, ibGood.getId());
tvPriceParam.addRule(RelativeLayout.ALIGN_LEFT, ibGood.getId());
TextView tvPrice = new TextView(getActivity());
tvPrice.setId(2);
tvPrice.setHeight(tile.getHeight() / 5);
tvPrice.setWidth(tile.getWidth() / 5 * 2);
tvPrice.setPadding(5, 0, 0, 5);
tvPrice.setText(Integer.toString(price.get(0)));
tile.addView(tvPrice, tvPriceParam);
if (counter == 0) {
container1.addView(tile);
} else if (counter % 2 == 0) {
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lay.addRule(RelativeLayout.BELOW, -1);
tile.setLayoutParams(lay);
container1.addView(tile);
}
counter += 1;
}
you can make it with the custom gridview !! why you are doing this with the relative layout ?
here is the link which you can use to achieve what do you want..
http://www.learn-android-easily.com/2013/09/android-custom-gridview-example.html
http://www.caveofprogramming.com/uncategorized/custom-gridview-with-imageview-and-textview-in-android/
please visit 3 links are there you will get that you want .. with relative layout ..
Thanks,
Madhav
I have this code,
DisplayMetrics display = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(display);
_screenHeight = display.heightPixels;
...
int eFieldTopMargin = (int)(_screenHeight*0.25);
int pFieldTopMargin = (int)(_screenHeight*0.25+90);
int signInButtonTopMargin = (int)(_screenHeight*0.25+180);
RelativeLayout.LayoutParams lParamsForEField = (LayoutParams) eField.getLayoutParams();
RelativeLayout.LayoutParams lParamsForPField = (LayoutParams) pField.getLayoutParams();
RelativeLayout.LayoutParams lParamsForSignInButton = (LayoutParams) signInButton.getLayoutParams();
lParamsForEField.setMargins(0, eFieldTopMargin, 0, 0);
eField.setLayoutParams(lParamsForEField);
lParamsForPField.setMargins(0, pFieldTopMargin, 0, 0);
pField.setLayoutParams(lParamsForPField);
lParamsForSignInButton.setMargins(0, signInButtonTopMargin, 0, 0);
signInButton.setLayoutParams(lParamsForSignInButton);
Could anyone tell me why I get this? Button is above text fields, though its top margin is more.
Add this line
lParamsForSignInButton(ALIGN_PARENT_TOP);
before
signInButton.setLayoutParams(lParamsForSignInButton);
I have a little complicated layout I want to make in my ListView adpater.
Each item in the list is combined of 4 TextView
The first three TextView are basically one sentence, but I want the second TextView not to be ellipsized, and the first and the third I do want to be ellipsized.
The last TextView is need to be positioned to the right side.
so if the all the first three TextView can't be shown completely, the layout should be like this:
`TextView1...` `TextView2` `TextView3...` `TextView4`
My code is working in this case, but in simple case when no ellipsize is necessary, it doesn't and it looks like this:
`TextView1` `TextView2` `TextView3` `TextView4`
Here's my code:
LinearLayout layout = new LinearLayout(m_context);
layout.setGravity(Gravity.CENTER_VERTICAL);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout textLayout = new LinearLayout(m_context);
textLayout.setOrientation(LinearLayout.HORIZONTAL);
textLayout.setGravity(Gravity.CENTER_VERTICAL);
view.m_text1 = new TQTextView(m_context);
view.m_text1.setSingleLine();
view.m_text1.setEllipsize(TruncateAt.END);
view.m_text2 = new TQTextView(m_context);
view.m_text3 = new TQTextView(m_context);
view.m_text3.setSingleLine();
view.m_text3.setEllipsize(TruncateAt.END);
view.m_text1 = new TQTextView(m_context);
textLayout.setWeightSum(2f);
textLayout.addView(view.m_text1, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f));
textLayout.addView(view.m_text2, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0f));
textLayout.addView(view.m_text3, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f));
layout.addView(textLayout, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f));
view.m_text4 = new TQTextView(m_context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0f);
int margin = 10;
params.leftMargin = margin;
params.gravity = Gravity.CENTER_VERTICAL|Gravity.RIGHT;
layout.addView(view.m_text4, params);
I found a solution:
I add the three TextView without special LayoutParams like this:
textLayout.addView(view.m_attackeeName);
textLayout.addView(view.m_text);
textLayout.addView(view.m_attackerName);
and I call the following function in the end of getView function (after setting the texts)
private void updateTexts(View parent, ViewHolder view, Data data){
view.m_text1.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
view.m_text3.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
view.m_text2.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
view.m_text4.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int totalWidth = parent.getWidth() - parent.getPaddingLeft() - parent.getPaddingRight();
totalWidth = totalWidth - view.m_text4.getMeasuredWidth() - view.m_text2.getMeasuredWidth() - m_margin;
int attackerTextWidth = view.m_text1.getMeasuredWidth();
int attackeeTextWidth = view.m_text3.getMeasuredWidth();
if( attackerTextWidth + attackeeTextWidth > totalWidth )
{
int spareWidth = totalWidth / 2 - attackerTextWidth;
if( spareWidth > 0 )
{
attackeeTextWidth = totalWidth / 2 + spareWidth;
}
else
{
spareWidth = totalWidth / 2 - attackeeTextWidth;
if( spareWidth > 0 )
{
attackerTextWidth = totalWidth / 2 + spareWidth;
}
else
{
attackerTextWidth = totalWidth / 2;
attackeeTextWidth = totalWidth / 2;
}
}
}
String text = TextUtils.ellipsize(data.GetText1(), view.m_text1.getPaint(),
attackerTextWidth, TextUtils.TruncateAt.END).toString();
view.m_text1.setText(text);
text = TextUtils.ellipsize(data.GetText3(), view.m_text3.getPaint(),
attackeeTextWidth, TextUtils.TruncateAt.END).toString();
view.m_text3.setText(text);
}