I have my android rating bar with the following code:
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="22dp"
android:layout_marginTop="28dp"
android:stepSize="1.0" />
I want to initialize the first star value to -5, so that the remaining stars will get the values like -4,-3,-2...
But I don't know how to give that initial value to 1st star of my rating bar in android.
And I want my rating bar with three colors:
for the values -5,-4,-3,-2,-1 the star color should be RED,
for the value 0 star color should be BLUE
and for the values 1,2,3,4,5 the star color should be GREEN.
The minimum rating can be 0, negative numbers are not allowed.
However, you can create a rating bar with 11 stars to represent the values (-5 to +5)
In the listener of the rating bar, map the value to the range of -5 to +5 (by subtracting 6 from the parameter received) dynamically change the color as follows:
0 : Blue
Negative value : Red
Positive value : Green
So, the output will look like this:
Activity:
import android.app.Activity;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private RatingBar ratingBar;
private TextView tvRating;
private LayerDrawable stars;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.original_activity_main);
ratingBar = (RatingBar) findViewById(R.id.ratingBar);
tvRating = (TextView) findViewById(R.id.value);
stars = (LayerDrawable) ratingBar.getProgressDrawable();
ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float ratingValue,
boolean fromUser) {
int value = (int) (ratingValue) - 6;
tvRating.setText(String.valueOf(value));
int color = Color.BLUE;
if(value > 0)
color = Color.GREEN;
else if(value < 0)
color = Color.RED;
stars.getDrawable(2).setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
}
});
}
}
XML:
<?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" >
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result : " />
<TextView
android:id="#+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/label"
android:text="" />
<RatingBar
android:id="#+id/ratingBar"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/label"
android:isIndicator="false"
android:numStars="11"
android:rating="0.0"
android:stepSize="1.0" />
</RelativeLayout>
As far as I know, the minimum value you can set the RatingBar is 0 (no negative numbers).
If you set the stepSize to 1.0, then you would have to set the ratingbar using an if condition.
For example:
if(yourNumber ==(-5)){
ratingBar.setRating(1.0f); //you need to set it using a Float value
} else if (yourNumber ==(-4)){
ratingBar.setRating(2.0f);
} //And so on....
About changing the star colors - you need to define your own Ratingbar style - read this post.
Good luck!
Related
I have rating bar widget in my layout and set custom style in layout.
I want to set more that 10 stars in my rating bar, i want to display rating bar stars in two line, as in single line it cut off.
Here is my layout.
<RatingBar
style="#style/starRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="10"
android:stepSize="1" />
Any help is appreciated.
As far as I know, there is no way to linebreak a single RatingBar, so here is what I suggest:
Make TWO RatingBars and set their OnRatingBarChangeListeners to interact with one another.
I wrote up a quick example to show you exactly what I mean:
activity_my.xml
This would be your layout file. I used the default name when creating a new project in AS.
Use RelativeLayout.
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MyActivity">
<RatingBar
android:id="#+id/bar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="10"
android:stepSize="1" />
<RatingBar
android:id="#+id/bar2"
android:layout_below="#id/bar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="4"
android:stepSize="1" />
</RelativeLayout>
MyActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RatingBar;
public class MyActivity extends Activity {
private RatingBar ratingBar1;
private RatingBar ratingBar2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
setOnChangeListeners();
}
public void setOnChangeListeners(){
ratingBar1 = (RatingBar) findViewById(R.id.bar1);
ratingBar2 = (RatingBar) findViewById(R.id.bar2);
ratingBar1.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
ratingBar2.setRating(0);
ratingBar1.setRating(v);
}
});
ratingBar2.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
ratingBar1.setRating(10);
ratingBar2.setRating(v);
}
});
}
}
As you can see: we use a helper function: setOnChangeListeners() to initialize ratingBar1 and ratingBar2. After that we set onRatingChanged for both bars. For the first line bar made of 10 stars, we make sure ratingBar2 is set to 0. For the second bar, if it is changed, ratingBar1 must be set to 10 so it will be full.
Try it out! Hope it works for you!
Rating bar can't break in two line so add rating bar layout dynamically
int noOfStarts = 8;
int noOfRatingLine = (noOfStarts/5)+1;
LinearLayout[] linear = new LinearLayout[noOfRatingLine];
RatingBar[] ratingBarNames = new RatingBar[noOfRatingLine];
for(int i =0;i<noOfRatingLine;i++)
{
linear[i] = (LinearLayout)this.getLayoutInflater().inflate(R.layout.app_ratingbar, null);
ratingBarNames[i] = (RatingBar)linear[i].findViewById(R.id.rating);
if(noOfStarts<5){
ratingBarNames[i].setNumStars(noOfStarts);
}
else{
ratingBarNames[i].setNumStars(5);
}
ratingBarNames[i].setStepSize(1);
noOfStarts = noOfStarts-5;
ratingbarLayout.addView(linear[i]);
}
XML layout app_ratingbar.xml for inflate
<RatingBar
android:id="#+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</RatingBar>
I'm newbie in android development and going to create simple drag'n'Drop calculator.
My goal is to make buttons from 0 to 9 and create numbers by clicking them.
For example:
0 1 2 3 4 5 6 7 8 9
and by clicking them create number 245, then drag this number, for example, into the sum mark +, then repeat second number and automatically retrieve results.
First, I'll show my code now...
MainActivity.java
package com.coreprojects.calculator;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity {
TextView calc_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// This method comes from XML button = buttonClick
public void buttonClick(View v) {
// Textarea where numbers will be shown
TextView TV = (TextView) findViewById(R.id.calc_cache_1);
// Get the ID of buttons
Button calc_btn_1 = (Button) findViewById(R.id.button1);
Button calc_btn_2 = (Button) findViewById(R.id.button2);
// INT type numbers convert to string, because of [setText]
String buttonText_1 = calc_btn_1.getText().toString();
String buttonText_2 = calc_btn_2.getText().toString();
// Result as text
TV.setText(buttonText_1); // first number is not shown
TV.setText(buttonText_2); // second number
// alert
/**
String pressed = "Operation Done";
new AlertDialog.Builder(this).setTitle("result").setMessage(pressed)
.setNeutralButton("Done", null).show();
*/
}
}
activity_main.xml
<RelativeLayout 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"
tools:context=".MainActivity" >
<TextView
android:id="#+id/calc_cache_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/main_text" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:clickable="true"
android:onClick="buttonClick"
android:text="#string/button_1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="64dp"
android:clickable="true"
android:onClick="buttonClick"
android:text="#string/button_2" />
</RelativeLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Calculator</string>
<string name="main_text"></string>
<string name="menu_settings">Settings</string>
<string name="button_1">1</string>
<string name="button_2">2</string>
</resources>
Now problem...
When i click on buttons, any time result shows number 2, because i think
TV.setText(buttonText_2) is second command and first one is not read any more.
I tried to write together, like TV.setText(buttonText_1 + buttonText_2), but now it results 12, even if i click any single button...
so how can I append numbers by clicking them? and also, if u see here, i have clickable button in XML, and get ID of pressed button, but i want it to be more dynamic.
For example: when i press button, don't get the ID of it, but simply identify the button ID and get value. i can show u simple analog in jQuery:
$(".button_class_not_ID").click(
var elementValue = $(this).html();
or
var elementValue = $(this).attr("id");
console.log(elementValue);
);
I have made an Android Rating Bar program to accept user rating, calculate the total ratings and display it in indicator rating bar.
Now i want to add two more functionalities in my Rating Bar program:
I want to use my existing Rating Bar in ListView
Secondly want to show high to low rated rating items in another activity like MyRatings
My Java Code:-
public class InteractiveRatingBarActivity extends Activity implements
OnRatingBarChangeListener {
RatingBar getRatingBar;
RatingBar setRatingBar;
TextView countText;
int count;
float curRate;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewsById();
setRatingBar.setRating(curRate);
getRatingBar.setOnRatingBarChangeListener(this);
}
private void findViewsById() {
getRatingBar = (RatingBar) findViewById(R.id.getRating);
setRatingBar = (RatingBar) findViewById(R.id.setRating);
countText = (TextView) findViewById(R.id.countText);
}
public void onRatingChanged(RatingBar rateBar, float rating,
boolean fromUser) {
DecimalFormat decimalFormat = new DecimalFormat("#.#");
curRate = Float.valueOf(decimalFormat.format((curRate * count + rating)
/ ++count));
Toast.makeText(InteractiveRatingBarActivity.this,
"New Rating: " + curRate, Toast.LENGTH_SHORT).show();
setRatingBar.setRating(curRate);
countText.setText(count + " Ratings");
}
}
main.xml:
<?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"
android:orientation="vertical" >
<RatingBar
android:id="#+id/getRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0" />
<RatingBar
android:id="#+id/setRating"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/getRating"
android:isIndicator="true"
android:numStars="5"
android:stepSize="0.1" />
<TextView
android:id="#+id/countText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/getRating"
android:layout_toRightOf="#+id/setRating"
android:paddingLeft="10dp" />
</RelativeLayout>
I want a Button like this:
+-----------------------+
| |
| +-----+ |
| |Image| |
| +-----+ |
| Text |
| |
| |
+-----------------------+
EDIT: Explanation to the picture: I want the COMBINATION of Image and text centered (text ALWAYS below the image)
I want the Button to stretch to a parent object (to make the whole area the button click area) and still align imgage AND text at center.
I achieve only top center alignment with folowing code, but I don't get the desired behaviour...
<Button
android:id="#+id/btInfo"
android:paddingTop="20dp"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="top|center_horizontal"
android:layout_weight="1"
android:background="#drawable/border_button_main_menu"
android:drawableTop="#drawable/bt_info"
android:onClick="onClick"
android:text="#string/info"
android:textColor="#drawable/bt_white_red_text"
android:textSize="15dp" />
changing android:gravity="top|center_horizontal" to android:gravity="center_vertical|center_horizontal" only leads to image centered at top and text centered at bottom...
---- EDIT2 -----
Wanted behaviour:
1) look as described (Image and text is a optical group and the group is centered in the button)
2) text should be part of the button (I want the onclick behaviour to work with selectors)
---- EDIT3 -----
added my own solution... but thanks to all that tried to help
Use the Following Code, your problem will be solve.
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="#+id/foreground"
android:layout_alignLeft="#+id/foreground"
android:layout_alignRight="#+id/foreground"
android:layout_alignTop="#+id/foreground"
android:background="#android:drawable/dialog_frame"
android:onClick="clickedMe" />
<RelativeLayout
android:id="#+id/foreground"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true" >
<TextView
android:id="#+id/button_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="112dp"
android:text="#string/hello_world" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button_text"
android:layout_centerHorizontal="true"
android:paddingBottom="10dip"
android:paddingTop="10dip"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</RelativeLayout>
------------- EDIT --------------------
oNclick Method:
final TextView text = (TextView) findViewById(R.id.button_text);
RelativeLayout foreground = (RelativeLayout) findViewById(R.id.foreground);
foreground.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "clicked");
Toast.makeText(getApplicationContext(), "Clicked...!!!",
Toast.LENGTH_LONG).show();
text.setTextColor(Color.RED);
}
});
Try this:
<LinearLayout
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/btInfo"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/border_button_main_menu"
android:drawableTop="#drawable/bt_info"
android:onClick="onClick"
android:text="#string/info"
android:textColor="#drawable/bt_white_red_text"
android:textSize="15dp" />
</LinearLayout>
Call the setBackgroundDrawable() the text you will then add to the button will appear below the drawable!
I think the best way to achieve this kind of UI is using ImageButton instead of Button. But you can still achieve this by some hackish ways. One of them is here:
How to have Image and Text Center within a Button
You'll just have to mention inner RelativeLayout's orientation as "vertical" in the solution given in above link.
I hope it helps.
May be this can help you too:
How to center icon and text in a android button with width set to "fill parent"
The output you are trying to achieve can not be done in that way using drawableTop.
reason why? - View can set background or drawable and while setting drawable Android gives only 4 options to set bounds of the drawable either top,left,right or bottom and nothing for center.
Now in your XML you are having view's height and width as MATCH_PARENT so every time the drawable set using drawableTOP or LEFT etc. it will go to that edge of the view. which is happening right now with you.
From Comments :
Android OS is ultimately a Software.. and a software has always been developed within it's scope. The thing you are asking is out of scope so it's directly not supported by android using any default Form widget..
I demonstrated that how the android have written the Drawable class and how you can use it so please try to understand the answer and not only the Solution to your problem.. by the way to get click of whole area you can write click on that LinearLayout instead of the button.
Solution will be :
<LinearLayout height = MATCH_PARENT and width = 0dp>
<Button height and width = WRAP_CONTENT with drawableTop=image and gravity = center>
</LinearLayout>
My solution is now deriving from a button... that fullfills all my requirements... I don't know, if the measuring of the text is really exact that way, but it looks to be so... if not, everyone get's the idea behind and can adjust that...
Thanks for the help, though
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.widget.Button;
public class CenterImageTextButton extends Button {
private Paint mPaint = new Paint();
private String mText = null;
private float mTextSize = 0;
public CenterImageTextButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CenterImageTextButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CenterImageTextButton(Context context) {
super(context);
}
#Override
public void onDraw(Canvas canvas) {
mText = getText().toString();
mTextSize = getTextSize();
mPaint.setStyle(Style.FILL);
mPaint.setColor(getCurrentTextColor());
// get image top
Drawable drawable = getCompoundDrawables()[1];
Drawable curDrawable = null;
if (drawable instanceof StateListDrawable)
curDrawable = ((StateListDrawable)drawable).getCurrent();
else
curDrawable = ((BitmapDrawable)drawable).getCurrent();
Bitmap image = ((BitmapDrawable)curDrawable).getBitmap();
// call default drawing method without image/text
setText("");
setCompoundDrawables(null, null, null, null);
super.onDraw(canvas);
setText(mText);
setCompoundDrawables(null, drawable, null, null);
// get measurements of button and Image
int width = getMeasuredWidth();
int height = getMeasuredHeight();
int imgWidth = image.getWidth();
int imgHeight = image.getHeight();
// get measurements of text
//float densityMultiplier = getContext().getResources().getDisplayMetrics().density;
//float scaledPx = textSize * densityMultiplier;
//paint.setTextSize(scaledPx);
mPaint.setTextSize(mTextSize);
float textWidth = mPaint.measureText(mText);
// draw Image and text
float groupHeight = imgHeight + mTextSize;
canvas.drawBitmap(image, (width - imgWidth) / 2, (height - groupHeight) / 2, null);
canvas.drawText(mText, (width - textWidth) / 2, mTextSize + (height - groupHeight) / 2 + imgHeight, mPaint);
}
}
I have two textviews like this:
=======================
= TextView1 TextView2 =
=======================
And I would like to detect when the textviews are too long such that they are displayed like this:
=======================
= TextView1 =
= TextView2 =
=======================
currently for longer text, it is displayed like this:
=======================
= TextView1 Text =
= View2 =
=======================
how can I do this, such that when the text is short the textviews are side by side and when it is too long, the second textview is not splitted but moved to the second line?
I tought at a solution to create a single textview and build the text according to length (text 1 + padding + text 2 if short, and text 1 + "\n" + text 2 if long) but I do not like this solution.
Is there any way to detect if the second text will be split such that to change the orientation of the layout that contains the textviews from horizontal cu vertical?
UPDATE
This is my xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TextView
android:id="#+id/my_text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="10dp"
android:text="#string/text1"/>
<TextView
android:id="#+id/my_text_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp"/>
</LinearLayout>
I have found a better solution. Changed my textviews into autoresizable textviews (more info here)
Also, each textview is in a separate layout, to make sure both textviews are resized to the same value.
My xml looks like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/value_linear_layout"
android:gravity="center">
<LinearLayout
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content">
<com.mihaela.view.AutoResizeTextView
android:id="#+id/my_text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content">
<com.mihaela.view.AutoResizeTextView
android:id="#+id/my_text_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
and I have implemented the OnTextResizeListener from AutoResizeTextView to do this:
public class TextWidthResizeListener implements OnTextResizeListener {
#Override
public void onTextResize(TextView textView, float oldSize, float newSize) {
TextPaint paint = textView.getPaint();
if (paint.measureText(textView.getText().toString()) > (valueLinearLayout.getWidth() / 2)){
valueLinearLayout.setOrientation(LinearLayout.VERTICAL);
}
}
}
where valueLinearLayout is:
valueLinearLayout = (LinearLayout)findViewById(R.id.value_linear_layout);
This solution best fits for me, as the textviews are dimensioned when they are side by side until a minimum size. When the minimum size is reached, and the text still does not fit, the textviews will be aligned one under another.
Also, this idea with the listener can be applied to non-resizable textviews also.
I will set this answer as the correct one.
You should use a single, multi-line TextView and set the text as follows :
mTextView.setText(text1+" "+text2);
or
mTextView.setText(text1+"\n"+text2);
depending on your particular needs.
EDIT: you could specify your text in html, and then use Html.fromHtml(htmlString) and display this text in your TextView.
String text1 ="<font color=\"red\">This is some text!</font>"
String text2="<font color=\"blue\">This is some other text!</font>"
textView.setText(Html.fromHtml(text1+ "<br/>"+ text2);
I made a slightly different version of the accepted answer. I did not alter my layout xml in any way and did not use onTextResize() or AutoResizeTextView as that seemed an overkill for my situation. I needed my LinearLayout to switch from Horizontal orientation to Vertical orientation if the device's language setting caused a long string to be used.
Layout
<LinearLayout
android:id="#+id/customer_care_bottom_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:layout_marginBottom="#dimen/lmargin_bottom_10">
<TextView
android:id="#+id/customer_care_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/CUSTOMER_CARE_TITLE" />
<TextView
android:id="#+id/customer_care_number_information"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/CUSTOMER_CARE_INFORMATION"/>
</LinearLayout>
Java
private void setCustomerServiceLayoutOrientationBasedOnTextWidth() {
TextPaint paint = customer_care_number_text.getPaint();
TextView tvCustomerCareTitle = (TextView) findViewById(R.id.customer_care_title);
TextView tvCustomerCareInformation = (TextView) findViewById(R.id.customer_care_information);
int halfCustomerServiceLayoutWidth = getScreenWidth() / 2;
boolean isCustomerCareTitleTooLong = paint.measureText(tvCustomerCareTitle.getText().toString()) > customerServiceLayoutWidth;
boolean isCustomerCareInformationTooLong = paint.measureText(tvCustomerCareInformation.getText().toString) > customerServiceLayoutWidth;
if (isCustomerCareTitleTooLong || isCustomerCareInformationTooLong) {
LinearLayout llCustomerCareBottom = (LinearLayout) findViewById(R.id.customer_care_bottom_layout);
llCustomerCareBottom.setOrientation(LinearLayout.VERTICAL);
}
}
private int getScreenWidth() {
int screenWidth;Display display = getWindowManager().getDefaultDisplay();
if (Build.VERSION.SDK_INT < 13) {
screenWidth = display.getWidth();
} else {
Point point = new Point();
display.getSize(point);
screenWidth = point.x;
}
return screenWidth;
}