Android image background height - android

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));

Related

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.

How to keep images in Android layout proportional like in the original?

I need to place an image (a logo) on top of the android screen and I need it to be as wide as the entire screen and I need its' height to be proportional to the width, the same proportion as in the original image. So far, everything I've tried resulted in misproportioned images or images covering the entire screen. How do I fix this?
<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"
tools:context=".MainActivity"
android:background="#color/blue">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/linearLayout"
android:layout_centerVertical="true"
>
<EditText
android:id="#+id/username"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginLeft="#dimen/margin_main"
android:layout_marginRight="#dimen/margin_main"
android:hint="#string/username"
android:textColorHint="#fff"
android:gravity="center"
android:background="#drawable/input_background"
android:layout_marginBottom="#dimen/margin_main"
android:textColor="#fff"
/>
<EditText
android:id="#+id/password"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:inputType="textPassword"
android:layout_marginLeft="#dimen/margin_main"
android:layout_marginRight="#dimen/margin_main"
android:hint="#string/password"
android:textColorHint="#fff"
android:gravity="center"
android:background="#drawable/input_background"
android:layout_marginBottom="#dimen/margin_main"
android:textColor="#fff"/>
<Button
android:id="#+id/login_button"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="#string/login"
android:layout_marginLeft="#dimen/margin_main"
android:layout_marginRight="#dimen/margin_main"
android:background="#drawable/button_yellow_background"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="#color/blue"/>
</LinearLayout>
<ImageView
android:id="#+id/logo"
android:layout_width="315dp"
android:layout_height="100dp"
android:background="#drawable/logo"/>
</RelativeLayout>
The image in question is the logo.
All you need to do is change
<ImageView
android:id="#+id/logo"
android:layout_width="315dp"
android:layout_height="100dp"
android:background="#drawable/logo"/>
to
<ImageView
android:id="#+id/logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/logo"/>
Note: Doing this might result in a stretch image if your image is smaller than the screen width.
Try to set scaleType to fitXY:
<ImageView
android:id="#+id/fullImg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/your_image_resource"
android:scaleType="fitXY" />
If you want to do it at runtime you can do that:
// set the ratio (if it can change it's also possible get it from resource id)
double aspect_ratio = 16/9;
// getting a ref to your ImageView
ImageView yourImageView = (LinearLayout) findViewById(R.id.imageView);
// get DisplayMetrics to get screen sizes
DisplayMetrics met = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(met);
// if you have a "activity_horizontal_margin" in your style
int t = getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin);
int width = (int)((met.widthPixels-2*t));
// otherwise just
int width = (int)((met.widthPixels-2*t));
// go on calculate height
int height = width * aspect_ratio;
// ending with layour params setting
yourImageView.setLayoutParams(new LayoutParams(width, height);
else if you want to do it in xml, you could do something like that
<ImageView
android:id="#+id/yourImageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/logo"
android:scaleType="centerInside" />
You should do that at runtime.
Solution 1
Display display = getWindowManager().getDefaultDisplay();
ImageView imageView = (LinearLayout) findViewById(R.id.imageView);
int width = display.getWidth();
int height = (int) width * 0.75; // 0.75 if image aspect ration is 4:3, change accordingly
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, height);
This would make the image to fill the screen width and height as per the aspect ratio of the original image. To use this method you need to hardcode the aspect ratio.
Solution 2 From Scale Image to fill ImageView width and keep aspect ratio
Create a custom image view and do this
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
try {
Drawable drawable = getDrawable();
if (drawable == null) {
setMeasuredDimension(0, 0);
} else {
float imageSideRatio = (float)drawable.getIntrinsicWidth() / (float)drawable.getIntrinsicHeight();
float viewSideRatio = (float)MeasureSpec.getSize(widthMeasureSpec) / (float)MeasureSpec.getSize(heightMeasureSpec);
if (imageSideRatio >= viewSideRatio) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = (int)(width / imageSideRatio);
setMeasuredDimension(width, height);
} else {
int height = MeasureSpec.getSize(heightMeasureSpec);
int width = (int)(height * imageSideRatio);
setMeasuredDimension(width, height);
}
}
} catch (Exception e) {
e.printStackTrace();
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

Wrap_content doesn't work on RelativeLayout : Action bar custom View

I have created a custom layout for the Action Bar : which I add with this code on my Activity :
LayoutInflater inflater = (LayoutInflater)getSystemService (Context.LAYOUT_INFLATER_SERVICE);
View actionBarView = inflater.inflate (R.layout.main_actionbar, null);
getSupportActionBar ().setCustomView (actionBarView);
and the main_actionbar.xml layout :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:style="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white" >
<RelativeLayout
android:id="#+id/leftIcons"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toLeftOf="#+id/empty"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:background="#drawable/uiclickable_gray_states"
android:layout_alignParentLeft="true" >
<ImageView
android:id="#+id/back"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:layout_marginRight="5dp"
android:src="#drawable/icon_back" />
<!-- The problem is here -->
<ImageView
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="#+id/back"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:src="#drawable/bg"
android:scaleType="fitXY"
android:adjustViewBounds="true" />
</RelativeLayout>
<View
android:id="#+id/empty"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#color/black_transparent"
android:layout_alignParentRight="true" />
</RelativeLayout>
My problem is that I would like the #+id/thumbnail to have it's height as match_parent and have it's width equals the height. means : height == match_parent AND width == height, so I can have a square Image.
it appears that this cannot be done by xml, so I created (with the help of some tutorials on the net) a subclass of ImageView and did this but without any positive results :
#Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
//int width = measureWidth (widthMeasureSpec);
int height = measureHeight (heightMeasureSpec);
setMeasuredDimension (height, height);
}
private int measureHeight (int measureSpecHeight) {
int result;
int specMode = MeasureSpec.getMode (measureSpecHeight);
int specSize = MeasureSpec.getSize (measureSpecHeight);
if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else if (specMode == MeasureSpec.AT_MOST) {
// The child can be as large as it wants up to the specified size.
result = specSize;
} else {
// Measure the text (beware: ascent is a negative number)
result = canvasSize;
}
return (result + 2);
}
Am I doing something wrong there ? Thank you.

How to arrange width and height of relative layout to be half of the screen

I am developing an android application and I want to set my relative layout's width and height as half of my screen width and height.
Thanks
Use Parent as the LinearLayout and use weightSum and weight attributes
Sample
<LinearLayout 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:orientation="vertical"
android:weightSum="100" >
<RelativeLayout
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50" >
<!-- Your RelativeLayout Items -->
</RelativeLayout>
</LinearLayout>
To Divide a Relative layout into two equal relative layouts
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
</RelativeLayout>
</RelativeLayout>
and to Divide a linear layout into two equal relative layouts
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
</RelativeLayout>
</LinearLayout>
*#SuppressLint("NewApi")
public static int getDeviceWidth(Activity activity) {
int deviceWidth = 0;
Point size = new Point();
WindowManager windowManager = activity.getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
windowManager.getDefaultDisplay().getSize(size);
deviceWidth = size.x;
} else {
Display display = windowManager.getDefaultDisplay();
deviceWidth = display.getWidth();
}
return deviceWidth;
}
#SuppressLint("NewApi")
public static int getDeviceHeight(Activity activity) {
int deviceHeight = 0;
Point size = new Point();
WindowManager windowManager = activity.getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
windowManager.getDefaultDisplay().getSize(size);
deviceHeight = size.y;
} else {
Display display = windowManager.getDefaultDisplay();
deviceHeight = display.getHeight();
}
return deviceHeight;
}*
above two function is get device height and width.
Use on this function, you can get half of the size of screen.
I hope its useful to you.
You need to put the RelativeLayout inside a LinearLayout and use android:weightsum=2 and android:layout_weight=1 to get the desired proportion
<?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="horizontal"
android:weightSum="2" >
<RelativeLayout
....
android:layout_weight=1
....>
</RelativeLAyout>
</LinearLayout>

I want to set max height in dialog fragment

<?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"
android:minWidth="#dimen/dialog_min_width"
android:padding="#dimen/dialog_padding"
android:background="#drawable/dialog_background" >
<TextView android:id="#+id/base_dialog_title"
style="#style/DialogTitleTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:gravity="center" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="16dp" >
</FrameLayout>
<!-- *********************** HERE ************************* -->
<FrameLayout android:id="#+id/base_dialog_content"
android:background="#drawable/dialog_description_background"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
<!-- *********************** HERE ************************* -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="16dp" >
</FrameLayout>
<LinearLayout android:id="#+id/base_dialog_button_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="#+id/base_dialog_button_negative"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="gone"/>
<Button
android:id="#+id/base_dialog_button_neutral"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="gone"/>
<Button
android:id="#+id/base_dialog_button_positive"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="gone"/>
</LinearLayout>
</LinearLayout>
This is dialog fragment layout.
I add content view (TextView or ListView) into FrameLayout (#id/base_dialog_content)
When ListView has many Items, Dialog is full height in window.
I want to set maximum height dialog or content view (or list view)
Edit:
I solve the problem using OnLayoutChangeListener.
But I need same function in lower version (below HoneyComb)
In DialogFragment.onCreateView()
FrameLayout contentContainer = (FrameLayout) view.findViewById(R.id.base_dialog_content);
final View contentView = createContentView(inflater, contentContainer);
contentContainer.addView(contentView);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
// TODO Check content view height and change height
} else {
view.addOnLayoutChangeListener(new OnLayoutChangeListener() {
#Override
public void onLayoutChange(View v, int left, int top, int right,
int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
int height = v.getHeight();
int contentHeight = contentView.getHeight();
int winHeight = ActivityUtil.getDisplayHeight(getSherlockActivity());
LogUtils.pe(height+" / "+winHeight+" / "+contentHeight);
int needHeight = height - winHeight*8/10;
if (needHeight > 0) {
contentView.setLayoutParams(
new LayoutParams(LayoutParams.MATCH_PARENT, contentHeight-needHeight));
}
}
});
}
to control the maximum height you have to put the content view in one layout container first (Linear/Relative) let's say in my example I'll use linearlayout
to measure the screen height (I put it in Util.java ):
public static int getDisplayHeight(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int height = display.getHeight();
return height;
}
after that you create a container :
LinearLayout llContainerContent = new LinearLayout(context);
LayoutParams lpContainerContent = new LayoutParams(LayoutParams.FILL_PARENT, (Util.getDisplayHeight(context) * 8) /10);
llContainerContent.setLayoutParams(lpContainerContent);
after that you add the textview/listview to the container then you add the container to the framelayout. I hope my answer is clear enough for you but if you have another question please feel free to ask in the comment :)
Use this code for full screen height and width for the dialog
WindowManager window = (WindowManager)context
.getSystemService(Context.WINDOW_SERVICE);
Display display = window.getDefaultDisplay();
displayheight = display.getHeight();
displaywidth = display.getWidth();
dialog.getWindow().setLayout(displaywidth ,
displayheight );
what about using the onResume to get it done:
override fun onResume() {
super.onResume()
val width = (resources.displayMetrics.widthPixels * 0.95).toInt()
val height = (resources.displayMetrics.heightPixels * 0.60).toInt()
dialog?.window?.setLayout(width, height)
}

Categories

Resources