I have a app that shows the camera view on the screen on a FrameLayout. The screen is in fixed LandScape mode.
I need to write a textView with dynamically determined coordinates of the screen. The coordinates are determined in percentages, for example :
write the textview on the coorinates x=80% of the screen & y=20% of the screen. write the textview on the coorinates x=35% of the screen & y=55% of the screen.
how to do it? i already have the percentages, i only need to know how to use them to write the textview on the desired position of the frameLayout
code examples are welcome
i tried with this but doesn't works, the textview isn't moved:
TextView poi..... etc etc
poi.setLayoutParams(new LayoutParams((int)(w*(xCoordPercent/100)), h/2));
thanks
MarginLayoutParams params=(MarginLayoutParams )poi.getLayoutParams();
params.leftMargin=80;
//here 100 means 100px,not 80% of the width of the parent view
//you may need a calculation to convert the percentage to pixels.
params.topMargin=50;
poi.setLayoutParams(params);
This may help.
Related
I need view2 to be next to view1 if view1 is not filling the whole width.
If the content of view1 is full width (the content fills whole width of screen) then view2 should be under it.
For example if my TextView text is short a timestamp be next to it
If TextView text is multiline the time stamp be under it
Can I achieve this using ConstraintLayout?
When view1 is not full width
When view1 is full width
Yes, but, i recommend use the Constraint Layout in head but a other layout (Relative or Recycle) to organize the other elements from view.
You can use a custom width, if you are a sure the position.
Or, still, you can try use "parent", if you divide in fragments the wrap content works too.
I have a TextView that can have a few different values, and is updated runtime (in Java code).
However, I need this TextView to retain its center point, so that when the text in that TextView is updated, it is always center justified. It should be centered around a point which is not the center of the screen or anything else, so setting gravity only will not help.
As the values that it may contain are already defined, I could try with the longest one first, position it to the correct top left position and set its gravity to center. In this case every shorter in length text should fit correctly.
However, I would like to know if there is better approach, for cases when the values are not known beforehand.
This TextView is placed below an ImageView and it could take the whole screen width (nothing else is placed left or right to it).
Note: I guess it could be also possible to position it every time to a new X axis position, whenever the text is changed, but I don't think it is a nice solution at all.
Set the android:layout_width to match_parent and set android:gravity to center_horizontal.
This way the View is stretched all the way horizontally, and the content (the text) will be centered. If the content changes, it will still be centered.
One thing that many people don't realize is the difference between android:gravity and android:layout_gravity. The first one defines the alignment of the content inside itself, the other one defines alignment of itself relative to its parent.
I have problem in resizing the text view .
I have a Layout like this with below components
ImageView| TextView | ImageView
placed in relativelayout(horizontal) and each one width is wrap content and height is fill parent.
PROBLEM
I have a text like this "My name","My name is xyzmnop" now, i have fixed the TextView size to 15sp, so what is happening is in larger devices since Textviews width increases the text "My name is xyzmnop" will fit but in smaller devices since the width is small it will spill out to second line. But i want it to be in same line, as I have the flexibility to decrease the size of the text if the text is lengthy.
please help me with this problem.
i have seen this
Android TextView doesn't resize after changing text size
Android EditText Resize Programatically
Resizing TextView does not shrink its height on Android 3.1
EDIT:
I have also added setSingleLine = "true"; in my xml
Say, tv is your TextView.
Just call tv.setMaxLines(1);
TextView has the setMaxLines (int maxlines) method:
Makes the TextView at most this many lines tall. Setting this value overrides any other (maximum) height setting.
I suppose this might help you if you set the maximum number of lines to 1.
Note: there is also the android:maxlines attribute if you need to set it in your XML layout.
i have solved it using ViewTreeObserver.
I have a app that shows the camera view on the screen on a FrameLayout. The screen is in fixed LandSape mode.
I need to write a textView with dinamically determined coordinates of the screen. The coordinates are determined in percentages, for example:
write the textview on the coorinates x=80% of the screen & y=20% of the screen.
write the textview on the coorinates x=35% of the screen & y=55% of the screen.
how to do it? i allready have the percentages, i only need to know how to use them to write the textview on the desired position of the frameLayout
code examples are welcome
thanks
You might want to add an AbsoluteLayout overlay, calculate the absolute position on the screen using screen dimensions and set the position of the view accordingly.
Edit
Since the AbsoluteLayout is deprecated, you can use the following technique to position the text in a RelativeLayout:
text = (TextView) findViewById(R.id.text);
RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position.leftMargin = 20;
position.topMargin = 40;
text.setLayoutParams(position);
Is it possible to display a TextView dynamically so that the height of the text can be retrieved in the middle of the Application but not at the end that is done using onGlobalLayout() or onSizeChanged().
My requirement is to display the text in the TextView to a certain height limit. I need to check the height of the text in the TextView so that I can achieve my requirement but using any of the onGlobalLayout() or onSizeChanged(), I am getting the height at the end which is of no use to me because I can't check at the end.
Is there anyway to achieve my requirement?
I think this will help you.
Dynamic Views in Layout