How to make TableLayout center in ScrollView for android - java

I am trying to display table at center in ScrollView but it didn't work. But if I delete ScrollView, the table will be displayed at center. I don't know on which part is my mistake. Can anyone help to find the mistake ?
This is the xml coding:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_rekod"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="com.example.asiqin.attendance.Rekod"
android:background="#E9F1F2">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button2">
<TableLayout
android:id="#+id/table"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:shrinkColumns="1"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="middle">
</TableLayout>
</ScrollView>
</RelativeLayout>
This is the java code :
TableRow rowHeader = new TableRow(context);
rowHeader.setBackgroundColor(Color.parseColor("#c0c0c0"));
rowHeader.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT));
String[] headertext = {"TARIKH","ALASAN","STATUS"};
for(String c:headertext){
TextView tv = new TextView(this);
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.MATCH_PARENT));
tv.setGravity(Gravity.CENTER);
tv.setTextSize(15);
tv.setPadding(5,5,5,5);
tv.setText(c);
// tv.setBackgroundDrawable(border1);
tv.setBackgroundResource(R.drawable.valuecellborder);
rowHeader.addView(tv);
}

There are two ways in which you can achieve what you are trying to achieve
1) Use a relative layout inside the scrollview
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button2">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TableLayout
android:id="#+id/table"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:shrinkColumns="1"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="middle">
</TableLayout>
</RelativeLayout>
</ScrollView>
2) Use android:layout_gravity = "center" in Table layout
<TableLayout
android:id="#+id/table"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:shrinkColumns="1"
android:divider="?android:attr/dividerHorizontal"
android:layout_gravity="center"
android:showDividers="middle">
The problem with your code is that you are using android:layout_centerHorizontal="true" which works when the parent layout is a Relative Layout but it won't work if the parent layout is a ScrollView.

Related

ScrollView - How to enable horizontal scrolling in ScrollView

So I have a ScrollView which has a TextView inside. When the String for the text is longer than the screen width, It simply shifting in the next line. I want to avoid the new lining by adding HorizontalScrolling in ScrollView. Also, I want to make the text selectable so the user can copy the text. Is it possible?
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="15dp"
android:ellipsize="end"
android:textIsSelectable="true"
android:justificationMode="inter_word"
android:lineSpacingExtra="5dp"
android:textSize="15sp"
tools:targetApi="o" />
</ScrollView>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:scrollbars="vertical">
<HorizontalScrollView
android:layout_width="match_parent" android:layout_height="wrap_content">
<FrameLayout android:layout_width="320dp"
android:layout_height="match_parent"
android:background="#000000"/>
</HorizontalScrollView>
</ScrollView>
Try this:
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:scrollHorizontally="true"
android:text="Horizontal scroll text view"/>
</HorizontalScrollView>

Android Margin and Padding aren't working for RecyclerViews inside ScrollViews

When I place a RecyclerView as a child of a ScrollView, marginBottom and paddingBottom doesn't work. Setting these values for the parent LinearLayout doesn't affect anything either. It seems that the ScrollView doesn't scroll to the bottom because the ScrollBar doesn't reach the bottom. How can I fix this bug?
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<!--To avoid scroll view from scrolling to the bottom automatically-->
<View android:layout_width="match_parent" android:id="#+id/focus_view" android:layout_height="0dp" android:focusable="true" android:focusableInTouchMode="true"><requestFocus/></View>
<ScrollView
android:id="#+id/sv_categories_statistics"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="4dp">
<LinearLayout
android:id="#+id/ll_categories_statistics_filters_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="4dp">
<LinearLayout
android:id="#+id/ll_categories_statistics_select_date_fragment_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/border_primary_light"
android:padding="4dp"
android:layout_marginEnd="4dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/border_accent"
android:padding="4dp">
<Spinner
android:id="#+id/spn_categories_statistics_bill_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
<com.github.mikephil.charting.charts.PieChart
android:id="#+id/pc_categories_statistics"
android:layout_width="match_parent"
android:layout_height="325dp"
android:layout_margin="4dp"/>
<fragment
android:id="#+id/fgm_categories_statistics_not_enough_data"
class="com.dolinsek.elias.cashcockpit.NotEnoughDataFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
app:text="#string/label_not_enough_data_for_statistic"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:layout_marginBottom="8dp"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_categories_statistics"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
Have you tried clipToPadding
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_categories_statistics"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="50dp"
android:clipToPadding="false"/>

Android - unable to scroll view

I would like to ask, why is my scroll view in xml code not working on real mobile phone. I've seen here similar question but it didn't help me.I'm just beginner. Thank you.
If you would like to see java code or code of single row in list, I'am able to upload it.
<?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: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"
android:background="#000000">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:fillViewport="true"
android:focusable="false">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/scrollView"
android:clickable="true"
android:drawSelectorOnTop="false">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/viewGL"
android:clickable="true"
android:drawSelectorOnTop="false"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</ListView>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
If this is the only part of the xml you can leave out the ScrollView. The ListView is already a scrollable view.
Try this one:
<?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: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"
android:background="#000000">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/viewGL"
android:clickable="true"
android:drawSelectorOnTop="false"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</ListView>
</RelativeLayout>
In Android you CAN'T have a ScrollView and inside have another view that has another ScrollView. Remember the ListView adds a scroll when rows overflows the available space. It's logic, because the system will not know what scroll are you using
try to reduce the list view width because the listview already have scrolling
try like this...
<ListView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:id="#+id/viewGL"
android:clickable="true"
android:drawSelectorOnTop="false"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</ListView>

Scrollview doesn't scroll with 2 listview

i search lot of topic about scrollview which doesn't scroll but no one can resolve my issue :/
if someone could help me :)
each listview print some people when i do a request from my server
normally when i scroll down of my list the second list appear but it does not
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/selector_for_background_white"
android:orientation="vertical">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/selector_for_background_white"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/layoutScrollResult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/selector_for_background_white">
<LinearLayout
android:id="#+id/layout2"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<com.whask.whask.utils.font.FontTextView
android:id="#+id/text_hot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:background="#android:color/transparent"
android:text="5 HOT"
android:textSize="#dimen/abc_text_size_medium_material"
app:font="Neo-Sans-Std-Medium.otf"/>
</LinearLayout>
<ListView
android:id="#+id/result_whask_listview_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/layout2">
</ListView>
<LinearLayout
android:id="#+id/layout3"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="#+id/result_whask_listview_view"
android:orientation="horizontal">
<com.whask.whask.utils.font.FontTextView
android:id="#+id/text_not"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:background="#android:color/transparent"
android:text="2 NOT"
android:textSize="#dimen/abc_text_size_medium_material"
app:font="Neo-Sans-Std-Medium.otf"/>
</LinearLayout>
<ListView
android:id="#+id/result_whask_listview_view_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/layout3"
android:background="#color/my_whask_white_color">
</ListView>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
DO NOT use ListView inside ScrollView. ListView itself has a scroll property and you don't have to use another ScrollView to make it scroll. Use a LinearLayout instead.
Listview have own scrolling property ,so dont use within scrollview...
thanks

How to center in ScrollView and LinearLayout

Please have a look at the following code
<ScrollView 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"
tools:context=".DisplayResult" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/zodiac1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/aries"
/>
<TextView
android:id="#+id/orTxt"
android:text="Or"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/zodiac2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/gemini"
/>
</LinearLayout>
</ScrollView>
I need to center everything inside the LinearLayout. I must use scrollview because when the app is 100% done, there will be lot of information which cannot be viewed by a single glance, without scrolling. How can make these center? Please help!
UPDATE
use any layout inside the scroll, no prob. But I need those elements to be in the same order (one after another) and centered.
you need to set gravity as centered for linear layout
< LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal" >
This one worked for me:
<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:gravity="center">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
Try using layout_gravity on your LinearLayout to center_horizontal:
<LinearLayout
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:orientation="horizontal"/>
<ScrollView 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"
tools:context=".DisplayResult"
scrollbars="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal">
<ImageView
android:id="#+id/zodiac1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/aries"
/>
<TextView
android:id="#+id/orTxt"
android:text="Or"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/zodiac2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/gemini"
/>
</LinearLayout>
</ScrollView>
#If you want to Linear layout in a center you need to write android:gravity="center_horizontal" in your linear layout.

Categories