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);
Related
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.
I am having a Fragment, where I inflate "fragment_board.xml":
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<view
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.app.BoardView"
android:id="#+id/view_board"/>
</FrameLayout>
As you can see, fragment_board contains a custom view "BoardView", from where I want to load the following "view_board.xml":
<com.app.BoardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:fadingEdge="none"
android:requiresFadingEdge="none"
android:overScrollMode="always"
android:id="#+id/board_scrollview_vertical">
<android.widget.HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none"
android:fadingEdge="none"
android:requiresFadingEdge="none"
android:overScrollMode="always"
android:id="#+id/board_scrollview_horizontal"
android:foregroundGravity="left">
</android.widget.HorizontalScrollView>
</com.app.BoardView>
My custom view contains two scroll views (I use it for panning), and I want to be able to re-use it in other layouts. The BoardView extends the outer (vertical) scroll view like this:
public class BoardView extends ScrollView
When I use it stand-alone, it inflates fine and I can findViewById() both scroll views in the inflated layout. But when I use it in a layout tree (such as a fragment), I run into problems.
In the fragment, I inflate the layout tree like this:
View view = inflater.inflate(R.layout.fragment_board, container, false)
From the fragment, I can findViewById() the outer (vertical) scroll view, but findViewById() returns null for the inner (horizontal) scroll view.
In BoardView, I inflate like this:
View view = inflate(getContext(), R.layout.view_board, this);
As said, I can find both scroll views fine when inflated by itself, but when inflated as part of the fragment, I get StackOverflowError.
It is clear why: I inflate in the fragment first, and then I inflate the same XML in the view again, which triggers another inflation of the view and so on. I get a cyclic reference here. Problem I can't figure out how I can add the inner (horizontal) scroll view to the already existing layout. I think I need to merge somehow or inflate layouts manually, but I can't figure out how.
Here are some refs (which didn't help in my case):
Android - Writing a custom (compound) component
How to inflate XML-Layout-File correctly inside Custom ViewGroup?
Create a custom View by inflating a layout?
InvocationTargetException on inflating an xml - android
Android: StackOverFlowError with InvocationTargetException when inflating layout
Can anybody suggest what I should do. If possible, I'd want BoardView to work as a generic component, which I can plug into a layout tree where needed.
As Android [according to one of the refs above] does not officially support composite components, would my best option be to drop XML layouts for the inner views and add them in code?
Please check if something like this fixes the issue:
fragment_board.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.app.BoardView
android:id="#+id/view_board"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
view_board.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:fadingEdge="none"
android:requiresFadingEdge="none"
android:overScrollMode="always"
android:id="#+id/board_scrollview_vertical">
<android.widget.HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none"
android:fadingEdge="none"
android:requiresFadingEdge="none"
android:overScrollMode="always"
android:id="#+id/board_scrollview_horizontal"
android:foregroundGravity="left">
</android.widget.HorizontalScrollView>
</merge>
More information on using MERGE and INCLUDE
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" />
I want to put some imageview and buttons on top of screen using relative layout. When I use android:layout_alignParentTop = "true", it just does this:
instead of what I want to:
Any solutions how to implement this into RelativeLayout?
CODE: http://pastebin.com/NGcherYm
Due to long code I had to put in on pastebin.
Remove these lines
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
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