Off set android view while using android:layout_centerVertical="true"? - java

I'm trying to get a view center vertically in android, but I have a imageView at the top that is 75dps, so I need my centered view to be offset from the top by 75dps so it gets centered! Is there a way to do this? I tried using android:layout_marginTop="75dp" but it didn't work! Any ideas?

You could wrap your view into a ViewGroup (e.g. LinearLayout), then make the LinearLayout centered, and add the marginTop to your view. It should work.
Anyway, it would be helpful if you post also your layout xml.

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

Centering Object In LinearLayout (Android)

To StackOverFlow,
I am currently developing an Android App, and am facing a issue. The issue is that when I place two view items under a parent (LinearLayout) it will not center one view to use the entire width of the parent. The reason for this (I beleive) is because when the second view uses parent_fill it already takes the width of the previous view into account. So it centers the "empty space". The code for the layout is below.
http://i.stack.imgur.com/byqG3.png
As you can tell it is a simple layout. The problem is shown below.
http://puu.sh/jiKIV/4a30800776.png
The problem is that the Title Bar ("Mah App") is not centering on the entire Linear Layout Bar.
So my Question is how do I make the Title Bar center across the entire Linear Layout even though there are other views are on there as well?
Thanks,
Thomas.
Layout Weight is the concept you are missing here. Look at this:
http://developer.android.com/guide/topics/ui/layout/linear.html#Weight
In your case, you need to define android:layout_weight=1 for the view you want to center. This will make that view more important than the rest on the parent.
But really the best way to center views is to use a RelativeLayout instead of LinearLayout. Aligning possibilities are much better

Overriding: same onDraw for many Views (Button, TextView) elements

I need to draw custom borders in android to all of the views on my screen. Every view will have different parameters. To do this, I thought about making new CustomBotton, CustomTextView etc. classes and redefine their onDraw() methods. But the methods will contain the same code, so it's not nice to
make new classes for those Views and
rewrite the same onDraw() method with the same code.
Is there a more elegant/faster way to do this?
Do something like this for simple and fast solution (Doing things quick always has a performance trade off).
Dont deal with onDraw for anyview.
Make a single class extending LinearLayout.
2.1 Set Background of this Linearlayout as color of your border.
2.1.1 - More better read border color attribute from xml at runtime.
2.2 Set its padding as width of your border.
2.3 Set its width and height as wrap_content and wrap_content respectively.
Add single view to this LinearLayout. Either programatically or via xml.
<com.example.BorderLinearLayout >
<ImageView /> // or whatever. But a single view or viewgroup only
</com.example.BorderLinearLayout>
Hope this helps

Android: Prepend LinearLayout programmatically

I created a layout in my application that includes the followings:
main_layout (FrameLayout, fills whole screen)
-> includes: main_interface (LinearLayout, height/width fills parent)
Now I want to add another LinearLayout in the main_layout BEFORE the main_interface.
I tried this:
LinearLayout bar = new LinearLayout(this);
bar.setGravity(Gravity.TOP);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
bar.setLayoutParams(params);
main_layout.addView(bar);
but this just overlaps my main_interface.
Any Help?
If you want to add a View to specific position in a ViewGroup, you can use addView(view, position). In this case, assuming main_interface is the first View, you can call main_layout.addView(bar, 0).
However, you need to fix your layout first. If you use FrameLayout as your main_layout and set main_interface height and width to match_parent, then they will surely overlap. Try making your main_layout a LinearLayout with android:orientation="vertical" if you want vertically stacked Views.
If you could post your actual XML layout, I could help you modify and test it.

Categories