problem in showing listview in horizontal layout? - java

I am trying to show a list view with calendar view in a linear layout. when i am using vertical layout then list view appears but in horizontal layout the same listview disaappears. I solved the problem using relative layout, but can i do this using linear layout.here is my xml...
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.exina.android.calendar.CalendarView
android:layout_height="wrap_content"
android:id="#+id/calendar" android:layout_width="wrap_content">
</com.exina.android.calendar.CalendarView>
<ListView android:layout_height="fill_parent" android:id="#+id/android:list"
android:layout_weight="1" android:scrollingCache="false"
android:layout_width="fill_parent" android:drawSelectorOnTop="false"
android:dividerHeight="4.0sp"></ListView>
</LinearLayout>
</ScrollView>

You need to set the fillviewport flag in the scrollview :
android:fillViewPort="true"
Check this link to know what fillviewport flag does: ScrollView

Related

Transparent TextView on top of RecyclerView

I have this activity where I have a RecyclerView but above it I have a TextView, now when I scroll in the RecyclerView the RelativeLayout with the text stays at the top of the screen and doesn't scroll with it. Now my problem is that the RelativeLayout view is transparent so when I scroll in the RecyclerView the text in the RelativeLayout will lay on top of the list of text elements in the RecyclerView, in other words to simplify what I mean, I have "text on text" if that makes sense. How can I either have the RelativeLayout not be transparent so that the RecyclerView items scroll "behind" it and aren't visible "underneath" the TextView or how would I be able to make the whole layout scroll together with the RecyclerView?
(Even if I change the background color of the RelativeLayout the text in the RecyclerView will be visible through it)
Here is the XML for this activity:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".Activity"
android:background="#drawable/gradient_background">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:background="#color/colorPrimary">
<View
android:id="#+id/lineView"
android:layout_width="30dp"
android:layout_height="0.5dp"
android:background="#drawable/divider_line"
android:layout_centerVertical="true"/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:layout_toEndOf="#id/lineView"
android:layout_margin="8dp"/>
</RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
app:layout_constraintTop_toBottomOf="#+id/relativeLayout"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Use NestedScrollView to make whole view scrollable
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:fillViewport="true"
tools:context=".Activity"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/gradient_background">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary">
<View
android:id="#+id/lineView"
android:layout_width="30dp"
android:layout_height="0.5dp"
android:background="#drawable/divider_line"
android:layout_centerVertical="true"/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:layout_toEndOf="#id/lineView"
android:layout_margin="8dp"/>
</RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
Can I have the RelativeLayout not be transparent so that the
RecyclerView items scroll "behind" it?
Just change the Constraint Layout to Linear Layout.
How would I be able to make the whole layout scroll together with the
RecyclerView?
Logic: For that you would have to include the Relative layout in the XML file where you are inflating the RecyclerView content. Now that would inflate the Relative Layout every time right.
So to overcome this problem, whatever you are passing in the RecyclerView constructor, take a flag/count, only inflate the Relative Layout when count is equal to zero/ set the flag and when the count is greater than 0 or if flag is set, then hide the Relative Layout.
That way you would have Relative Layout scroll with the RecyclerView content and inflated only once.

unable to scroll the screen which has grid view and listview below it

I have the following xml which has Gridview and framelayout below it.
My requirement is if I scroll the screen upwards whole screen should scroll, but for me Only listview scrolls
Below is the xml file:
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:background="#color/white"
android:focusableInTouchMode="true"
android:paddingBottom="#dimen/activity_vertical_margin">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/time_card_header"
android:id="#+id/scrollView">
<GridView
android:id="#+id/timecard_grid"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_margin="3dp"
android:fitsSystemWindows="true"
android:gravity="center_horizontal"
android:horizontalSpacing="1dp"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp" />
</ScrollView>
<FrameLayout
android:id="#+id/timecard_reports"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#color/white"
android:layout_below="#+id/scrollView"/>
</RelativeLayout
Actually GridView and Listview have already scroll. No need to put the above widgets in scrollview. Firstly, remove the scrollview. I think you need the FrameLayout to be fixed at the bottom and show gridview above. For that first use FrameLayout in the linear layout with align_parentbottom true. after that please place the gridview above FrameLayout. That will fix the problem.
Just put linearlayout below scroll view and then gridview.
<scrollview>
<linearlayout >
<gridview />
</linearlayout >
</scrollview>
Sorry i am giving the ans from the mobile but the concept is same.
You can use RecyclerView with GridLayoutManager and set nestedScrollingEnabled of recyclerView to false.
and instead of ScrollView use NestedScrollView as parent of RecyclerView:
private void setUpRecyclerView(View view) {
recyclerView = view.findViewById(id.repliesList);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),numberOfColumns));
}
and xml file:
<android.support.v4.widget.NestedScrollView
android:id="#+id/nestedScroll"
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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:focusableInTouchMode="true"
android:paddingBottom="#dimen/activity_vertical_margin">
<RecyclerView
android:id="#+id/timecard_grid"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_margin="3dp"
android:fitsSystemWindows="true"
android:gravity="center_horizontal"
android:horizontalSpacing="1dp"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp" />
<FrameLayout
android:id="#+id/timecard_reports"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#color/white"
android:layout_below="#+id/scrollView"/>
</RelativeLayout
</android.support.v4.widget.NestedScrollView>

Horizontally and vertically center a ProgressBar in Relative Layout

I am trying to achieve the following progress dialog (without the text)
My current XML code is as follows:
<?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="fill_parent"
android:background="#color/background"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background"
>
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:visibility="gone"
/>
I am programatically hiding and showing the progress bar when required (and hiding/showing other screen elements), all of which are under the Relative Layout in the XML code above.
However, whenever the Progress Bar shows, it appears centered at the top of the screen instead of being centered horizontally AND vertically.
Setting the ScrollView's attribute "fillViewport" to true should do it.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background"
android:orientation="vertical"
android:fillViewport="true"
>
also make sure its child's height is set to "fill_parent" and the ProgressBar is properly centered
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"

android image and button inside a scrollview

I am using following configuration to properly fit image inside a 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="fill_parent"
android:scrollbars="vertical" >
<ImageView
android:id="#+id/textNimbo"
android:layout_width="match_parent"
android:layout_height="2492dp"
android:layout_alignParentRight="true"
android:src="#drawable/inicio"
android:adjustViewBounds="true" />
</ScrollView>
However I need to set also a button inside scrollView and below the ImageView. I tried relative and linear layouts inside scrollView but no succes, button is not visible or image doesn't fit to scrollView. Any recommended configuration? Thank you.
A ScrollView can only have one direct child element. So to add a Button and an ImageView, you'll need something like this:
<?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="fill_parent"
android:scrollbars="vertical" >
<LinearLayout
android:id="#+id/layout"
android:layout_width="fill_parent"
android"layout_height="fill_parent">
<ImageView
android:id="#+id/textNimbo"
android:layout_width="match_parent"
android:layout_height="2492dp"
android:layout_alignParentRight="true"
android:src="#drawable/inicio"
android:adjustViewBounds="true" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
You could also use ImageButton I
instead of a ImageView with a transparent Button on it

alignParentBottom In a Layout Inflate

I have a ListActivity that I'm trying to inflate a Button from this layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/add_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/add_prop"
android:layout_alignParentBottom="true"
/>
</LinearLayout>
Using this code:
View footer = getLayoutInflater().inflate(R.layout.footer, null);
ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.addFooterView(footer, null, false);
The problem is that since the list isn't so big that takes the entire screen (at least I think this is why) the button stays at the end of the list instead of sticking to the bottom because of the android:layout_alignParentBottom="true" property. What should I do to make it stick at the bottom?
You have to create a new layout file with this code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/add_button" />
<Button
android:id="#+id/add_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/add_prop"
android:layout_alignParentBottom="true" />
</RelativeLayout >
Then set it as content view of your Activity using setContentView()

Categories