Customized View to xml - android

I am making a game and I don't want the whole canvas to occupy the screen. Is there a way on how to put my class extending SurfaceView into xml? I want to put it to xml so that I can adjust the position and size of the SurfaceView on the screen.

in src:
public class MySurView extends SurfaceView {
public MySurView(Context context) {
super(context);
}
// you must overide this constructor
public MySurView(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
in xml:
<package.name.MySurView
android:id="#+id/surview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</package.name.MySurView>

Related

Rotated View's onTouch wrong coordinates

I must get upside down view w/o moving the System menu. So I do:
getWindow().getDecorView().getRootView().setRotation(180);
But all child buttons get onTouch event not when they being touched, but when I touch the areas where they would be without rotation.
How can I fix the problem?
Got solution without translation matrix and such... Just create a new Layout class like this:
public class RotatableView extends FrameLayout {
public RotatableView(Context context) {
this(context, null);
}
public RotatableView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
setUpsideDown();
}
private void setUpsideDown() {
this.setRotation(180);
}
}
and use it for your layouts

Custom view with children inside it(buttons, edittexts, etc.)

I have custom view which extends imageview (with glow touch). I want to add elements like buttons, textviews, to make glow touch when they are touched. I read that custom view cannot have children because only layouts can have them.
Is there any workaround to solve this stuff?
Code:
public class CustomView extends ImageView{
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomView(Context context) {
super(context);
.........
}
With regards
You are correct about the fact that only layouts (ViewGroups) can contain Views, and Views itself can not.
Instead of making a custom View, you can make a custom ViewGroup. In there you can add the Views you need, like the ImageViews and the others you mention (Buttons, EditText, etc).

how to use a same custom fontface for all the view in android?

i want use all the component in android which having the same font type face, for that i am creating a individual custom class for each component like CustomTextView, CustomEditText, etc,..
So instead of creating a individual class for each component can i create a view CustomView class that will automatically apply style for all the components in android
Just declare your own TextView and use it in your XML, it should appear in the custom Views
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setType(context);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setType(context);
}
public MyTextView(Context context) {
super(context);
setType(context);
}
private void setType(Context context){
this.setTypeface(Typeface.createFromAsset(context.getAssets(), "chalk_board.ttf"));
}
Oh dam u want it globally for all views, so this is the wrong approach.... Sorry for that
You have at least 2 ways:
create your own TextView class and set fontFace in constructor
you can use custom LayoutInflater. And every time view gets inflated check that it is textView (or other view not extending textView but having font settings) and set correct fontFace settings.

Creating custom view

I want to create a custom view TestView class for which I can create object via new TestView().
A new view class however needs a AttributeSet object. From where do I get that AttributeSet and what does it have to include?
It's not mandatory, and most times you don't even have to worry about it as long as you provide constructors from View that pass them along to super().
public CustomView(Context context) // No Attributes in this one.
{
super(context);
// Your code here
}
public CustomView(Context context, AttributeSet attrs)
{
super(context, attrs);
// Your code here
}
public CustomView(Context context, AttributeSet attrs, int default_style)
{
super(context, attrs, default_style);
// Your code here
}
View takes care of doing the heavy lifting for dealing with all of the android:* attributes that you'd usually pass in when adding the view to a layout. Your constructors could make use of those attributes or your own if you've defined them:
<com.blrfl.CustomView
android:id="#+id/customid"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
blrfl:foo="bar"
blrfl:quux="bletch"
/>
Either of 3 constructor provided by view class can be implemented.. so providing constructor with attributeset is not mandatory.

How can I add a decorator image on top of my image view

I have an ImageView in my android layout. And I would like to add a 'decorator' image at the lower right corner of my image view.
Can you tell me how can I do it?
I am thinking of doing with a FramLayout with 2 image views as its child, but how can i make one image the lower right corner of another?
<FrameLayout>
<ImageView .../>
<ImageView .../>
</FrameLayout>
You probably want to be using a RelativeLayout (Documentation) instead - it supports stacking views, and all you'd have to do is align the bottom and left of the overlay ImageView with the bottom and left.
You could create your own class that extends ImageView. It will always draw decorator over the ImageView content.
public class MyImage extends ImageView {
static Bitmap decorator;
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(decorator==null)
decorator = BitmapFactory.decodeResource(getResources(), R.drawable.decorator);
canvas.drawBitmap(decorator, 0, 0, null);
}
public MyImage(Context context) {
super(context);
}
public MyImage(Context context, AttributeSet attrs) {
super(context, attrs, 0);
}
public MyImage(Context context, AttributeSet attrs, int params) {
super(context, attrs, params);
}
}

Categories

Resources