OnClickListener working at padding area - android

I want to make that when I add ClickListener to #id/content it should work at whole area within RelativeLayout on the left of #id/group_indicator. Not only at selected area below. Analogously I want the same for #id/group_indicator.
I guess it's because of android:padding in RelativeLayout but I don't know how to make it work.
More of this, why height of #id/content and #id/group_indicator aren't equal? Both of them are aligned to parent bottom and top.
My layout file:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/selector_card_background"
android:descendantFocusability="afterDescendants"
android:padding="15dp">
<TextView
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Test text"
android:layout_toLeftOf="#+id/group_indicator" />
<ImageButton
android:id="#+id/group_indicator"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#null"
android:scaleType="fitCenter" />
</RelativeLayout>
Output:
If the title is incorrect, please fix it, I'm not sure if it's clear.

You cant actually listen to the padding when you click on the eddittext, because it is part of the relative layout.
Solution:
Create a parent view to the TextView and remove the
PAdding of the relativelayout and add the padding to the parentview(this parentview is inside the relative layout) and use that parentview for listening to the onclick so youll have the whole area to listen in onClick.

Related

How to keep RecyclerView always above View in RelativeLayout

Im using com.android.support:recyclerview-v7:23.2.1 and with that version it should be possible to use WRAP_CONTENT on a RecyclerView height. I for now have the following layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:importantForAccessibility="no">
<android.support.v7.widget.RecyclerView
android:id="#+id/message_list_row_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/message_list_message_divider"
android:layout_alignParentTop="true"
android:clipToPadding="false"
android:divider="#null"
android:importantForAccessibility="no"
android:listSelector="#android:color/transparent"
android:padding="10dp"
android:textColor="#color/textcolorprimary" />
<View
android:id="#+id/message_list_message_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_above="#+id/message_list_layout_message"
android:layout_gravity="center"
android:background="#android:color/darker_gray"
android:importantForAccessibility="no" />
<RelativeLayout
android:id="#+id/message_list_layout_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:importantForAccessibility="no"
android:orientation="horizontal">
<EditText
android:id="#+id/message_list_edittext_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginEnd="10dp"
android:layout_toStartOf="#+id/message_list_button_send"
android:background="#color/transparent"
android:hint="#string/message_input"
android:inputType="textMultiLine"
android:maxLines="10"
android:minLines="3"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:textColor="#color/primaryColor"
android:textSize="16sp" />
<ImageButton
android:id="#+id/message_list_button_send"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="#null"
android:contentDescription="#string/content_description_image_send"
android:gravity="center"
android:scaleType="fitXY"
android:src="#drawable/custom_button_send" />
</RelativeLayout>
</RelativeLayout>
The Preview in Android Studio is showing the correct layout with the RecyclerView above the dummy line View i inserted. I need this kind of layout because the RelativeLayout with the EditText inside can grow in height (if there are mor than 3 lines the EditText is growing in height and also the RelativeLayout).
When i install it on my Phone there is a problem. The RecylcerView is not above the view i defined. it is filling the complete height of the device. so the last row of my recylcerview is always behind the relative layout.
Why that? And how can i fix it? I need the RecyclerView to stay ALWAYS above the my divider and to shrink in height when relativelayout is growing in height.
Edit I want to provide some screenshots to show you the problem. The first Screenshot shows my Chat with the current layout:
But there is missing one item because the scroll height is bigger than defined by the layout. The missing item you can see here after i scrolled down:
When i now (just to find the source of the problem) remove alignParentTop from the RecyclerView and set its height to 300dp, the change of the height gets correctly respected:
But as you can see several items are missing now because they are hiding out of the field of view (yes... even when i programmatically scroll to bottom after initialization). To show that all elements are there:
So the height gets correctly set... but the last elements arent showing up until i manually scroll down. programmatically scrolling does not have an effect.
Add this to the RelativeLayout with EditText inside it layout_below="#+id/message_list_message_divider". And remove the layout_above on the divider
I fixed a similar problem with placing the element you want ti have on top as last child of the parent layout or try putting android:translationZ="1dp" into the recyclerview.

RelativeLayout positioning issue with first child LinearLayout using margin

I'm currently trying to position some LinearLayouts (which are then filled with some images) on an absolute position on the screen. Currently the LinearLayouts are alinged to the top-left of the parent RelativeLayout and then positioned using margins. This works properly for all child LinearLayouts EXCEPT the first one in the list. Setting some margins (as for example 25dp for the left margin) does not show any effect in this case. I hope someone has had the same issue and can provide some guidance.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_page"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="#+id/ll_first" android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="25dp" android:layout_marginTop="20dp"></LinearLayout>
<LinearLayout android:id="#+id/ll_second" android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="175dp" android:layout_marginTop="30dp"></LinearLayout>
<LinearLayout android:id="#+id/ll_third" android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="40dp" android:layout_marginTop="140dp"></LinearLayout>
</RelativeLayout>
Edit:
Example image http://i.stack.imgur.com/HuYjU.jpg
The RelativeLayout provides some tags for the positioning of Views/ViewGroups relative to another View/ViewGroup.
android:layout_below="id" and android:layout_above="id"
This will put the View below/above another View with the id.
android:layout_alignRightOf="id" and android:layout_alignLeftOf="id"
This will put the View to the right/left of another View with the id.
All tags have the same usability like this, where I use the android:layout_below="" to set a View/ViewGroup below another:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_page"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<--! YOUR LAYOUT AT THE TOP OF YOUR PARENT -->
<LinearLayout
android:id="#+id/ll_first"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"></LinearLayout>
<LinearLayout android:id="#+id/ll_second"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="#id/ll_first" ></LinearLayout>
<LinearLayout
android:id="#+id/ll_third"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="#id/ll_second"></LinearLayout>
</RelativeLayout>
Now you can use some margins for the position of every View.

Android: Align one element below and in the middle of another

I have an ImageView and TextView. I want to place the TextView below the image view but with the middle of the TextView aligned with the middle of the ImageView (along the horizontal axis). If the text in the TextView changes to something much larger or smaller, the middle of the text always needs to remain aligned with the middle of the ImageView. Is this possible in xml?
Yes, you are simply looking to contain both elements in a vertical LinearLayout which has android:gravity set to center_horizontal.
Something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
... >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
... />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
... />
</LinearLayout>
Because the TextView's width is wrap_content, setting its gravity shouldn't be necessary. I would do it just for safety (and additionally, I may set the width to match_parent as well).
You can make use of RealtiveLayout as follows..
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
... >
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
... />
<TextView
android:layout_below="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
If you want your textview to be of single line only then add following properties to TextView
android:ellipsezed="end"
android:singleLine="true"
Use a given layout and add it to any of your existing layout you get result what your looking for ..
Hope this explanation works for you..
set TextView android:layout_width="fill_parent" and set android:gravity="center" in TextView.
I think it help you.
Thanks

android center textview in layout

I have a listview with custom rows. In the row there are these items:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView1"
android:layout_width="10dp"
android:layout_height="fill_parent"
android:focusable="false"
android:scaleType="fitXY">
</ImageView>"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textsll"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:layout_marginLeft="5dp">
<TextView android:id="#+id/textView1"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:maxLines="1"
android:ellipsize="end">"
</TextView>
<TextView android:id="#+id/textView2"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_alignParentBottom="true"
android:layout_below="#+id/textView1"
android:maxLines="1"
android:ellipsize="end">
</TextView>
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textsll2"
android:layout_gravity="center_vertical|right"
android:layout_marginLeft="10dp">"
<TextView android:id="#+id/textView3"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:textColor="#000000"
android:paddingBottom="5dp"
android:maxLines="1"
android:ellipsize="end"
android:gravity="center_vertical"
android:background="#87EB81">
</TextView>
</LinearLayout>
</LinearLayout>
If both textviews has some content, they are centered vertically in the row (almost).
But if the lower textview is empty, I call:
holder.textView2.setVisibility(View.INVISIBLE);
holder.textView.setGravity(Gravity.CENTER_VERTICAL);
Still, the upper textview gets into the top of the row. Why?
If I set it to View.GONE, I get this, but I dont want the height of the row to change:
Use android:layout_weight to set all the childs of the main LinearLayout to mesure the same.
Use a LinearLayout as parent for textView1 and textView2. Use Use android:layout_weight to have the same size for each one.
Define TextView one with android:gravity="center_vertical|left" Then, if you don't want to show the second TextView, set its visibility as View.GONE.
Maybe this will resolve your problem,
Update:
In a RelativeLayout, views are aligned with their parent, with the RelativeLayout itself, or with other views. For instance, we declared that the description is aligned with the bottom of the RelativeLayout and that the title is positioned above the description and anchored to the parent's top. With the description GONE, RelativeLayout doesn't know where to position the title's bottom edge. To solve this problem, you can use a very special layout parameter called layout_alignWithParentIfMissing.
This boolean parameter simply tells RelativeLayout to use its own edges as anchors when a constraint target is missing. For instance, if you position a view to the right of a GONE view and set alignWithParentIfMissing to true, RelativeLayout will instead anchor the view to its left edge. In our case, using alignWithParentIfMissing will cause RelativeLayout to align the title's bottom with its own bottom.
For more look at Layout Tricks: Creating Efficient Layouts
This can be possible in your RelativeLayout by setting up the layout_below and layout_above with the help of parent alignment by using layout_centerInParent and set the value true of layout_centerHorizontal
Hope, this will work.
Try to use:
<TextView
...
android:layout_height="?android:attr/listPreferredItemHeight"
...
/>
in your_list_item.xml. And use View.GONE
Try to fix some value to your RelativeLayout's height like-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textsll"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:layout_marginLeft="5dp">
To Solve for your problem what u can do is have the LinearLayout fill Parent.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
And use View.GONE This will make the Row Height Same.
If you see a gap after Best Car in World then try using Relative Layout and align last Layout to bottom.

How to create fixed footer in android layout?

I am using following code to display button at the bottom of activity.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
>
<Button android:id="#+id/btnGetMoreResults"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Get more"
/>
</RelativeLayout>
and listview above it. when i display more data in listview this button pannel is moved down.can any one guide me how can i fix it at the bottom of activity?
any help would be appreciated.
The answer selected as correct is faulty, the button will hide the lower part of the list view. The correct way is to declare the button first and position the list above the button.
<Button android:id="#+id/btnGetMoreResults"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Get more"
android:layout_alignParentBottom="true"/>
<ListView
...
android:layout_above="#id/btnGetMoreResults"/>
The android:layout_alignParentBottom attribute has to be declared in an element of the RelativeLayout not in the RelativeLayout himself (unless there is another RelativeLayout as a parent).
You should do something like this, with the ListView inside the RelativeLayout also :
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<ListView ...>
<Button android:id="#+id/btnGetMoreResults"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Get more"
android:layout_alignParentBottom="true" />
</RelativeLayout>
If you had, for example, all the scrollable elements in a ScrollView, you should do like the following:
<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"
style="#style/rootElement"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- texts, buttons, images and anything that you want to scroll -->
</ScrollView>
<LinearLayout
android:orientation="vertical"
style="#style/footer"
android:id="#+id/footer"
android:layout_alignParentBottom="true"
>
</LinearLayout>
</RelativeLayout>
Note that if you want the footer to be fixed, then you shouldn't put it in the ScrollView, where the scrollable content will be placed. Make it child of RelativeLayout and set layout_alignParentBottom to true. Maybe you'll need to add a padding at the bottom of the ScrollView in this case (so that the last element do not get hidden by the footer).
The idea is similar for elements other than ScrollView

Categories

Resources