How to set the cardview height programmatically? - android

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.

Related

Can't get the top of one ImageView to align to top of TextVIew

I have an ImageView and a TextView within a RelativeLayout that is nested inside a ScrollView. I can't get the Top of the ImageView to line up with the top of the TextView. It hovers about 150dp down from the top of the text view top edge.
Here is my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/transitionForm"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/backgroundcolor"
android:orientation="horizontal" >
<ScrollView
android:id="#+id/svTutorial"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:splitMotionEvents="true"
android:layout_above="#+id/ImageBottom"
android:layout_below="#+id/ImageTop"
android:scrollbars="vertical"
>
<RelativeLayout
android:id="#+id/layoutTransitionPicWidgets"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/lblQuestionText"
android:layout_width="250dp"
android:layout_height="fill_parent"
android:layout_marginBottom="30px"
android:layout_marginLeft="50px"
android:layout_marginTop="10dp"
android:layout_marginRight="25px"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:text="get text from db."
android:textColor="#000000"
android:textSize="20sp" />
<ImageView
android:id="#+id/imgPic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/slidetest"
/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
I'm trying to programmatically change the width of the Image based on its size, so I am setting all the parameters programmatically.
Here is the code:
imgPic = (ImageView)findViewById(R.id.imgPic);
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
final float density = getResources().getDisplayMetrics().density;
final float dpHeight = outMetrics.heightPixels / density;
final float dpWidth = outMetrics.widthPixels / density;
ViewTreeObserver vto = imgPic.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
imgPic.getViewTreeObserver().removeOnPreDrawListener(this);
final int finalHeight = imgPic.getMeasuredHeight();
final int finalWidth = imgPic.getMeasuredWidth();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.width = Math.round(.4f * dpWidth * density);
params.leftMargin = 50;
params.topMargin = 0;
params.rightMargin = 25;
//params.gravity= Gravity.TOP;
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.RIGHT_OF, R.id.lblQuestionText);
imgPic.setLayoutParams(params);
return true;
}
});
I've tried doing this with a LinearLayout and get the same results. What am I missing?
You can add this to your ImageView
android:layout_alignTop="#+id/lblQuestionText"
but I think there's more going on than that. You don't specify any alignment attributes on the ImageView, so it may not layout where you think it's going to layout.
It seems like maybe you want the ImageView to be to the right of the TextView, in which case adding this to the ImageView should help:
android:layout_toRightOf="#+id/lblQuestionText"

How to set weight for width and height?

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.

switch videoview to fullscreen mode android

I want to display a videoview in fullscreen mode when the user press a full screen button
I have searched on the internet but all solutions I have found doesnt' fix the issue
here is my current code which doesnt' work too :
public void setAnchorView(final View view) {
super.setAnchorView(view);
Button fullScreen = new Button(VideoDetails.this);
fullScreen.setText("FullScreen");
Log.e("media controller","Set anchorView");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(view.getWidth(), 0, 5, 20);
params.gravity = Gravity.RIGHT;
addView(fullScreen, params);
fullScreen.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics);
android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) videoview.getLayoutParams();
params.leftMargin = 0;
videoview.setLayoutParams(params);
}
});
}
and here is the xml layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#818181" >
<RelativeLayout
android:id="#+id/video_details_header"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:background="#000000" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="12dp"
android:clickable="true"
android:onClick="returnback"
android:text="Back"
android:textColor="#ffffff" />
<TextView
android:id="#+id/video_details_text_header_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#ffffff" />
</RelativeLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/video_details_header"
android:layout_centerInParent="true" >
<RelativeLayout
android:id="#+id/layout_page"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/video_details_video"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<ImageView
android:id="#+id/video_details_thumbnail"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:onClick="showvideo"
android:padding="11dp"
android:visibility="visible" />
<VideoView
android:id="#+id/video_details_videoView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:padding="11dp"
android:visibility="gone" />
</FrameLayout>
which contains the videoview
do you have any idea
Try requestLayout() call after setting the layout params
...
params.leftMargin = 0;
videoview.setLayoutParams(params);
videoview.requestLayout();
try to change the width of the view too:
WindowManager wm = (WindowManager)videoview.getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int displayWidth = display.getWidth();
params.leftMargin = 0;
params.width = displayWidth;
videoview.setLayoutParams(params);
videoview.requestLayout();
Not sure because can't all code, but for me it seems like you don't need setting margin at all
you may just manipulate with view's width and gravity.
You might have forgotten this line of code for it to fit the full screen.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics);
android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) videoView.getLayoutParams();
params.width = metrics.widthPixels;
params.height = metrics.heightPixels;
params.leftMargin = 0;
videoView.setLayoutParams(params);

ListView height android

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>

How to make button to fill 50% from the screen width in android?

I want to make the button to fill always 50% from the width of the screen, as when I rotate the device horizontally or vertically always the button fill 50% from the width.
This should be possible by using weightSum and layout_weight:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:weightSum="1.0">
<Button
android:id="#+id/textbox3"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:layout_width="0dip"
android:textSize="12sp" />
</LinearLayout>
To do this in code.
// Get screen width.
DisplayMetrics metrics = new DisplayMetrics();
WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(metrics);
final float width = metrics.widthPixels;
LinearLayout.LayoutParams layoutParamsWidth50 = new LinearLayout.LayoutParams(
(int) (width/2) , ViewGroup.LayoutParams.MATCH_PARENT );
textbox3.setLayoutParams(layoutParamsWidth50);

Categories

Resources