i have a problem with the TextSwitcher.
In my app i have 3 Textswitcher with wrap_content (width and higth) inside an LinearLayout!
Each TextSwitcher is declared with an in and outAnimation.
My problem:
When the text is switch the first time, i have spaces between the elements!
like: 1.23 45 678
but it has to: 1.2345678 without spaces
when the text switches the second or 3. time the spaces are gone.
I cant imagine why there are spaces?? There is no other Element, no padding, no margin.
maybie the reason is the ViewFactory:
ViewFactory for the middle part switche:
public View makeView() {
TextView t = new TextView(SmartTraderFxTrading.this);
t.setTextColor(Color.BLACK);
t.setTypeface(null, Typeface.BOLD);
t.setTextSize(26f);
return t;
}
an for the switcher left and right from the middle part:
public View makeView() {
TextView t = new TextView(this);
t.setTextColor(Color.DKGRAY);
t.setTextSize(20f);
return t;
}
but i can change everything, the spaces are always there after the first switch, and also maybie after the second.
Is it a bug? Please help!
EDIT:
Problem solved!
The problem is that the Text befor has more digits as after! So the TextSwitcher has a larger width and after redraw of the activitie it wraps the TS.
Related
I have a Linear Layout that has 2 buttons(not really, this is just a simplified version), I want the 2 buttons to have the same height but different widths so that they use as little space as possible.Like this:
Good:
Bad(lots of free space in button 2):
This is of course a bit unfortunate because android does not seem to wrap very well in both directions.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:singleLine="false"
android:text="Rar riego todune ieyed nili rilo noledo oso ilow. Resulieh hi itepitep. Nena pis te osur uporeta, vef rilan te ege enono vesanep re caba? Hirica bugus le relam liri delira. Naneva recimen te netegar tic anote sul ese da; sie pihic lasimiw rife nepime ediyira mic eja hos. Kinir ragovo orero gegol haroy di nacecid cotam anana idom. Cienad keti cu. Tano nosaroh hu yih; reheh ratasu iwayag cuc detiwo elalir irerun, ero iyuluv pe pumip atage dono epam secehag re nineq. Gisi upur yogeget tikige. Ka etex ateyusip tetediey ke nina irahi nirayo nimidor! Nuda de racur pas. Cof nurar mije ron nom saxuv. Sepuno so ner, pa fakun laki le som." />
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:singleLine="false"
android:text="Mebieron hina ciras risa ne, neno telonup edinieba irara we eyacarob til re lum guyiga. Sagiko rubana tit rula gun sisan. Hil romiene to pute sip tuyud risomim etasi vicaro, tedoc te coke rel arietota epa masisir nenacec, gorula fol anu sep rie. Har vipahier metodo siebegu ival are ureh dodor rorel. " />
</LinearLayout>
What I have noticed up until now:
1 I need the Linear Layout to have WMP(width match parent) and HWC(height wrap content).
2 Button WMP+HMP(both because button1 should be symmetrical to button2) or WMP+HWC forces the other button out of the screen(and in a weird way its height to maxout the screen, making view after the LL to not appear, because the LL thinks it needs the whole screen, unless you have a scrollable layout and then the LL will expand to the height of each word summed and then display the rest of the content. If the second button has only 1 word the LL will not expand like crazy).
3 WWC+HWC same scenario as above but only if text is big/multiline, if text is small then they are ok.
4 WWC+HMP same scenario except now the first button is as big as the screen.
5 If I use layout_weight the 2 buttons use 50-50 of the screen and if one of the buttons has 2x more text the other button has a huge amount of whitespace.
6 Grid layout behaves just like linear layout
First of all I tried to find if there are any default implementation of that in android default views but there no XML attribute to use for that, even in TableLayout although we can make a simple formula to calculate that and give yours views similar height without care about the width!
Here is how:
In your class declare member variables of the views of interest!
public class MainActivity extends AppCompatActivity {
private LinearLayout linearLayout;
private Button btn_one;
private Button btn_two;
Then in the onCreate method, after setContentView() get the views by id for example
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout=(LinearLayout)findViewById(R.id.activity_main);
btn_one = (Button) findViewById(R.id.btn_one);
btn_two = (Button) findViewById(R.id.btn_two);
After that here comes the most important part! We need to find the width of the linear layout before we know how to divide it to the buttons.But we cant use the width by calling linearLayout.getWidth() because we are in onCreate() and the linear layout may have not been created yet and the method will return zero. So in the onCreate() we are going to use a Runnable that will give us a confirmation that the linearLayout is made and so it will run.
In onCreate() add the code:
linearLayout.post(new Runnable() {
#Override
public void run() {
balanceHeight(btn_one,btn_two);
}
});
}
balanceHeight is the method that we are going to use to divide our width so as to balance Height.
Outside your onCreate()but inside your activity class copy the following code!:
private void balanceHeight(Button btn_one, Button btn_two) {
int length1 = btn_one.getText().length();
int length2 = btn_two.getText().length();
int total = length1+length2;
int total_width=linearLayout.getWidth();
LinearLayout.LayoutParams params1=new LinearLayout.LayoutParams((int)(((float)length1/(float)total)*(float) total_width), ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams params2=new LinearLayout.LayoutParams((int)(((float)length2/(float)total)*(float) total_width), ViewGroup.LayoutParams.WRAP_CONTENT);
btn_one.setLayoutParams(params1);
btn_two.setLayoutParams(params2);
}
This method accept Two buttons as parameters and get their text length compare them and use the linear layout width to divide it according to their content and available space. I have casted values to float to improve accuracy.
Note: When importing layout params class make sure its LinearLayout.LayoutParamsevery root views has .LayoutParams so make sure you import the right one.
Also if you real want button you can continue using it. But in Android any View is clickable if you real want a button do it, but if not use a TextView and set OnClickListener to it!
I set my TextView to be scrollable:
textView.setMovementMethod(new ScrollingMovementMethod());
But, when I update my TextView's texts, the scrolling position keeps at the last text position, resulting in, sometimes, the text getting invisible and then, I need to move the scroll to see the new text (I update with: textView.setText(newText)).
I tried those codes, but no changes were noted:
textView.invalidate();
textView.requestLayout();
textView.scrollBy(0, 0)
textView.scrollTo(0, 0);
P.S.: the textView is a child of a RelativeLayout.
Why don't you just use a ScrollView in xml will be much easier open the ScrollView open a layout set it how you want it to be horizontal or vertical put your TextViews in and then close first the layout and then ScrollView and Voila done..
What I did to solve my problem:
textView.post(new Runnable() {
#Override
public void run() {
textHeight = textViews.getLineHeight() * textViews.getLineCount();
if (textHeight > relativeLayout.getHeight())
textView.setMovementMethod(new ScrollingMovementMethod());
else
textView.setMovementMethod(null);
}
});
Currently I have several views and the screen is divided into two section
Example:
text1 image1
text2
text3
text4
The problem is , if the image1 is tall , it will overlap the textview on left side, so I use to left of to force the textview width not exceed the imageview's left.
android:layout_toLeftOf="#id/imageView1"
however, every textview are aligned to left of the imageview as I do not know the height of it until the view is created . And I want all textview below the baseline of imageview remove the layout rules of android:layout_toLeftOf
So I search for the solution and find two ways ?
1.onWindowFocusChanged
2.getViewTreeObserver().addOnGlobalLayoutListener
Both can get the yaxis of the view.
The problems are:
1. what is the difference between them ?
2. I tried approach 2 , but it is not working, how to fix it?
Code:
image.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
ImgY = image.getY();
}
});
lng.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (lng.getY() > ImgY) {
lng.removeRule(RelativeLayout.LEFT_OF);
}
}
});
The error is I would like to set a global value to store the y of imageview, but it warns The final local variable ImgY cannot be assigned, since it is defined in an enclosing type Also, the removeRule function return
The method removeRule(int) is undefined for the type TextView
Thanks a lot for helping.
In my app I display several text views containing text of various length that is loaded in at run time. I do not know the dimensions of the text view or the length of the text until run time. Sometimes, when the text is long and the textview small some of the text is partially visible, for example:
I want to remove the partially visible text as it looks a bit naff, but I can't find a way to do this. Any help would be appreciated!
Thanks,
Dave
You can hard code the TextView height in a way that the second row of text will not be visible.
Or use:
android:maxLines , Makes the TextView be at most this many lines tall.
as suggested above.
Put your textviews in a scrollview layout.And specify a specific width to your textview and make the height wrap content.So that your text doesn't get cut.
This is how I did it. I ran this code after the activity had loaded by posting the method CheckTextIsVisible to the parent relativelayout's handler queue, otherwise the height of the textviews will not be known:
m_eventsLayout.Post(new Action(CheckTextIsVisible));
Then the method CheckTextIsVisible finds each textview with text in it, calculates the height of the font, works out how many lines can fit in the textview, and sets the number of maximum lines accordingly:
private void CheckTextIsVisible()
{
View view;
TextView tView;
Android.Text.TextPaint tPaint;
float height;
int heightOfTextView;
int noLinesInTextView;
for (int i = 0; i < m_eventsLayout.ChildCount; i++)
{
view = m_eventsLayout.GetChildAt(i);
if (view is TextView)
{
tView = (TextView)view;
if (tView.Text != "")
{
//calculate font height
tPaint = tView.Paint;
height = CalculateTextHeight(tPaint.GetFontMetrics());
//calculate the no of lines that will fit in the text box based on this height
heightOfTextView = tView.Height;
noLinesInTextView = (int)(heightOfTextView / height);
//set max lines to this
tView.SetMaxLines(noLinesInTextView);
}
}
}
}
private float CalculateTextHeight(Android.Graphics.Paint.FontMetrics fm)
{
return fm.Bottom - fm.Top;
}
This results in no partially visible text!
So I have a setup where I'm creating my own View and I'm adding some TextViews into it. However, the gravity setting is broken for it. (It centers horizontally, but not vertically) I'm doing it this way because there's other stuff I'm also drawing within my view besides just the TextViews, but those work fine. There's only a problem with the TextView gravity. Here's partial code of what I have.
public class myView extends View {
protected RelativeLayout baseLayout;
protected TextView textView1;
protected TextView textView2;
public myView (Context context) {
super(context);
setLayoutParams(new LayoutParams(FILL_PARENT, FILL_PARENT));
baseLayout = new RelativeLayout(context);
baseLayout.setLayoutParams(new LayoutParams(FILL_PARENT, FILL_PARENT));
textView1 = new TextView(context);
// initialize textView1 string, id, textsize, and color here
textView2 = new TextView(context);
// initialize textView2 string, id, textsize, and color here
baseLayout.addView(textView1);
baseLayout.addView(textView2);
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Resources res = getResources();
// calculate out size and position of both textViews here
textView1.layout(left1, top1, left1 + width1, top1 + height1);
textView1.setGravity(Gravity.CENTER);
textView1.setBackgroundColor(green); // just to make sure it's drawn in the right spot
textView2.layout(left2, top2, left2 + width2, top2 + height2);
textView2.setGravity(Gravity.CENTER);
textView2.setBackgroundColor(blue); // same as above
baseLayout.draw(canvas);
}
}
This draws the TextViews in the exact spots and sizes that I want them (I know because of the background color), but the gravity sets them to be only centered horizontally.. not vertically. (yes, the TextViews are larger than the actual text strings)
I could PROBABLY implement the solution found here (TextView gravity), but that doesn't seem like a very efficient or reliable way to get around this. Is there something I'm doing wrong that's causing gravity to stop working correctly? Any input/help is appreciated.
Ok.. So I figured this out. I just had to run the measure() method on each TextView.. So my new code looks like:
textView1.measure(MeasureSpec.makeMeasureSpec(width1, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height1, MeasureSpec.EXACTLY));
textView1.layout(left1, top1, left1 + width1, top1 + height1);
textView1.setGravity(Gravity.CENTER);
textView2.measure(MeasureSpec.makeMeasureSpec(width2, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height2, MeasureSpec.EXACTLY));
textView2.layout(left2, top2, left2 + width2, top2 + height2);
textView2.setGravity(Gravity.CENTER);
Now it centers both horizontally and vertically like it should. If you're having the same issues, try this out.
You can set multiple Gravity parameters using setGravity like this.
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL );
Hava a look on How do I center text horizontally and vertical in a TextView in Android?
I just ran into this problem, too. My issue was that when I set gravity on TextView by calling TextView.setGravity(), it silently affects the layout of itself(Textview) in parent view.
Here's what I did for the workaround fix:
TextView schedule = getBubbleTextView (item, hasZeroSpanEvent);
// There's a bug in TextView, we need to use wrapper to fix it
LinearLayout wrapper = new LinearLayout (getApplicationContext ());
wrapper.addView (schedule);
hourlyBubbleParent.addView (wrapper, llp);
You should grab the idea that you should use wrapper to workaround this.