I have a simple GridView. The following is the XML
<LinearLayout
android:id="#+id/calendar_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:orientation="vertical" >
<GridView
android:id="#+id/grid_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="7"
android:verticalSpacing="2dp"
android:rotationY="180" >
</GridView>
</LinearLayout>
I create a TextView and insert it as an item in the gridview. Basically the idea is to create a customized calendar.
In the adaptor I have
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = new TextView(mContext);
textView.setText(days.get(position).toString());
textView.setRotationY(180);
textView.setGravity(Gravity.CENTER);
textView.setBackground(mContext.getResources().getDrawable(R.drawable.grey_box));
int x = mContext.getResources().getDimensionPixelSize(R.dimen.calendar_slot);
textView.setLayoutParams(new GridView.LayoutParams(x, x));
return textView;
}
R.dimen.calendar_slot equals to 30dp.
What I fail to understand is , given the above why does my gridview appear like below ? . I need the columns to be merged together. But they have spaces between them.
Can anyone aid ?
The reason why there is a lot of space between the columns of gridview is that the textview in your layout is not occupying the complete column space provided by the grid view
To avoid this problem , you have to calculate the device's screen width and height and divide it by number of columns and rows respectively. This will give you the exact width and height you needed for your single textview.Set this as the dimensions of your text view. You will get equal space between your rows and columns
The code will be as follows
DisplayMetrics displayMetrics=getResources().getDisplayMetrics();
screen_width=displayMetrics.widthPixels; //width of the device screen
screen_height=displayMetrics.heightPixels; //height of device screen
int view_width=screen_width/columns; //width for text view
int view_height=screen_height/rows; //height for text view
textview.getgetLayoutParams().width=view_width;
textview.getgetLayoutParams().height=view_height;
Related
I have 4 imageview in one rows but i wanted to visible and gone need to do based on response. so how to remove unused space which i hardcoded layout.
<ImageView
android:id="#+id/imgFacebookUrl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/facebook_login_guest"
app:layout_anchor="#id/appbar"
android:visibility="gone"
app:layout_anchorGravity="bottom|center" />
<ImageView
android:id="#+id/imgLinkdinUrl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="200dp"
android:src="#drawable/icon_linkdin"
app:layout_anchor="#id/appbar"
android:visibility="gone"
app:layout_anchorGravity="bottom|center" />
public void setPadding (int left, int top, int right, int bottom)
Sets the padding. The view may add on the space required to display
the scrollbars, depending on the style and visibility of the
scrollbars. So the values returned from getPaddingLeft(),
getPaddingTop(), getPaddingRight() and getPaddingBottom() may be
different from the values set in this call.
Finally
ImageView ImageViewObj = (ImageView) findViewById(R.id.imgLinkdinUrl);
ImageViewObj.setPadding(0, 0, 0, 0);
This deletes all paddings (all sides) for your imgLinkdinUrl ImageView:
ImageView linkedInUrlImage = (ImageView) findViewById(R.id.imgLinkdinUrl);
linkedInUrlImage.setPadding(0, 0, 0, 0);
I have a page that consists of an ImageView, and a RecyclerView. The RecyclerView contains a small number of items (currently three) and only takes up around a quarter of the screen on my test device. However, despite trying numerous layout options, I cannot get the RecyclerView to effectively wrap its content and take up just enough space required to contain these three rows, and leave the rest of the space for my ImageView.
To help illustrate what I mean, I have drawn two diagrams. The first shows what I would like to happen, and the second what is happening:
So far, I have tried several different combinations of RelativeLayout - for instance, I will set RecyclerView to layout:align_ParentBottom and the second RelativeLayout that contains the ImageView to layout:alignComponent so that its bottom matches the RecyclerView top (this would normally drag the ImageView layout so that it fills any reminaing space, which is what I would like to happen.)
The RecyclerView though just keeps occupying all the space it can, even though it only contains a few rows. The current "solution" I have is to place everything inside a LinearLayout and set less gravity to the RecyclerView, but it isn't ideal, because on different emulators it wont line up completely with the bottom of the screen, and in others there isn't enough room and the RecyclerView becomes scrollable.
Thanks in advance for any help and suggestions anyone can offer.
Many thanks to everyone who contributed, but I have found a programmatic solution outside of the layout files. In case anyone else is looking for a solution to this problem, I found one here.
It appears as if there is an issue with RecyclerView currently where it doesn't wrap content. The answer is to construct a custom class that extends LinearLayoutManager. I have posted the solution that worked for me below - most of it is copy and pasted from the answer given in the link I quoted. The only small issue is that it doesn't account for the extra space added by decorations, which is why I had to make a small tweak to the following line near the end of the code:
//I added the =2 at the end.
measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin + 2;
Here is the code in its entirety:
public class HomeLinearLayoutManager extends LinearLayoutManager{
HomeLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
private int[] mMeasuredDimension = new int[2];
#Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
int widthSpec, int heightSpec) {
final int widthMode = View.MeasureSpec.getMode(widthSpec);
final int heightMode = View.MeasureSpec.getMode(heightSpec);
final int widthSize = View.MeasureSpec.getSize(widthSpec);
final int heightSize = View.MeasureSpec.getSize(heightSpec);
int width = 0;
int height = 0;
for (int i = 0; i < getItemCount(); i++) {
measureScrapChild(recycler, i,
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
mMeasuredDimension);
if (getOrientation() == HORIZONTAL) {
width = width + mMeasuredDimension[0];
if (i == 0) {
height = mMeasuredDimension[1];
}
} else {
height = height + mMeasuredDimension[1];
if (i == 0) {
width = mMeasuredDimension[0];
}
}
}
switch (widthMode) {
case View.MeasureSpec.EXACTLY:
width = widthSize;
case View.MeasureSpec.AT_MOST:
case View.MeasureSpec.UNSPECIFIED:
}
switch (heightMode) {
case View.MeasureSpec.EXACTLY:
height = heightSize;
case View.MeasureSpec.AT_MOST:
case View.MeasureSpec.UNSPECIFIED:
}
setMeasuredDimension(width, height);
}
private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
int heightSpec, int[] measuredDimension) {
View view = recycler.getViewForPosition(position);
if (view != null) {
RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
getPaddingLeft() + getPaddingRight(), p.width);
int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
getPaddingTop() + getPaddingBottom(), p.height);
view.measure(childWidthSpec, childHeightSpec);
measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;
measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin + 2;
recycler.recycleView(view);
}
}
}
Thanks again.
Put the two in a RelativeLayout and make ImageView fill parent:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<RecyclerView
android:id="#+id/recyclerView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/recyclerView"
/>
</RelativeLayout>
Edit: Wrote TextView by accident. Fixed.
The only solution I can think of is using layout weight. Specify 70% of the screen for the image and 30% for the Recyclerview as you said you have just 3 rows. Use adjustViewByBounds to ensure the images aspect ratio is maintained.
My code below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:src="#drawable/ic_round_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_weight=".9"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=".1"/>
</LinearLayout>
How can I add a int value to just one cell in a gridView? for example, using only the first cells and assigning them a value . Is there a way to do that?
This is my grid in the xml:
<GridView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:numColumns="3" >
</GridView>
this is my java:
int first = 5;
int second = 6
GridView grid = (GridView) findViewById(R.id.grid);
//I'm missing here a line something like: grid.setContentOfCell (1, first); grid.setContentOfCell (2, second)
I want to assign the int A to the first cell in the grid and B to the second.
In GridView getView Method Check the position is grater than 4 and also put other Cell value according to postion value that you want.
public View getView(int position, View convertView, ViewGroup arg2) {
if(position>3)
{
productPriceTextView.setText("1-4")
}else if(position==a)
Display your value
}else if(position==b)
Display your value
}
Thanks
I am working on an android application.
In an XML Layout I need to do the following:
I have a list view at the top (listViewProducts) , and another Relative view under it (receiptSection).
The list view should take as much space as it has items. And the rest is taken by the receiptSection.
So for example if I have 2 items in the listViewProducts:
The list view is as big as the 2 items and the rest is taken by the receiptView.
If I add another item, the list view now take more space and push the receiptView lower:
However if I add a lot more items, I want the list view height to stop growing to leave a minimum height for the receiptView that cannot go smaller:
As you see in the picture, the receiptVIew has a minimum height of 50dp. once the receipt view get to that height, it should stop shrinking and now the list view has a fixed size based on the remaining of the space. The rest will be scrollable.
What I have tried
I created a list view. I have android:layout_alignParentTop="true" and android:layout_height="wrap_content".
This will make it grow with its content and its at the top of the view
<ListView
android:id="#+id/listViewProducts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
</ListView>
THen I created a RelativeLayout that will hold the checkout_receipt_view that is in a seperate xml layout file.
For this view I have android:layout_alignParentBottom="true" and android:layout_below="#id/listViewProducts" that will make it go under the list view and align with the bottom of the view.
I also used android:minHeight="50d" in order to set the minimum height of the receiptSection.
<RelativeLayout
android:id="#+id/receiptSection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_below="#id/listViewProducts"
android:minHeight="50dp" >
<include layout="#layout/checkout_receipt_view" />
</RelativeLayout>
The listViewProducts is growing with the items, and the receiptView is taking the remaining space correctly.
The problem
however the minimum height did not work. The list view keeps on growing infinitely and the receiptSection will be pushed out of the view.
Is there a way I can make the listView stop growing when the receiptView reaches 50dp?
Thanks a lot for any help.
Unfortunately I think your best bet is to do this by making a custom view that extends ListView and overrides onMeasure.
public class CustomView extends ListView {
#Override
int maxHeight = 0;
View parentView = (RelativeLayout) (or whatever) getParent();
if (parentView != null){
maxHeight = parentView.getHeight() - 50;
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
Try swapping the 'layout_below'.
What you are actually saying is the following: please put my relativelayout BELOW the listview. If you want your listview to respect the height of the relativelayout, you'll have to say in the listview:
<ListView
android:id="#+id/listViewProducts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/receiptSection"
android:layout_alignParentTop="true" >
</ListView>
And your relativelayout:
<RelativeLayout
android:id="#+id/receiptSection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:minHeight="50dp" >
<include layout="#layout/checkout_receipt_view" />
</RelativeLayout>
I have a Table Layout defined within LinearLayout as follows:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#E6E6E6"
android:orientation="vertical" >
<TableLayout
android:id="#+id/fbTableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#android:color/darker_gray"
android:stretchColumns="*" >
</TableLayout>
</LinearLayout>
I am adding dynamic rows to the TableLayout as follows:
fbTableLayout = (TableLayout) findViewById(R.id.fbTableLayout);
for (int i = 0; i < 4; i++) {
fbTableRow = new TableRow(this);
fbTableRow.setBackgroundColor(Color.WHITE);
LayoutParams layoutParams = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
int leftMargin=10;
int topMargin=2;
int rightMargin=10;
int bottomMargin=2;
layoutParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
fbTableRow.setLayoutParams(layoutParams);
ImageView iv = new ImageView(this);
iv.setBackgroundDrawable(getResources().getDrawable(
R.drawable.ic_launcher));
iv.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT,
0.25f));
TextView tv = new TextView(this);
tv.setText("Album "+ i);
tv.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT,
0.75f));
fbTableRow.addView(iv);
fbTableRow.addView(tv);
fbTableLayout.addView(fbTableRow, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
But I am not able to generate spaces between the rows generated. The layout is as shown in the figure attached.
I have gone through a number of solutions given in stackoverflow to resolve this issue but none of them are working for me. Not sure what I am missing.
Any help would be greatly appreciated.
Thanks in advance.
Add another table row (in the loop) with a blank image. Specify the size of the image to create the size needed for your space.
Have you tried this when you're adding the table row?
fbTableLayout.addView(fbTableRow, layoutParams);
If not, you can try setting the margins on the individual views within the table row, but I'm pretty sure the above should apply the layout params when the row is being added to the table layout.
The docs for the setMargins method include this note:
A call to requestLayout() needs to be done so that the new margins are taken into account. Left and right margins may be overriden by requestLayout() depending on layout direction.
...so one thing to try would be to call fbTableLayout.requestLayout() after you've added all the rows. This probably won't make a difference, since the view should be getting invalidated in the code you've already posted, but it wouldn't hurt to try.
If that doesn't work, you could use another viewgroup (e.g. a FrameLayout) within each table row to contain your ImageView and TextView and set your padding there.