Little confused with the design in android..suppose i need to position two blocks
as in the figure..what shall i do?..if we use pixels the design looks different in different phones.
Design should be fixed in all phones..but how without using px we can design as above
Use two LinearLayout in your layout xml and set layout_marginLeft,layout_marginTop,layout_marginRight in dip according to the need.
See the following layout, its very similar to the one you want:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:text="Button01"
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dip"
android:layout_marginLeft="30dip"
></Button>
<Button
android:text="Button02"
android:id="#+id/Button02"
android:layout_below="#id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="140dip"
android:layout_marginRight="30dip"
></Button>
</RelativeLayout>
Hope this will solve your issue.
You could generate the layout in Java by getting the screen size and placing two objects (defined in xml or java) into the parent layout, taking into account the ratios.
If dip is unable 2 solve ur problm , surely any of these approaches will work :
1) take a 9patch backgroud with proportionate padding . now put first textview with gravity left|top and second with grvity right|bottom . done
or
2)Take two child of a vertical linearlayot with layout_weight=1 , so will each will take 50% height now to put put textview center vertical . now lets try fix it horizontally.4 each text
take two child textview with layout_weight=1 ,put text center_horizontal .for upper text male second invisible n for lower make first one invisible ......
Related
I am new to Android.
I am stuck off in understanding the concept of using weight sum with weighted LinearLayouts. Have skimmed through a number of related questions on StackOverflow but still haven't found the solution.
Scenario
I have an activity layout page with number of components . The components are oriented vertically in the linear layout according to their layout_weights.
Code
<ScrollView fillViewPort="true" scrollBars="true">
<LinearLayout android:weightSum="150">
<!-- Various components present here each assigned layout_weight attribute
Total sum being 150 itself -->
</LinearLayout>
</ScrollView>
Question
My question is that how can I use scroll View and weightSum together such that a total of weightSum=100 displays on one page and for the rest 50 user needs to scroll down.
This is the one idea how it could possible
<LinearLayout weightsum="150" android:orientation="vertical">
<LinearLayout layout_weight="100">
//your LinearLayout
</LinearLayout>
<ScrollView Layout_weight="50">
//your scrollview layout
</ScrollView>
</LinearLayout>
Documentation says that the mission of weightSum is managing spaces below/under all the items of LinearLayout. Moreover, the weight does not purpose for specifying exact size of layout (but only for relative size), so nor for the paging. If you want to scroll Views page-by-page, you can use ViewPager.
I have a FrameLayout which is a wrapper for a TextView. The TextView can be either one of two TextView objects with dynamically modified text. I would like to add an image immediately to the right of the text contained in the FrameLayout. I have tried two things:
I set up the FrameLayout inside a RelativeLayout. I added an ImageView:
<RelativeLayout>
<!-- Two TextViews Here -->
<FrameLayout
android:id="#+id/my_frame_layout"
android:layout_width="match_parent"
...
/>
<ImageView
android:id="#+id/my_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/my_frame_layout"
/>
</RelativeLayout>
This worked the first time the layout was loaded. However, when the text contained in the FrameLayout changed, the image stayed at its originally calculated position, rather than recalculating to stay to the right of the TextView.
So I tried making it a drawable and setting it in code. I fetched the TextView held in the FrameLayout, and called:
setCompoundDrawablesWithIntrinsicBounds(0,0,myDrawable,0)
This worked well, except that the drawable is always to the furthest right point of the FrameLayout, despite the TextView being only half full. I want the image to be immediately to the right of the text.
Does anyone have any other suggestions I could try to make this happen?
I ended up wrapping the ImageView in an additional LinearLayout, like so:
<LinearLayout
android:id="#+id/my_image_wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="my_frame_layout"
android:gravity="left|center_vertical"
>
<ImageView
android:id="#+id/my_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Unlike the ImageView alone, the LinearLayout is redrawn when the text changes size, so this worked for me.
I have a problem with a fixed size scrollview that i'm trying to make. I've read many questions that are similar (but not equal) to mine here in StackOverflow and other sites and none of the answers have helped me, so I decided to ask my own question.
Basically, i want a fixed size scrollview with different controls inside. The basic one would be a textview of a dynamically changing size inside it. When I change the text of the textview and it is bigger than the scrollview, the scrollbars flash quickly, as they should, indicating that I can scroll but no matter how many times I swipe my finger, it doesn't scroll. Then I tried swiping with two or three fingers and sometimes (only a counted number of times) it scrolls.
I have tried many different approaches to this, like changing the textview to an edittext with focusable = false so it doesn't give the user chance to edit the text; or putting the textview alone in the scrollview, or wrapping it in linearlayouts, relative layouts etc. and it still doesnt scroll.
Below is the code as it is today. This scrollview is inside a vertical linearlayout along with other controls that I'm not putting 'cause of the length, but if someone needs it, I'll put it. I would appreciate very much if someone can point to my problem or help me solving this.
XML:
<ScrollView
android:id="#+id/scrollFifthHorizontalLineDetails"
android:layout_width="304dp"
android:layout_height="133dp"
android:layout_gravity="center"
android:layout_marginTop="5dp" >
<LinearLayout
android:id="#+id/rrrr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/txtlblPlaceDetails"
android:layout_width="302dp"
android:layout_height="wrap_content"
android:background="#drawable/placedescriptionbg"
android:text="#string/null_text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</ScrollView>
the line in which I populate the TextView:
((TextView) findViewById(R.id.txtlblPlaceDetails)).setText(mJsonOb.getString("placeDescription"));
I had a similar problem with an Android app I was developing a few months back. I got around the scrolling problem by increasing the layout width for the scroll view. In the editor you can actually drag the RHS of the scroll view to exceed the visible area. I think I set the android:layout_width to about 1024.
I'm designing an app with many images, buttons and textViews strewn across the screen. At the moment I am using the relative layout as it seemed the most flexible of the lot. However were I place my elements and their size is still restricted to being aligned with other elements. Even worse if an element changes size any elements aligned to it will also change size.
There must be a simple solution to this! Apple's nib files perform this so easily with an effortless drag and drop to any location; yet android appears to be stuck with restrictive table/linear/relative/grid layouts.
If possible can the solution be performed via eclipse. If not please guide me to the relevant documentation to learn to create my own layouts via xml, create a huge grid layout or whatever horrors await me :)
Thanks
I think what you want is something like an absolute layout tho these were deprecated a while ago, Im pretty sure you can still do this via a Relative layout, you don't necessarily need to align the via with another view, I guess you could just do something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="70dp"
android:layout_marginTop="82dp"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="172dp"
android:layout_marginRight="84dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
all I'm doing here is aligning it with the side of the parent and having a margin between it
You can do what you ask with a Frame Layout and setting the position of each object. But do so at your own risk. The reason Apple nib files let you arbitrarily place objects is taht the aspect ratio of all their devices is the same. So your layouts just scale up and down evenly.
Android is a more diverse ecosystem, and you should try to embrace layouts that adapt to different screen sizes and orientations.
Take a look at Android custom layout and Android - How to draw a letter at a specific point?
Are you planning to only use the app on a single android device model? If yes, check AbsoluteLayout's Alternatives. (FrameLayout or RelativeLayout)
It's not a good idea to put "Anything Anywhere you want" since Android devices have a lot of different screen sizes and properties. The only option would be to define your own custom layout.
I actually really like the way Android tries to make your layout compatible with as much devices as it can using alignment and structured layout views.
The reason it's simple for Apple is that you're only targeting iPhone, which has a fixed screen properties accross all devices.
Hope it helps. Good luck.
I am adding accessebility support to some application. It works fine with standart UI elements (buttons for example), but for some reason does not work with my custom element, wich is RelativeLayout with ImageView and TextView (it looks like icon). I've defined android:focusable="true" and set contentDescription.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
android:contentDescription=”my content description”
android:focusable="true">
<ImageView
...
/>
<TextView
...
/>
</RelativeLayout>
Could someone please list here all posible causes?
UPDATE:
Is there some way to know what layouts are on the screen at the moment and what order do they have (some layouts are transparent)?
Use hierarchy viewer for understanding where is your invisible views.
The android docs have a section about designing for accessibility, is this any use to you?