Default view padding - android

I'm just starting to learn android and i had a doubt concerning to view padding. Referring to What controls the default padding between views? , Is the padding included in the size we specify to the view ? All help appreciated!
EDIT: Also does a layout add padding to itself by default ?

In the question you linked to, the padding is caused by the button's background being a nine-patch and having some transparent borders in it. Since the "padding" is part of the background resource, it is include in the image size and you don't need to do anything to make it show up.
However, if you are creating your own Views, or changing the background or foreground resources of the system provided Widgets, you may want to use the android:padding* attributes to specify the various paddings.

Related

How is the line under the text of an EditText created?

By default android.widget.EditText has a line under its text. How exactly is that technically done? I checked the source code of the EditText class, but couldn't find where this is done?
I think to know that the line is technically a background, but where is this background set? And especially, as it's just a background, how does Android ensure that this line is drawn below the text and not behind it? Answers with proofs / links to source highly appreciated.
yes, this underline is a background, which is probably ColorStateList (e.g. it changes color when pressed), but if you run your code on Android 4.x or older (pre Material Design) then this background is fixed bitmap/drawable
EditText have an android:background attribute, but it is also present for all other Views. so it is set inside source code of View, not extending EditText. and pointer to the proper drawable of background is set by styling/theming (in HERE you have some example)
EditText is using TextPaint (getPaint method) which may be used to add some extra spacing between/over/under/next to letters to ensure not-overlaying. also drawable may have padding attribute - may draw line in last row of pixels but set bottom padding to 1px and that ensures that content of this View (in this case EditText) will not be drawn under/over padding (but drawable does) - sooner View will expand a bit if have wrap_content for height
sorry for not-so-precise answer, topic needs more inwestigation and searching in sources, but maybe my points will help you find answers by yourself

add shadow to layout programmatically in Android?

So I have many LinearLayouts and RelativeLayouts that I need to apply shadows to. But all these layouts will have different background color.
For example, I have 3 LinearLayouts. I need one Blue, One Green and One Red but all will have the same shadows and style (except for the color).
I already have these layouts created so what can I do to apply shadows to them manually?
If I create an XML drawable then I have to create a different drawable for each color. Is there any other way I can do this?
I was thinking about making a common function where I can pass either a LinearLayout or RelativeLayout and apply the shadow effects with a specific solid color as background.
But I am not sure if there is a way to apply shadow programmatically. Please let me know what my choices are.
Thank you for your time.
Sounds like you need Outline. Since you mention that you want to do this programmatically Outline will do the trick. Fetch your layouts by findViewById(), use getOutlineProvider() from your layouts, get the ViewOutlineProvider then use the getoutline() method to set a custom Outline object that you will create as a shadow to any View that you desire. (also check this from the official documentation)
As for what your choices are I would have to say that generally speaking you have 2 choices.
1) Implement the shadows via XML declarations as a property of any other View that you inflate
2) Do it programmatically with Outline.
There is no such attribute in Android, to show a shadow. But possible ways to do it are:
1)Add a plain LinearLayout/RelativeLayout with colors, over which add your actual layout, with margin at bottom and right equal to 1 or 2 dp
2)Have a 9-patch image with a shadow and set it as the background to your Linear layout/RelativeLayout

Add a static border to a view that has a dynamic background

I've searched SO for a while but couldn't find an answer to my exact question. I usually found 2 similar problems that do not suit my needs:
Many people asked for how to draw a border around a view, and the solution is often to use a shape with a stroke as the background of the view. But this uses a static background.
I can't use this (or don't know how in my case), because I'm creating a color picker and I draw the background programmatically.
Other people asked how to draw a border programmatically, but that's only a fallback option for me.
One solution obviously came to my mind: wrapping my view with dynamic background in another view with the stroke. But I don't like this one as it unnecessarily complicates the layout, and could have an impact on performance too. I'd rather draw the border programmatically with the background than use nested views.
Hence my question:
Is there a clean (androidic) way to set the border of a view in the XML when the background is set programmatically (without using nested views)?
If not, I guess I'll just go for a dynamic stroke.
You can create your own view which does the same as your current view but with an added border.
I haven't tried this yet so I apologize for the lack of information but I believe this should be possible.

Want help to make Android layout same like attachd image

I am getting stuck with making android screen layout same like i have in my iPhone application. The iPhone screen layout is given in below image. Please anyone can help me out to make same screen for android. You can give me some idea with having some code snippet please.
Thanks in advance.
First off, you should be creating an XML-based layout. The Android SDK has a graphical layout tool that will help you with that.
Start looking into 9-patch images for your custom button graphics and setting your android:background attribute to them within a <Button />. Make sure you use actual text for the buttons, not graphics of the text. You should probably use a <RelativeLayout /> for the main layout, as that will let you target multiple screen sizes easily without a mess of nested <LinearLayout />s.
For the tinted area, just use a translucent PNG (with an alpha channel) instead of attempting to line up graphics.
use relative layout to design this screen like use image for navigation bar and align it as parent top (use no title bar in mainfest) add its listener
similarly you can align other text and image view

Android: Overriding the scrollbar drawable in an application

Is there a way to replace the scrollbar drawable in an application (i.e. I have a customized scrollbar view that I would like to use instead of the default gray scrollbar that comes with the Android framework). I looked through I believe all of the android attributes and none seem to support it (at least the simplest way I know how which I would assume would be setting a drawable to some android attribute).
If not and say it requires a change in the framework, where would I start looking for it (in the framework)?
Thanks
MB
Custom scrollbar thumb:
android:scrollbarThumbHorizontal="..."
android:scrollbarThumbVertical="..."
Custom scrollbar track (scrollbar background):
android:scrollbarTrackHorizontal="..."
android:scrollbarTrackVertical="..."

Categories

Resources