Reputation: 4727
I have one relative view that contains one listview. I am creating this relative view on run time and add contents (basically an string array) to my list view. I have set the listview width and height to "wrap_content". The problem is my list parent relative view is not resizing to show my listview. it just shows first value of the list . I understand I have to override onmeasure method. So, how exactly can I get the height of my listview in onmeasure method so i can pass it to my parent view I have read many problems like this but couldn't get it working.
One more thing getMeasuredHeight() returns 0 in my onmeasure() method.
Update
my main layout activity_main
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_route_search"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</RelativeLayout>
</ScrollView>
lo_block.xml
<?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"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text_view" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
here is my code
Frag.java
public class Frag extends Fragment {
private View mView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.activity_main, null);
showView();
return mView;
}
public void showView() {
block card = new block(getActivity());
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
card.setLayoutParams(params);
ViewGroup vg = (ViewGroup) mView.findViewById(R.id.layout_main);
vg.addView(card);
}
private class block extends RelativeLayout {
// int listViewh = 0; // private float listViewHeight;
public block(Context context) {
super(context);
View mView = View.inflate(getContext(), R.layout.lo_block, this);
ListView lst = (ListView) mView.findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1,
new String[] { "ad", "sfsd", "sfsf", "asfsfs" });
lst.setAdapter(adapter);
}
public block(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public int getMinimumHeight() {
return super.getMinimumHeight();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int w = MeasureSpec.getSize(widthMeasureSpec);
int h = MeasureSpec.getSize(heightMeasureSpec);
// setMeasuredDimension(w, h); }
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
My main activity public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getFragmentManager().beginTransaction().replace(R.id.fragment,
new Frag()).commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Upvotes: 2
Views: 6595
Reputation: 4727
Exact problem in my case is I am trying to put listview inside scrollview. and thats NOT RECOMMENDED in general. But I have managed to expand my listview. though its not scroll able its better then before. Below is the code I have added in my constructor
ListView lst = (ListView) mView.findViewById(R.id.listView1);
String[] arrayList = new String[] { "ad", "sfsd", "sfsf", "asfsfs",
"ssfsff", "llkllk", "4323" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1,
arrayList);
int totalHeight = 0;
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, lst);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
LayoutParams lp = (LayoutParams) lst.getLayoutParams();
int height = totalHeight;
// arraylist list is in which all data is kept
lp.height = height;
lst.setLayoutParams(lp);
lst.setAdapter(adapter);
Upvotes: 2