I'm creating a simple custom view. My view inherits from AppCompatImageButton with the goal of drawing some simple geometric shapes for my custom button and animating them.
If my XML view contains:
android:background="#color/transparent"
My view draws correctly inside a vertical LinearLayout, even though the android:background seems to otherwise do nothing. However, if I leave out android:background, the top and bottom displays an additional unwanted gap between the custom views.
The documentation says these two are "Related":
View.setBackgroundResource(int resid)
android:background
I've tried calling setBackgroundResource(R.color.transparent) in my Java initialization code, thinking this would do the same thing as XML android:background, but the unwanted padding won't go away. Here's a screenshot that shows the view without the android:background.
With android:background, the image fills the entire height not leaving the gap at the top and bottom.
How can I get my custom view to fill the entire drawing area without forcing an artificial android:background into the XML? The solution will also need to work if the programmer creates the custom view in Java without XML.
Thx.
Related
I want to create a custom view
this view
and when add a view to XML I want to add static child view(textview,imageButtom and ...) into custom view like cardview and final view look like this picture
.how can we create a view?
You could start by extending an already defined layout like relative layout because creating a layout from scratch and implementing all methods in it is a little hard process. First thing came to my mind is adding a padding the custom layout for edge perspective effects so the layout don't place any view to this area. Then you can draw you perspective effect in onDispatchDraw() method. This method provide you with to draw canvas of the layout. In addition, using an drawable like nine patch as background makes things easier compared to custom drawing operation. In summary, you should give padding the layout to prevent overlapping with edge perspective. Then draw or set a background for the layout.
I am finding it impossible to produce my desired layout in android studio.
I have a very simple gimmick drawing app and I'm trying to add a colour palette as a gridView. The palette is too big to not overlap the canvas, so I'd like to show it while the colour button is pressed so you can swipe to your desired colour.
Positioning the palette gridview is proving difficult. I would like it overlapping the canvas, above and horizontally centred with the 'pencil' colour selection button. I've tried two methods:
Having the gridview exist in the top-level relative layout, so it can easily overlap the canvas. This is as per the screenshot. However, I can't position it relative to the button here because the button is in a different layout.
Having the relative layout containing button and palette contained in the control view, as shown in the tree diagram. However, the palette won't draw outside its parent layout, and the relativeview can't overlap the canvas because it's in the linear layout for controls which goes beneath the canvas.
It seems like such a trivial thing - is it really impossible? Should I abandon all pretense of linear layouts and place everything in one relative layout?
Tree diagram
I have added a view to a layout which occupies a part of my screen. To this layout I want to add another layout which will be transparent. On this layout there should be only two lines which will scroll over the background layout. This I am doing so that my background layout is not invalidated and only the foreground is invalidated.
How can I add another layout which will be transparent?
Use FrameLayout as a parent layout and stack it up with as many layouts as you want. Thats one part of the answer. To make a layer (a layout in this case) transparent, set the alpha value in its background (a color) to 0. For instance android:background="#00777777" sets a background which is translucent with a dull gray.
You get the idea.
Use fram layout, which will allow you to add two views on each other.
The following screenshot illustrates a simple example of what I have now:
What I'd like to achieve, is that the selected (blue) view not be clipped at the boundary of the red container. My first try was clipChildren="false", which causes the blue view to expand outside of its borders, filling the the red area. I just want to see the portion overlaying the green area.
I think you'll have to float the blue on top of both the red and green. You can't have a child outside of its parent ViewGroup (AFAIK). You'll need to redesign your layout.
Getting what you want should be pretty easy, though. I don't use the graphical designer, so would need XML.
FrameLayout with LinearLayout inside to show the Red/Green, then another Linear or Relative after the first LinearyLayout (inside the FrameLayout). With LinearLayout, I'd align right, and give the blue element some padding.
It may be possible to do this all with RelativeLayout, but I tend to stay away from it.
Essentially what you're looking for is overlapping views. This can be done with a FrameLayout. For information on how to do this, please checkout this example.
i was requested to make in android a view that groups several items like checkboxes or text views in vertical rows, separated by transparent dividers while the background is with a certain alpha level and the edges are round.
I thought of two solutions and i hope for some feedback on good\bad or other solutions if you got'em.
just use regualr linear layout but have a single style A that uses a 9 patch as background, includes padding,margins and whatever i need to make it look like what i want. i then create another style A.up and A.down that represents the upper most and lower most items that will use a different 9-path with round corners.
inherit from linear layout, in the onMeasure and layoutChildren add to all the children some kind of space between them, i can create new attribute for it that can be customized in a style. i can override the dispatchDraw to paint the background for each view before it draws so i can paint my round borders, my only demand will be that each View added to this layout will have to be with transparent background.
So what do you think ?
Eventually i decided to use a List with customized divider.
It looks good, however a list got a very nasty bug when it comes down to items with states like buttons and clickable textViews,
you get no focus for the item and don't see the ornage bar
you don't seem to get the evnets flowing to the children of the View in the list.
I'm notsure how to resolve that one, i've seen numerous mails about it in the developres mailing list and here, most saying don't put statefull objects in a list.
So it mist not be the solution for me.
Nest thing i'll try is extending the normal layouts to have a bar in their bottom and use regualr linear layout with round corners drawable.