Alternative to dp for text view alignment with background - java

I have a text view on my android app which I want to place in a specific place on the screen. This is because there is a background on the screen and the text view has to show in line with the space I have made for it in the background.
I'm moving the text around using the margins and entering for example 20dp for top margin 10dp for right margin.
Is there any other method I can use in order to ensure that the text view will be displayed in the right place on all devices? As other dvices mas have a higher or lower res.

use relativelayout and you can control the position of a view in a much better way.
android:layoutalign_parentright=true tag makes the view to be the right always.
android:align_right="#id/someViewId" makes the right edge of this view aligned with the right edge of someViewId view.
It packs a lot of tags that enables you to do such stuff.

Related

How to expand layout when scrolling in Android?

Now I want to create layout like this in my MainActivity
and I want to set up my scroll view. When I scrolling Content view move to top of Main view like this
It move to Top of main view and expand content view follow my scroll view
and when it expanded reach the specified point I want to make it like this
set the top layout of content view (Red color) to fix not moving and go to scroll only main content layout (Blue Green Orange color)
and then finish of scrolling when i scroll down it all of layout return to first picture
Anyone can help recommend me.
I have been stuck in this problem for many days.
Java, Android Studio
I'm sorry for my English I'm not good at English

How to resize image as I scroll?

I have a ScrollView in which I have inserted a ConstraintLayout that contains a fullscreen ImageView and some other components below it.
What I want is for the image to shrink in height (to a certain limit) whenever I scroll down.
Here's an example of what I'm seeking: https://imgur.com/rlOr0HA
As for resizing, after some research I figured I'd have to create a LayoutParams object and then affect it to the ImageView. No problem there.
But as for detecting the scroll event, I've had some trouble. I tried the setOnScrollChangeListener on my ScrollView, but the problem is that it requires an API level of at least 23. So I wonder if there is another solution that works for lower levels as well.
Another problem I'm having is how to make the resizing proportionate to how much the user has scrolled.
You are looking to do the coordinator layout with a collapsing toolbar. This is built into android and you do not need any code changes. The inflater will inflate the layout and everything will work.
In your xml layout file you will need
<coordinatorlayout
<Appbar layout
<collapsingtoolbar
<ImageView> <-- your image goes here
/collapsingtoolbar
/Appbar layout
/coordinatorlayout
<nestedscrollview
<textview> <--Your content that moves up goes here
/nestedscrollview
<floating action button> <-- your example shows one of these buttons but its optional

Draggable overlay that can go offscreen?

I have created an overlay menu that is a rectangle.
It displays on top of everything else while active. All other apps, the home screen, etc.
If you click anywhere outside of this rectangle your click hits
whatever is visible.
If you click the red part it allows you to drag the menu around the
screen. Clicking the green part performs some function.
I have created this rectangle from inflating layout xml. The rectangle itself is a relativeLayout and the green and red parts are both imageView children.
Since I'm unable to move the relativeView off screen and since the background needs to be clickable my only solution was to calculate margins and apply them to the relative view to imitate the desired effect.
However, the code to calculate these margins is almost 50 lines and fairly complex and hard to understand at a glance if someone else needs to refactor. I can post the code but I don't believe it's relevant to the actual problem's context.
Is there a much simpler way to do this that I'm missing? Is my approach wrong?
I hope this simple project may help you Android Floating Widget

Writing text to certain positions in a TextView

My application has a TextView, used for showing lots of data for the user. The data would be best represented in a structured, "semi-spreadsheet way", ie. some data would need to be always shown on topmost line of the view, some data on bottom-left corner, some on bottom-right corner, and the "middle" of my view would need to be reserved for other stuff.
As a minimum, I would thus need to reserve:
topmost row for certain data
bottom left corner for some
bottom right corner for some
the rest of my view to a rapidly changing data stream
Is there any UI component which would allow me to position my txt freely around the component, to achieve my goal? I need to keep this view (currently TextView) as one, since I need the view to be clickable for other purposes, and dividing the area into millions of small TextViews really is not the answer I'm looking for.
You can use RelativeLayout creatively to achieve what you need. There is no specific layout that will satisfy your needs. But RelativeLayout is something i can see that would serve your purpose if used creatively.
You can use this in your xml file for alinment.
android:gravity="left"
OR in java you can do
TextView textView = (TextView)findViewById(R.id.mytextviewid);
textView.setGravity(Gravity.RIGHT);
For aligning view inside relative layout you can do any of this
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
You have to use Relative layout in your layout xml file and position different textviews according to your need using align properties as described above.
Note: Android does not provide facility to write text in textview at particular position, what at most android provide is align text within textView to left/right/middle position using layout_gravity parameter, but this applies to whole text in textview.

Adding a tag in the upper right hand corner of a widget

How could I add a small tag containing a text in the upper right hand corner of my widget? I'm looking for something like the tag which can be seen on mail apps widgets.
You are going to need a FrameLayout with your widget on the background and a TextView (your tag) with a gravity setting of top|right.

Categories