I have RelativeLayout view with RadioGroup inside. I'm trying to embed RadioGroup into RelativeLayout. So to add elements to my Layout. My code goes here
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<RadioGroup
android:id="#+id/rg1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RadioButton
android:id="#+id/r1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Example1"
android:checked="true"
/>
<RadioButton
android:id="#+id/r2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Example2"
android:checked="true"
/>
</RadioGroup>
Doing this shows an exception:
This RadioGroup layout or its RelativeLayout parent is possibly useless
How can I solve this warning?
Normally this warning is shown when you have a Layout with only one element inside.
For example a LinearLayour with only one LinearLayour inside (this last one could have many Views) would be useless, because it doesn't serve of any layout purposes (the purpose of a layout is distributing views).
In your specific case you could get rid of the outer RelativeLayout.
Yes you are right, It is just a warning to reduce view hierarchy.
If you layout has only Single RadioGroup the use the following code
<RadioGroup
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rg1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin" android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<RadioButton
android:id="#+id/r1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Example1"
android:checked="true"
/>
<RadioButton
android:id="#+id/r2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Example2"
android:checked="true"
/>
</RadioGroup>
Related
I want to show the word "STATS" above a Listview containing either 2 or 3 list items. I want the ListView at the bottom edge of the screen and the remaining space to be filled by the "STATS" TextView and the text centered vertically and horizontally. Basically a single word floating lonely in the exact middle of the remaining space.
I cannot do this in the "drag and drop" layout editor because it shows 8 placeholder items in the ListView, so I can't snap to the edges of things properly.
I've got the ListView how I want it, but the "STATS" is causing problems.
I've tried all sorts of things (apart from the right one, obviously). Here's my currently broken layout...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="uk.co.xxxxxxxxxxx.xxxxxxx.MainActivity"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="bottom"
>
<TextView
android:id="#+id/tv_reboot_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textStyle="bold"
android:textAlignment="center"
android:text="#string/stats"
/>
<ListView
android:id="#+id/lv_app_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/reboot_screen_items"
android:layout_gravity="center_horizontal|center_vertical"
/>
</LinearLayout>
I'm sure it's something trivial to do but its driving me mad.
Try this, it works well
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tv_reboot_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textStyle="bold"
android:textAlignment="center"
android:text="STATE"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center" />
<ListView
android:id="#+id/lv_app_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/reboot_screen_items"
android:layout_gravity="bottom" />
</LinearLayout>
Result
Can you try this?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="uk.co.xxxxxxxxxxx.xxxxxxx.MainActivity"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/tv_reboot_message"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:textStyle="bold"
android:textAlignment="center"
android:gravity="center"
android:text="STATS"
android:layout_above="#+id/lv_app_info"
/>
<ListView
android:id="#+id/lv_app_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/values"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
/>
</RelativeLayout>
You can use RelativeLayout to fill up the remaining space to show your TextView.
Use gravity and layout_gravity attributes.
Gravity and layout_gravity
this is my XML code
<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=".MainActivity">
<RelativeLayout
android:id="#+id/RelativeLayout01" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:id="#+id/search_text"
android:layout_alignParentRight="true"
android:text="Download"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:id="#+id/search_box"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/search_text"
android:layout_centerVertical="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Paste Link Here"
android:inputType="text"
android:maxLines="1" />
</RelativeLayout>
</RelativeLayout>
i need to clone the inner part of code, i mean i want to create 4 row like first row , and i need to show this link and download button of inner relative layout 4 time,under the first one, like a list
how can i clone and show 5 of this relative view in my main page?
i want to copy this relative view just under the first one,and third one just under the second one and ...
its better to add them dynamically from code. Tomorrow you might think to give 6 or 8 download links... :) Use addView() method.
See this http://developer.android.com/reference/android/view/ViewGroup.html#addView%28android.view.View%29
Create a new row.xml to represent each row
<RelativeLayout
android:id="#+id/RelativeLayout01" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:id="#+id/search_text"
android:layout_alignParentRight="true"
android:text="Download"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:id="#+id/search_box"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/search_text"
android:layout_centerVertical="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Paste Link Here"
android:inputType="text"
android:maxLines="1" />
</RelativeLayout>
And then use to replicate it in your main layout
<LinearLayout 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=".MainActivity"
android:orientation="vertical">
<include layout="#layout/row" id="#+id/row1" />
<include layout="#layout/row" id="#+id/row2" />
<include layout="#layout/row" id="#+id/row3" />
<include layout="#layout/row" id="#+id/row4" />
</LinearLayout>
For more information on reusing android layouts, have a look here Re-using layuts
As mentioned in a comment above, if you have no specific reason to do it this way, you should use a listview instead or a gridlayout or another UI element that fits your needs
not sure what I am doing wrong, nothing complicated 2 buttons inside a LinearLayout and I get
"This LinearLayout layout or its RelativeLayout parent is useless; transfer the background attribute to the other view" warning....here is the code, is boggling my mind
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/GCBS_Template_BKGND"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/Partfinder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Part Finder" />
<Button
android:id="#+id/Heaterfinder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Heater Finder" />
</LinearLayout>
If you just want to show two buttons vertically - there's no need to use the LinearLayout in this case. This is increasing your view level hierarchy- that's why the warning is shown.
You can simply use android:layout_below="#+id/Partfinder" to your 2nd button to achieve what you want.
In Eclipse Juno, you can Project's Properties -> Android Lint Preferences. Now, Look for UselessParent and set it's severity to ignore or click Ignore All.
But at the end, Your problem is your RelativeLayout only have one child. You can remove it like that:
<LinearLayout 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: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"
android:background="#drawable/GCBS_Template_BKGND"
tools:context=".MainActivity" >
<Button
android:id="#+id/Partfinder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Part Finder" />
<Button
android:id="#+id/Heaterfinder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Heater Finder" />
</LinearLayout>
The warning is very clear as per your xml layout creation. For just adding 2 buttons you may not need both Relative & Linear Layout but only one of the layouts is more than enough. So, by deleting the Linear Layout in your XML should help you proceed further like below.
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/GCBS_Template_BKGND"
tools:context=".MainActivity" >
<Button
android:id="#+id/Partfinder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Part Finder" />
<Button
android:id="#+id/Heaterfinder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Heater Finder" />
</RelativeLayout>
After this just drag and drop the buttons in layout view of the xml and position them wherever you want the buttons. That should put the XML script automatically in your XML file.
I'm trying to put a TextView on top, another on bottom, and a ListView in between using the remaining space.
However, my ListView is taking all the space to the bottom, making the last TextView to not even show up. The only way I can make it show up is to give the ListView a fixed height, like 100dp.
My layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/id_task_list_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title_task_list"
style="?android:attr/listSeparatorTextViewStyle" />
<ListView
android:id="#+id/id_task_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/id_task_list_title" />
<TextView
android:id="#+id/id_task_list_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/id_task_list_view"
android:text="OMG Test" />
</RelativeLayout>
Use LinearLayout as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_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"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="#+id/id_task_list_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title_task_list"
style="?android:attr/listSeparatorTextViewStyle" />
<ListView
android:id="#+id/id_task_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TextView
android:id="#+id/id_task_list_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OMG Test" />
</LinearLayout>
This should solve your issue by placing your ListView between the TextViews using both android:layout_above and android:layout_below :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/id_task_list_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title_task_list"
style="?android:attr/listSeparatorTextViewStyle" />
<TextView
android:id="#+id/id_task_list_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="OMG Test" />
<ListView
android:id="#+id/id_task_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/id_task_list_test"
android:layout_below="#id/id_task_list_title"/>
</RelativeLayout>
It can be achieved one of the two ways
1) Using Linear Layout (Straight forward)
2) Using Relative Layout with referencing the view position with respected to the other views.
In your case
1) Change Relative to LinearLayout and work will be done.
2)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/id_task_list_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title_task_list"
style="?android:attr/listSeparatorTextViewStyle" />
<ListView
android:layout_below="#+id/id_task_list_title"
android:id="#+id/id_task_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/id_task_list_title" />
<TextView
android:layout_below="#+id/id_task_list_view"
android:id="#+id/id_task_list_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/id_task_list_view"
android:text="OMG Test" />
</RelativeLayout>
This can be done through coding by getting the height of screen and alloting layout param width to list view like this
height of list view=screen height-(height of textView1+Height of textView2).
Instead of Using RelativeLayout , use LinearLayout with Orientation Vertical.
I think it solves your problem
Thank you
I have a LinearLayout & i have 2 TextViews inside it. Dynamically i want to hide one TextView or i can add TextViews at runtime. My problem is when TextView disappears from LinearLayout it collapses in no time. I want to animate the LinearLayout(ScaleAnimation) when views get removed from LinearLayout.
activity_main.xml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#000"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:background="#fff"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Hello, World" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Hello, World 2" />
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Remove TextView" />
</RelativeLayout>
Snapshot of layout...
I want to animate LinearLayout when second TextView got removed from LinearLayout & if another TextView got added into LinearLayout. ScaleAnimation is good for the above task but i don't know values required for this type of animation.
Pretty sure that you do not need it anymore, but for future searchers:
For the remove animation, it is possible to make the item invisible using view.setVisibility(View.INVISIBLE) and after the animation (addAnimationListener) effectually remove it from the linear layout. When a view is invisible its parent keeps measuring it.