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

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

Related

Why can't ListView be top level view?

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.

Image not resizing in Android Studio's emulator

I'm using a USB connected device to run my app. In this practice app, I used an Android logo image to implement the zooming feature. When I try to change the image dimensions in the activity, it does not show the changes when the app imported into the usb device is loaded. The logo is simply zoomed to fit. But I want it to display the image with the way I see it in the Design view: http://i.imgur.com/hcaOHjW.png
I forked the code from this source to implement the zooming: http://www.c-sharpcorner.com/UploadFile/88b6e5/multi-touch-panning-pinch-zoom-image-view-in-android-using/
It does not have the double tap zoom in and out feature so let me know what I can add to implement that
And here is the code for my activity:
<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: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="com.example.test.firstapp.Login"
android:id="#+id/activity_activity2"
android:focusable="false"
android:nestedScrollingEnabled="false">
<ImageView
android:layout_width="180dp"
android:layout_height="220dp"
android:id="#+id/botImg"
android:src="#drawable/bot"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"/>
in your java class try creating this view and then try changing the height and width parameters .inflate this view dynamically and assign parameters in the java itself dont use xml for this view .

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" />

Android XML putting object on maximum top (RelativeLayout)

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"

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