Measuring switchCompat throws exception - android

Here is my code for measuring height of items inside a listview and setting the list view height based on the total height of it's children.
I do not have any problems when using any other view.
public static void setListViewHeightBasedOnChildren(TwoWayView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
int totalHeight = 0;
View listItem = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
listItem = listAdapter.getView(i, listItem, listView);
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
}
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED); // this is where the exception happens
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getItemMargin() * (listAdapter.getCount() - 1)) + listView.getPaddingTop() + listView.getPaddingBottom();
listView.setLayoutParams(params);
listView.requestLayout();
}
Here's the stack trace:
java.lang.NullPointerException
at android.text.StaticLayout.<init>(StaticLayout.java:50)
at android.support.v7.widget.SwitchCompat.makeLayout(SwitchCompat.java:606)
at android.support.v7.widget.SwitchCompat.onMeasure(SwitchCompat.java:526)
at android.view.View.measure(View.java:15395)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4826)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1038)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:576)
at android.view.View.measure(View.java:15395)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4826)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1038)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:576)
at android.view.View.measure(View.java:15395)

In Method onMeasue of SwtichCompat, if showText is true, it will make layout with textOn and textOff:
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mShowText) {
if (mOnLayout == null) {
mOnLayout = makeLayout(mTextOn);
}
if (mOffLayout == null) {
mOffLayout = makeLayout(mTextOff);
}
}
...
}
Have a look at method makeLayout:
private Layout makeLayout(CharSequence text) {
final CharSequence transformed = (mSwitchTransformationMethod != null)
? mSwitchTransformationMethod.getTransformation(text, this)
: text;
return new StaticLayout(transformed, mTextPaint,
transformed != null ?
(int) Math.ceil(Layout.getDesiredWidth(transformed, mTextPaint)) : 0,
Layout.Alignment.ALIGN_NORMAL, 1.f, 0, true);
}
If the param text is null, transformed will be null too. Then it will be passed to the constructor of StaticLayout, causing NullPointException.
So, you should set the attributes android:textOn and android:textOff for SwitchCompat, or set app:showText="false":
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="#string/text_on"
android:textOff="#string/text_off" />
or
<android.support.v7.widget.SwitchCompat
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:showText="false" />

SwitchCompat requires you to specify android:textOn and android:textOff
example, you can change like this
<android.support.v7.widget.SwitchCompat
android:id="#+id/on_off_switch"
android:textOn="On"
android:textOff="Off"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"/>

Related

How to set the max height of a listView in android

I put the listView in a popupWindow, when it has many items, the popupWindow exceed the screen size, so I want to limit the max height(or max item, show scroll bar if exceed) of the listView, thanks all
Try this it may be help to you
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
for (int i = 0; i < 5; i++) //here you can set 5 row at a time if row excceded the scroll automatically available
{
View listItem = listAdapter.getView(i, null, listView);
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
and you can use like this
YourAdapter mYoursAdapter = new YourAdapter();
mListview.setAdapter(mYoursAdapter);
setListViewHeightBasedOnChildren(mListview);
Create a class AppUtil in your project.Try like this:
public class AppUtil{
public static void setListViewHeightBasedOnChildren(ListView listView) {
if (listView == null) {
return;
}
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
}
To call the above method in your activity or fragment you can proceed like this:
public class MedicalReportsFragment extends Fragment {
#Bind(R.id.lv_investigative_report)
ListView investigativeReportLV;
private MedicalReportsAdapter mMedicalReportsAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mMedicalReportsAdapter = new MedicalReportsAdapter(getActivity(), mMedicalReportOverviews,true);
investigativeReportLV.setAdapter(mMedicalReportsAdapter);
AppUtil.setListViewHeightBasedOnChildren(investigativeReportLV);
}
}
I think you can try this trick in your layout.xml:
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp" />
<LinearLayout
android:id="#+id/layout_bottom_button"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="-40dp" />
In fact the marginBottom will reserve 40dp for the layouts below ListView, so your ListView would not cover the whole screen like before.

Get height of ListView with custom adapter

I have listView within scrollView (yes, I know thats bad approach. Better to use headers/footers). I know that scrollView and listView have both scroll and that causes some conflict. In this case I need to set height of list manually. And thats the problem since listView has custom adapter.
Layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_lessons"
android:background="#FAF9F9"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.cbsystematic.mobile.itvdn.LessonsFragment">
<TextView
android:id="#+id/course_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Описание курса: "
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#000000"/>
<TextView
android:id="#+id/course_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_below="#id/course_title"
android:textColor="#000000"/>
<TextView
android:id="#+id/course_duration"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textStyle="italic"
android:layout_below="#id/course_description"
android:textColor="#000000"/>
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/course_duration"/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
Custom List adapter:
class LessonsItemAdapter extends ArrayAdapter<LessonData> {
private Activity myContext;
private ArrayList<LessonData> datas;
public LessonsItemAdapter(Context context, int textViewResourceId, ArrayList<LessonData> objects){
super(context, textViewResourceId, objects);
myContext = (Activity) context;
datas = objects;
}
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder viewHolder;
if(convertView == null) {
LayoutInflater inflater = myContext.getLayoutInflater();
convertView = inflater.inflate(R.layout.lessons_list_item, null);
viewHolder = new ViewHolder();
viewHolder.postNameView = (TextView) convertView.findViewById(R.id.lessons_name);
viewHolder.postDurationView = (TextView) convertView.findViewById(R.id.lessons_duration);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
//(position + 1) + ") " used to display 1) 2) 3) etc of lessons
viewHolder.postNameView.setText((position + 1) + ") " + datas.get(position).getName());
viewHolder.postDurationView.setText("Длительность: " + datas.get(position).getDuration().substring(0,8));
return convertView;
}
static class ViewHolder{
TextView postNameView;
TextView postDurationView;
}
}
Method of finding height:
public static void setListViewSize(AbsListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, AbsListView.LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getMeasuredHeight() * (listAdapter.getCount() + 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
Thats how I set the content of list:
mAdapter = new LessonsItemAdapter(getActivity(),
R.layout.lessons_list_item, parserJson.getLessons(courseUrl).getLessonsArray());
// Set the adapter
mListView = (AbsListView) view.findViewById(android.R.id.list);
((AdapterView<ListAdapter>) mListView).setAdapter(mAdapter);
// Set OnItemClickListener so we can be notified on item clicks
mListView.setOnItemClickListener(this);
ListHelper.setListViewSize(mListView);
And thats the result:
The entire View is scrolling. But the last item always shows only by half. You see, the field Duration ("Длительность") is not showing. Although the last item of list is clicakble.
Please help me with that method of defining the total height.
Thanks in advance. Appreciate any help.
Use this piece of code. It will reset the size of listview according to number of items.
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(),
MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,
LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
Your ListView in your layout file should be like
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/course_duration"/>
Now in your code where you want the ListView's height to be adjusted according to its children place the following method.
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
just call it by passing your ListView inside it
setListViewHeightBasedOnChildren(list);
Simple but powerful solution
try code below from this blog
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int newHeight = 0;
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY)
{
ListAdapter listAdapter = getAdapter();
if (listAdapter != null && !listAdapter.isEmpty())
{
int listPosition = 0;
for (listPosition = 0; listPosition < listAdapter.getCount()
&& listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++)
{
View listItem = listAdapter.getView(listPosition, null, this);
//now it will not throw a NPE if listItem is a ViewGroup instance
if (listItem instanceof ViewGroup)
{
listItem.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
listItem.measure(widthMeasureSpec, heightMeasureSpec);
newHeight += listItem.getMeasuredHeight();
}
newHeight += getDividerHeight() * listPosition;
}
if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize))
{
if (newHeight > heightSize)
{
newHeight = heightSize;
}
}
}
else
{
newHeight = getMeasuredHeight();
}
setMeasuredDimension(getMeasuredWidth(), newHeight);
}

Expandable listview inside scrollview

I have few contents follwed by Expandable listview.I am not able to scroll the entire layout.
I have been searching for appropriate answer more than a week..Suggest some answers.
This solution worked for me, when i used custom layout inside navigation view having scroll view with LinearLayout which has Expandable listview.
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header">
<ScrollView
android:fillViewport="true"
android:layout_marginTop="130dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/homeLayout"
android:clickable="true"
android:gravity="center_vertical"
android:background="#drawable/layout_click_effect"
android:layout_width="match_parent"
android:layout_height="45dp">
<ImageView
android:id="#+id/homeIv"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:src="#mipmap/ic_home_black_24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home" />
</LinearLayout>
<View
android:background="#android:color/darker_gray"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<ExpandableListView
android:id="#+id/topCatgExpLv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:groupIndicator="#null"
android:dividerHeight="1dp" />
</ScrollView>
</android.support.design.widget.NavigationView>
In your onCreate
mListView = (ExpandableListView) findViewById(R.id.activity_expandable_list_view);
MyExpandableListAdapter adapter = new MyExpandableListAdapter(this,
mGroups);
mListView.setAdapter(adapter);
mListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
setListViewHeight(parent, groupPosition);
return false;
}
});
private void setListViewHeight(ExpandableListView listView, int group) {
ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter();
int totalHeight = 0;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
View.MeasureSpec.EXACTLY);
for (int i = 0; i < listAdapter.getGroupCount(); i++) {
View groupItem = listAdapter.getGroupView(i, false, null, listView);
groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += groupItem.getMeasuredHeight();
if (((listView.isGroupExpanded(i)) && (i != group))
|| ((!listView.isGroupExpanded(i)) && (i == group))) {
for (int j = 0; j < listAdapter.getChildrenCount(i); j++) {
View listItem = listAdapter.getChildView(i, j, false, null,
listView);
listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
//Add Divider Height
totalHeight += listView.getDividerHeight() * (listAdapter.getChildrenCount(i) - 1);
}
}
//Add Divider Height
totalHeight += listView.getDividerHeight() * (listAdapter.getGroupCount() - 1);
ViewGroup.LayoutParams params = listView.getLayoutParams();
int height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
if (height < 10)
height = 200;
params.height = height;
listView.setLayoutParams(params);
listView.requestLayout();
}
you can do this very simple . set your height of scroll view to wrap_content. set your height of root view inside scroll view as wrap_content (such as linear layout) and set your expandable list view to wrap_content. after that set OnGroupExpandListener and OnGroupCollapseListener for your expandable list view and set height for that as long as child of expandable list view. like this code :
your layout :
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollViewDrawer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/mainGray"
>
<ExpandableListView
android:id="#+id/expandableListCategory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</ScrollView>
and this is your fragment: (or in your activity)
public class DrawerFragment extends Fragment implements, OnGroupExpandListener, OnGroupCollapseListener{
ScrollView scrollView;
ExpandableListView expListView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view rootView = inflater.inflate(R.layout.fragment_layout, container, false);
scrollView = (ScrollView) rootView.findViewById(R.id.scrollViewDrawer);
expListView = (ExpandableListView) rootView.findViewById(R.id.expandableListCategory);
....
}
#Override
public void onGroupExpand(int groupPosition) {
LinearLayout.LayoutParams param = (LinearLayout.LayoutParams) expListView.getLayoutParams();
param.height = (childCount * expListView.getHeight());
expListView.setLayoutParams(param);
expListView.refreshDrawableState();
scrollView.refreshDrawableState();
}
#Override
public void onGroupCollapse(int groupPosition) {
LinearLayout.LayoutParams param = (LinearLayout.LayoutParams) expListView.getLayoutParams();
param.height = LinearLayout.LayoutParams.WRAP_CONTENT;
expListView.setLayoutParams(param);
expListView.refreshDrawableState();
scrollView.refreshDrawableState();
}
I know the question is pretty old, but may be it will be helpful for someone. Basically my answer is some what a combination of the answers of Amit Tumkur and user2141833. After a lot of trial and error, the following code is working for me:
First calculating the initial height of the Expandable list view i.e. when the whole thing is collapsed
for (Integer i = 0; i < mAdapter.getGroupCount(); i++) {
View groupItem = mAdapter.getGroupView(i, false, null, mExpandableListView);
groupItem.measure(mExpandableListView.getWidth(), View.MeasureSpec.UNSPECIFIED);
mInitialHeight += groupItem.getMeasuredHeight();
}
Then when the group is clicked set the height of the Expandable List view to Wrap Content
mExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
//Other Expansion/Collapsing Logic
setListHeightToWrap();
return true;
}
});
setListHeightToWrap is a different method:
private void setListHeightToWrap() {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mExpandableListView.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
mExpandableListView.setLayoutParams(params);
mExpandableListView.refreshDrawableState();
mScrollView.refreshDrawableState();
}
Then in OnGroupExpandListener set the height of the Expandable List View as:
mExpandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mStitchingWorksListView.getLayoutParams();
//The Logic here will change as per your requirements and the height of each of the children in the group
if (mAdapter.getRealChildrenCount(groupPosition) > 6) {
params.height = 9 * mInitialHeight;
} else {
params.height = 6 * mInitialHeight;
}
//For Last Group in the list and the number of children were less as compared to other groups
if (groupPosition == mAdapter.getGroupCount() - 1) {
params.height = 3 * mInitialHeight;
}
mExpandableListView.setLayoutParams(params);
mExpandableListView.refreshDrawableState();
}
});
Also the layout was ExpandableListView inside LinearLayout inside ScrollView.
Hope this helps someone. :)
Firstly I suppose you are not using more than one child layout inside a scroll view, as a scroll view can have only one direct child.
Secondly you should not use any scrolling component like expandable list, inside a scroll view
here's why ListView inside ScrollView is not scrolling on Android.
For users who are looking for expandable listview into scrollview, use views instead of expandable list follow the below link & code-
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/linear_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
And In Java Class some thing like this-
for (int i = 0; i < pProductArrayList.size(); i++) {
LayoutInflater inflater = null;
inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mLinearView = inflater.inflate(R.layout.row_first, null);
final TextView mProductName = (TextView) mLinearView.findViewById(R.id.textViewName);
final RelativeLayout mLinearFirstArrow=(RelativeLayout)mLinearView.findViewById(R.id.linearFirst);
final ImageView mImageArrowFirst=(ImageView)mLinearView.findViewById(R.id.imageFirstArrow);
final LinearLayout mLinearScrollSecond=(LinearLayout)mLinearView.findViewById(R.id.linear_scroll);
if(isFirstViewClick==false){
mLinearScrollSecond.setVisibility(View.GONE);
mImageArrowFirst.setBackgroundResource(R.drawable.arw_lt);
}
else{
mLinearScrollSecond.setVisibility(View.VISIBLE);
mImageArrowFirst.setBackgroundResource(R.drawable.arw_down);
}
mLinearFirstArrow.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(isFirstViewClick==false){
isFirstViewClick=true;
mImageArrowFirst.setBackgroundResource(R.drawable.arw_down);
mLinearScrollSecond.setVisibility(View.VISIBLE);
}else{
isFirstViewClick=false;
mImageArrowFirst.setBackgroundResource(R.drawable.arw_lt);
mLinearScrollSecond.setVisibility(View.GONE);
}
return false;
}
});
Enter the code below adapteSet
enter code hereListAdapter listAdapter = listView.getAdapter();
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
Plese, try this once.
1) Add this line setExpandableListViewHeight(expandableListViewCategories) right after setting your adapter.
expandableListCategoriesAdapter = new ExpandableListCategoriesAdapter(getActivity(), listDataHeader, listDataChild);
expandableListViewCategories.setAdapter(expandableListCategoriesAdapter);
expandableListCategoriesAdapter.notifyDataSetChanged();
setExpandableListViewHeight(expandableListView);
2) Coppy the bellow content and past it in to setExpandableListViewHeight() method.
private void setExpandableListViewHeight(ExpandableListView listView) {
try {
ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter();
int totalHeight = 0;
for (int i = 0; i < listAdapter.getGroupCount(); i++) {
View listItem = listAdapter.getGroupView(i, false, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
int height = totalHeight + (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
if (height < 10) height = 200;
params.height = height;
listView.setLayoutParams(params);
listView.requestLayout();
scrollBody.post(new Runnable() {
public void run() {
scrollBody.fullScroll(ScrollView.FOCUS_UP);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
Note: This will automatically bring down your ExpandableListView and you can scroll with full view.
After trying too many solutions,i had combined two solutions any it's work properly menuHeight is important.
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
LinearLayout.LayoutParams param = (LinearLayout.LayoutParams)
expandableListView.getLayoutParams();
param.height = (2 * menuHeight); //adjust 2 according to you
expandableListView.setLayoutParams(param);
expandableListView.refreshDrawableState();
findViewById(R.id.sv_menu).refreshDrawableState();
}
});
static int menuHeight =0; // declare globally
public static void setListViewHeightBasedOnChildren(ExpandableListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) return;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
View.MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0) view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
menuHeight = totalHeight;}
Starting from API Level 21 (Lollipop) nested scroll containers are officially supported by Android SDK. There're a bunch of methods in View and ViewGroup classes which provide this functionality. To make nested scrolling work on the Lollipop you have to enable it for a child scroll view by adding android:nestedScrollingEnabled="true" to its XML declaration or by explicitly calling setNestedScrollingEnabled(true).

how to scroll an expandable list view till its end?

I made a class extending ExpandableListView. Following is the code :
class CustomExpandableListView extends ExpandableListView {
public CustomExpandableListView(Context context) {
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
/*
* Adjust height
*/
heightMeasureSpec = MeasureSpec.makeMeasureSpec(
700, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
And I used it as.
ExpandableListView list = new CustomExpandableListView(KaasMain.this);
Adapter adapter = new Adapter(KaasMain.this, objectsLvl1);
list.setAdapter(adapter);
parent.addView(list);
// list.setGroupIndicator();
list.setTranscriptMode(ExpandableListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
list.setIndicatorBounds(0, 0);
list.setChildIndicatorBounds(0, 0);
I also set its transcript mode TRANSCRIPT_MODE_ALWAYS_SCROLL. But its not scrolling till the end if i clicking on multiple items then the length increases and its hiding end items.
I want something like this:
You need to use these parameters in your list view:
list.setTranscriptMode(ExpandableListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
Set the head of the list to it bottom
list.setStackFromBottom(true);
You can also set in your xml
android:transcriptMode="alwaysScroll"
Try this:
list.smoothScrollToPosition(list.getCount() - 1);
If need you try to scroll right away after updating the list, see this answer:
How to scroll to the bottom of ListView programmatically?
Step 1: Create Layout Using this:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="#dimen/dp10"
android:layout_marginTop="#dimen/toolbar_after_margin"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ExpandableListView
android:id="#+id/expand"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/dp10"
android:dividerHeight="1dp"
android:indicatorLeft="#dimen/dp20"
android:layoutDirection="rtl" />
</LinearLayout>
</ScrollView>
Step 2:
Create A Function as below
public void setListViewHeight(ExpandableListView listView,
int group,int type) {
ExpandableListAdapter listAdapter = listView.getExpandableListAdapter();
int totalHeight = 0;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
View.MeasureSpec.EXACTLY);
for (int i = 0; i < listAdapter.getGroupCount(); i++) {
View groupItem = listAdapter.getGroupView(i, false, null, listView);
groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += groupItem.getMeasuredHeight();
if(type!=1){
if (((listView.isGroupExpanded(i)) && (i != group))
|| ((!listView.isGroupExpanded(i)) && (i == group))) {
for (int j = 0; j < listAdapter.getChildrenCount(i); j++) {
View listItem = listAdapter.getChildView(i, j, false, null,
listView);
listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
}
}
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
int height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
if (height < 10)
height = 200;
params.height = height;
listView.setLayoutParams(params);
listView.requestLayout();
}
Step3: Call setListViewHeight on groupClickListener
elv_categories.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
setListViewHeight(parent, groupPosition, 2);
return false;
}
});
}
step4 Call setListViewHeight() after Adapter Set
Categories categories[] = response_data.getCategories();
elv_categories.setAdapter(new ExpandableListAdapter(categories, getApplicationContext()));
setListViewHeight(elv_categories, 0, 1);

Set Height of ListView at run time in android

How to set Height of ListView.
XML Code (This code is working fine and fixed height as 200 dip)
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip"
android:orientation="vertical" >
<ListView
android:id="#+id/dialog_ListView"
android:layout_width="wrap_content"
android:layout_height="200dip"
android:padding="5dip"
android:layout_weight="1"
android:dividerHeight="5dip"
android:divider="#android:color/transparent" />
</LinearLayout>
Android Code (This code not fixed width and height)
convertView = mInflater.inflate( R.layout.dialog_page, null );
convertView.setLayoutParams( new ListView.LayoutParams( 200, 300) );
Android Code (This code i got Error Message)
convertView = mInflater.inflate( R.layout.lock_file_page, parent );
convertView.setLayoutParams( new ListView.LayoutParams( 200, 300) );
Error Message
java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
How do fix Width and Height at Runtime.
Thanks in advance.
Try This code..
LayoutParams lp = (LayoutParams) mListView.getLayoutParams();
lp.height = 300;
mListView.setLayoutParams(lp);
public static boolean setListViewHeightBasedOnItems(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter != null) {
int numberOfItems = listAdapter.getCount();
// Get total height of all items.
int totalItemsHeight = 0;
for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
View item = listAdapter.getView(itemPos, null, listView);
item.measure(0, 0);
totalItemsHeight += item.getMeasuredHeight();
}
// Get total height of all item dividers.
int totalDividersHeight = listView.getDividerHeight() *
(numberOfItems - 1);
// Set list height.
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalItemsHeight + totalDividersHeight;
listView.setLayoutParams(params);
listView.requestLayout();
return true;
} else {
return false;
}
}
First Remove import files for LayoutParams and after that Use below code for LayoutParams.
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(200, 300);
convertView.setLayoutParams(params);
try this
public void setDynamicHeight(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
if(listItem != null){
listItem.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();}

Categories

Resources