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

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

Related

Custom card view shape

I have an idea for a card item. But I don't know what is the best way to implement this. I can't imagine, how to do this with xml (how to cut off that half of the circle?), or how to do this using canvas? Extend card view and redefine background or?
This should set you down the right path: http://developer.android.com/tools/help/draw9patch.html
It allows you to define how a custom image will stretch.
I recommend you use a dialog or custom view with the 9patch image set as the background. If the custom view is a relative layout then you can just center the textview within the relative layout.
Alternatively, if you really don't want to use an image, you can create a custom view and override the onDraw method. #see http://developer.android.com/training/custom-views/custom-drawing.html

Programmatically set view width

I am developing an android app. There are six main long variables that are displayed on-screen at any time. These are continually incremented using a timer on a separate thread. The user can watch these variables increase on screen. Each of these variables has a maximum amount.
What I'd like to do is draw a 'progress bar' for each of these variables. At the moment, I am using a View with a solid red colour.
Please see below for prototype:
The red bar on the left would represent a variable that has reached it's maximum amount, whilst the others are empty and the bar has a width of 0, so is invisible. Each variable is placed in a RelativeLayout (which represents one 'section'), which is then placed in a LinearLayout. At a future point I may need to add/remove some - so the solution needs to not rely on hard-coded layout positionings.
My question is how can I programmaticaly set the width of these 'progress bar's in code, whilst not hard-coding the layout co-ordinates of any of the variables?
You should check http://developer.android.com/reference/android/widget/ProgressBar.html ProgressBar. It will ease you with displaying the progress.
In the case of LinearLayout (and any ViewGroup for that matter) you can generate your own LayoutParams programatically:
LayoutParams params = new LayoutParams(width,height) // in px
yourLinearLayout.setLayoutParams(params);
you will need to cast the LayoutParams to the according ViewGroup type.
The regular way would be to use the ProgressBar, but if you want to use your custom view, use this:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(withInPx, heightInPx);
yourView.setLayoutParams(lp);
This assumes you use LinearLayout, but you should edit that if you use a different layout (i.e. RelativeLayout)
The heightInPx will be fixed in your case, but the withInPx you can vary, according to the progress.

How do I create this Transparent Dialog/Canvas for Android?

I'm a beginner Android developer and I'm making my first app. I'd like to make a simple view that is overlayed on the map with a semi-transparent background. This is what it would hopefully look like:
As you can see, one of these dialogs/windows has a simple integer displayed and the other will have a rendered graph/chart.
What would be the best way to go about making this? A dialog? The problem with that might be that I would for the user to be able to work with the mapview below while this is displayed. And I'm not sure if a simple transparent rectangular canvas is the right way to get this done.
Any suggestions/ideas would be much appreciated. Thanks so much.
two ways to go
-Make it a FrameLayout so add MapFragment first then add a Linearlayout with orientation horizontal and make gravity top and translucent background,so it will be displayed on top of map, and it will also involve the elements inside,
-Instead of FrameLayout make it a RelativeLayout and the same process as above.
-Make the overlay as seperate Activity(which is the worst case scenario)
Sample code for overlay can be like this
<LinearLayout
....
android:divider=".." // some drawable or color whatever you want
android:showDividers="middle"
android:orientation="horizontal"
android:background="a000">
<TextView
....
android:value="58"/>
<com.example.custom.view
..../>
</LinearLayout>
I think you can use a RelativeLayout on the top of the MapFragment, with a black background and some opacity (for that you can use a drawable).
Then, on the RelativeLayout you can add other controls like TextView and ImageView
I don't recommand you to use a Dialog, since it sounds like it's not corresponding to your needs.

Does a paint app on an android phone require an xml layout?

Do i need a layout for a view for when the user "paints" on the screen? And what type of layout should i have? Can it be virtually anything? Like a white screen? Thanks.
You need to use at least one layout which will act as your holder View.
It can be a white screen or whatever you want etc.Instead of programming creating all the views make a holder View for your paint stuff and do the painting on it.
No, XML layouts are optional. You can create it programmatically:
View view = new MyCustomPaintView(context);
setContentView(view);
But XML layouts are very usefull (and strictly recommended) when you need to place many Views on a screen.

LinearLayout inside RelativeLayout uses super-parent as parent

I have the following layout:
<LinearLayout width:fill height:fill >
<RelativeLayout width:fill height:wrap >
<ImageView width:fill height:wrap >
<LinearLayout width:wrap height:wrap />
</RelativeLayout>
</LinearLayout>
The innermost LinearLayout seems to have no idea that it is inside the RelativeLayout when it comes to vertical properties. Gravity works only horizontally. For all intents and purposes (vertically), the inner LinearLayout thinks that the outer LinearLayout is its parent. Aligning parent Top/Bottom stretches it and the RelativeLayout to fill the outer LinearLayout.
Ultimately what I want here is for the relative layout height to wrap the imageview almost as if it was its background (though it's meant to be an overlay on top of another view on top of the background), and the linear layout to simply work inside the constraints of the relative layout.
Furthermore, I want to be able to clip anything that goes outside the bounds of the relative layout wrapping the imageview. What's my best option here?
I think your issue here is that wrap_content does not bound the upper limit of the View (at least by the parent). As such, the height of your RelativeLayout can go on to the full size of it's parent. If it's parent wasn't wrap_content too, it would stretch out to it's parent and so forth until it reaches a parent that has an upper-bound.
In this case, your top parent is bound by the size of the phone, so it stops there. Instead of using gravity on your inner-most LinearLayout. Use the attribute layout_centerInParent="true" on your inner-most LinearLayout. Unless the layout becomes bigger than your ImageView it should conform to the center of your RelativeLayout.
EDIT:
You may also want to experiment with the different Scale Types of the ImageView. There are some that will scale the image as big as it can get without cropping or distorting the ratio. There are some that will always fill the entire view but may crop or distort the image.
There are a lot of things that are unclear from your post, probably you should post the complete version of your XML code.
From my understanding, I guess you're not using RelativeLayout constraints such as layout_alignParentBottom="true" or layout_centerVertical="true". Those will work, unlike the gravity properties.

Categories