My requirement is to display a pie chart and a bar graph in a page in Android. To do so, I am generating two views using the following:-
ChartFactory.getBarChartView(getActivity(), buildBarDataset(titles, values), renderer, Type.DEFAULT)
ChartFactory.getPieChartView(getActivity(), categorySeries, defaultRenderer)
I am able to display them individually. But I need to display both the views in a single page with scrolling. How can I achieve this??
Just create a layout like:
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/view1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:id="#+id/view2"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
Save the result of your calls ChartFactory.getBarChartView() and ChartFactory.getPieChartView() in 2 variables graph1 and graph2
Then in code add the 2 charts to the layout
LinearLayout view1 = (LinearLayout)findViewById(R.id.view1);
LinearLayout view2 = (LinearLayout)findViewById(R.id.view2);
view1.removeAllViews();
view1.addView(graph1);
view2.removeAllViews();
view2.addView(graph2);
In my solution both charts are shown on one page. If you want each chart to fill one screen maybe this can help:
scrollable linearlayout with weights bigger than screen in android
Related
<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"
android:background="#color/black"
android:fillViewport="true"
tools:context=".Details_Act.About_Act">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView
android:id="#+id/master_nest"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Some More Layouts>...</Some More Layouts>
<androidx.recyclerview.widget.RecyclerView
android:nestedScrollingEnabled="false"
android:id="#+id/eps_rcview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/relative"
android:layout_marginTop="20dp" />
</androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</RelativeLayout>
In above case when i backpress from another activity recycler position is reset
But if i give recyclerview a height of any 400dp,500dp,etc... instead of fill parent or match parent it work it retain recycler position
Is there anyone who can help me😩
I know nestedscrollview take only one child , i am not going to give full code cuz it has too many lines
I have a CollapsingToolbarLayout that shows an image (placed inside the ConstraintLayout to keep the ratio 1:1) and a RecyclerView that has the com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior attached to it.
Image of Layout
The image is in green and the RecyclerView is in blue.
The problem is when I scroll up on the RecyclerView, the CollapsingToolbarLayout doesn't collapse. Instead, the RecyclerView just scrolls underneath the entire layout. I have to manually scroll inside the CollapsingToolbarLayout for it to collapse.
Here is the code for the layout:
<androidx.coordinatorlayout.widget.CoordinatorLayout
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"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="#+id/toolbar">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:src="#drawable/ic_launcher_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorAccent"
android:nestedScrollingEnabled="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/fab_margin"
app:layout_anchor="#id/app_bar"
app:layout_anchorGravity="bottom|end"
app:srcCompat="#android:drawable/ic_dialog_email" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
I originally had the RecyclerView inside a NestedScrollView with the scrolling behavior attached to the NestedScrollView, but this was causing issues with the Adapter creating a ViewHolder object for EVERY single item in the RecyclerView and that was causing a lot of performance and lagging/freezing issues.
From my understanding, a RecyclerView should not be placed inside a NestedScrollView for this reason and that is why I took the NestedScrollView out.
Any help would be appreciated!
Simply you should disable nested scrolling in recycler view
android:nestedScrollingEnabled="false"
Also, if you have nested recycler views you should disable nested scrolling in child recycler views.
If you want the image to collapse when recyclerview scrolls, use this scroll flags
app:layout_scrollFlags="scroll|snap|enterAlwaysCollapsed"
and use
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
in your recyclerview , and android:nestedScrollingEnabled attribute is not required for what you want to achieve.
have you looked into https://www.journaldev.com/13927/android-collapsingtoolbarlayout-example
Also see some of the things you are missing
The below settings give the left or right border of the recyclerview a vertical line.
I also tried to remove all the layout parameter of the recyclerview, still it does not work.
I did try to hide all the viewholder and found that the line truly belongs to the recyclerview
I am wondering if this line is default, or added by my code and how to get rid of it.
<?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:background="#color/list_background_color">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/fake_space"
android:layout_gravity="center"
android:layout_marginBottom="0dp"
android:layout_marginEnd="1dp"
android:layout_marginStart="1dp"
android:clipToPadding="false"
android:gravity="center"
android:horizontalSpacing="20dp"
android:scrollbarStyle="insideOverlay"
android:scrollbars="vertical"
android:visibility="visible" />
</RelativeLayout>
</RelativeLayout>
Have you tried removing the background from the relative layout. I think you'll find that's what's giving you your border.
There's nothing in your RecyclerView as shown that could do it, even if there is anything that could do it, and I don't think there is. I think you need an ItemDecoration to get borders into the RecyclerView itself, and you're not showing one of those.
After switching to androidx the following code stopped working. The TextView below the Toolbar used to scroll with the content, but now it doesn't. I managed to make it work only when I set the same scrollFlags to Toolbar, but I would like to keep that in place. Is there a solution to this besides moving the Toolbar out of AppBarLayout and CoordinatorLayout?
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
app:title="Non-scrollable title"
android:layout_width="wrap_content"
android:layout_height="?android:attr/actionBarSize"/>
<TextView
app:layout_scrollFlags="scroll"
android:layout_margin="16dp"
android:textSize="20sp"
android:text="this should scroll with the content"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- some scrollable content here -->
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
replace attribute
old:
app:layout_behavior="#string/appbar_scrolling_view_behavior"
new:
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
and add
implementation 'com.google.android.material:material:1.0.0'
In my case, the problem was in the margins of NestedScrollView. I removed margins from NestedScrollView and everything started work.
I'm trying to create a a screen layout of the following though:
A line with some TextView in it
A scroll view where the user actions will add or remove View from
A buttons line.
I have no problem with 1. or 3. But 2. gives me troubles. My scheme is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainX"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/labels"
android:layout_weight="1">
<!--Some text view of various sizes -->
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/dataScroll">
<LinearLayout
android:id="#+id/dataShow"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/buttons"
android:layout_weight="1">
<!--some buttons-->
</LinearLayout>
</LinearLayout>
When I run the program, the buttons line is in the middle of the screen and When I add element to the screen (using the addView on the dataShow layout), the frist one is added without a problem but after that I can't tell what is happening.
Thanks
I'm not exactly sure what you are trying to do.
It seems like you want to dynamically add items to the dataShow layout. Instead of using a ScrollView containing a LinearLayout, you should be using a ListView (vertical scrolling) or Gallery (horizontal scrolling) for your dataShow layout. This will provide the scroll functionality and allow you to add items dynamically to the list/gallery.
Try using a RelativeLayout, then align the header at the top, and the footer at the bottom. Set the ScrollView paddingBottom.
<?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">
<TextView
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="text"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/header"
android:layout_above="#+id/footer"
android:paddingBottom="100dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="center main content"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"/>
</ScrollView>
<RelativeLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#color/colorPrimary"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"/>
</RelativeLayout>
</RelativeLayout>