Android layout_below not working - android

I use RelativeLayouts extensively in my app and thought I knew how to specify them, but this has me at a loss. I am basically positioning 4 TextViews in two rows of two each consisting of a label and text that will be supplied. It should look something like:
Born: 23 Aug 1810 Mason Co., Kentucky
Died: 15 Jul 1865 Cincinnati, Hamilton Co., Ohio
This is the relevant portion of the layout:
<TextView android:id="#+id/birth_lbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/given_layout"
android:layout_alignLeft="#+id/given_layout"
android:layout_marginTop="6dip"
style="#style/label"
android:text="#string/born"
/>
<TextView android:id="#+id/birth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/birth_lbl"
android:layout_alignBaseline="#+id/birth_lbl"
android:layout_marginLeft="10dip"
android:layout_marginRight="6dip"
style="#style/main_text"
android:text="dd Mmm yy"
/>
<TextView android:id="#+id/death_lbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/birth"
android:layout_alignLeft="#+id/birth_lbl"
android:layout_marginTop="4dip"
style="#style/label"
android:text="#string/died"
/>
<TextView android:id="#+id/death"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/death_lbl"
android:layout_alignLeft="#+id/birth"
android:layout_alignBaseline="#+id/death_lbl"
android:layout_marginRight="6dip"
style="#style/main_text"
android:text="dd Mmm yy"
/>
For some reason, this displays the death line views ABOVE the birth line views! If I change the spec of the death_lbl view to instead be 'layout_below="#+id/birth_lbl"', the lines are positioned correctly! However, it is possible for the "birth" view to wrap to multiple lines, so I really need to position the 2nd line below "birth", not "birth_lbl".
Anyone know the reason for this behavior? It occurs both in the Graphical Layout editor in Eclipse and at runtime on my tablet running Android 4.0.

Try changing android:layout_below="#+id/birth" in death_lbl to android:layout_below="#id/birth", because at this point it is already declared, which the + implies, it could lead to problems when declaring it again.

I was also facing the same problem because I was using constraint layout, but align_below works only in Relative Layout. so check which layout you are using.

If fixing your + signs doesn't help, you could always position your id/death below id/birth, and then put id/death_label toLeftOf id/death.

I was actually able to duplicate this phenomenon by coding up a temporary layout with only those fields, and was able to determine that the problem went away if I did not position the initial birth_lbl view relative to the view above it ("given_layout" in this case).
So I'm not sure if this is classified as a fix, a workaround, or a kludge (is that still a word these days?), but what I did was to position the text views inside their own RelativeLayout and position the RelativeLayout relative to id/given_layout. In any case, it works...

Your problem is that you are calling the properties on the relative layout as if though you are declaring it again.
When you declare an ID for a view then it should be done like this android:id="#+id/button"
and when you want Android to position that particular view above or below any other view in a relative layout you have to call it like this android:layout_below="#id/textview"
This tells android that you declared a button with id button and want it to be positioned below the textview, remember do not use #+id to postion view use #id instead.

Related

Android RTL layout direction align center issue

I'm developing an Android App that needs to be support Arabic language. (Which should be read from Right To Left). After quick googled the solutions, I figure out android fully support Arabic language natively in API level 17 with the declaration of
android:supportsRtl="true"
in the application tag inside of the AndroidManifest so that I can use the layout mirroring to automatically flip the layout for better right to left reading experience. However, I've noticed there is an issue happening while I use centerInParent in a view that inside of a sub RelativeLayout during the layout mirroring. Below are my codes and expected layout.
<RelativeLayout
android:background="#color/white"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="20dp">
<RelativeLayout
android:background="#drawable/shape_flag_imageview_boarder"
android:id="#+id/imageLayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_height="100dp"
android:layout_width="100dp"
android:visibility="invisible" />
<ProgressBar
android:id="#+id/progressbar"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</RelativeLayout>
<TextView
android:id="#+id/text"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_toEndOf="#id/imageLayout"
android:layout_width="wrap_content"
android:text="Some text here bla bla bla"
android:textColor="#color/black" />
</RelativeLayout>
Image above showing the expected result in normal layout direction which is Left to Right. The purpose I wrap the ImageView and ProgressBar together in a sub view is because I want the ProgressBar showing in the middle of the ImageView while the image is loading from the internet. After the I've changed Locale to Arabic, it becomes like
As I've try and error and figure out that this is causing by the centerInParent of the ProgressBar. It instead of centering inside the sub view, it align center to the root parent view which is the most outer RelativeLayout. Below is the screen shot of removing centerInParent code from the ProgressBar.
It clearly shows the layout mirroring works good, but the ProgressBar position is not what I'm expected. So I've try to work on centerVertical and centerHorizontal, the result are shown in images below respectively.
None of the solutions works, and none of the topic I've searched related to this issue. So I guess this might be a bug from Android library? If anyone knows the issues or solutions, please share to me. Thanks
I fixed it by adding android:layoutDirection="ltr" into the child RelativeLayout. Basically, it deactivates the RTL formatting for this particular RelativeLayout, and the android:layout_centerInParent="true" behaves correctly again. It solves our particular issue as our particular RelativeLayout contains only centred elements. But this trick should not be used if the Layout contains other elements which have to support correctly RTL, like text views for example. Hope it helps.
This is an RTL layout bug in the Android framework, which only affects Android 4.2 specifically (API 17) and when android:supportsRtl="true" is enabled in AndroidManifest.xml.
It happens when you use a RelativeLayout that contains items positioned with android:layout_centerVertical="true" or android:layout_centerInParent="true".
You can fix it in Java code like this:
View relativeLayout = layoutInflater.inflate(R.layout.my_relative_layout, parent, false);
// Fix RTL layout bug on Android 4.2 (for Arabic and Hebrew mode)
if (Build.VERSION.SDK_INT == 17 &&
getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
// Force a left-to-right layout
relativeLayout.setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
}
let me tell you correct answer, look your RelativeLayout(id:imageLayout),it's width is wrap_content, and your ProgressBar(id:progressbar) add an attribute android:layout_centerInParent="true".It means parent not limit witdh,and child also want to center,so parent will be stretched.

Two views referencing each other

I have two textviews declared as below. My intention is to have two views side by side, where left view (displaying user name) has bigger text size, right view displays time of last message send and should be always visible (even if user has really long name, that's why I'm using android:layout_toLeftOf). However, left view is smaller and I want to align its baseline to right view. It's really nice dependency where and I'm not able to solve it.
Partially acceptable solution is to use "android:layout_toRightOf" in right view, but if user has really long name, then time (right view) will be ellipsized (it's declared in AppTheme.TextView.SingleLine).
So basically, my questions is, is it possible for two views to reference each other? I understand why I'm getting this error, but I'm not able to solve it.
I remember from my C/C++ times that it was possible to declare function in the top of the file and then define it somewhere else (so the compiler doesn't complain) and I think it's something what I need here.
<TextView
android:id="#+id/fragment_messages_item_sender_name"
style="#style/AppTheme.TextView.SingleLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/fragment_messages_item_last_msg_time"
android:textSize="#dimen/global_text_large"/>
<TextView
android:id="#+id/fragment_messages_item_last_msg_time"
style="#style/AppTheme.TextView.SingleLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/fragment_messages_item_sender_name"
android:layout_alignParentEnd="true"
android:gravity="right"/>
All I'm getting is
Error:(27, 38) No resource found that matches the given name (at 'layout_toLeftOf' with value '#id/fragment_messages_item_last_msg_time').
in R class id like
fragment_messages_item_last_msg_time
not exist
for avoid that problem need to use "+" before declaration field with id relation
android:layout_toLeftOf="+#id/fragment_messages_item_last_msg_time"
you could move the android:layout_alignBaseline in the other TextView. Be aware of id loops, that usually generated nasty crashes.
About your issue, you have to remember the entries inside R are marked as public static final, and that the + generates a new entry for the specify id, if it does not exists, So you can have:
android:layout_toLeftOf="#+id/fragment_messages_item_last_msg_time"
in the first TextView, and
android:id="#id/fragment_messages_item_last_msg_time"
to the second one. As I mentioned before, loops in RelativeLayout are not allowed, and those will make your app crash
Hi use below code :
<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="horizontal" >
<TextView
android:id="#+id/fragment_messages_item_sender_name"
style="#style/AppTheme.TextView.SingleLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Manish" />
<TextView
android:id="#+id/fragment_messages_item_last_msg_time"
style="#style/AppTheme.TextView.SingleLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Khamar" />
</LinearLayout>

Android Eclipse placing a text view in specific places

I'm creating this in a XML file and want a text view to be placed in a specific spot.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="200dp"
android:text="Text"
android:textColor="#color/blue"
/>
It's underneath the last button that was placed so it's around in the middle of the screen. I want it to be at the bottom of the screen of to the right.
Easy if you're using a RelativeLayout as parent.
android:layout_alignParentRight
android:layout_alignParentBottom
(check the capitalization, I'm not anywhere I can double check).
Using a RelativeLayout would be perfect if you would be specifying each views' positions. It is the most flexible layout among the Android Layouts.
Take a look at this tutorial. It discussed how to position views in a RelativeLayout.
I hope you can get ideas from it in solving your problem.

LinearLayout in App Widget

I am working with modified version of sample WeatherListWidget to get a better understanding of App Widgets. Things are fine - except when I try to replace the dark_widget_item and light_widget_item layout files with slightly more complex layout files. Here is original layout:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget_item"
android:layout_width="match_parent"
android:layout_height="46dp"
android:paddingLeft="25dp"
android:gravity="center_vertical"
android:background="#drawable/item_bg_light"
android:textColor="#e5e5e1"
android:textSize="24sp" />
I would like to be able to have multiple text lines. But:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget_item"
android:background="#drawable/item_bg"
android:layout_width="match_parent"
android:layout_height="46dp"
android:paddingLeft="25dp">
<TextView android:id="#+id/type_string"
android:textColor="#666666"
android:textSize="20sp" />
<TextView android:id="#+id/title_string"
android:textColor="#666666"
android:textSize="18sp" />
</LinearLayout>
fails.
In fact, it results in "Sorry! The application Launcher (process com.android.launcher) has stopped unexpectedly. Please try again. Force close".
Reinstating TextView widget_item.xml fixes this. I suspect that part of the problem is how I reference RemoteViews in WeatherWidgetService.getViewAt() - but I am getting very little help from DDMS or LogCat or anything else.
Thanks guys, I got the notification. (SO requires a username with length > 2, hence the dot)
Answer as per comment:
I don't see any layout_width and layout_height attributes for both of the TextViews in your LinearLayout - they are mandatory. Also, if you want the two TextViews to be above eachother, add android:orientation="vertical" to the LinearLayout. And just to the record, you can break a CharSequence to multiple lines in a single TextView by adding "\n" inbetween the different elements.
If you're going to include an image as well, then you're probably better off with a LinearLayout than a single TextView indeed, although you could potentially use the intrinsic drawable option of the latter. That could get a little messy though, especially if you're planning on using different styles for the different lines of text... Not impossible, but I'd stick with the LinearLayout. ;)

How to wrap text in textview in Android

Does any one know how to wrap text in TextView in Android platform. i.e if the text in textview exceed the screen length it should be displayed in the second line.
I have searched and tried the following:
android:scrollHorizontally="false",
android:inputType="textMultiLine",
android:singleLine="false"
But none work..
Can anyone suggest how can I do it.
Constraint Layout
<TextView
android:id="#+id/some_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="#id/textview_above"
app:layout_constraintRight_toLeftOf="#id/button_to_right"/>
Ensure your layout width is zero
left / right constraints are defined
layout height of wrap_content allows expansion up/down.
Set android:maxLines="2" to prevent vertical expansion (2 is just an e.g.)
Ellipses are prob. a good idea with max lines android:ellipsize="end"
0dp width allows left/right constraints to determine how wide your widget is.
Setting left/right constraints sets the actual width of your widget, within which your text will wrap.
Constraint Layout docs
For me this issue only occurred on Android < 4.0
The combination of parameters I used were:
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
The maxLines count seemed to be the random final piece that made my TextView wrap.
For the case where the TextView is inside a TableLayout, the solution is to set android:shrinkColumns="1" on the TableLayout. (Replace 1 with the column number the TextView you want to wrap is in. (0-indexed))
AFAICT, no other attributes are needed on the TextView.
For other cases, see the other answers here.
FWIW, I had initially gotten it to sort of work with
<TextView
android:id="#+id/inventory_text"
android:layout_width="fill_parent"
android:layout_weight="1"
android:width="0dp"
but that resulted in some extra empty space at the bottom of the Dialog it was all in.
Use app:breakStrategy="simple" in AppCompatTextView, it will control over paragraph layout.
It has three constant values
balanced
high_quality
simple
Designing in your TextView xml
<android.support.v7.widget.AppCompatTextView
android:id="#+id/textquestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:scrollHorizontally="false"
android:text="Your Question Display Hear....Your Question Display Hear....Your Question Display Hear....Your Question Display Hear...."
android:textColor="#android:color/black"
android:textSize="20sp"
android:textStyle="bold"
app:breakStrategy="simple" />
If your current minimum api level is 23 or more then in Coding
yourtextview.setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE);
For more refrence refer this BreakStrategy
You must use 2 parameters :
android:ellipsize="none" : the text is not cut on textview width
android:scrollHorizontally="false" the text wraps on as many lines as necessary
This should fix your problem: android:layout_weight="1".
By setting android:maxEms to a given value together with android:layout_weight="1" will cause the TextView to wrap once it reaches the given length of the ems.
OK guys the truth is somewhere in the middle cause you have to see the issue from the parent's view and child's. The solution below works ONLY when spinner mode = dialog regardless of Android version (no problem there.. tested it in VD and DesireS with Android =>2.2) :
.Set you spinner's(the parent) mode like :
android:spinnerMode="dialog"
Set the textview's(child custom view) properties to :
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
I hope this works for you also.
In Android Studio 2.2.3 under the inputType property there is a property called textMultiLine. Selecting this option sorted out a similar problem for me. I hope that helps.
Just was working on a TextView inside a layout inside a RecyclerView. I had text getting cut off, ex, for Read this message, I saw: Read this. I tried setting android:maxLines="2" on the TextView, but nothing changed. However, android:lines="2" resulted in Read this on first line and message on the 2nd.
Try #Guykun's approach
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
Also, make sure that parents width is not set to wrap content. This is the thing that I was missing.
I had the same problem. Following change made it work -
android:layout_width="wrap_content"
The ellipsis, maxLines, or layout_weight - all didn't make any difference.
Note - The parent width is also set as wrap_content.
All you have to do is to set your textview width.
android:layout_width="60dp"
you can change the width to your choice. Just type long sentence to check if it working like this
android:text="i want to be among world class software engineer"
I am using Android 2.2 and my textview will automatically goto the next line if it exceeds the screen.
If you would like to have the text goto the next line before the end of the screen, just add in (just put in your own dp value). This will be useful if you have a picture on the right of the text.
android:layout_marginRight="52dp"
Strange enough - I created my TextView in Code and it wrapped - despite me not setting anything except standard stuff - but see for yourself:
LinearLayout.LayoutParams childParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
childParams.setMargins(5, 5, 5, 5);
Label label = new Label(this);
label.setText("This is a testing label This is a testing label This is a testing label This is a testing labelThis is a testing label This is a testing label");
label.setLayoutParams(childParams);
As you can see from the params definition I am using a LinearLayout. The class Label simply extends TextView - not doing anything there except setting the font size and the font color.
When running it in the emulator (API Level 9) it automatically wraps the text across 3 lines.
Just set layout_with to a definate size, when the text fills the maximum width it will overflow to the next line causing a wrap effect.
<TextView
android:id="#+id/segmentText"
android:layout_marginTop="10dp"
android:layout_below="#+id/segmentHeader"
android:text="You have the option to record in one go or segments(if you swap options
you will loose your current recordings)"
android:layout_width="300dp"
android:layout_height="wrap_content"/>
The trick is with the textView width, try to make it dedicated number like:
<TextView
android:layout_width="300dp"
android:layout_height="wrap_content"/>
I've tried many solutions without any result, I've tried:
android:ellipsize="none"
android:scrollHorizontally="false"
the only one thing triggred the wrap option is the dedicated width
You need to add your TextView in a ScrollView with something like this :
<ScrollView android:id="#+id/SCROLL_VIEW"
android:layout_height="150px"
android:layout_width="fill_parent">
<TextView
android:id="#+id/TEXT_VIEW"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header" />
</ScrollView>

Categories

Resources