I have a FrameLayout and a Listview inside the FrameLayout.
I populate the Listview with another Layout.
The problem is if I set the layout_height to wrap_content my getview is getting called multiple time for a single record and if I set the layout_height to fill_parent or wrap_content my getview is being called 3 times for a single record. If I set the layout_height to some dp value it is working fine. I mean I dont have any issues with performance and my getview is getting called only once for a record.
So I get the screen height and width programmatically using
Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
But if I set the width and height of my Listview with the value measured using the above code the last record of my Listview is partially not visible.
I guess this should be because the obtained screen height includes the title bar height, status bar height and notification bar height.
So if that is the prob can someone help me with calculating the available screen height? That is screen height-titlebar height-statusbar height-notification bar height?
else What is the problem? why my last record in my ListView is not fully visible?
XML File
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listview"
android:background="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ListView android:id="#+id/android:list"
android:background="#ffffff"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:clickable="true"
android:fastScrollEnabled="true"
android:smoothScrollbar="true"
/>
</FrameLayout>
Then I set the layout_height and layout_width of the list view in my java code as
Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
FrameLayout.LayoutParams parameters = new FrameLayout.LayoutParams((int)(width),(int)(height));
ListView listview=(ListView) getListView();
listview.setLayoutParams(parameters );
Hi all thanks for your help
I changed my code as below and now its working :)
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height=0;
int width = display.getWidth();
switch (metrics.densityDpi)
{
case DisplayMetrics.DENSITY_HIGH:
height = display.getHeight() - (48*2);
break;
case DisplayMetrics.DENSITY_MEDIUM:
height = display.getHeight() - (32*2);
break;
case DisplayMetrics.DENSITY_LOW:
height = display.getHeight() - (24*2);
break;
default:
height = display.getHeight() - (48*2);
}
FrameLayout.LayoutParams parameters = new FrameLayout.LayoutParams((int)(width),(int)(height));
ListView listview=(ListView) getListView();
listview.setLayoutParams(parameters );
in java:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int s_width,s_height;
s_width = metrics.widthPixels;
s_height = metrics.heightPixels/3;
TableRow.LayoutParams parameters = new TableRow.LayoutParams(s_width,s_height);
LinearLayout b_list_table=(LinearLayout) findViewById(R.id.b_list_table);
b_list_table.setLayoutParams(parameters);
in xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="false"
>
<TableRow
android:layout_width="match_parent"
android:layout_height="180dip"
android:id="#+id/b_list_table"
>
<ListView android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ListView android:id="#+id/listf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</TableRow>
</LinearLayout>
Related
I am new in android development and I have searched this question, but not getting any solution.I want to ask how to make the cardview with recyclerview divide into three pieces in one screen?
this is my XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:id="#+id/card"
android:layout_height="250dp"
app:cardCornerRadius="4dp"
app:cardElevation="1dp"
app:cardMaxElevation="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.github.siyamed.shapeimageview.RoundedImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="#drawable/ic_launcher_background" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
and here is the code I try in Activity
View rootLayout = findViewById(R.id.root_layout);
double height =rootLayout.getLayoutParams().height/3.0;
CardView card= (CardView)findViewById(R.id.card);
CardView.LayoutParams layoutParams= (CardView.LayoutParams) card.getLayoutParams();
layoutParams.height=(int)height;
layoutParams.width=MATCH_PARENT;
Thanks.
try getting height of window and divide it by 3,
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels/3;
CardView card= (CardView)findViewById(R.id.card);
LinearLayout.LayoutParams layoutParams= (LinearLayout.LayoutParams) card.getLayoutParams();
layoutParams.height= height;
layoutParams.width=MATCH_PARENT;
card.setLayoutParams(layoutParams);
Just as #V-rund suggested
Get the Screen/Window height and divide it by 3 using,
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels/3;
CardView card = (CardView)findViewById(R.id.card);
but instead of using CardView.LayoutParams use the LayoutParams for the parent layout of your CardView which in your case will be a LinearLayout.
LinearLayout.LayoutParams layoutParams= (LinearLayout.LayoutParams)
card.getLayoutParams();
layoutParams.height = height;
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
card.setLayoutParams(layoutParams);
I hope this helps solve your problem.
I have a GridView, and setting the number of columns and the overal width of the grid layout. Need items with 66dp x 66dp size. But somehow items are squared, but smaller the 66x66. What am I forget?
int zz = .. // number of columns comes from outside
gridview.setNumColumns(zz);
LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams)gridview.getLayoutParams();
linearParams.width=66*zz;
gridview.setLayoutParams(linearParams);
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:columnWidth="66dp"
android:gravity="center"
android:horizontalSpacing="0dp"
android:scrollbarAlwaysDrawHorizontalTrack="true"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbars="horizontal"
android:stretchMode="none"
android:verticalSpacing="0dp">
</GridView>
item prototype
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="66dp" android:layout_height="66dp">
<ImageView
android:id="#+id/icon"
android:layout_width="66dp"
android:layout_height="66dp"
android:padding="0dp"
android:background="#android:color/holo_orange_light"/>
</RelativeLayout>
shreen shot
You can Use This..
int width = 66*zz;
view.setLayoutParams(new GridView.LayoutParams(GridView.width, YOUR_HEIGHT));
LinearLayout.LayoutParams require value in pixel not dp
int gridItemWithInDp = 66;
int gridItemWithInPx = (int) (gridItemWithInDp * Resources.getSystem().getDisplayMetrics().density);
linearParams.width=gridItemWithInPx *zz;
gridview.setLayoutParams(linearParams);
You should set the width of gridview in dp either from code or from declared into resource xml
from code:
final float density = getContext().getResources().getDisplayMetrics().density;
int pixels = (int) ((66 * zz) * density + 0.5f);
or from xml
int pixels = getResources().getDimensionPixelSize(R.dimen.example_dimen);
gridview.setNumColumns(zz);
LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams)gridview.getLayoutParams();
linearParams.width=pixels;
gridview.setLayoutParams(linearParams);
Set item prototype width and height MATCH_PARENT
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:id="#+id/icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:background="#android:color/holo_orange_light"/>
</RelativeLayout>
I need to create view dynamical with width and height which are the percent of parent size.So I need to add weight for width and height for one view.
Is it possible?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:weightSum="4"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_weight="1"
android:gravity="center"
android:id="#+id/imgtype"
android:scaleType="fitCenter"/>
<TextView
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_weight="3"
android:id="#+id/txtsubject"
android:textStyle="bold"
android:textSize="#dimen/lat_sub_text_size"
android:textColor="#android:color/black"/>
<LinearLayout/>
here you will be able to divide the major portion of parent linearlayout's width to the textview and remaining minor part for imageview this is for aligning horizontally the same can be done for vertical as well.Weightsum property of parent decides number of parts it will contain.
For example, I have the below layout:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/ll_forImage"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_forList"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
</LinearLayout>
Now, I will set width and height for each View depend on Screen width and Screen Height.
int screenWidth = dpToPx(getResources().getConfiguration().screenWidthDp);
int screenHeight = dpToPx(getResources().getConfiguration().screenHeightDp);
int ratioW = ....;//
int ratioH = ....;//
setLayoutSize(ll_forImage, screenWidth/ratioW , screenHeight/ratioH );
setLayoutSize(ll_forList, screenWidth/ratioW , screenHeight - screenHeight/ratioH );
// You can use setTranslation to translate View to expected position.
with:
private int dpToPx(int dp) {
return (int) (dp * getResources().getDisplayMetrics().density + 0.5f);
}
private static void setLayoutSize(View view, int width, int height) {
ViewGroup.LayoutParams params = view.getLayoutParams();
params.width = width;
params.height = height;
view.setLayoutParams(params);
}
Adding layout_weight is a good approach while using LinearLayout.
Remember, layout_weight works only in case of LinearLayout and cannot be used with RelativeLayout.
I have a scrollview with multiple different views inside of it. What I want is to have all the elements of the scrollview EXCEPT the last element fill the whole screen. The user can then scroll down to reveal the last item in the scrollview. I do not want that last item to be visible in any other case.
My XML looks like this:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="3.45"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<View 1 ... android:weight="2.25" />
<View 2 ... android:weight="0.8" />
<View 3 ... android:weight="0.40" /> //last item
</LinearLayout>
</ScrollView>
What this currently does is completely fill the whole screen, but does not have the last item push off.
according to the last post(now deleted),You should place the last item outside LinearLayout group or in a different LinearLayout.
It will be smth like
<ScrollView
android:layout_width="match_parent"<!-- added-->
android:layout_height="match_parent"><!-- added-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:oriention="vertical"><!-- added-->
<LinearLayout
android:id="#+id/visible_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"><!-- changed-->
...{items}
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
{last item}
</LinearLayout>
</LinearLayout>
<ScrollView>
UPDATE: now you need to extend height of linearLayout to fill screen:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
final Point point = new Point();
try {
display.getSize(point);
} catch (java.lang.NoSuchMethodError ignore) { // Older device
point.x = display.getWidth();
point.y = display.getHeight();
}
LinearLayout layout = (LinearLayout) findViewById(R.id.visible_items);
LayoutParams lp = layout.getLayoutParams();
lp.height = point.y;
layout.setLayoutParams(lp);
NOTE: put above code in onWindowFocusChanged() methos of activity
I write this code:
android:background="#drawable/back"
this code load the image and set the full screen modebut i want set to the 70% of screen heightHow can i do this?
My This is helpful to you:
Make android:layout_height="0dp" and android:layout_weight="0.70" of ImageView, other Invisible View for 30%.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutexample"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.70"
android:src="#drawable/ic_launcher" />
<View
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.30"
android:visibility="invisible" />
</LinearLayout>
public static int width = 0, height = 0;
/* GET WIDTH AND HEIGHT OF SCREEN */
if (width == 0 && height == 0) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
w.getDefaultDisplay().getSize(size);
width = size.x;
height = size.y;
} else {
Display d = w.getDefaultDisplay();
width = d.getWidth();
height = d.getHeight();
}
}
here u can get device height and width set 70% like this
RelativeLayout.LayoutParams mPar = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, (int) (height*0.70));