Why can't ListView be top level view? - android

So to create a Fragment which contains a single ListView, I must do
<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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivityFragment">
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
Why do I have to place the ListView inside a FrameLayout? Why can't the ListView be the top level view?
UPDATE
I kind of asked the question wrong: what I meant is: Why is it not best practice to make the ListView itself the top level view? I always see the FrameLayout wrapper in online codes.

It can, as long as you don't expect to put anything else in the layout.
FrameLayout, same as Linear and Relative, are containers for several views, but if you only have one you don't need them.
Most layout files seen online assume you may want to expand on your ui by adding necessities like an empty state or fab. Your empty state needs to be on the same layout too.

Related

Android: Tag child element of a view on touch_down event

I have this architecture (ViewGroup with a View and RecyclerView):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.wacom.matchapoc.MainActivity"
tools:showIn="#layout/activity_main">
<com.wacom.matchapoc.view.SmartRecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:scrollbars="vertical" />
<com.wacom.matchapoc.view.DrawingView
android:id="#+id/drawingView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
The DrawingView is ontop of the RecyclerView and has an Overridden onTouch() event, so that when isEnableDrawing() flag is true (you can set it from the settings in the main activity) it handles the touch_down/move/up events so that you can draw over the elements in the RecyclerView.
What I want to do now is essentially to link the user-made drawings on the DrawingView to the child elements (text, pictures etc.) of the RecyclerView. To do that I need to know which child element of the RecyclerView is under the initial touch_down(x,y) coordinates, tag it and then continue drawing.
Currently I have these problems:
I'm not sure whether the getFocusedChild() method of the RecyclerView will correctly return the child element that is under the touch_down(x,y);
In order to let the touch event through to the RecyclerView, I need to not consume it in the DrawingView, then tag the child element in the RecyclerView and not consume it again, then return it to the DrawingView and continue with the drawing.
This is proving rather difficult for me since I've only recently started Android programming and am not all that familiar with the API and how it works.
Any suggestions about how to link/tag the child elements so that I can easily know which drawing/path belongs to which child element would be most welcome.
Apparently there's a method in RecyclerView called findChildViewUnder() which does exactly what I needed.

How to preview Android ListView with custom row and header layout

I have created a list view with a custom layout for the header and rows items many times but what always annoys me is the UI preview in Android Studio does not show a preview. Obviously because the custom layouts are loaded programmatically by the ListView or CursorAdapter but what if I wanted to some how specify a header and footer layout in xml so that I could see a preview. Any one know how to do that?
You can use tools:listitem. Just add the tools namespace in the root of the layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
(...)
and then in your ListView set your list item layout
<ListView
android:id="#+id/mylistView"
tools:listitem="#layout/my_list_item"
(...)
Is also possible to set header/footer with listheader/listfooter.
xmlns:tools="http://schemas.android.com/tools" namespace are using for development purposes(preview) and are not compiled in the application.
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listfooter="#layout/item_footer"
tools:listheader="#layout/item_header"
tools:listitem="#layout/item" />

How to use two activites in a single screen in android?

I want to display two different activities in a single screen how can i do that in android?Please if anybody has idea share it.And I don't wanna use fragments.
I want to display a screen which contains some fields and below(at the bottom of the screen) I want another screen with some buttons.
Is this possible in android?
If so, How can i do this ?
You can't have two activities in one screen. You can have only one. So, ultimate solution is Fragments.
An activity is not directly a visual component, so I'm thinking that what you're really asking is how to have a single activity display different views.
There's nothing that says you can't rerun setContentView() with a different layout/view ID. But there's another non-fragments way of doing what your probably want.
You can define more than one full-size (match_parent) view in a layout. What you want to do is set the visibility for one of them to "visible" with android:visibility="visible" and all the others to "gone" with android:visibility="gone".
Then when you want to switch the displayed view, you'll run setVisibility(View.GONE) on the outgoing view and setVisibility(View.VISIBLE) on the incoming. It's important to use GONE and not INVISIBLE or the layouts won't render correctly.
Sample layout file:
<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"
tools:context=".MainActivity" >
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible" />
<SurfaceView
android:id="#+id/video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<WebView
android:id="#+id/web"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</FrameLayout>
Sample Code to switch view:
video.setVisibility(View.VISIBLE);
img.setVisibility(View.GONE);
web.setVisibility(View.GONE);
That said, you probably want to learn how to use fragments since you can handle switching the view along with other state in a single unit of work (a transaction). But the above approach above does work for simple view changes.

In Android, what is similar with UI view of IOS?

In IOS, an image which can moved is attached like a magnet to a place to a place. I don't know what is like those method in android.
With an Activity you have a layout file. In that layout file you have multiple items that represent your view. The top most item is what is the same as UIView. You can give this layout an id like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/uiview"
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" >
Edit
Today we also have ConstraintLayout, which is like RelativeLayout but uses constraints (like iOS) to layout its childs. You can look here for more information: https://developer.android.com/training/constraint-layout/index.html

Muliple CustomViews to add in single Layout :: Android

I have many custom Views. I want to show a specific custom view on Layout. I am using a View and trying to initialize it with custom view. its not working any help please?
View custom=(View)findViewById(R.id.animation_View);
custom=new CustomeView(this, null);
setContentView(R.layout.activity_animation);
Activity_XMl
<View
android:id="#+id/animation_View"
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" >
</View>
Unfortunately. your approach is wrong. You are inflating a view from xml and then again assigning a new programmatically created CustomView. If your custom view is CustomView, then there is no need to programmatically create a new CustomView.
Your final code should be,
View custom=(View)findViewById(R.id.animation_View);
setContentView(R.layout.activity_animation);
assuming you have correctly extended a View class and added it to your activity_animation xml. For help regarding creating custom view components, check out-
http://www.vogella.com/articles/AndroidCustomViews/article.html
http://mobile.tutsplus.com/tutorials/android/android-sdk-creating-custom-views-2/
http://developer.android.com/training/custom-views/index.html
There are two ways of using a CustomView
First way is using XML file:-
For example, change the activity_animation.xml file as
<com.myapp.CustomView
android:id="#+id/animation_View"
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" >
</com.myapp.CustomView>
Note that the com.myapp should be replaced with the package name in which you have CustomView.
Then set this xml in ur activity as
setContentView(R.layout.activity_animation);
Second way, is doing it dynamically, without using any xml:-
CustomView customView = new CustomeView(this, null);
setContentView(customView);

Categories

Resources