Putting custom View into ScrollView (Or HorizontalScrollView) programmatically (No Scrolling) - android

I'm struggeling to put a self-made view (size 3000px * 750 px) into any scrollView programmatically.
Information to my custom view: It simply extends View and there are just some drawing in OnDraw(), nothing special.
In order to gather all information for the drawing, this CustomView should be added, once a AsyncTask has loaded all information.
My problem is, that when I added my view programmatically to any ViewGroup containing a ScrollView or HorizontalScrollview, there is no Scrolling.
I've tried several layouts, for example this one:
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:layout_marginTop="50dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="#+id/linLayoutScroll">
</LinearLayout>
</HorizontalScrollView>
And once the AsynTask is finished:
LinearLayout linLayout = (LinearLayout)rootView.findViewById(R.id.linLayoutScroll);
linLayout.addView(new MyChartView(getActivity(), chartBuilder, ChartBuilder.DATASET.SPEED));*/
I tried to add my view to the LinearLayout, but often my View does not appear on screen at all or there is no scroling option and my view gets cut off at the end of display.
I guess there must be some magic trick to tell the scrollView to recheck its content and to decide whether there has to be a scrolling option or not. Can anyone help me out?

The error was inside my customView. I didn't set the Width and Height of my view (setWidth(), setHeight()). So the view did not know how big it really is. :-(
Sorry for wasting your time!

Related

Android Designing a custom view

Good day,
I am working on an application, and I want to design the view below, I am unsure which view to use.
From my experience with Android development, I'm thinking it should be a list view or a recyclerview with a custom header, I may be wrong, but looking at the view could someone explain what view I should use. I have red arrows indicating what I am talking about.
I was thinking a custom scrollable list view, and defining the list of items, shown by the three arrows, basically three custom list items.
Should it be a list view or recyclerview with a custom header for the arrow marked as 1.
Should the arrow marked as 3. be a gridview that's filled dynamically? As I understand it, the gridview doesn't work well with a scrollable parent, like a listview, as I want the whole view to scroll based on the content, like in Instagram, what is a good approach to accomplish this.
I came here because I have searched and really couldn't find any good advice
Thanks in advance
It seems that you don't need one single RecyclerView here. You can use a combination of ViewGroups and Views. Simple example:
<?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
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Code for 1,2-->
<!-- Your grid from 3-->
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
</ScrollView>
Edit:
Also do this on your RecyclerView: setNestedScrollingEnabled(false).
This will make your whole layout scrollable, while your RecyclerView, having a height of wrap_content will just expand(adding to the height of the whole layout) without the need for internal scrolling.

How to align view (like in RelativeLayout) with a nephew view?

Related question. Answer: RelativeLayout can't do it. I'm asking how to do it anyway, with not just RL, or with something else.
General story: you have a complex layout that would be difficult to adjust, and along comes a request for something to be added, aligning with a nested view.
What is the best approach? A popup with a custom style? (not familiar with those yet)? Spending days changing the whole hierarchy to a single RelativeLayout? A custom Layout class as wrapper?
AbsoluteLayout (deprecated) or FrameLayout with programmatically changed LayoutParams or margins? (this I'd rather avoid, I prefer not to touch onMeasure, etc)
Simplified example (no relation to pic above):
LinearLayout defines relative heights of the elements. I don't know to do it with RelativeLayout.
anExpandableView is something to be animated as sliding from under someBar (here; full-width, but perhaps it may need to align its width, as well as vertical position).
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/topStuff"
layout="#layout/incl_topstuff"
android:layout_width="match_parent"
android:layout_weight="7"
android:layout_height="0dip" />
<include
android:id="#+id/someBar"
layout="#layout/incl_filters_and_stuff"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include
android:id="#+id/bottomStuff"
layout="#layout/incl_bottomstuff"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="10" />
</LinearLayout>
<include
android:id="#+id/anExpandableView"
layout="#layout/incl_filters"
android:visibility="gone"
android:layout_below="#id/someBar"/>
</RelativeLayout>
I know SO has an aversion to general questions, but I don't want an ad-hoc solution. I am asking what to do in cases which would be solved if only a wrapping RelativeLayout would allow alignment to a view that is not a direct sibling.
Putting it simply, RelativeLayout can only measure and layout it's direct children based on each other, but I guess you already knew that.
The only general solution would be to implement your own custom Layout class, which I wouldn't recommend. If I had to guess why RelativeLayout does not traverse the entire layout hierarchy at it's level and below, it's probably for performance reasons.
Unfortunately if you're using RelativeLayouts and LinearLayouts and you want views to be dependent on each other you have to pick one approach and stick to it, either the flat hierarchy of RelativeLayout, or the nested one of LinearLayout.
Based on your example, as far as I know, there is no way to implement weighted views with a RelativeLayout, so you're stuck with using a LinearLayout.
The easiest way to do what you want is to inflate your expandableView in code, align it with the bottom of the RelativeLayout, set it's height and position based on bottomStuff, and animate from there.
If you really want to do it in xml, I can think of one somewhat hacky, ad-hoc approach, but which can can be generalized to mirroring the measurement and layout of any hierarchy with a bit of work.
Create a parallel but invisible LinearLayout that is a sibling of the first one. Give it an empty view with weight 7 on top, an invisible copy of someBar in the middle, then your expandable view under that with weight 10. To have it slide up, either animate the height of the invisible someBar and the weight of the empty view on top towards 0, or remove them/set them to gone and set animateLayoutChanges on your LinearLayout.

how to control the transparency of a view in a RelativeLayout in android?

I have a RelativeLayout that holds a set of views and i would like that one of the views will
appear in front of all the other views? how can i do that ?
Thanks
EDIT :
OK, i will be more specific.
here is the code of my relativelayout, this is where i put all of my view.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cfff"
android:id="#+id/myMap">
i'm adding view to this relativelayout this way :
creating different views......
relativelayout.addView(view);
now, some of the view intersect with each other and when this happens i want to control which view will be on top of the other one. so how can i do that ?
Thanks.
The view will be ordered on the Z-axis in the order that they are added. So the first view will be the further in your layout. The next view added will be on top of the first view and so on.

How to float/overlap a view in android?

I have many activities with a scrollview inside a tablelayout. However, it is necessary a small design change, so I have to put a black transparent view over the whole screen from the top to the bottom. Is it possible to do it in the tablelayout or the scrollview?
RelativeLayout allows for easy overlapping of views. You'll have to adjust the existing views in your app because it doesn't do anything automatically.
EDIT:
A quick way to do this would be to take your existing view (the ScrollView) that is already organized and put it in a top-level RelativeLayout. Then, all you have to do is add new view inside the RelativeLayout with the width and height both set to MATCH_PARENT. The result should be the black transparent view will be visible over the ScrollView.
I normally use FrameLayout to achieve any kind of 'layering' of views.
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
//your existing layout
<View
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#33000000" />
</FrameLayout>
As DeeV said, you can probably use RelativeLayout in a similar way, but you might have to set additional attributes on its children to achieve this.

Contents stretching to fill HorizontalScrollView

I'm doing an inadvisable thing here, and I know it, but I have a webview nested in a horizontalscrollview. (I needed more scrolling control than webview provides.)
The layout looks like this:
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webRel"
android:layout_below="#+id/titlebar"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webZ"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</HorizontalScrollView>
And the results are as follows:
The webview isn't filling up the horizontalscrollview. The horizontalscrollview is filing the view, so I guess I could say the webview isn't filling the view even though I have both set to fill_parent. I would really, really like it if someone would help me figure out how to have the webview fill up the horizontalscrollview widthwise... because then I measure the height and use that measurement to paginate the text (which is then displayed in chunks widthwise which is why I chose horizontalscrollview.)
:D
(Alternatively, suggestions for a cleaner way to have smooth horizontal scrolling in a webview would also be helpful.)
The answer is: use android:fillViewPort. It'll have the contents of the scrollview stretch to fill the view.
WebView already supports horizontal scrolling, you should not put it inside a HorizontalScrollView.

Categories

Resources