set TextView in HorizontalScrollView to the right - android

I have a simple construction; a TextView in a Scrollview, but I cannot make it work as I want.
The problems I get now;
The ScrollView keeps expanded when once the TextView was larger than the width of the screen, even when I set the text of TextView to nothing.
The TextView is on the left side.
I got the following:
<HorizontalScrollView
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:layout_gravity="right"
android:background="#drawable/background_scrollviews_display"
android:id="#+id/scrollViewSum">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right">
<TextView
style="#style/TextStyleDisplay"
android:textSize="#dimen/fontSizeTextViewSum"
android:paddingLeft="#dimen/paddingDisplayTextViews"
android:paddingRight="#dimen/paddingDisplayTextViews"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:gravity="right|center"
android:id="#+id/textViewSum"/>
</LinearLayout>
</HorizontalScrollView>
What I want;
The TextView on the right;
The ScrollView only expand when the TextView is larger than width of the screen;
I tried a lot, but I simply cannot find the right solution.
I hope someone can help me out!

I have a two clue to solve your problems:
1) make LinearLayout orientation Vertical, then you can position TextView on the right
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right">
<TextView
style="#style/TextStyleDisplay"
android:textSize="#dimen/fontSizeTextViewSum"
android:paddingLeft="#dimen/paddingDisplayTextViews"
android:paddingRight="#dimen/paddingDisplayTextViews"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:gravity="right|center"
android:id="#+id/textViewSum"/>
</LinearLayout>
2) make TextView wrap_content instead of match_parent
<TextView
style="#style/TextStyleDisplay"
android:textSize="#dimen/fontSizeTextViewSum"
android:paddingLeft="#dimen/paddingDisplayTextViews"
android:paddingRight="#dimen/paddingDisplayTextViews"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:gravity="right|center"
android:id="#+id/textViewSum"/>

make the textview width wrap_content

I have hard coded stuff like padding and text size etc. as I did't have your dimensions, you can change it later as per your requirements.
check if following code snipet helps.
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollViewSum"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/textViewSum"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right|center"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="ldksjfkdlfkfldksjfkdlfkf"
android:textSize="20sp" />
</LinearLayout>
</HorizontalScrollView>

Related

Horizontal Centering TextView in RelativeLayout doesn't work

I can't place my textview in RelativeLayout in the center. I added this parameters
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
but it doesn't help.
My Layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/colorBackground"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="ru.myseolife.a99torrentsalpha2.MainActivity"
android:gravity="center"
tools:showIn="#layout/app_bar_main"
>
<TextView
android:id="#+id/nothing_found"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:text="#string/nothingfound"
/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<LinearLayout
android:id="#+id/linlaHeaderProgress"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone"
>
<ProgressBar
android:id="#+id/progressBar"
style="#style/Widget.AppCompat.ProgressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</RelativeLayout>
Anybody know what can it be? TextView is centering vertically, but horizontally it appears in the left side.
In your TextView layout_width is match_parent If you want to keep that and show it as centered you need to center the text inside the view not the TextView
So use android:gravity="center" inside your TextView or you can make TextView's width wrap_content and keep the way as it is.
Since it's a simple answer i'll tell you a trick > go to
settings > developer options > and tick Show layout boundaries then you can check the view bounds and get the idea where actually your view boundaries are !
the width of TextView should be "wrap_content"
Add
android:gravity="center"
to your textview, this will center the text since you are setting the width as match parent.
Add this:
android:gravity="center_horizontal"

My rotated TextView is cut off. What i have to do?

This is my layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<!--left-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:background="#color/nero"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Nothing important"
android:textSize="40dp"/>
</LinearLayout>
<!--right yellow column-->
<LinearLayout
android:id="#+id/layout_barra_nord"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="6"
android:background="#color/giallo"
android:orientation="vertical">
<TextView
android:paddingTop="20dp"
android:background="#android:color/background_light"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rotation="90"
android:singleLine="true"
android:gravity="center"
android:layout_gravity="bottom"
android:textColor="#color/nero"
android:textSize="30sp"
android:textStyle="bold"
android:text="try"
/>
<!--android:text="long text"-->
</LinearLayout>
</LinearLayout>
</LinearLayout>
i'd like to have this effect for every text:
but if I try to add a longer text, my text will be cut off:
I see that lot of people have this problem, but I haven't found anything helpful.
How to fix this problem?
Your TextView width is match_parent which means your TextView will have the same width as your parent LinearLayout. Your LinearLayout have a narrow width so your TextView will have a narrow width too.
EDIT: You have to think of your TextView as if he is on the same orientation as your parent. If he had the same orientation you would have the TextView with a narrow width.
Change the following attribute of TextView
android:layout_height="wrap_content"
to
android:layout_height="match_parent"
so that it stretch down to the full parent height.

Android: Layout looks different on different devices

Hi I have an activity with an imageview and a row with buttons at the bottom.The buttons are added to the iconView layout programmatically.When I test my app on my HTC one the buttons are displayed correct.
Once I run it on a device with a smaller screen it is cropped i.e. only the top of the buttons is displayed.
How can I make my code device independent?
<?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:background="#000000"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/mainView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background2"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.95"
android:orientation="vertical"
android:paddingLeft="4dp"
android:paddingTop="4dp"
android:paddingRight="4dp"
android:paddingBottom="4dp"
android:src="#android:drawable/ic_menu_camera" />
<LinearLayout
android:id="#+id/iconView"
android:layout_weight="0.05"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
</LinearLayout>
Thanks in advance.
Try this layout:
<?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:background="#000000" >
<RelativeLayout
android:id="#+id/mainView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background2" >
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp"
android:layout_above="#+id/iconView"
android:src="#android:drawable/ic_menu_camera" />
<LinearLayout
android:id="#+id/iconView"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal" />
</RelativeLayout>
</LinearLayout>
Try this
make the first view with android:layout_weight="1"
and the with a static height without the layout_weight, this should solve it
Unfortunately the solutions did not work. Eventually I changed my code and add the buttons in the xml, not programmatically. This works fine. I don't know why...
When adding button either programmatically or in XML layout, try to use LinearLayout as the parent for button and leverage the "weightsum" property of that LinearLayout.
When you're going to share the real state of screen's width or height equally between a number of views such as buttons or textview or ..., you can set "layout_weight" property on those controls. Notice that, this is only possible when using LinearLayouts.
What happens at the end is that the linearlayout's real estate is calculated and allocated according to each child's element's "layout_weight" value.
The example below shows how to put two buttons beside each other and make them fill the screen's width equally :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
>
<Button
android:id="#+id/btn_left"
android:layout_width="0dp" // width is calculated by weight
android:layout_height= "wrap_content"
android:layout_weight="1" />
<Button
android:id="#+id/btn_right"
android:layout_width="0dp" // width is calculated by weight
android:layout_height= "wrap_content"
android:layout_weight="1" />
<LinearLayout/>

Android textview troubles in one page

Hy everyone,
i have got a lot of text to diplay in one page, but i saw only first textview, other disapiard. Where is other textrview?
Code, i dont know why i see only first texttt and a lot offf blank page. And that isall:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="texttttttttt"
android:id="#+id/textView" style="#style/MetricPrefixTitle"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="texttttttttt"
android:id="#+id/textView1" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Valute"
android:id="#+id/textView" style="#style/MetricPrefixHeader"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="textttttttttttttt"
android:id="#+id/textView1" android:layout_gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="text"
android:id="#+id/textView" style="#style/MetricPrefixHeader"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="textttttttttttttttttt"
android:id="#+id/textView1" android:layout_gravity="center"/>
</LinearLayout>
</ScrollView>
You didn't set android:orientation attr in LinearLayout, the default one is horizontally so your textViews are positioned next to each other, if you scroll your text view from left to right you will see the other ones. What you want is to set android:orientation to vertical.
Besides the orientation, your layout_height-"match_parent" will cause your view to expand to the size the parent allows. You should use layout_height="wrap_content" when you want the view height to be the height of the contents.

no gravity for scrollview. how to make content inside scrollview as center

I want the content inside the scrollView as center.
<ScrollView
android:id="#+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="12dp"
android:paddingBottom="20dp"
android:scrollbarStyle="outsideOverlay"
android:layout_gravity="center" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check"
android:gravity="center_vertical|center_horizontal"/>
</ScrollView>
Note: there is no android:gravity attribute for scrollvew.
any sol:-
I had the same issue and finally figured it out. This is for a vertical ScrollView.
Put your ScrollView inside a RelativeLayout and center it in the RelativeLayout. In order for this to work, your ScrollView should have
android:layout_height="wrap_content"
This is how the final code should look like:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="b1"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="b2"/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="b3"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
How about this?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="12dp"
android:paddingBottom="20dp"
android:scrollbarStyle="outsideOverlay" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check"
android:gravity="center_vertical|center_horizontal"/>
</LinearLayout>
</ScrollView>
I think a more elegant solution would be to use the ScrollView's android:fillViewport property. A ScrollView is a little different in how it treats it's content view (can only have one), even if you set match_parent (fill_parent) to the ScrollView it won't give that much spacing to it's content view, instead the default behavior is for the ScrollView to wrap the content regardless of what you specify for that view. What android:fillViewport does is tell the ScrollViewto stretch its content to fill the viewport (http://developer.android.com/reference/android/widget/ScrollView.html#attr_android:fillViewport). So in this case, your LinearLayout would be stretched to match the viewport and if the height goes behind the viewport then it will be scrollable which is exactly what you want!
The accepted answer won't work properly when the content extends beyond the ScrollView because it will still center the content view first causing it to cut off a portion of the view, and the ScrollView centered in another layout works but just doesn't feel right, besides I think it will also result in a lint error (useless parent or something along those lines).
Try something like this:
<ScrollView
android:id="#+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="12dp"
android:paddingBottom="20dp"
android:scrollbarStyle="outsideOverlay"
android:fillViewport="true">
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check" />
</LinearLayout>
</ScrollView>
Just remember that the reason it is being centered here now is because of the android:gravity on the LinearLayout since the ScrollView will stretch the LinearLayout so keep that in mind depending on what you add to the layout.
Another good read on ScrollView although not about centering but about fillViewport is http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/
Use this attribute in ScrollView
android:fillViewport="true"
Example
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarStyle="outsideOverlay"
android:fillViewport="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--Your Code goes here-->
</RelativeLayout>
</ScrollView>
Make sure to use Single Child in ScrollView just like in above example which is Relativelayout.
For #Patrick_Boss - what stopped the content from cutting of in my application was to change the gravity and layout_gravity to center_horizontal for the LinearLayout.
It worked for me...but not sure if it will work for you.
Modification of #Ghost's answer -
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="12dp"
android:paddingBottom="20dp"
android:scrollbarStyle="outsideOverlay" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check"
android:gravity="center_vertical|center_horizontal"/>
</LinearLayout>
</ScrollView>
One thing to consider is what NOT to set. Make certain your child controls, especially EditText controls, do not have the RequestFocus property set.
This may be one of the last interpreted properties on the layout and it will override gravity settings on its parents (layout or ScrollView).
using the scroll view inside the ConstraintLayout. It is worked for me
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Scroll view height should be wrap_content
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"

Categories

Resources