I have nested calendarview in ScrollView . My problem begins when height of view increases.
I can scroll calendar months vertically but when scrollview scrollbar comes it does not let scroll calendar. Instead of scrolling calendar the whole view scroll up or down.
<?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:fillViewport="true">
<LinearLayout android:orientation="vertical" android:id="#+id/ReminderLayout"
android:layout_width="fill_parent" android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/TaskName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
android:singleLine="true"
android:textSize="26.0sp" />
<View
android:id="#+id/seperator"
android:layout_width="fill_parent"
android:layout_height="1dip" />
<TextView
android:id="#+id/lblNotes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Notes" />
<EditText
android:id="#+id/TaskNotes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:textSize="26.0sp" />
<View
android:id="#+id/seperator"
android:layout_width="fill_parent"
android:layout_height="1dip" />
<TextView
android:id="#+id/lblReminder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Reminder" />
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content">"
<CalendarView
android:id="#+id/calendarReminder"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:scrollbars="vertical"
android:isScrollContainer="true" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TimePicker
android:id="#+id/timePickerReminder"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:gravity="center" />
</LinearLayout>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content" >
<View android:id="#+id/seperator" android:layout_width="fill_parent" android:layout_height="1dip"/>
<TextView android:id="#+id/ReminderTime" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="On Time" android:paddingLeft="20dip"/>
<View android:id="#+id/seperator" android:layout_width="fill_parent" android:layout_height="1dip"/>
<TextView android:id="#+id/Recurring" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="Only Once" android:paddingLeft="20dip"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
I've found answer to this problem here:
https://stackoverflow.com/a/9687545
Basically you need to implement custom CalendarView and override onInterceptTouchEvent method.
I did it as following:
package com.example.app.views;
public class CalendarViewScrollable extends CalendarView {
public CalendarViewScrollable(Context context) {
super(context);
}
public CalendarViewScrollable(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CalendarViewScrollable(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN)
{
ViewParent p = getParent();
if (p != null)
p.requestDisallowInterceptTouchEvent(true);
}
return false;
}
}
Then just use this view in XML layout file:
<com.example.app.views.CalendarViewScrollable
android:id="#+id/datePicker1"
android:layout_width="400dp"
android:layout_height="400dp"
/>
Here's one solution, you can implement a custom ScrollView which will only intercept verticale swipe.
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {...}
"onInterceptTouchEvent" will return false for horizontal swipe, so horizontal swipe will be enable for the CalendarView.
You can read more about this heree : https://stackoverflow.com/a/2655740/5158173
Related
As part of one of the views in my app, I have a place where users can choose to navigate to one of many other pages depending on which icon is tapped. To render these icons, we are using a combination of custom-designed icons and the FontAwesome icon library.
However, on some devices, such as the Samsung Galaxy S7, some of the icons do not always render as expected. In particular one of them renders in an overlapping fashion with a Chinese character. So far we have been unable to figure out why this is happening, so any help would be greatly appreciated.
We are using Xamarin Android and MvvmCross to create our app.
Some of the view code for the main layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
style="#style/RelativeLayoutPageStyle"
android:background="#color/white">
<RelativeLayout
style="#style/RelativeHeader"
android:id="#+id/linearLayout1">
<FontAwesomeIcon
android:id="#+id/flag"
android:layout_alignParentLeft="true"
style="#style/MenuButton" />
<ImageView
android:id="#+id/Header"
style="#style/HeaderLogo"
android:layout_centerInParent="true"
android:src="#drawable/TitleWithIcon" />
<FontAwesomeIcon
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ViewFailedRecordButton"
android:layout_toLeftOf="#+id/ViewAssignmentsButton"
android:layout_toRightOf="#id/Header"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:textColor="#b8860b"
android:text="\uf06a"
android:textSize="16dp"
local:MvxBind="Click ViewFailedRecordsCommand;Visibility BooleanVisibility(FailedRecordsDetected)" />
<FontAwesomeIcon
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ViewAssignmentsButton"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="\uf022"
android:textSize="16dp"
local:MvxBind="Click ViewAssignmentCommand;Visibility BooleanVisibility(HaveAssignmentsForUser);TextColor AssignmentButtonColor, Converter=NativeColor" />
</RelativeLayout>
<LinearLayout
style="#style/CollectionViewHeader"
android:id="#+id/collectionViewHeader"
android:layout_below="#id/linearLayout1"
local:MvxBind="BackgroundColor HeaderBackgroundColor, Converter=NativeColor">
<TextView
android:layout_centerInParent="true"
style="#style/TextViewHeaderResponsive"
local:MvxBind="Text HeaderText" />
</LinearLayout>
<GridView
android:id="#+id/gridView"
android:layout_below="#id/collectionViewHeader"
android:layout_above="#+id/collectionViewFooter"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2" />
<LinearLayout
style="#style/CollectionViewFooter"
android:id="#id/collectionViewFooter"
android:layout_alignParentBottom="true"
local:MvxBind="Visibility HideOnEmptyText(FooterText);BackgroundColor FooterBackgroundColor, Converter=NativeColor;Click FooterClickCommand">
<TextView
android:layout_centerInParent="true"
style="#style/StatusBarFooterTextView"
local:MvxBind="Text FooterText" />
</LinearLayout>
</RelativeLayout>
<MvxListView
android:id="#+id/left_drawer"
style="#style/Menu"
local:MvxItemTemplate="#layout/drawerlistitem"
local:MvxBind="ItemsSource NavigationCellModels" />
</android.support.v4.widget.DrawerLayout>
View for the Template used in the above GridView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
local:MvxBind="Click ClickCommand">
<RelativeLayout
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<FontAwesomeIcon
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="100dp"
android:layout_gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
local:MvxBind="Text RecordableDataTypeToIcon(RecordableDataType);Visibility IsRecordableDataTypeFontAwesome(RecordableDataType);TextColor Color, Converter=NativeColor" />
<CbtGlyphIcon
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="100dp"
android:layout_gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
local:MvxBind="Text RecordableDataTypeToIcon(RecordableDataType);Visibility IsRecordableDataTypeCbtGlyphValueConverter(RecordableDataType);TextColor Color, Converter=NativeColor" />
</RelativeLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:textSize="20sp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="10dp"
android:maxLines="2"
android:ellipsize="end"
local:MvxBind="Text Title;TextColor Color, Converter=NativeColor" />
</LinearLayout>
Code for the FontAwesomeIcon Class:
public class FontAwesomeIcon : TextView
{
private Context _context;
protected FontAwesomeIcon(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{
Initialise(null, null);
}
public FontAwesomeIcon(Context context)
: base(context)
{
Initialise(context, null);
}
public FontAwesomeIcon(Context context, IAttributeSet attrs)
: base(context, attrs)
{
Initialise(context, attrs);
}
public FontAwesomeIcon(Context context, IAttributeSet attrs, int defStyle)
: base(context, attrs, defStyle)
{
Initialise(context, attrs);
}
private void Initialise(Context context, IAttributeSet attrs)
{
_context = context ?? Application.Context;
Typeface = Android.Graphics.Typeface.CreateFromAsset(context.Assets, "FontAwesome.otf");
}
}
Finally here's a screenshot of what I'm seeing:
I pull data into my listview from servers. The listview is inside an activity. The activity has buttons above it.
When the data is received and when I scroll data, only listview is scrolled. The buttons remain fixed at the top.
I want the listview to take the required vertical height it needs so that I would need to scroll the activity instead of the listview.
My XML code :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:text="text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/view_c_name"
android:textSize="18sp"
android:textStyle="bold"/>
<TextView
android:text="text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/view_c_name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/view_c_motto" />
<TextView
android:text="text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/view_c_motto"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/view_c_details" />
<TextView
android:text="text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/view_c_address"
android:layout_below="#+id/view_c_details"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text="text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/view_c_act_text"
android:layout_below="#+id/view_c_address"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="71dp" />
<Button
android:text="Button 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:id="#+id/view_button"
android:layout_below="#+id/view_c_address"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="button 2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/view_button"
android:layout_toRightOf="#+id/view_button"
android:layout_toEndOf="#+id/view_button"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:id="#+id/view_button2" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/view_c_act_text"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/my_listview"
android:scrollbars="none"
/>
</RelativeLayout>
The perfect code should be;
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--Inside the below layout your buttons will come. Modify it accordingly-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!--Your buttons will come here-->
</LinearLayout>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
As shown above, Nested Scroll View should be used instead of Scroll View because
there is a need of scrolling view (in this case List View) inside another scrolling view. The system is unable to decide which view to scroll and this is where Nested Scroll View comes in.
Wrap your existing layout in a ScrollView to scroll the entire thing, not just the ListView
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- your existing layout here, change your top container's height from match_parent to wrap_content -->
</ScrollView>
Try this one
<ScrollView
android:id="#+id/scrollViewMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/linearLayoutMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:text="Button"
android:id="#+id/buttonMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent">
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
"your existing layout here, change your top container's height from match_parent or wrap_content "
the best case secanrio is to use scroll View inside of an relative layout and all the existing code inside of the scrollView
I had similar problem then I created a class
public class ListViewExpanded extends ListView {
public ListViewExpanded(Context context, AttributeSet attrs) {
super(context, attrs);
setDividerHeight(0);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST));
}
}
then I used in xml as try it out it will help for sure
<android.support.v4.widget.NestedScrollView 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"
>
<TextView
android:id="#+id/text_goal_scored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<example.ListViewExpanded
android:id="#+id/list_home"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
</example.ListViewExpanded>
</android.support.v4.widget.NestedScrollView>
Go through this Example. Make ScroolView as Parent. It Work Form me.
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<include
android:id="#+id/maintool"
layout="#layout/toolbar"></include>
<RelativeLayout
android:id="#+id/re1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:textSize="18sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/text" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/re2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Email template"
android:textSize="18sp" />
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/text2"
android:paddingTop="10dp" />
</RelativeLayout>
</LinearLayout>
</ScroolView>
this is how you can do your task.this is Scroll disabled listview so you can scroll your activity easily.
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ListView;
/**
* Created by sudesh Regmi on 4/20/2017.
*/
public class ScrollDisabledListView extends ListView {
private int mPosition;
public ScrollDisabledListView(Context context) {
super(context);
}
public ScrollDisabledListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ScrollDisabledListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
final int actionMasked = ev.getActionMasked() & MotionEvent.ACTION_MASK;
if (actionMasked == MotionEvent.ACTION_DOWN) {
// Record the position the list the touch landed on
mPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
return super.dispatchTouchEvent(ev);
}
if (actionMasked == MotionEvent.ACTION_MOVE) {
// Ignore move events
return true;
}
if (actionMasked == MotionEvent.ACTION_UP) {
// Check if we are still within the same view
if (pointToPosition((int) ev.getX(), (int) ev.getY()) == mPosition) {
super.dispatchTouchEvent(ev);
} else {
// Clear pressed state, cancel the action
setPressed(false);
invalidate();
return true;
}
}
return super.dispatchTouchEvent(ev);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
setListViewHeightBasedOnChildren(your_listview);
add this line in your activity
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
add scrollview at parent in your 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">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/view_c_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text1"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/view_c_motto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/view_c_name"
android:text="text4" />
<TextView
android:id="#+id/view_c_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/view_c_motto"
android:text="text5" />
<TextView
android:id="#+id/view_c_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/view_c_details"
android:text="text2" />
<TextView
android:id="#+id/view_c_act_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/view_c_address"
android:layout_marginTop="71dp"
android:text="text3" />
<Button
android:id="#+id/view_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/view_c_address"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:text="Button 1" />
<Button
android:id="#+id/view_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/view_button"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_toEndOf="#+id/view_button"
android:layout_toRightOf="#+id/view_button"
android:text="button 2" />
<ListView
android:id="#+id/my_listview"
android:layout_width="match_parent"
android:layout_height="700dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/view_c_act_text"
android:scrollbars="none" />
</RelativeLayout>
</ScrollView>
I have created a custom view Game4 that extends LinearLayout. When I tried finding them, it field and returned null (and then nullPointerException and then crash/!!)
This is activity_play_game.xml:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.sortthegraph.PlayGameActivity"
tools:ignore="MergeRootFrame" >
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.example.sortthegraph.Game_4 // A custom View extends LinearLayout
android:id="#+id/game4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
</com.example.sortthegraph.Game_4>
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#000000" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
This is my OnCreate function:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_game);
tempGame = (Game_4) findViewById(R.id.game4); // ?? Why null ??
This is Game_4 constructors:
public class Game_4 extends LinearLayout {
public Game_4(Context context, AttributeSet attrs) {
this(context);
}
public Game_4(Context context) {
super(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.game_4_layout, this, true);
this.setWillNotDraw(false);
}
The game_4_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="20dp"
android:paddingBottom="25dp"
android:layout_marginLeft="15dip"
android:layout_marginRight="15dip"
android:gravity="center" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<View android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
<com.example.sortthegraph.BackButton
android:id="#+id/backButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp" >
</com.example.sortthegraph.BackButton>
<View android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="20dip"
android:layout_marginTop="20dip" >
<com.example.sortthegraph.BackButton
android:id="#+id/backButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</com.example.sortthegraph.BackButton>
<View
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
<com.example.sortthegraph.BackButton
android:id="#+id/backButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</com.example.sortthegraph.BackButton>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<View android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
<com.example.sortthegraph.BackButton
android:id="#+id/backButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" >
</com.example.sortthegraph.BackButton>
<View android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
</TableRow>
public Game_4(Context context, AttributeSet attrs) {
this(context);
}
You need to pass the attrs to the super constructor, e.g.
super(context, attrs);
Otherwise the attributes consumed by the superclass such as android:id are lost.
You can do your custom init in this two-arg constructor and call it from the 1-arg constructor as well:
public Game_4(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.game_4_layout, this, true);
this.setWillNotDraw(false);
}
public Game_4(Context context) {
this(context, null);
}
Game_4 has not been imported properly. Also in layout , press CTRL and take the cursor to com.example.sortthegraph.Game_4 and click to go to the corresponding class. If it does not direct to that corresponding class, then there may be some issue.
in my code there is four listview how do i make screen scrollable to show all data of all listview? not this code make listview scrollable i dont want listview scrolling want ScreenScrollable like this image
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/imagelogo2"
android:orientation="horizontal" >
<ImageView
android:id="#+id/test_button_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="20dp"
android:src="#drawable/back" >
</ImageView>
<ImageView
android:id="#+id/test_button_image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingLeft="5dp"
android:paddingRight="15dp"
android:paddingTop="15dp"
android:src="#drawable/options1" />
</RelativeLayout>
<TextView
android:id="#+id/ElemenrySchool"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/btnback"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingTop="10dp"
android:text="elementry"
android:textSize="17dp" >
</TextView>
<LinearLayout
android:id="#+id/lytContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#drawable/border2"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<ListView
android:id="#+id/listMainMenu11"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dip"
android:fadeScrollbars="true"
android:listSelector="#drawable/listview_selector" />
</LinearLayout>
<TextView
android:id="#+id/MiddleSchool"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingTop="10dp"
android:text="Middle"
android:textSize="17dp" >
</TextView>
<LinearLayout
android:id="#+id/lytContent2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#drawable/border2"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<ListView
android:id="#+id/listMainMenu22"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dip"
android:fadeScrollbars="true"
android:listSelector="#drawable/listview_selector" />
</LinearLayout>
<TextView
android:id="#+id/HighSchool"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/btnback"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingTop="10dp"
android:text="HighSchool"
android:textSize="17dp" >
</TextView>
<LinearLayout
android:id="#+id/lytContent3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#drawable/border2"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<ListView
android:id="#+id/listMainMenu33"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dip"
android:fadeScrollbars="true"
android:listSelector="#drawable/listview_selector" />
</LinearLayout>
<TextView
android:id="#+id/AtipicalSchool"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/btnback"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingTop="10dp"
android:text="Atipical"
android:textSize="17dp" >
</TextView>
<LinearLayout
android:id="#+id/lytContent4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#drawable/border2"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<ListView
android:id="#+id/listMainMenu44"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dip"
android:fadeScrollbars="true"
android:listSelector="#drawable/listview_selector" />
</LinearLayout>
</LinearLayout>
use this custom listview component inside scrollview.
package com.app.Settings;
import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ListView;
public class ExpandableHeightListView extends ListView
{
boolean expanded = false;
public ExpandableHeightListView(Context context)
{
super(context);
}
public ExpandableHeightListView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public ExpandableHeightListView(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.
// But do not use the highest 2 bits of this integer; those are
// reserved for the MeasureSpec mode.
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, 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;
}
}
HOW TO USE IN XML?
<yourPackageName.ExpandableHeightListView
android:id="#+id/expandableHeightlist1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:fadingEdge="none"
android:focusable="false"
android:scrollbars="none"
>
</yourPackageName.ExpandableHeightGridView>
and set
yourListView.setExpanded(true);
By using this custom component you can set multiple listview inside scrollview. i am using this custom component.
Enjoy :)
Never use two listview inside Scrollview
Don't try to use listview , use a LinearLayout instead , if you have got a fixed number of views. As listview will have its own scroll and this won't be a good user experience too.
I have a TabWidget with 5 tabs in it. Tabs : "One","Two","Three","Four","Five". The TabHost is in a HorizontalScrollView. Now I've one arrow (< and >) each on each side of the TabWidget to indicate that there are more tabs in that direction(which are currently not visible in view).
Now what I want to do is, if the first tab is not visible(in current view) then the arrow to left (<) should be visible and if the last tab is not visible(in current view) then the arrow to right should be visible and if both tabs are not visible(in current view) then both arrows should be visible. I tried doing it with:
if (horizontablscrollview.getLeft == 0)
{
arrowRight.setVisiblity(View.VISIBLE)
arrowLeft.setVisiblity(View.INVISIBLE)
}
else if( horizontablscrollview.getRight == 0)
{
arrowLeft.setVisiblity(View.VISIBLE)
arrowRight.setVisiblity(View.INVISIBLE)
}
else if ( horizontablscrollview.getRight == 0 && horizontablscrollview.getLeft == 0)
{
arrowRight.setVisiblity(View.VISIBLE)
arrowLeft.setVisiblity(View.VISIBLE)
}
But this doesn't work, my both tabs are invisible in the start:
my xml looks like this..
<?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"
android:background="#ffffff" >
<RelativeLayout
android:id="#+id/RL_Header"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_marginBottom="3dip"
android:background="#drawable/gradient" >
<ImageButton
android:id="#+id/btn_Home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="3dp"
android:background="#android:color/transparent"
android:contentDescription="#string/description_home"
android:onClick="onHomeButtonClick"
android:src="#drawable/title_home" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dip"
android:layout_marginTop="5dip"
android:background="#android:color/transparent"
android:contentDescription="#string/description_about"
android:onClick="onClickAbout"
android:src="#drawable/title_about" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/FL_Tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/RL_Header" >
</RelativeLayout>
<RelativeLayout
android:id="#+id/FL_Tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/RL_Header" >
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<ImageView android:id="#+id/arrow_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/i_button"
android:layout_alignParentLeft="true"/>
<RelativeLayout
android:id="#+id/rl_tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView android:id="#+id/arrow_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/i_button"
android:layout_alignParentLeft="true"
android:visibility="invisible"/>
<ImageView android:id="#+id/arrow_Right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/i_button"
android:layout_alignParentRight="true"
android:visibility="invisible"/>
<HorizontalScrollView
android:id="#+id/my_scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none"
android:layout_toRightOf="#+id/arrow_left"
android:layout_toLeftOf="#+id/arrow_Right">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:tabStripEnabled="true"/>
</HorizontalScrollView>
</RelativeLayout>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="65dip" >
<RelativeLayout
android:id="#+id/myTabContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dip" >
<TableLayout
android:id="#+id/tabSubs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:shrinkColumns="*"
android:stretchColumns="*" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_marginTop="20dip"
android:gravity="center_horizontal" >
<Button
android:id="#+id/btntestno1"
android:layout_weight="1"
android:gravity="center"
android:padding="20dip"
android:text="Test 1"
android:textColor="#000000" />
<Button
android:id="#+id/btntestno2"
android:layout_weight="1"
android:gravity="center"
android:padding="20dip"
android:text="Test 2"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:gravity="center_horizontal" >
<Button
android:id="#+id/btntestno3"
android:layout_weight="1"
android:gravity="center"
android:padding="20dip"
android:text="Test 3"
android:textColor="#000000" />
<Button
android:id="#+id/btntestno4"
android:layout_weight="1"
android:gravity="center"
android:padding="20dip"
android:text="Test 4"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:gravity="center_horizontal" >
<Button
android:id="#+id/btntestno5"
android:layout_weight="1"
android:gravity="center"
android:padding="20dip"
android:text="Test 5"
android:textColor="#000000" />
<Button
android:id="#+id/btntestno6"
android:layout_weight="1"
android:gravity="center"
android:padding="20dip"
android:text="Test 6"
android:textColor="#000000" />
</TableRow>
</TableLayout>
</RelativeLayout>
</FrameLayout>
</TabHost>
</RelativeLayout>
</RelativeLayout>
Any help would be greatly appreciated.
One solution would be to make you own HorizontalScrollView class and show the ImageViews based on the scroll(although I didn't tested that much the code below should work). Your layout will look like this:
//...
<RelativeLayout
android:id="#+id/rl_tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView android:id="#+id/arrow_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/i_button"
android:layout_alignParentLeft="true"
android:visibility="gone"/>
<ImageView android:id="#+id/arrow_Right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/i_button"
android:layout_alignParentRight="true"
/>
<com.your.package.here.SpecialScroll
android:id="#+id/my_scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:tabStripEnabled="true"/>
</com.your.package.here.SpecialScroll>
</RelativeLayout>
//...
The SpecialScroll class will look like this:
public class SpecialScroll extends HorizontalScrollView {
public interface PositionListener {
public void onLeftArrowRequired(boolean required);
public void onRightArrowRequired(boolean required);
public View implementScrolledView();
}
private PositionListener listener;
public void setPositionListener(PositionListener listener) {
this.listener = listener;
}
public SpecialScroll(Context context) {
super(context);
}
public SpecialScroll(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public SpecialScroll(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (l == 0) {
listener.onLeftArrowRequired(false);
} else {
listener.onLeftArrowRequired(true);
}
View v = listener.implementScrolledView();
Rect r = new Rect();
v.getDrawingRect(r);
if ((r.width() - l) == getWidth()) {
listener.onRightArrowRequired(false);
} else {
listener.onRightArrowRequired(true);
}
}
}
Then in your activity's onCreate method you'll do:
SpecialScroll hsv = (SpecialScroll) findViewById(R.id.my_scrollView);
hsv.setPositionListener(this); // I made the activity implement the SpecialScroll.PositionListener interface
and the activity's implementation of the PositionListener interface is:
#Override
public void onLeftArrowRequired(boolean required) {
if (required) {
((ImageView) findViewById(R.id.arrow_left))
.setVisibility(View.VISIBLE);
} else {
((ImageView) findViewById(R.id.arrow_left))
.setVisibility(View.GONE);
}
}
#Override
public void onRightArrowRequired(boolean required) {
if (required) {
((ImageView) findViewById(R.id.arrow_right))
.setVisibility(View.VISIBLE);
} else {
((ImageView) findViewById(R.id.arrow_right))
.setVisibility(View.GONE);
}
}
#Override
public View implementScrolledView() {
return findViewById(android.R.id.tabs);
}
This isn't an elegant solution, but you could display the left arrow if the x coordinate of the leftmost tab was less than zero and display the right tab if the rightmost tab's x plus its width was greater than the screen width.