ScrollView not scrolling down completely in XML in Android - android

I have a XML code which displays the Cricket scorecard of a team.
This is the my scorecard.xml code where I am using NonScrollableList under Linear Layout in Scrollview:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fillViewport="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/windowBackground"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="50.0dip"
android:layout_marginLeft="10.0dip"
android:layout_marginTop="5.0dip"
android:layout_marginRight="10.0dip"
android:background="#drawable/scorecard_uperlayout">
<TextView
android:id="#+id/Batteam_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="10.0dip"
android:layout_marginLeft="10.0dip"
android:text="Aus"
android:textColor="#color/textColorPrimary"
android:textSize="18.0sp" />
</RelativeLayout>
<LinearLayout
android:id="#id/tabllayout"
android:layout_width="fill_parent"
android:layout_height="40.0dip"
android:layout_marginLeft="10.0dip"
android:layout_marginTop="5.0dip"
android:layout_marginRight="10.0dip"
android:background="#color/graycolor2"
android:visibility="visible"
android:weightSum="11.0">
<RelativeLayout
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="5.5">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10.0dip"
android:layout_marginLeft="10.0dip"
android:text="Batsman"
android:textColor="#color/textcolor2"
android:textSize="18.0sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.0">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="R"
android:textColor="#color/textcolor2"
android:textSize="18.0sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.0">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
android:textColor="#color/textcolor2"
android:textSize="18.0sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.0">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4s"
android:textColor="#color/textcolor2"
android:textSize="18.0sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.0">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6s"
android:textColor="#color/textcolor2"
android:textSize="18.0sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2.0">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SR"
android:textColor="#color/textcolor2"
android:textSize="18.0sp" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10.0dip"
android:layout_marginTop="5.0dip"
android:layout_marginRight="10.0dip">
<com.example.live_score.activity.NonScrollableList
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10.0dip"
android:layout_marginLeft="10.0dip"
android:layout_marginTop="5.0dip"
android:layout_marginRight="10.0dip"
android:background="#color/layoutBackground"
android:visibility="visible">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10.0dip"
android:layout_marginLeft="10.0dip"
android:text=" Extras"
android:textColor="#color/textcolor"
android:textSize="16.0sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10.0dip"
android:layout_marginLeft="10.0dip"
android:layout_marginTop="5.0dip"
android:layout_marginRight="10.0dip"
android:background="#color/layoutBackground"
android:visibility="visible">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10.0dip"
android:layout_marginLeft="10.0dip"
android:text="Yet to Bat"
android:textColor="#color/textcolor"
android:textSize="16.0sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/tabllayout_bowl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10.0dip"
android:layout_marginTop="5.0dip"
android:layout_marginRight="10.0dip"
android:background="#color/graycolor2"
android:visibility="visible"
android:weightSum="11.0">
<RelativeLayout
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="5.3">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="10.0dip"
android:layout_marginLeft="10.0dip"
android:text="Bowler"
android:textColor="#color/textcolor2"
android:textSize="18.0sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.0">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="O"
android:textColor="#color/textcolor2"
android:textSize="18.0sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.0">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="M"
android:textColor="#color/textcolor2"
android:textSize="18.0sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.0">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="R"
android:textColor="#color/textcolor2"
android:textSize="18.0sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.0">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="W"
android:textColor="#color/textcolor2"
android:textSize="18.0sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.0">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wd"
android:textColor="#color/textcolor2"
android:textSize="18.0sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.0">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nb"
android:textColor="#color/textcolor2"
android:textSize="18.0sp" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10.0dip"
android:layout_marginTop="5.0dip"
android:layout_marginRight="10.0dip"
android:visibility="visible">
<com.example.live_score.activity.NonScrollableList
android:id="#+id/list_bowlings"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="40.0dip"
android:layout_marginLeft="10.0dip"
android:layout_marginTop="5.0dip"
android:layout_marginRight="10.0dip"
android:background="#color/graycolor2"
android:visibility="visible"
android:weightSum="9.0">
<RelativeLayout
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="6.0">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:layout_marginStart="10.0dip"
android:layout_marginLeft="10.0dip"
android:text="Fall of Wickets"
android:textColor="#color/textcolor2"
android:textSize="18.0sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.0">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="W"
android:textColor="#color/textcolor2"
android:textSize="18.0sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.0">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="R"
android:textColor="#color/textcolor2"
android:textSize="18.0sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.0">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="O"
android:textColor="#color/textcolor2"
android:textSize="18.0sp" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
Below is the NonScrollableList activity code:
public class NonScrollableList extends ListView {
public NonScrollableList(Context context) {
super(context);
}
public NonScrollableList(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollableList(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}
Below is onCreateView for the above code:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.scorecard, container, false);
listView = (NonScrollableList) rootView.findViewById(R.id.list);
list_bowlings = (NonScrollableList) rootView.findViewById(R.id.list_bowlings);
}
But the problem I am not able to scroll down completely. Where do I need to add more height to fix the scroll ? Can anyone help ? Thanks in advance

just use
android:nestedScrollingEnabled="true" in ListView

Try using this custom ListView -
Custom ListView:
public class MyCustomListView extends ListView {
public MyCustomListView(Context context) {
super(context);
}
public MyCustomListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyCustomListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightSpec;
if (getLayoutParams().height == LayoutParams.FILL_PARENT) {
heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
} else {
heightSpec = heightMeasureSpec;
}
super.onMeasure(widthMeasureSpec, heightSpec);
}
}
In XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/slistLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/Batteam_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="10.0dip"
android:layout_marginLeft="10.0dip"
android:text="Aus"
android:textColor="#color/textColorPrimary"
android:textSize="18.0sp" />
<your_packagename.MyCustomListView
android:id="#+id/pListView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
android:clickable="true"
android:descendantFocusability="blocksDescendants"
android:fadeScrollbars="false"
android:focusable="true"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarStyle="insideInset" />
<your_packagename.MyCustomListView
android:id="#+id/pListView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
android:clickable="true"
android:descendantFocusability="blocksDescendants"
android:fadeScrollbars="false"
android:focusable="true"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarStyle="insideInset" />
</LinearLayout>
</ScrollView>

In your xml code make sure you have only one child inside ScrollView, also change the height of the LinearLayout inside ScrollView from "wrap_content" to "match_parent".

Related

How to limit ListView to particular number of list items?

I am working on a functionality in which I have a ListView and I want listView to show only 3 items. I have set ListView height to "wrap_content" and in adapter I have set get count to 3 so that it will show only 3 items but it is showing only 1 item. I am not able to figure out what is happening.My API is returing only 3 items but not able to show all the items in ListView.
Adapter Code:
public class Adapter3Testimonials extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
private Context context;
public Adapter3Testimonials(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
this.data = d;
context = a;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View itemView = inflater.inflate(R.layout.item_3_testimonials, parent, false);
TextView txtTestimonial = (TextView) itemView.findViewById(R.id.txtAdvisorTestimonails);
Log.e("TAG", "item_3_testimonials: " + position);
HashMap<String, String> testimonialsList;
testimonialsList = new HashMap<String, String>();
testimonialsList = data.get(position);
txtTestimonial.setText(testimonialsList.get("description"));
return itemView;
}
}
Xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dp10"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dp5">
<FrameLayout
android:id="#+id/layImageProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/dp5"
android:background="#drawable/half_blue_circle">
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image"
android:layout_width="#dimen/dp80"
android:layout_height="#dimen/dp80"
android:padding="#dimen/dp2"
android:src="#drawable/blank_profile" />
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher_background"
android:visibility="gone"
app:civ_border_width="2dp" />
<ImageView
android:id="#+id/imgBlockUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/profile_image2"
android:layout_gravity="right"
android:src="#drawable/phone" />
</FrameLayout>
<TextView
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="#dimen/dp13"
android:layout_toRightOf="#+id/layImageProfile"
android:text="John doe"
android:textColor="#android:color/black"
android:textSize="#dimen/dp15" />
<TextView
android:id="#+id/txtFirmName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/txtName"
android:layout_alignStart="#id/txtName"
android:layout_below="#id/txtName"
android:text="Demo Corp. Inc."
android:textColor="#android:color/black" />
<TextView
android:id="#+id/txtRnf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/txtFirmName"
android:layout_alignStart="#id/txtFirmName"
android:layout_below="#id/txtFirmName"
android:text="Rnf No #12234"
android:textColor="#android:color/black" />
<com.iarcuschin.simpleratingbar.SimpleRatingBar
android:id="#+id/imgReviewStarts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/txtRnf"
android:layout_below="#id/txtRnf"
android:layout_marginTop="#dimen/dp5"
android:layout_toRightOf="#id/layImageProfile"
app:srb_borderColor="#color/colorPrimary"
app:srb_fillColor="#color/colorPrimary"
app:srb_isIndicator="true"
app:srb_numberOfStars="5"
app:srb_starBorderWidth="1.0"
app:srb_starCornerRadius="2.5"
app:srb_starSize="#dimen/dp15"
app:srb_stepSize="1" />
<ImageView
android:id="#+id/imgClickToConnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="#dimen/dp25"
android:src="#drawable/click" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="#dimen/dp1"
android:layout_marginTop="#dimen/dp10"
android:background="#color/colorPrimary" />
<TextView
android:id="#+id/txtStatement"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dp5"
android:background="#color/colorPrimary"
android:paddingBottom="#dimen/dp5"
android:paddingLeft="#dimen/dp5"
android:paddingTop="#dimen/dp3"
android:text="Mission statement Lorum ipsum dolar sit or elit lamet,consector cillium"
android:textColor="#android:color/white" />
<View
android:layout_width="match_parent"
android:layout_height="#dimen/dp1"
android:layout_marginTop="#dimen/dp5"
android:background="#color/colorPrimary" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dp5"
android:paddingLeft="#dimen/dp10"
android:text="#string/customer_review_score"
android:textColor="#android:color/black"
android:textSize="#dimen/dp15" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/dp10"
android:progress="100"
android:progressTint="#android:color/black" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dp10"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buying_exp" />
<com.iarcuschin.simpleratingbar.SimpleRatingBar
android:id="#+id/imgBuyingExp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtRnf"
android:layout_marginLeft="#dimen/dp20"
app:srb_borderColor="#color/colorPrimary"
app:srb_fillColor="#color/colorPrimary"
app:srb_isIndicator="true"
app:srb_numberOfStars="5"
app:srb_starBorderWidth="1.0"
app:srb_starCornerRadius="2.5"
app:srb_starSize="#dimen/dp15"
app:srb_stepSize="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dp10"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/knowledge_comp" />
<com.iarcuschin.simpleratingbar.SimpleRatingBar
android:id="#+id/imgKnowledgeComp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtRnf"
android:layout_marginLeft="#dimen/dp20"
app:srb_borderColor="#color/colorPrimary"
app:srb_fillColor="#color/colorPrimary"
app:srb_isIndicator="true"
app:srb_numberOfStars="5"
app:srb_starBorderWidth="1.0"
app:srb_starCornerRadius="2.5"
app:srb_starSize="#dimen/dp15"
app:srb_stepSize="1" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dp10"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.9"
android:text="#string/percentage_of_customer" />
<TextView
android:id="#+id/txtPercentageRecommended"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/dp20"
android:layout_toRightOf="#id/txt"
android:text="100%" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="#dimen/dp1"
android:layout_marginTop="#dimen/dp10"
android:background="#color/colorPrimary" />
<LinearLayout
android:id="#+id/layTestimonials"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dp5"
android:background="#color/colorPrimary"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/dp10"
android:paddingTop="#dimen/dp10"
android:text="#string/customer_testimonials"
android:textColor="#android:color/white"
android:textSize="#dimen/dp15" />
<TextView
android:id="#+id/btnTestimonialNumber"
android:layout_width="#dimen/dp35"
android:layout_height="#dimen/dp35"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/dp10"
android:layout_marginTop="#dimen/dp5"
android:layout_toRightOf="#id/textView"
android:background="#drawable/round_button"
android:padding="#dimen/dp5"
android:text=""
android:textAlignment="center"
android:textColor="#color/colorPrimary" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView"
android:paddingLeft="#dimen/dp10"
android:progress="100"
android:progressTint="#android:color/white" />
</RelativeLayout>
<TextView
android:id="#+id/noTestimonial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="#dimen/dp20"
android:layout_marginTop="#dimen/dp20"
android:gravity="center"
android:text="#string/no_testimonials"
android:textColor="#android:color/white"
android:visibility="gone" />
<ListView
android:id="#+id/listCustomer_testimonial"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dp10"
android:layout_marginTop="#dimen/dp10"
android:divider="#android:color/white">
</ListView>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="#dimen/dp1"
android:layout_marginTop="#dimen/dp5"
android:background="#color/colorPrimary" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dp10"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/dp5"
android:text="Years of experience:"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/txtAdExperience"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dp8"
android:text="2" />
</LinearLayout>
<TextView
android:id="#+id/txtExpertise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/dp5"
android:text="#string/expertise"
android:textColor="#android:color/black"
android:visibility="gone" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="#dimen/dp5"
android:orientation="horizontal">
<TextView
android:id="#+id/btnProtect_money"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dp5"
android:background="#drawable/bg_skyblue"
android:padding="#dimen/dp5"
android:text="#string/protect_money"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:visibility="gone" />
<TextView
android:id="#+id/btnGrow_money"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dp5"
android:background="#drawable/bg_skyblue"
android:padding="#dimen/dp5"
android:text="#string/grow_money"
android:textColor="#android:color/white"
android:visibility="gone" />
<TextView
android:id="#+id/btnMedical_Needs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dp5"
android:background="#drawable/bg_skyblue"
android:padding="#dimen/dp5"
android:text="#string/medical_need"
android:textColor="#android:color/white"
android:visibility="gone" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="#dimen/dp15"
android:layout_marginLeft="#dimen/dp10"
android:layout_marginTop="#dimen/dp10">
<TextView
android:id="#+id/txtSocialMedia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/find_me_on_social_media"
android:textColor="#android:color/black"
android:textSize="#dimen/dp15" />
<ImageView
android:id="#+id/imgFB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtSocialMedia"
android:layout_marginTop="#dimen/dp10"
android:src="#drawable/fb_icon" />
<ImageView
android:id="#+id/imgYoutube"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtSocialMedia"
android:layout_marginLeft="#dimen/dp5"
android:layout_marginTop="#dimen/dp10"
android:layout_toRightOf="#id/imgFB"
android:src="#drawable/youtube_icon" />
<ImageView
android:id="#+id/imgLinkden"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtSocialMedia"
android:layout_marginLeft="#dimen/dp5"
android:layout_marginTop="#dimen/dp10"
android:layout_toRightOf="#id/imgYoutube"
android:src="#drawable/linkedin_icon" />
<ImageView
android:id="#+id/imgRSS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtSocialMedia"
android:layout_marginLeft="#dimen/dp5"
android:layout_marginTop="#dimen/dp10"
android:layout_toRightOf="#id/imgLinkden"
android:src="#drawable/rss_icon" />
<ImageView
android:id="#+id/imgCartoon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/txtSocialMedia"
android:src="#drawable/man_after_feedback" />
<ImageView
android:id="#+id/imgRead_my_articles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#id/imgFB"
android:layout_marginTop="#dimen/dp10"
android:src="#drawable/read_my_articles" />
<ImageView
android:id="#+id/imgClick_for_feedback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/imgFB"
android:layout_marginRight="#dimen/dp10"
android:layout_marginTop="#dimen/dp10"
android:src="#drawable/click_for_feedback" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
<ProgressBar
android:id="#+id/pBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" />
Inside this code I have ListView with id "listCustomer_testimonial".
Never use ListView inside Scrollview. But if you must use ListView inside ScrollView create a scrollhandler.
public class scrollhandler {
public static void getListViewSize(ListView myListView) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null) {
//do nothing return null
return;
}
//set listAdapter in loop for getting final size
int totalHeight = 0;
int rowheight=56;
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, myListView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
rowheight=listItem.getMeasuredHeight();
}
//setting listview item in adapter
ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
myListView.setLayoutParams(params);
// print height of adapter on log
Log.i("height of listItem:", String.valueOf(totalHeight)+","+myListAdapter.getCount());
}
}
Then lastly bind the ListView with the Handler
lv.setAdapter(adapter);
scrollhandler.getListViewSize(lv);
Your problem is not with the getCount() method or list items count.
You have used listview inside scrollview, so only one item will be visible and others can be seen on scroll.
You can use custom Listview to avoid issue in height of listview,
public class NonScrollableListview extends ListView {
public NonScrollableListview(Context context) {
super(context);
}
public NonScrollableListview(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollableListview(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}
In your xml ,
<com.yourpack.NonScrollableListview
android:id="#+id/list_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Or use NestedScrollView with RecyclerView instead of listview.
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:fillViewport="true">
<!--otherviews-->
<android.support.v7.widget.RecyclerView
android:id="#+id/list_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!--otherviews-->
</android.support.v4.widget.NestedScrollView>

Android: List View and Scroll View compatability

A quick note about my application. This is an app that uses an API to get server requests about specific players for a game called League of Legends (LoL)
I recently changed my code from a static layout of 10 pre-set most recent matches to a ListView in order to avoid cludder as well in case a player did not have 10 recent matches.
The issue I am having with this ListView, is that when it is created, you can not scroll up and down through it. That is to say that it is created in a very small height, and when you scroll in it, the page doesn't scroll, the ListView itself scrolls. This is not desirable. I would rather have the user scroll the whole screen than just the small portion of the ListView.
I constructed my xml file like so:
<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"
tools:context=".MainActivity">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:paddingLeft="#dimen/activity_horizontal_margin">
<EditText
android:id="#+id/summoner_name"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:hint="Summoner Name"
android:imeOptions="actionSend"
android:lines="1" />
<Spinner
android:id="#+id/regions"
android:layout_width="60dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/search_button"
android:layout_width="45dp"
android:layout_height="45dp"
android:background="#drawable/searchbuttononclick" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="9dp"
android:orientation="horizontal"
android:paddingLeft="#dimen/activity_horizontal_margin">
<ImageView
android:id="#+id/leagueicon"
android:layout_width="160dp"
android:layout_height="150dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="20dp" />
<TextView
android:id="#+id/summoner_name_after_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="22dp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:id="#+id/league_tier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0060a8"
android:textSize="18dp" />
<TextView
android:id="#+id/league_points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textSize="18dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:id="#+id/ranked_wins"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff00b515"
android:textSize="18dp" />
<TextView
android:id="#+id/slash_between_wins_and_losses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textColor="#ff817a81"
android:textSize="18dp" />
<TextView
android:id="#+id/ranked_losses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textColor="#ffb5000f"
android:textSize="18dp" />
</LinearLayout>
<TextView
android:id="#+id/what_were_you_last_season"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textColor="#ff817a81"
android:textSize="18dp" />
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:textSize="16dp"
android:id="#+id/normal_title"
android:textStyle="bold"
android:textColor="#ff000000" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="120dp"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="120dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="38dp"
android:paddingLeft="5dp"
android:id="#+id/normal_wins"
android:gravity="center_vertical"
android:textSize="16dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="#+id/normal_space_one"/>
<TextView
android:layout_width="match_parent"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:textSize="16dp"
android:id="#+id/normal_kills"
android:layout_height="38dp"
/>
<TextView
android:layout_width="match_parent"
android:id="#+id/normal_space_two"
android:layout_height="2dp"/>
<TextView
android:layout_width="match_parent"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:id="#+id/normal_assists"
android:textSize="16dp"
android:layout_height="38dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="120dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="38dp"
android:gravity="center_vertical"
android:id="#+id/normal_wins_value"
android:paddingLeft="5dp"
android:textSize="16dp"
/>
<TextView
android:layout_width="match_parent"
android:id="#+id/normal_space_three"
android:layout_height="2dp"/>
<TextView
android:layout_width="match_parent"
android:gravity="center_vertical"
android:id="#+id/normal_kills_value"
android:textSize="16dp"
android:paddingLeft="5dp"
android:layout_height="38dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="#+id/normal_space_four"/>
<TextView
android:layout_width="match_parent"
android:id="#+id/normal_assists_value"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:textSize="16dp"
android:layout_height="38dp"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="120dp"
android:orientation="vertical"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="120dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="38dp"
android:paddingLeft="5dp"
android:id="#+id/normal_minion_kills"
android:gravity="center_vertical"
android:textSize="16dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="#+id/normal_space_five"/>
<TextView
android:layout_width="match_parent"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:textSize="14dp"
android:id="#+id/normal_neutral_minion_kills"
android:layout_height="38dp"
android:layout_gravity="center_horizontal"/>
<TextView
android:layout_width="match_parent"
android:id="#+id/normal_space_six"
android:layout_height="2dp"/>
<TextView
android:layout_width="match_parent"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:id="#+id/normal_turrets_destroyed"
android:textSize="12dp"
android:layout_height="38dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="120dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="38dp"
android:paddingLeft="5dp"
android:gravity="center_vertical"
android:id="#+id/normal_minion_kills_value"
android:textSize="16dp"
/>
<TextView
android:layout_width="match_parent"
android:id="#+id/normal_space_seven"
android:layout_height="2dp"/>
<TextView
android:layout_width="match_parent"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:id="#+id/neutral_minion_kills_value"
android:textSize="16dp"
android:layout_height="38dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="#+id/normal_space_eight"/>
<TextView
android:layout_width="match_parent"
android:id="#+id/normal_turrets_destroyed_value"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:textSize="16dp"
android:layout_height="38dp"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<com.ryanfolz.riotgamesapi.SwagObListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="20dp"></LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
As you see, I created a custom ListView called "SwagObListView"
Here is the code for that:
public class SwagObListView extends ListView implements AdapterView.OnItemClickListener {
private List<SwagOb> swagList;
private SwagObClickListener swagClicked;
public SwagObListView(Context context) {
super(context);
}
public SwagObListView(Context context, AttributeSet attrs) {
super(context, attrs);
this.swagList = swagList;
this.swagClicked = swagClicked;
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (swagClicked != null) {
swagClicked.onConnectionClicked(swagList.get(i));
}
}
public void setOnSwagObClickListener(SwagObClickListener c){
this.swagClicked = c;
}
public void setSwag(List<SwagOb> swagList) {
this.swagList = swagList;
SwagObAdapter swagDapter = new SwagObAdapter(getContext(), swagList);
setAdapter(swagDapter);
setOnItemClickListener(this);
}
}
As for the SwagOb class, here is the code for that:
public class SwagOb {
private String gameType;
private Drawable championPicturePlayed;
private String kills;
private String deaths;
private Drawable[] summonerSpells;
private String assists;
private String cs;
private String gold;
private boolean won;
private Drawable itemOne, itemTwo, itemThree, itemFour, itemFive, itemSix;
private Drawable[] tempDrawable;
private Drawable[] tempDrawable2;
private String[] tempString;
private SearchPlayerFragment activty;
private CollectUserData data;
public SwagOb(String gameType, Drawable championPicturePlayed, String kills, String deaths, Drawable[] summonerSpells,
String assists, String cs, String gold, boolean won, Drawable itemOne, Drawable itemTwo, Drawable itemThree, Drawable itemFour,
Drawable itemFive, Drawable itemSix, Drawable[] tempDrawable, Drawable[] tempDrawable2, String[] tempString, SearchPlayerFragment activity, CollectUserData data){
this.gameType = gameType;
this.championPicturePlayed = championPicturePlayed;
this.kills = kills;
this.deaths = deaths;
this.summonerSpells = summonerSpells;
this.itemFive = itemFive;
this.itemTwo = itemTwo;
this.itemOne = itemOne;
this.itemThree = itemThree;
this.itemFour = itemFour;
this.itemSix = itemSix;
this.assists = assists;
this.cs = cs;
this.gold = gold;
this.won = won;
this.tempDrawable = tempDrawable;
this.tempDrawable2 = tempDrawable2;
this.tempString = tempString;
this.activty = activity;
this.data = data;
}
(This also has getters and setters but that isn't important.)
Anyone know any ideas as to why this is acting this way?
Android does not allow to add ListView inside ScrollView. Possible workaround for this will be remove scrollview and adding top content to listivew header and bottom content into listview footer.
In your scroll view add this property That will do the trick
android:fillViewport="true"

GridView does not expand

I am kinda new in android and got such a problem, I am trying to use ExpandableHeightGridView for gridview inside scrollview, but layout doesn't render, some advice please.
It always gives me this error:
Exception raised during rendering: ScrollView can host only one direct child
ExpandableHeightGridView.class:
public class ExpandableHeightGridView extends GridView {
boolean expanded = false;
public ExpandableHeightGridView(Context context)
{
super(context);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
}
public boolean isExpanded()
{
return expanded;
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// HACK! TAKE THAT ANDROID!
if (isExpanded())
{
// Calculate entire height by providing a very large height hint.
// View.MEASURED_SIZE_MASK represents the largest height possible.
int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
else
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded)
{
this.expanded = expanded;
} }
layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#fed70d"
android:orientation="horizontal">
<ImageView
android:id="#+id/iconBack"
android:layout_width="45dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:paddingBottom="16dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="16dp"
android:src="#drawable/icon_back_white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="20dp"
android:text="Wishlist"
android:textColor="#ac920d"
android:textSize="24sp" />
<LinearLayout
android:id="#+id/myCart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center|right"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/addedInCart"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_gravity="center|right"
android:src="#drawable/icon_menu_cart" />
<TextView
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginTop="5dp"
android:background="#drawable/icon_mycart_count"
android:gravity="center_horizontal|center"
android:text="2"
android:textColor="#ffffff"
android:textSize="10dp" />
</LinearLayout>
<TextView
android:id="#+id/myMoneyInMyPocket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|right"
android:layout_marginRight="10dp"
android:text="2000$"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ad8c22" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#fed70d"
android:orientation="horizontal"
android:weightSum="5.0">
<LinearLayout
android:id="#+id/menuItemStores"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical"
android:paddingTop="8dp">
<ImageView
android:id="#+id/menuIconStores"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_gravity="center"
android:src="#drawable/icon_menu_logo" />
<TextView
android:id="#+id/menuTextStores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Stores"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/menuItemInfo"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical"
android:paddingTop="8dp">
<ImageView
android:id="#+id/menuIconInfo"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_gravity="center"
android:src="#drawable/icon_menu_info" />
<TextView
android:id="#+id/menuTextInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Info"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:background="#drawable/selected_menu_background"
android:orientation="vertical"
android:paddingTop="8dp">
<ImageView
android:id="#+id/menuIconWishlist"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_gravity="center"
android:src="#drawable/icon_wishlist" />
<TextView
android:id="#+id/menuTextWishlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Wishlist"
android:textColor="#ffffff"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/menuItemAccount"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical"
android:paddingTop="8dp">
<ImageView
android:id="#+id/menuIconAccount"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_gravity="center"
android:src="#drawable/icon_menu_account" />
<TextView
android:id="#+id/menuTextAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Account"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/menuItemCart"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical"
android:paddingTop="8dp">
<ImageView
android:id="#+id/menuIconCart"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_gravity="center"
android:src="#drawable/icon_menu_cart" />
<TextView
android:id="#+id/menuTextCart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Cart"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
<View
android:id="#+id/dropShadow"
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#drawable/drop_shadow"
android:cacheColorHint="#f1e7dd"
android:paddingTop="8dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<com.example.utils.ExpandableHeightGridView
android:id="#+id/wishListGridView"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:columnWidth="80dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:isScrollContainer="false"
android:numColumns="2"
android:paddingBottom="#dimen/activity_horizontal_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>
This problem is discussed here.
Try adding one more LinearLayout inside your ScrollView as a direct child and put all other layouts/controls inside of the new LinearLayout.

The ScrollView is scrolled up when the first elements of its LinearLayout become visible

I have a LinearLayout inside a ScrollView. The visibility of the first 3 elements of the LinearLayout is initialilly set to GONE. When I change visibility to VISIBLE, the ScrollView is scrolled up. Is it possible not to scroll it when the first views are become visible?
The code:
<com.mypackage.customWidgets.CustomScrollView
android:id="#+id/scrollView"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true" >
<LinearLayout
android:id="#+id/layout2"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical"
android:divider="#drawable/table_layout_horizontal_divider"
android:showDividers="middle|beginning" >
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
style="#style/kidProfileServicesRowStyle">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:layout_marginRight="#dimen/largeSpace"
android:layout_gravity="center"
android:src="#drawable/consult_now_kid_profile"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/consultNow1Row"
android:layout_marginRight="#dimen/largeSpace"
android:layout_gravity="center_vertical"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/doctors_arrow_right"
android:layout_gravity="right"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
style="#style/kidProfileServicesRowStyle">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:layout_marginRight="#dimen/largeSpace"
android:layout_gravity="center"
android:src="#drawable/choose_a_specialist_kid_profile"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/chooseSpecialist"
android:layout_marginRight="#dimen/largeSpace"
android:layout_gravity="center_vertical"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/doctors_arrow_right"
android:layout_gravity="right"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
style="#style/kidProfileServicesRowStyle">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:layout_marginRight="#dimen/largeSpace"
android:layout_gravity="center"
android:src="#drawable/other_services_kid_profile"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/otherServices"
android:layout_marginRight="#dimen/largeSpace"
android:layout_gravity="center_vertical"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/doctors_arrow_right"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
style="#style/kidProfileServicesRowStyle">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:layout_marginRight="#dimen/largeSpace"
android:src="#drawable/medical_card_kid_profile"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/medicalCard"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/doctors_arrow_right"
android:layout_gravity="right"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
style="#style/kidProfileServicesRowStyle">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:layout_marginRight="#dimen/largeSpace"
android:src="#drawable/more_about_kid"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/moreAboutKid"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/doctors_arrow_right"
android:layout_gravity="right"/>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/medical_card_kid_profile"
android:visibility="invisible"
android:layout_margin="#dimen/mediumSpace"/>
</LinearLayout>
</com.mypackage.customWidgets.CustomScrollView>
public class CustomScrollView extends ScrollView
{
public CustomScrollView(Context context, AttributeSet attrs)
{
super(context, attrs);
setHorizontalScrollBarEnabled(false);
setVerticalScrollBarEnabled(false);
}
#Override
public boolean onTouchEvent(MotionEvent ev)
{
return false;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
return false;
}
}
The code which I use to show the elements:
RelativeLayout.LayoutParams scrollViewParams = (RelativeLayout.LayoutParams) scrollView.getLayoutParams();
scrollViewParams.height = scrollView.getHeight();
layout2.getChildAt(2).setVisibility(View.VISIBLE);
layout2.getChildAt(1).setVisibility(View.VISIBLE);
layout2.getChildAt(0).setVisibility(View.VISIBLE);
scrollView.setLayoutParams(scrollViewParams);
Mention focusable attribute for those 3 elements. Like:
android:focusable="false"
otherwise, In customScrollView override below method:
#Override
protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
return true;
}

Scrolling Not working when keyboard open in android

I have made a layout. When I start to enter the login credential then keyboard appear and now i am not able to scroll to fill the other details. So all the views hide behind the keyboard. Please help me fix this so that when keyboard open then it should start the scrolling to see other views
<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="fill_parent"
android:background="#drawable/login_bg" >
<RelativeLayout
android:id="#+id/top_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="hideKeyboard" >
<ImageView
android:id="#+id/top_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/blank_header"
android:scaleType="fitXY" />
<ImageView
android:id="#+id/top_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="fitXY"
android:src="#drawable/login_font" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="hideKeyboard" >
<Utility.LoginRL
android:id="#+id/scrollview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="85dp"
android:onClick="hideKeyboard"
android:scrollbars="none" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:onClick="hideKeyboard"
android:paddingBottom="15dp"
android:src="#drawable/logo" />
<RelativeLayout
android:id="#+id/user_pass_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/logo"
android:layout_centerHorizontal="true"
android:layout_margin="5dip">
<LinearLayout
android:id="#+id/username_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/user_text1"
android:gravity="center_vertical"
android:onClick="showKeyboard"
android:paddingBottom="20dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:paddingLeft="10dp"
android:text="User Name"
android:textColor="#9D9D9D"
android:textSize="20dp" />
<EditText
android:id="#+id/uName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#00000000"
android:paddingLeft="10dp"
android:singleLine="true"
android:text=""
android:textColor="#228ED3"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/password_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/username_layout"
android:background="#drawable/user_text2"
android:gravity="center_vertical"
android:onClick="showKeyboard"
android:layout_margin="5dip"
android:paddingBottom="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="showKeyboard"
android:paddingLeft="10dp"
android:paddingRight="5dp"
android:text="Password"
android:textColor="#9D9D9D"
android:textSize="20dp" />
<EditText
android:id="#+id/pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#00000000"
android:paddingLeft="10dp"
android:password="true"
android:singleLine="true"
android:text=""
android:textColor="#228ED3"
android:textSize="20dp" />
</LinearLayout>
</RelativeLayout>
<!--
<LinearLayout
android:id="#+id/site_url_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/user_pass_layout"
android:layout_centerHorizontal="true"
android:paddingBottom="10dp"
android:paddingTop="20dp" >
-->
<TextView
android:id="#+id/lbl_siteUrl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/user_pass_layout"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#drawable/site_url1"
android:gravity="center_vertical"
android:onClick="showKeyboard"
android:paddingLeft="10dp"
android:layout_margin="5dip"
android:paddingTop="10dp"
android:text="Site Url"
android:textColor="#9D9D9D"
android:textSize="20dp" />
<EditText
android:id="#+id/siteUrl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/lbl_siteUrl"
android:layout_centerHorizontal="true"
android:background="#drawable/site_url2"
android:gravity="top"
android:paddingLeft="10dp"
android:singleLine="true"
android:text="http://"
android:textColor="#228ED3"
android:textSize="20dp" />
<!-- </LinearLayout> -->
<ImageView
android:id="#+id/login_btn"
android:layout_width="wrap_content"
android:layout_margin="5dip"
android:layout_height="wrap_content"
android:layout_below="#id/siteUrl"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:paddingBottom="10dp"
android:src="#drawable/login_btn" />
<ImageView
android:id="#+id/forgot_pass_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/login_btn"
android:layout_centerHorizontal="true"
android:src="#drawable/forgot_pass_btn" />
</RelativeLayout>
</Utility.LoginRL>
</RelativeLayout>
</RelativeLayout>
LoginRL
public class LoginRL extends ScrollView {
public boolean isKeyboardHidden = true;
public LoginRL(Context context) {
super(context);
}
public LoginRL(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public LoginRL(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);
final int actualHeight = getHeight();
if (actualHeight > proposedheight){
isKeyboardHidden = false;
//LoginScreen.keyboardToolbar.setVisibility(View.VISIBLE);
} else {
isKeyboardHidden = true;
//LoginScreen.keyboardToolbar.setVisibility(View.GONE);
}
}
}
Use this:
InputMethodManager inputManager = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
Wrap your layout inside a ScrollView and you will be able to scroll when the keyboard is shown.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/top_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="hideKeyboard" >
.......
</ScrollView>

Categories

Resources