I have a LinearLayout, I set the width to match_parent. How can I match the height to the width to get a square layout?
<LinearLayout
android:id="#+id/linearLayout"
android:orientation="horizontal"
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="2dp"
android:gravity="center"
android:background="#drawable/card_layout">
</LinearLayout>
To achive this you need to get width of your #+id/linearLayout programatically
LinearLayout mLinearLayout=(LinearLayout)findViewById(R.id.linearLayout);
int screenWidth = mContext.getResources().getDisplayMetrics().widthPixels;
int screenHeight = mContext.getResources().getDisplayMetrics().widthPixels;
mLinearLayout.measure(screenWidth, screenHeight);
int layoutWidth = mLinearLayout.getMeasuredWidth();
And then set width values as height
ViewGroup.LayoutParams params = mLinearLayout.getLayoutParams();
params.height = layoutWidth;
mLinearLayout.setLayoutParams(params);
You can also do this like #balaji koduri showed in OnGlobalLayoutListener
try this:
final LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
layout.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
LayoutParams params = (LayoutParams) layout
.getLayoutParams();
params.height = params.width;
layout.setLayoutParams(params);
layout.getViewTreeObserver().addOnGlobalLayoutListener(
null);
}
});
Do it programmatically -
LinearLayout mLinearLayout=(LinearLayout)findViewById(R.id.linearLayout);
ViewGroup.LayoutParams params = mDialogLayout.getLayoutParams();
params.width = mContext.getResources().getDisplayMetrics().widthPixels;
params.height = mContext.getResources().getDisplayMetrics().widthPixels;
if you want other size -
params.width = 200;
params.height = 200;
Make your custom layout with overrided onMeasure
#Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
Related
I'm working on a custom view, which should have equal height and width.
I draw it in the following way:
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2, mPaint);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
widthMeasureSpec = heightMeasureSpec;
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
If I just add one view onto the screen, the width/height is ok. Though, if I add two of these views next to each other, the size of the views should be adjusted (each view: half width of the screen).
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<at.guger.widget.Light
android:id="#+id/lights_light1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="5dp" />
<at.guger.widget.Light
android:id="#+id/lights_light2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="5dp" />
</LinearLayout>
How can I do this?
Wouldn't it be easier to let the views take as much space as it can, but constrained them from the parent layoutmanager ? You can implement it with a recyclerview and your own LayoutManager overloading the measureChild method so that it would return the widthscreen divided by the number of elememnt in your adapter. Not a working example but something along the lines:
// setting up your recycler view
mRecyclerView = (RecyclerView) mView.findViewById(R.id.recycler);
// attaching your custom adapter
mRecyclerAdapter = new MyRecyclerAdapter(getActivity(), this);
mRecyclerView.setAdapter(statusRecyclerAdapter);
// setting the layoutmanager
mRecyclerLayoutManager = new MyRecyclerLayoutManager(
getActivity(),
LinearLayoutManager.HORIZONTAL, false);
mRecyclerView.setLayoutManager(mRecyclerLayoutManager);
Then you would create an innerclass MyRecyclerLayoutManager as follow:
private class MyRecyclerLayoutManager extends LinearLayoutManager {
MyRecyclerLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public void measureChild(View child, int widthUsed, int heightUsed) {
super.measureChild(child, widthUsed, heightUsed);
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int widthSpec = View.MeasureSpec.makeMeasureSpec((int) Math.round(displaymetrics.widthPixels / mRecyclerAdapter.size()), View.MeasureSpec.EXACTLY);
final RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) child.getLayoutParams();
final int heightSpec = getChildMeasureSpec(getHeight(), getHeightMode(),
getPaddingTop() + getPaddingBottom() + heightUsed, lp.height,
canScrollVertically());
child.measure(widthSpec, heightSpec);
}
public void measureChildWithMargins(View child, int widthUsed, int heightUsed) {
super.measureChildWithMargins(child, widthUsed, heightUsed);
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int widthSpec = View.MeasureSpec.makeMeasureSpec((int) Math.round(displaymetrics.widthPixels * 0.85), View.MeasureSpec.EXACTLY);
final RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) child.getLayoutParams();
final int heightSpec = getChildMeasureSpec(getHeight(), getHeightMode(),
getPaddingTop() + getPaddingBottom() + heightUsed, lp.height,
canScrollVertically());
child.measure(widthSpec, heightSpec);
}
}
Here the important bit is at widthSpec which is window width divided by your recycleradapter size.
I have to implementing a RecylerView,it should wrap the content if its contain only item content height is less. If the item's increase like 250dp, it should be set to max heght(250dp) and that be able to scroll.How to achieve this.my parent layout is a Relative layout.
This is My Layout file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom" />
</RelativeLayout>
Screen Shot if its only one item.This should be wrap_content.
you can programetically set height to RecylerView
if only one item... wrap content
ViewGroup.LayoutParams params=recycler_view.getLayoutParams();
params.height= RecyclerView.LayoutParams.WRAP_CONTENT;
recycler_view.setLayoutParams(params);
else
<dimen name="view_height">250dp</dimen>
float height= getResources().getDimension(R.dimen.view_height); //get height
ViewGroup.LayoutParams params_new=recycler_view.getLayoutParams();
params_new.height=height;
recycler_view.setLayoutParams(params_new);
1.) Create a class to handle setting maximum height to what is passed by the user:
public class OnViewGlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {
private Context context;
private int maxHeight;
private View view;
public OnViewGlobalLayoutListener(View view, int maxHeight, Context context) {
this.context = context;
this.view = view;
this.maxHeight = dpToPx(maxHeight);
}
#Override
public void onGlobalLayout() {
if (view.getHeight() > maxHeight) {
ViewGroup.LayoutParams params = view.getLayoutParams();
params.height = maxHeight;
view.setLayoutParams(params);
}
}
public int pxToDp(int px) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int dp = Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
return dp;
}
public int dpToPx(int dp) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
return px;
}
}
2.) Attach this to the view and pass the maximum height in DP:
messageBody.getViewTreeObserver()
.addOnGlobalLayoutListener(
new OnViewGlobalLayoutListener(messageBody, 256, context)
);
I have an answer here:
https://stackoverflow.com/a/29178364/1148784
Just create a new class extending RecyclerView and override it's onMeasure method.
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (maxHeight > 0){
int hSize = MeasureSpec.getSize(heightMeasureSpec);
int hMode = MeasureSpec.getMode(heightMeasureSpec);
switch (hMode){
case MeasureSpec.AT_MOST:
heightMeasureSpec = MeasureSpec.makeMeasureSpec(Math.min(hSize, maxHeight), MeasureSpec.AT_MOST);
break;
case MeasureSpec.UNSPECIFIED:
heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
break;
case MeasureSpec.EXACTLY:
heightMeasureSpec = MeasureSpec.makeMeasureSpec(Math.min(hSize, maxHeight), MeasureSpec.EXACTLY);
break;
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
maxHeight is in pixels.
You can use this:
maxHeight = (int) getContext().getResources().getDisplayMetrics().density * 250;
to convert 250 dp into pixels
I'm trying to resize my ViewPager depends on the screen size of a device. My ViewPager is at the center of screen and has only a width of 300dp. I want my ViewPager height to resize depends on the screen size. Underneath of my ViewPager it has a ImageView, this ImageView covers a part of my ViewPager.
In my activity_main.xml
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_below="#+id/img_memtext"
android:layout_centerHorizontal="true"/>
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/indicator"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/pager"
android:layout_centerHorizontal="true"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#drawable/notification_bg"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/attention"
android:layout_below="#id/indicator"/>
In my viewpager_item.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:background="#ffffffff"
android:isScrollContainer="true"
android:fillViewport="true"
android:layout_gravity="center"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
>
<ImageView
android:layout_width="280dp"
android:layout_height="100dp"
android:id="#+id/news_image"
android:layout_centerHorizontal="true"/>
<TextView
android:id="#+id/news_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="bold"
android:textSize="18dp"
android:layout_below="#+id/news_image"/>
<TextView
android:id="#+id/space"
android:layout_width="20dp"
android:layout_height="20dp"
android:gravity="center"
android:layout_below="#+id/news_title"
/>
<TextView
android:id="#+id/news_body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/news_title"
android:textSize="14dp"/>
<TextView
android:id="#+id/read_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/news_body"
android:textColor="#ff28c0ff"
android:clickable="true"
android:textSize="14dp"/>
</RelativeLayout>
</ScrollView>
Could anyone can help me please? Thank you very much.
Use LinearLayout with match_parent height and width to wrap your ViewPager and other Views you want to be in the same row or column as your ViewPager. Remove explicit dimensions, then, add layout_weight property to Views wrapped by the LinearLayout. More information on layout_weight in LinearLayout.
If you want to support a large range of screens sizes and resolutions, I suggest you to read this article, and look deeper in the android developers website for best practices and instructions to make your app behave weel on (almost) all screen sizes.
i make some class to resize view's width and height. I dont like Dp unit. it's hard to me, every device fit.
here part of my class :
public class SetViewScale {
private static SetViewScale instance;
public int height, width; //device height, width
public final static int Full_w = 720; // base on design
public final static int Full_h = 1230; //base on design
private SetViewScale(Activity a){
DisplayMetrics displayMetrics = new DisplayMetrics();
a.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int deviceWidth = displayMetrics.widthPixels;
int deviceHeight = displayMetrics.heightPixels;
this.width = deviceWidth;
this.height = deviceHeight;
}
private SetViewScale(WindowManager wm){
DisplayMetrics displayMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(displayMetrics);
int deviceWidth = displayMetrics.widthPixels;
int deviceHeight = displayMetrics.heightPixels;
this.width = deviceWidth;
this.height = deviceHeight;
}
public synchronized static SetViewScale getInstance(Activity a){
if(instance == null)
instance = new SetViewScale(a);
return instance;
}
public synchronized static SetViewScale getInstance(WindowManager wm){
if(instance == null)
instance = new SetViewScale(wm);
return instance;
}
public synchronized static SetViewScale getInstance(){
if(instance == null) {
WindowManager wm = (WindowManager) AppManager.context.getSystemService(Context.WINDOW_SERVICE);
instance = new SetViewScale(wm);
}
return instance;
}
/**
* view resizing device width height
* #param view
* #param w
* #param h
* #return
*/
public View setViewScale(View view, int w, int h) {
int setW, setH;
int x = width * w;
setW = (int)(x/Full_w);
int y = h*setW;
setH = (int)(y/w);
ViewParent parent = view.getParent();
if(parent instanceof LinearLayout){
LinearLayout.LayoutParams lp = (LayoutParams) view.getLayoutParams();
lp.width = setW;
lp.height = setH;
view.setLayoutParams(lp);
}else if(parent instanceof RelativeLayout){
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)view.getLayoutParams();
lp.width = setW;
lp.height = setH;
view.setLayoutParams(lp);
}else if(parent instanceof FrameLayout){
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)view.getLayoutParams();
lp.width = setW;
lp.height = setH;
view.setLayoutParams(lp);
}else if(parent instanceof AbsListView){
AbsListView.LayoutParams lp = (AbsListView.LayoutParams)view.getLayoutParams();
lp.width = width;
lp.height = height;
view.setLayoutParams(lp);
}else{
return null;
}
return view;
}
public View setFillWidthScale(View view, int w, int h) {
int setW, setH;
int x = width * w;
setW = (int)(x/Full_w);
int y = h*setW;
setH = (int)(y/w);
ViewParent parent = view.getParent();
if(parent instanceof LinearLayout){
LinearLayout.LayoutParams lp = (LayoutParams) view.getLayoutParams();
lp.width = LayoutParams.MATCH_PARENT;
lp.height = setH;
view.setLayoutParams(lp);
}else if(parent instanceof RelativeLayout){
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)view.getLayoutParams();
lp.width = RelativeLayout.LayoutParams.MATCH_PARENT;
lp.height = setH;
view.setLayoutParams(lp);
}else if(parent instanceof FrameLayout){
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)view.getLayoutParams();
lp.width = FrameLayout.LayoutParams.MATCH_PARENT;
lp.height = setH;
view.setLayoutParams(lp);
}else if(parent instanceof AbsListView){
AbsListView.LayoutParams lp = (AbsListView.LayoutParams)view.getLayoutParams();
lp.width = width;
lp.height = height;
view.setLayoutParams(lp);
}else{
return null;
}
return view;
}
use px unit.
i hope it is some hint to you.
using a dialog as a window I tried to set its layout as
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.x = 120;
lp.y = 120;
lp.width = 800;
lp.height = 450;
Dialog dialog = new Dialog(cxt);
dialog.getWindow().setAttributes(lp);
dialog.setContentView(vFirst);
dialog.show();
where
View vFirst = inflater.inflate(R.layout.popup, container, false);
which is inside a button click,but it is not setting it.
I think you should use this
dialog.getWindow().setLayout(800, 450);
This will set the dialog window size.
public class Myclass extends Dialog
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.FILL_PARENT;
getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
}
}
layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/dialogWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
// contents here
</LinearLayout>
According to documentation: http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html,
X position for this window. With the default gravity it is ignored. When using LEFT or START or RIGHT or END it provides an offset from the given edge.
Y position for this window. With the default gravity it is ignored. When using TOP or BOTTOM it provides an offset from the given edge.
So to make x and y work you should set Gravity attribute before. For example:
lp.gravity = Gravity.TOP;
lp.gravity = Gravity.LEFT;
lp.y = 120;
lp.x = 120;
//dialog width and height are set according to screen, you can take you want
Dialog myDialog = new Dialog(context, R.style.CustomTheme);
myDialog.setContentView(R.layout.share_post_dialog);
int[] widthHeight = getDeviceWidthAndHeight(context);
int dialogHeight = widthHeight[1] - widthHeight[1] / 4;
int dialogWidth = widthHeight[0] - widthHeight[0] / 5;
WindowManager.LayoutParams params = myDialog.getWindow().getAttributes();
params.width= dialogWidth;
params.height = dialogHeight;
myDialog.getWindow().setAttributes((WindowManager.LayoutParams) params);
myDialog.show();
//This is used to get height width of screen
#SuppressWarnings("deprecation")
public static int[] getDeviceWidthAndHeight(Context context) {
int widthHeght[] = new int[2];
int measuredWidth = 0;
int measuredHeight = 0;
WindowManager w = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
Point size = new Point();
w.getDefaultDisplay().getSize(size);
measuredWidth = size.x;
measuredHeight = size.y;
widthHeght[0] = measuredWidth;
widthHeght[1] = measuredHeight;
} else {
Display d = w.getDefaultDisplay();
measuredWidth = d.getWidth();
measuredHeight = d.getHeight();
widthHeght[0] = measuredWidth;
widthHeght[1] = measuredHeight;
}
return widthHeght;
}
I've tried to set my ImageView resized but it did not work either by bitmap or other methods I'm going to place my code so if can anyone help me how to size the ImageView to fit inside the table row thnx.
public class Test extends Activity
{
private TableLayout Table1;
protected void onCreate(Bundle savedInstanceState)
{
int[] ImageArray={R.raw.hospital_image,R.raw.hotel_image};
super.onCreate(savedInstanceState);
setContentView(R.layout.favorites);
Table1 = (TableLayout) findViewById(R.id.Table);
TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f);
for(int i=0;i<ImageArray.length;i++)
{
TableRow TR = new TableRow(this);
TR.setLayoutParams(tableRowParams);
ImageView img=new ImageView(this);
//What should i do here to resize it ???
Drawable d = getResources().getDrawable(ImageArray[i]);
img.setImageDrawable(d);
TR.addView(img, new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
Table1.addView(TR);
}
}
My XMl only holds these :
<?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" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<TableLayout
android:id="#+id/Table1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableLayout>
</ScrollView>
</RelativeLayout>
try this,
public class ResizableImageView extends ImageView {
public ResizableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ResizableImageView(Context context) {
super(context);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable d = getDrawable();
if (d == null) {
super.setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
return;
}
int imageHeight = d.getIntrinsicHeight();
int imageWidth = d.getIntrinsicWidth();
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
float imageRatio = 0.0F;
if (imageHeight > 0) {
imageRatio = imageWidth / imageHeight;
}
float sizeRatio = 0.0F;
if (heightSize > 0) {
sizeRatio = widthSize / heightSize;
}
int width;
int height;
if (imageRatio >= sizeRatio) {
// set width to maximum allowed
width = widthSize;
// scale height
height = width * imageHeight / imageWidth;
} else {
// set height to maximum allowed
height = heightSize;
// scale width
width = height * imageWidth / imageHeight;
}
setMeasuredDimension(width, height);
}
}
use it in your layout as you would plain old ImageView, just change the tag, like this,
<com.example.ResizableImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:padding="1px"
android:scaleType="fitCenter"
android:src="..." />
setLayoutParams() should do the trick, in your case I believe you would need TableRow.LayoutParams, like so:
ImageView img = new ImageView(this);
img.setLayoutParams(new TableRow.LayoutParams(w, h));
Edit
Try to set the Drawable before the setLayoutParams:
ImageView img = new ImageView(this);
Drawable d = getResources().getDrawable(ImageArray[i]);
img.setImageDrawable(d);
img.setLayoutParams(new TableRow.LayoutParams(w, h));