The Group Indicator of the expandable List view disappears - android

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.

Related

Percentage of a layout inside RecyclerView visible on Screen

Is there a way from which I can know what percentage of my layout inside a recyclerView is visible on the Screen.
The layout inflated inside RecyclerView has params of
android:layout_width="match_parent"
android:layout_height="match_parent"
Edit added layout file
Layout file - custom_card.xml
<?xml version="1.0" encoding="utf-8"?
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/card"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="#id/imageView"
android:layout_alignTop="#id/imageView"
android:scaleType="fitCenter"
android:src="#drawable/image_view_screen" />
</RelativeLayout>
Activity Layout File.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:visibility="gone"
android:layout_width="56dp"
android:layout_height="56dp"
android:id="#+id/progress_bar"
android:layout_centerInParent="true"/>
</RelativeLayout>
I am using a Vertical LinearLayoutManager
After much Try I figured it out. If it helps anyone.
Since my layout height was match_parent it was easier for me. But this method will work for everyone with some calculations.
int firstItemPos=mLayoutManager.findFirstVisibleItemPosition();
View firstItemView=mLayoutManager.findViewByPosition(firstItemPos);
int lastItemPos=mLayoutManager.findLastVisibleItemPosition();
View lastItemView=mLayoutManager.findViewByPosition(lastItemPos);
Now all items between firstItemPos and lastItemPos is visible 100%.
For firstItemView-
Math.abs(firstItemView.getY()) / firstItemView.getHeight()
Similarly for lastItemView-
Math.abs(lastItemView.getY()) / lastItemView.getHeight()
use this;
in your adapter;
private final Rect mCurrentViewRect = new Rect();
... your inits...
in your onBindViewHolder;
View firstItemView = linearLayoutManager.findViewByPosition(position);
int pos= getVisibilityPercents(position);
use these metods;
public int getVisibilityPercents(View currentView) {
int percents = 100;
currentView.getLocalVisibleRect(mCurrentViewRect);
int height = currentView.getHeight();
if (viewIsPartiallyHiddenTop()) {
percents = (height - mCurrentViewRect.top) * 100 / height;
} else if (viewIsPartiallyHiddenBottom(height)) {
percents = mCurrentViewRect.bottom * 100 / height;
}
return percents;
}
private boolean viewIsPartiallyHiddenBottom(int height) {
return mCurrentViewRect.bottom > 0 && mCurrentViewRect.bottom < height;
}
private boolean viewIsPartiallyHiddenTop() {
return mCurrentViewRect.top > 0;
}
btw, dont remember get your recyclerView and linearLayoutManager to your adapter in constructor.
For more => https://github.com/danylovolokh/VideoPlayerManager

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

ImageView height is always 0

I am trying to get the height and width of my imageview so I can scale my image down the the appropriate size but everytime I try to get the height I get the height as 0. I thought maybe it just was not measured yet so I put a onPreDrawListener on the imageview so that I know it would be measured at that time but I still get 0 so I am not sure the problem.
this is my full layout
<?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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:id="#+id/content"
android:layout_above="#+id/comment_layout">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/comments"
android:background="#android:color/transparent"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/comment_layout">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/send_button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/send_button"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</RelativeLayout>
here is where I try to get the height
ViewTreeObserver obs = imageView.getViewTreeObserver();
if(obs != null){
obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
if(image != null){
imageView.getViewTreeObserver().removeOnPreDrawListener(this);
int height = imageView.getHeight(); // 0
int width = imageView.getWidth(); // 720
}
return false;
}
});
}
the width displays 720 but the height always displays 0.
putting in a hard width also has no effect
This should work with an OnGlobalLayoutListener like so
final ImageView image = new ImageView(this);
image.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
// TODO Auto-generated method stub
int height = image.getHeight();
}
});
This callback is invoked when the global layout state or the visibility changes

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