Android - unable to scroll view - java

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>

Related

How to clear navigation header view?

I designed an app with two layouts(nav_header_main.xml & nav_header_main_user.xml) if the user logged in means nav_header_main_user.xml should be shown, if user logged out nav_header_main should shown. I got the view but its not clearing each time. In menu we'll clear likenavigationView.getMenu().clear();. I need to know how to clear for navigation header view.
The first xml file(nav_header_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#color/colorPrimary"
android:gravity="bottom"
android:id="#+id/headerView"
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"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="#+id/nav_profile_image"
android:layout_width="wrap_content"
android:layout_height="#dimen/_50sdp"
android:visibility="visible"
android:src="#drawable/splash_screen_logo"
android:layout_gravity="center" />
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/nav_round_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/ic_account"
android:visibility="gone"/>
<TextView
android:id="#+id/nav_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_10sdp" />
</LinearLayout>
and the second xml file(nav_header_main_user.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#color/colorPrimary"
android:gravity="bottom"
android:id="#+id/user_headerView"
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"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<TextView
android:id="#+id/player_name_after_signin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/player_name"
android:textSize="#dimen/_18sdp" />
<TextView
android:id="#+id/email_after_user_signin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/_18sdp"
android:text="#string/email_email_com" />
</LinearLayout>
my java code will be like,
if( sharedpreference.getString("PlayerID",null)!=null )
{
navigationView.inflateHeaderView(R.layout.nav_header_main_user);
}
else
{
navigationView.inflateHeaderView(R.layout.nav_header_main);
}
Now i'm missing the code, to clear the xml files according to that. Any help will be appreciated

How to make TableLayout center in ScrollView for android

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.

How to design this Grid view xml page?

I want to design xml page this type. I tried many of the code. Every time new problems are come. I need bottom navigation bar is fixed. In grid view ,i want android:numColums=3, android:verticalSpacing and android:horizontalSpacing Same(Look like instagram profile view grid images ).
XMl Code:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainSettingClass">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="175dp"
android:background="#dadada"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/profile_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/com_facebook_button_background"/>
<TextView
android:id="#+id/amsc_txt_fbusername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TextView"
android:layout_marginTop="2dp" />
</LinearLayout>
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/grid_test"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:background="#FFFFFF"
android:stretchMode="columnWidth"
android:columnWidth="85dp"
android:numColumns="3"/>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#ffffff"
app:menu="#menu/bottom_navigation_gallery"/>
</FrameLayout>
As per your design you need scrollview for 2 above components and your BottomNavigationView is fix (not movable).
In your code you are not using scrollview first and second i'm not sure why you're using framelayout!
Here's my code which is exactly as per your given design try this if any concern you can ask! :)
Here's my code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottom_gallery">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/rel_titleHolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#dadada"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/profile_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#mipmap/ic_launcher" />
<TextView
android:id="#+id/amsc_txt_fbusername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:gravity="center"
android:text="TextView" />
</LinearLayout>
<GridView
android:id="#+id/grid_test"
android:layout_width="match_parent"
android:layout_height="400dp"
android:columnWidth="85dp"
android:horizontalSpacing="2dp"
android:numColumns="3"/>
</LinearLayout>
</ScrollView>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:layout_alignParentBottom="true" />
</RelativeLayout>

Android Studio: Layout hierarchy problems

I have a horizontalScrollView containing 1 image that is used as the background. This image can be scrolled left to right. I would like to place other imageviews on to the background image but not have them scroll with it. At the moment if I place another imageview on top of this, when I scroll the image comes as well.
The scroll view at far left:
The scroll view at far right:
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/activity_rear_gunner"
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"
android:focusable="true"
tools:context="com.example.mr_br.ibcc_bomber_command.rear_gunner">
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:layout_alignParentTop="true"
>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent" app:srcCompat="#drawable/background_scroll_2"
android:id="#+id/scrollView" android:layout_centerVertical="true"
android:scaleType="centerCrop"
/>
</HorizontalScrollView>
<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:id="#+id/item_frame"
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"
android:focusable="true"
tools:context="com.example.mr_br.ibcc_bomber_command.rear_gunner">
</RelativeLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" app:srcCompat="#drawable/placeholder_bomber"
android:id="#+id/enemy" android:layout_centerVertical="true"
/>
</RelativeLayout>
Very new to using layouts in android. Hope I made my problem clear if not I do apologize and will try to clarify if asked. I know this could have something to do with what layouts I am using and the child and parents of them layouts but like I said new to this.
The HorizontalSCrollView can have only one child, but that child could be another layout, so you can add as many ImageView you want.
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
>
<ImageView
android:layout_width="3000dp"
android:layout_height="match_parent"
android:background="#a30"
/>
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#aa0"
/>
</RelativeLayout>
</HorizontalScrollView>

Stretch FrameLayout to visible screen

My interface looks like this:
http://i.stack.imgur.com/zADUL.png
Is there any simple solution which helps me to stretch FrameLayout to visible screen? I tried to set fillViewport and wrap_content to tabcontent children, but it didn't help.
Try this :
<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"
tools:context=".Form" >
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:background="#cccccc" >
</RelativeLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/footer"
android:layout_below="#+id/header"
android:background="#ff0000" >
</FrameLayout>
<RelativeLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#cccccc" >
</RelativeLayout>
</RelativeLayout>
Thanks.

Categories