Want to mix layout_centerHorizontal with layout_toLeftOf - android-layout

I am struggling with a specific layout requirement.
I want a TextView that it centred. It is single-line and uses ellipsis if the text is too long. On the same line, to the right of it, I want a small ImageView. The presence of the ImageView must not affect what the TextView thinks is the horizontal centre of the layout. So, although the text might be centred, there is actually less space available to the right of the centre line than to its left.
Within a RelativeLayout, the sort-of intuitive thing to use is:
<ImageView
android:id="#+id/image_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
<TextView
android:id="#+id/text_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/image_view_id"
android:layout_centerHorizontal="true"
android:singleLine="true"
android:ellipsize="middle" />
but this does not work because you cannot have both android:layout_toLeftOf and android:layout_centerHorizontal. It behaves as if the android:layout_centerHorizontal was not there.
I have also tried using a LinearLayout with android:layout_gravity without success. Is it possible?

It turns out that something close to what I want to achieve is very simple:
<ImageView
android:id="#+id/image_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
<TextView
android:id="#+id/text_view_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/image_view_id"
android:layout_marginLeft="#dimen/size_of_image_view"
android:singleLine="true"
android:gravity="center"
android:ellipsize="middle" />
Using android:gravity="center" with android:layout_width="match_parent" centres the text in the available space. It is not perfect as the marginLeft is there to keep the text centred within the full layout, thus robbing a little space from its left end but it is good enough for my purposes.

Related

When I switch to RelativeLayout, everything I put in the designs shows up in the top left corner of screen

I switched from ConstraintLayout to RelativeLayout in Android studio, now everything I put in the design shows up in the top left for some reason.
I can drag and re-position it in Design, but writing padding is very time consuming and tiresome. I even changed the default to RelativeLayout but nothing changes.
<?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">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
I want to re-position them by dragging them on screen without them being stucked in the top left corner of the screen.
Yes, this is the property of RelativeLayout.
RelativeLayout needs you to tell the position relationship between widgets. If you don't specify the position relationship for all widgets, then all widgets will collapse to the (0, 0) of parent, which results in what you see,
everything I put in the design shows up in the top left.
To specify the relationship between widgets, you may use android:layout_* series property, for example:
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" /> <!-- this will at top left -->
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/textView"
android:text="Button" /> <!-- this will at right of textView -->
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toBottomOf="#id/button"
android:text="TextView" /> <!-- this will below button -->
You may try to specify them in Design Layout, however I found it is a little difficult.
Click a widget, then there will be circles around this widget.
Click on one circle, hold on. All widgets will have circles around.
Drag this circle to the circles of one other widget, this will make a connection between them, if you are using ConstraintLayout.
However, it seems impossible to do so if you are using RelativeLayout, since all widgets are collapsed to the same position.
See Android Guide to RelativeLayout for more details.

ScrollView and text

How can I make my screen scroll?
How to use ScrollView? At my activity, in it there is a picture and the text, it is necessary for me that at movement downwards or upwards the screen scroll.
The ScrollView XML resource is fairly easy to use and powerful. Just wrap whatever content you want to be scrollable in the XML with the ScrollView tags and the rest will be taken care of for you. Simple example:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
android:background="#color/colorPrimary"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/yourID"
android:layout_weight="1" />
...
</LinearLayout></ScrollView>
This example shows a LinearLayout filled with whatever you need (TextView in this case) that will scroll on screen when it has to.

Making two textviews with different font sizes have independent lines

I just started android app programming, so this might be a very dumb question, but I can't find the answer anywhere.
I want to have two textviews next to each other, where one shows three lines in a small font and the other contains a number in a very big font (which is as big as the three lines of the other textview). What now happens is that the first line of the textview with the smaller font sizes is aligned to the line of the other textview on the base line. So, I get some whitespace above the first line, because the font size is much smaller, and the three lines don't fit because so much space is wasted.
My code looks like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/historytextviewId"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/countertextViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="60sp" />
</LinearLayout>
What I want is that the left textview just acts as if the other textview had the same font size.
EDIT: For some reason, replacing android:layout_height="wrap_content" by android:layout_height="fill_parent" in the first TextView worked for me (which I tried because I found some almost completely unrelated question where that was the answer), but I still don't get why that works.

EditText in Layout not full width

I have problem with xml. Why my EditText doesn't show for full screen width? It showing about 80% of screen width, but not all..
<ScrollView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#color/colorGray"
android:layout_weight=".35" >
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="horizontal|vertical" >
<LinearLayout
android:id="#+id/rightCont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="test" />
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
Because the EditText is wrapped in a ScrollView wich takes 0.35 weight of the screen. So it will expand until it reaches the boundaries of its parent ViewGroup.
To test this, change the weight of ScrollView to 1:
android:layout_weight="1"
beside what Andy Res said, a scrollview in a scrollview is not recommended. newer android versions can handle that but older one will have trouble determining which scrollview should scroll on touch events.
Also a LinearLayout with wrap_content around the EditText with fill_parent should result in a pure wrap_content behavior of the EditText, too.
You should rethink your layout.

button not exactly centered in it's parent

I am surprised, i did not notice this during my 6 months of development time on Android. I have a realtivelayout whose bottom is aligned to the bottom of the screen/parent. I wanted to place a button at the center of this relativelayout. I notice that the button is not exactly centered vertically. There appears to be couple of pixels between the bottom of the button and that of the relativeLayout. Here's the piece of relevant layout code:
<RelativeLayout
android:id="#+id/some_layout"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center"
android:layout_alignParentBottom="true"
android:background="#888">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ping">
</Button>
</RelativeLayout>
Any ideas? My targetSDK is 2.2 and above. Thanks in advance.

Resources