I want to set max height in dialog fragment - android

<?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)
}

Related

The Group Indicator of the expandable List view disappears

I want to change the location of the group Indicator of an expandable List view , from left to right, but the group Indicator disappears .
Code From Main Activity:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
mExpandableListView.setIndicatorBounds(width - getDipsFromPixel(35), width
- getDipsFromPixel(5));
} else {
mExpandableListView.setIndicatorBoundsRelative(width - getDipsFromPixel(35), width
- getDipsFromPixel(5));
}
}
// Convert pixel to dip
public int getDipsFromPixel(float pixels) {
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (pixels * scale + 0.5f);
}
code from Group Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/tv_menu_background_color"
android:paddingLeft="32dp"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#color/list_item_title"
android:gravity="center_vertical"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
Code from activity_main:
<android.support.v4.widget.DrawerLayout
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:id="#+id/drawer_layout">
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- Expandable list view -->
<ExpandableListView
android:id="#+id/list_slideMenu"
android:layout_width="320dp"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector"
android:background="#color/list_background"
android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
Thanks!
Seems to me that you're fetching the screen's width and using it to set the indicator's bounds. However, your ExpandableListView's width is 320dp. I believe you should calculate the width like this:
int width = mExpandableListView.getWidth();
Also all of this should definitely not be called in onWindowFocusChanged, as it can be called multiple times.
Instead you should put this code into onCreate (after setContentView):
mExpandableListView = findViewById(R.id.elv);
mExpandableListView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
#Override
public void onLayoutChange(View v, int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) {
mExpandableListView.removeOnLayoutChangeListener(this);
int width = mExpandableListView.getWidth();
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
mExpandableListView.setIndicatorBounds(width - getDipsFromPixel(35), width
- getDipsFromPixel(5));
} else {
mExpandableListView.setIndicatorBoundsRelative(width - getDipsFromPixel(35), width
- getDipsFromPixel(5));
}
}
});
This adds a listener to the ELV, to wait until it's measured, and then set the indicator.

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.

Android image background height

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

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>

Why does my FrameLayout have 0 height?

I have a FrameLayout, containing various overlayed ImageViews:
<FrameLayout
android:id="#+id/australia_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/australia"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/map_australia"
/>
<ImageView
android:id="#+id/nsw"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/map_nsw"
android:visibility="invisible"
/>
<ImageView
android:id="#+id/victoria"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/map_victoria"
android:visibility="invisible"
/>
...etc...
</FrameLayout>
In my code I try and render my FrameLayout:
final Dialog dialog = new Dialog((Context) parent);
dialog.setContentView(R.layout.dialog_region);
FrameLayout fl = (FrameLayout) dialog.findViewById(R.id.australia_frame);
final int height = fl.getHeight();
Validate.isTrue(height > 0);
My code crashes with:
java.lang.IllegalArgumentException: The validated expression is false
at org.apache.commons.lang3.Validate.isTrue(Validate.java:180)
The line that's crashing is Validate.isTrue(height > 0);.
Why does my FrameLayout not have a height? Is there a way to get the exact px height and width that my FrameLayout is rendering at?
This may help you
FrameLayout target = (FrameLayout) dialog.findViewById(R.id.australia_frame);
target.post(new Runnable() {
#Override
public void run() {
int width = target.getWidth();
int height = width/2;
/*your code with width and height*/
}
});
}

Categories

Resources