Android get container width instead of display width
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
I want to display a text of variable length in a cardView
. To make the text fit, I split the text after every 500 Chars and add the substring to another relativeLayout
which is then added to a HorizontalScrollView
.
But I must define a length of the relativeLayout
, otherwise it is just a long one-liner in the ScrollView. Currently I do it with display.getWidth()
and then shrink it to 0.8.
The code works fine like this. But
firstly this method is depreciated and secondly is it a bit static I think.
What are better solutions?
The creation of the fragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// Inflate the layout for this fragment
View layoutView = inflater.inflate(R.layout.fragment_story,
container, false);
LinearLayout storyText = layoutView.findViewById(R.id.story_text_linear_layout);
//Here I get The width of the display:
android.view.Display display = ((android.view.WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
(int)(display.getWidth()*0.8),
ViewGroup.LayoutParams.WRAP_CONTENT
);
params.setMargins(0, 0, 10, 0);
String text = story.getStory();
while(text.length()>500)
TextView t = new TextView(storyText.getContext());
t.setText(text.substring(0,500));
text =text.substring(500);
RelativeLayout rl = new RelativeLayout(storyText.getContext());
rl.setLayoutParams(params);
rl.addView(t);
storyText.addView(rl);
TextView t = new TextView(storyText.getContext());
t.setText(text);
RelativeLayout rl = new RelativeLayout(storyText.getContext());
rl.setLayoutParams(params);
rl.addView(t);
storyText.addView(rl);
TextView author = layoutView.findViewById(R.id.author_text);
author.setText(story.getAuthor());
return layoutView;
and the xml if someone needs that to have a better understanding:
<FrameLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="xyz.myrating.stories.mystories.RecyclerList.StoryFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/LinearLayout_root_storyfrag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:src="@drawable/spacer" />
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/story_text_linear_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="horizontal" />
</HorizontalScrollView>
<TextView
android:id="@+id/author_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="Huckleberry Finn"
android:textAlignment="textEnd" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:src="@drawable/spacer" />
</LinearLayout>
</android.support.v7.widget.CardView>
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numStars="1"
android:paddingTop="20dp"
android:rating="1" />
</LinearLayout>
java android
add a comment |Â
up vote
2
down vote
favorite
I want to display a text of variable length in a cardView
. To make the text fit, I split the text after every 500 Chars and add the substring to another relativeLayout
which is then added to a HorizontalScrollView
.
But I must define a length of the relativeLayout
, otherwise it is just a long one-liner in the ScrollView. Currently I do it with display.getWidth()
and then shrink it to 0.8.
The code works fine like this. But
firstly this method is depreciated and secondly is it a bit static I think.
What are better solutions?
The creation of the fragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// Inflate the layout for this fragment
View layoutView = inflater.inflate(R.layout.fragment_story,
container, false);
LinearLayout storyText = layoutView.findViewById(R.id.story_text_linear_layout);
//Here I get The width of the display:
android.view.Display display = ((android.view.WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
(int)(display.getWidth()*0.8),
ViewGroup.LayoutParams.WRAP_CONTENT
);
params.setMargins(0, 0, 10, 0);
String text = story.getStory();
while(text.length()>500)
TextView t = new TextView(storyText.getContext());
t.setText(text.substring(0,500));
text =text.substring(500);
RelativeLayout rl = new RelativeLayout(storyText.getContext());
rl.setLayoutParams(params);
rl.addView(t);
storyText.addView(rl);
TextView t = new TextView(storyText.getContext());
t.setText(text);
RelativeLayout rl = new RelativeLayout(storyText.getContext());
rl.setLayoutParams(params);
rl.addView(t);
storyText.addView(rl);
TextView author = layoutView.findViewById(R.id.author_text);
author.setText(story.getAuthor());
return layoutView;
and the xml if someone needs that to have a better understanding:
<FrameLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="xyz.myrating.stories.mystories.RecyclerList.StoryFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/LinearLayout_root_storyfrag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:src="@drawable/spacer" />
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/story_text_linear_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="horizontal" />
</HorizontalScrollView>
<TextView
android:id="@+id/author_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="Huckleberry Finn"
android:textAlignment="textEnd" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:src="@drawable/spacer" />
</LinearLayout>
</android.support.v7.widget.CardView>
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numStars="1"
android:paddingTop="20dp"
android:rating="1" />
</LinearLayout>
java android
What exactly is the outcome you're trying to achieve ? What kind of scrolling you want ? If you have a long text and fix size card layout then it would be scroll
â Jabbar_Jigariyo
Jun 14 at 6:48
I want to display a text of variable length in a cardView. Scroll: HorizontalScrollView (Nothing more, Nothing less). Have you read the question?
â Emanuel Graf
Jun 14 at 16:40
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I want to display a text of variable length in a cardView
. To make the text fit, I split the text after every 500 Chars and add the substring to another relativeLayout
which is then added to a HorizontalScrollView
.
But I must define a length of the relativeLayout
, otherwise it is just a long one-liner in the ScrollView. Currently I do it with display.getWidth()
and then shrink it to 0.8.
The code works fine like this. But
firstly this method is depreciated and secondly is it a bit static I think.
What are better solutions?
The creation of the fragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// Inflate the layout for this fragment
View layoutView = inflater.inflate(R.layout.fragment_story,
container, false);
LinearLayout storyText = layoutView.findViewById(R.id.story_text_linear_layout);
//Here I get The width of the display:
android.view.Display display = ((android.view.WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
(int)(display.getWidth()*0.8),
ViewGroup.LayoutParams.WRAP_CONTENT
);
params.setMargins(0, 0, 10, 0);
String text = story.getStory();
while(text.length()>500)
TextView t = new TextView(storyText.getContext());
t.setText(text.substring(0,500));
text =text.substring(500);
RelativeLayout rl = new RelativeLayout(storyText.getContext());
rl.setLayoutParams(params);
rl.addView(t);
storyText.addView(rl);
TextView t = new TextView(storyText.getContext());
t.setText(text);
RelativeLayout rl = new RelativeLayout(storyText.getContext());
rl.setLayoutParams(params);
rl.addView(t);
storyText.addView(rl);
TextView author = layoutView.findViewById(R.id.author_text);
author.setText(story.getAuthor());
return layoutView;
and the xml if someone needs that to have a better understanding:
<FrameLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="xyz.myrating.stories.mystories.RecyclerList.StoryFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/LinearLayout_root_storyfrag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:src="@drawable/spacer" />
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/story_text_linear_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="horizontal" />
</HorizontalScrollView>
<TextView
android:id="@+id/author_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="Huckleberry Finn"
android:textAlignment="textEnd" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:src="@drawable/spacer" />
</LinearLayout>
</android.support.v7.widget.CardView>
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numStars="1"
android:paddingTop="20dp"
android:rating="1" />
</LinearLayout>
java android
I want to display a text of variable length in a cardView
. To make the text fit, I split the text after every 500 Chars and add the substring to another relativeLayout
which is then added to a HorizontalScrollView
.
But I must define a length of the relativeLayout
, otherwise it is just a long one-liner in the ScrollView. Currently I do it with display.getWidth()
and then shrink it to 0.8.
The code works fine like this. But
firstly this method is depreciated and secondly is it a bit static I think.
What are better solutions?
The creation of the fragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// Inflate the layout for this fragment
View layoutView = inflater.inflate(R.layout.fragment_story,
container, false);
LinearLayout storyText = layoutView.findViewById(R.id.story_text_linear_layout);
//Here I get The width of the display:
android.view.Display display = ((android.view.WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
(int)(display.getWidth()*0.8),
ViewGroup.LayoutParams.WRAP_CONTENT
);
params.setMargins(0, 0, 10, 0);
String text = story.getStory();
while(text.length()>500)
TextView t = new TextView(storyText.getContext());
t.setText(text.substring(0,500));
text =text.substring(500);
RelativeLayout rl = new RelativeLayout(storyText.getContext());
rl.setLayoutParams(params);
rl.addView(t);
storyText.addView(rl);
TextView t = new TextView(storyText.getContext());
t.setText(text);
RelativeLayout rl = new RelativeLayout(storyText.getContext());
rl.setLayoutParams(params);
rl.addView(t);
storyText.addView(rl);
TextView author = layoutView.findViewById(R.id.author_text);
author.setText(story.getAuthor());
return layoutView;
and the xml if someone needs that to have a better understanding:
<FrameLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="xyz.myrating.stories.mystories.RecyclerList.StoryFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/LinearLayout_root_storyfrag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:src="@drawable/spacer" />
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/story_text_linear_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="horizontal" />
</HorizontalScrollView>
<TextView
android:id="@+id/author_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="Huckleberry Finn"
android:textAlignment="textEnd" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:src="@drawable/spacer" />
</LinearLayout>
</android.support.v7.widget.CardView>
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numStars="1"
android:paddingTop="20dp"
android:rating="1" />
</LinearLayout>
java android
asked Apr 28 at 10:11
Emanuel Graf
1112
1112
What exactly is the outcome you're trying to achieve ? What kind of scrolling you want ? If you have a long text and fix size card layout then it would be scroll
â Jabbar_Jigariyo
Jun 14 at 6:48
I want to display a text of variable length in a cardView. Scroll: HorizontalScrollView (Nothing more, Nothing less). Have you read the question?
â Emanuel Graf
Jun 14 at 16:40
add a comment |Â
What exactly is the outcome you're trying to achieve ? What kind of scrolling you want ? If you have a long text and fix size card layout then it would be scroll
â Jabbar_Jigariyo
Jun 14 at 6:48
I want to display a text of variable length in a cardView. Scroll: HorizontalScrollView (Nothing more, Nothing less). Have you read the question?
â Emanuel Graf
Jun 14 at 16:40
What exactly is the outcome you're trying to achieve ? What kind of scrolling you want ? If you have a long text and fix size card layout then it would be scroll
â Jabbar_Jigariyo
Jun 14 at 6:48
What exactly is the outcome you're trying to achieve ? What kind of scrolling you want ? If you have a long text and fix size card layout then it would be scroll
â Jabbar_Jigariyo
Jun 14 at 6:48
I want to display a text of variable length in a cardView. Scroll: HorizontalScrollView (Nothing more, Nothing less). Have you read the question?
â Emanuel Graf
Jun 14 at 16:40
I want to display a text of variable length in a cardView. Scroll: HorizontalScrollView (Nothing more, Nothing less). Have you read the question?
â Emanuel Graf
Jun 14 at 16:40
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
Obtaining display width is wrong for this purpose because your application is not guaranteed to be ran fullscreen.
Any android.view.View
has getWidth()
and getHeight()
methods which return the actual width and height in the container. They will return 0
until the layout process finished, so you should obtain the values after layout has been finished. This is achieved as follows
storyFrag.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener()
@Override
public void onGlobalLayout()
storyFrag.getViewTreeObserver().removeOnGlobalLayoutListener(this)
handleYourDimensions(storyFrag.getWidth())
)
However, there is something conceptually wrong with what you are trying to achieve. You should not split your text manually, instead, set the required width of your TextView
so it cuts the text for you automatically.
Also, why do you need a HorizontalScrollView
for your text? Do you want the actual text container to be wider than a screen and be scrollable horizontally? This seems pretty inconvenient.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Obtaining display width is wrong for this purpose because your application is not guaranteed to be ran fullscreen.
Any android.view.View
has getWidth()
and getHeight()
methods which return the actual width and height in the container. They will return 0
until the layout process finished, so you should obtain the values after layout has been finished. This is achieved as follows
storyFrag.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener()
@Override
public void onGlobalLayout()
storyFrag.getViewTreeObserver().removeOnGlobalLayoutListener(this)
handleYourDimensions(storyFrag.getWidth())
)
However, there is something conceptually wrong with what you are trying to achieve. You should not split your text manually, instead, set the required width of your TextView
so it cuts the text for you automatically.
Also, why do you need a HorizontalScrollView
for your text? Do you want the actual text container to be wider than a screen and be scrollable horizontally? This seems pretty inconvenient.
add a comment |Â
up vote
2
down vote
Obtaining display width is wrong for this purpose because your application is not guaranteed to be ran fullscreen.
Any android.view.View
has getWidth()
and getHeight()
methods which return the actual width and height in the container. They will return 0
until the layout process finished, so you should obtain the values after layout has been finished. This is achieved as follows
storyFrag.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener()
@Override
public void onGlobalLayout()
storyFrag.getViewTreeObserver().removeOnGlobalLayoutListener(this)
handleYourDimensions(storyFrag.getWidth())
)
However, there is something conceptually wrong with what you are trying to achieve. You should not split your text manually, instead, set the required width of your TextView
so it cuts the text for you automatically.
Also, why do you need a HorizontalScrollView
for your text? Do you want the actual text container to be wider than a screen and be scrollable horizontally? This seems pretty inconvenient.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Obtaining display width is wrong for this purpose because your application is not guaranteed to be ran fullscreen.
Any android.view.View
has getWidth()
and getHeight()
methods which return the actual width and height in the container. They will return 0
until the layout process finished, so you should obtain the values after layout has been finished. This is achieved as follows
storyFrag.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener()
@Override
public void onGlobalLayout()
storyFrag.getViewTreeObserver().removeOnGlobalLayoutListener(this)
handleYourDimensions(storyFrag.getWidth())
)
However, there is something conceptually wrong with what you are trying to achieve. You should not split your text manually, instead, set the required width of your TextView
so it cuts the text for you automatically.
Also, why do you need a HorizontalScrollView
for your text? Do you want the actual text container to be wider than a screen and be scrollable horizontally? This seems pretty inconvenient.
Obtaining display width is wrong for this purpose because your application is not guaranteed to be ran fullscreen.
Any android.view.View
has getWidth()
and getHeight()
methods which return the actual width and height in the container. They will return 0
until the layout process finished, so you should obtain the values after layout has been finished. This is achieved as follows
storyFrag.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener()
@Override
public void onGlobalLayout()
storyFrag.getViewTreeObserver().removeOnGlobalLayoutListener(this)
handleYourDimensions(storyFrag.getWidth())
)
However, there is something conceptually wrong with what you are trying to achieve. You should not split your text manually, instead, set the required width of your TextView
so it cuts the text for you automatically.
Also, why do you need a HorizontalScrollView
for your text? Do you want the actual text container to be wider than a screen and be scrollable horizontally? This seems pretty inconvenient.
answered Jun 14 at 20:14
Yaroslav Mytkalyk
1213
1213
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f193138%2fandroid-get-container-width-instead-of-display-width%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
What exactly is the outcome you're trying to achieve ? What kind of scrolling you want ? If you have a long text and fix size card layout then it would be scroll
â Jabbar_Jigariyo
Jun 14 at 6:48
I want to display a text of variable length in a cardView. Scroll: HorizontalScrollView (Nothing more, Nothing less). Have you read the question?
â Emanuel Graf
Jun 14 at 16:40