Android textview troubles in one page - android

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.

Related

Adding two linear layout in scrollview the layout either shrink or compress. Here i am using layout_weight in them that's why the problem occurs

when i used the single linear layout in scrollview then the output and code is like that
Xml code:
<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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10">
<ImageView
android:id="#+id/disp_img"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:background="#color/grey"
android:scaleType="fitXY" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="10dp"
android:layout_weight="5"
android:orientation="vertical">
<TextView
android:id="#+id/disp_title"
style="#style/disTitleTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView
android:id="#+id/disp_cont"
style="#style/disdiscTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"></TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#drawable/disptextdes"
android:orientation="vertical"
>
<TextView
android:id="#+id/disp_readmore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="15dp"
android:text="To read more click on this"
android:textColor="#android:color/black"
android:textSize="15sp"></TextView>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
output is:
But when i add another layout then code and output is look like
<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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
//first linear layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10">
<ImageView
android:id="#+id/disp_img"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:background="#color/grey"
android:scaleType="fitXY" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="10dp"
android:layout_weight="5"
android:orientation="vertical">
<TextView
android:id="#+id/disp_title"
style="#style/disTitleTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView
android:id="#+id/disp_cont"
style="#style/disdiscTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"></TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#drawable/disptextdes"
android:orientation="vertical"
>
<TextView
android:id="#+id/disp_readmore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="15dp"
android:text="To read more click on this"
android:textColor="#android:color/black"
android:textSize="15sp"></TextView>
</LinearLayout>
// when i add second linear layout then the first linear layout is varied,I don't know why
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/citispotterlogo"></ImageView>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
output is :
When i added the image in linear layout the first linear layout shrink I don't know why?
Please help me to solve this,I try to search this on google in stackoverflow but i don't find nothing about this. Can anybody solve this or give me some another approach for solving this problem.
So my final output is look like this
I have added two images which are in scrollview
(https://i.stack.imgur.com/DpJ9C.png)
(https://i.stack.imgur.com/QhQoL.png)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10">
This LinearLayout has height which is match_parent, you should have used wrap_content or use a fixed height so that the second layout must be visible
This layout uses a weightSum in the first linear layout when you add the last LinearLayout you do not include a weight for it. This is causing the UI to render incorrectly.
I've included a couple of option solution for you both should do the job.
Option 1
change the weighted sum in the first LinearLayout to 11 and then add a weight value of 1 to the last LinearLayout.
Option 2
Add a weighted value to the last LinearLayout but you will need to adjust the rest of the weighted values so it equals weight sum in the first LinearLayout (10).
LayoutWeight Review
LinearLayout supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view. Child views can specify a weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. The default weight is zero.

set TextView in HorizontalScrollView to the right

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>

Horizontal scroll view expanding when views added programmatically

I'm having problems when adding views programmatically to a horizontal scroll view. I have a horizontal scroll view with a dotted background and have given the linear layouts around it clear colour backgrounds to aid with understanding.
This is what the view looks like prior
Then I add a view to the scroll view after a drag and drop event:
MemoNodeView mn = new MemoNodeView(this);
mn.setMemo(m);
int time = (int) Math.round(pixelsPerMilliSecond()*m.getTimeLength());
mn.setMemoSize(time, timelineHeight);
timeline.addView(mn, new ViewGroup.MarginLayoutParams(time, timelineHeight));
This leads to the view expanding in size vertically:
I attempted to fix this by re-setting the layout params of the linear layout inside the scroll view based on the height it was supposed to have:
ViewGroup.LayoutParams p = timeline.getLayoutParams();
p.height = timelineHeight;
timeline.setLayoutParams(p);
Leading to this situation where the scroll view is still expanded but the content is the right size:
I then tried to resize the scroll view itself so that the size matched what it was supposed to:
ViewGroup.LayoutParams p2 = timelineScrollView.getLayoutParams();
p2.height = timelineHeight;
timelineScrollView.setLayoutParams(p2);
which gave:
Leaving the size of the scroll view correct, but the size of the other views shrunken and slightly in the wrong position. What I want is for the scroll view to remain the same as in the first image and for nothing to move around. It may be something basic, but I cant for the life of me figure out what I'm doing wrong to give this behaviour.
The xml for the area is as follows (note that the colours/ text views are placeholders used to try and help make the issue stand out visually):
`<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/black_frame"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Small Text" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="3"
android:orientation="vertical"
android:paddingLeft="#dimen/toggle_draw_padding"
android:paddingRight="#dimen/toggle_draw_padding" >
<HorizontalScrollView
android:id="#+id/horizontal_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/timeline_dots_bitmap"
android:fillViewport="true">
<LinearLayout
android:id="#+id/timeline_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#F0AA4444" >
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Small Text" />
</LinearLayout>
</LinearLayout>`
Any ideas as to what I am doing wrong here?
I found out what was causing the issue. I'll post here in case anyone else has a similar issue and it is also driving them mental...
The linear layouts that had the white backgrounds had the layout_height="match_parent" which gave the weird behaviour when other views were added. Changing the two layouts to layout_height="0dip" solves the issue. As shown below:
`
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#ffffff" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Small Text" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="3"
android:orientation="vertical"
android:paddingLeft="#dimen/toggle_draw_padding"
android:paddingRight="#dimen/toggle_draw_padding"
>
<HorizontalScrollView
android:id="#+id/horizontal_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/timeline_dots_bitmap"
android:fillViewport="true"
android:layout_margin="0dip"
android:padding="0dip">
<LinearLayout
android:id="#+id/timeline_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#F0AA4444"
android:layout_margin="0dip"
android:padding="0dip">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#ffffff" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Small Text" />
</LinearLayout>
</LinearLayout>`
I hope that this can help anyone who somehow makes the same mistake I did.

How to make View fill remaining space

So I have a two "sections" to my screen. I have the top section which is a ScrollView and a bottom section which is random text. The random text at the bottom will always be changing sizes, so sometimes it can take 40dp, sometimes 20dp, etc.
My question is, is there a way to make the bottom part dynamic (without loss of text) and make the top scrollview adapt to the new size and restricts its own size to "fill the remaining space" based on what portion the bottom part is using?
Like this:
As you can see, the scroll view just fills the remaining space available.
I'm searching for a solution using XML only
Need HELP!
You can try this.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottomTextView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#android:color/holo_red_dark"
android:text="Test" />
</LinearLayout>
</ScrollView>
<TextView
android:id="#+id/bottomTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#android:color/darker_gray"/>
Or
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#android:color/holo_red_dark"
android:text="Test" />
</LinearLayout>
</ScrollView>
<TextView
android:id="#+id/bottomTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"/>
Wrap both Views in a vertical LinearLayout;
The view which is at the top: height = "0dp", weight = 1
And the view which is in the bottom: height = "wrap_content"

How can I get a ScrollView with centered content and a footer?

i am trying to achieve the following in one of my layouts for my android app:
I would like to have a layout that holds the main content (linearlayout with other content) and a footer (linear layout with other content). The main content should be centered both horizontally and vertically. The footer should just be centered horizontally and always stay at the bottom.
In addition to that, the main content and the footer should be wrapped in a scrollview because it is possible that the main content grows and all the content should stil be accessible.
How can I do that?
I think the following gets close to what you want with the assumption that when you wrote 'The footer should just be centered horizontally and always stay at the bottom.' you meant the bottom of the screen.
<?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"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:layout_above="#+id/footer" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:minLines="0"
android:visibility="invisible"/>
<TextView
android:id="#+id/body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My main body text to display"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:minLines="0"
android:visibility="invisible"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
<LinearLayout
android:id="#+id/footer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My footer text to display"/>
</LinearLayout>
Assuming this isn't quite right it should hopefully at least give you some ideas.

Categories

Resources