Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you properly write and use a View subclass in Android?

I am trying to implement one of the solutions found here.

My problem is that I'm not sure if I am implementing and using my subclass correctly. I am subclassing a WebView here:

public class myWebView extends WebView{

  public myWebView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

  @Override
protected void onSizeChanged(int w, int h, int ow, int oh) {
    // TODO Auto-generated method stub

      scrollTo(xScroll - (widthScroll/2), yScroll - (heightScroll/2));


    super.onSizeChanged(w, h, ow, oh);
}

   }}

It should be private but forget that for now. I have the code inside of one of my activities that has an inner webview in the view hierarchy. Outside of the onCreate method of that activity.

Inside the onCreate method, I have:myWebView mapImage = (myWebView) findViewById(R.id.mapcroppic);

This gives me a ClassCastException for that call. (Does the xml layout file need to use <myWebView>? Can it?) How do I use this the correct way?

like image 454
joepetrakovich Avatar asked Jan 19 '11 02:01

joepetrakovich


People also ask

What does view view do in Android?

View is a basic building block of UI (User Interface) in android. A view is a small rectangular box that responds to user inputs. Eg: EditText, Button, CheckBox, etc. ViewGroup is an invisible container of other views (child views) and other ViewGroup.

When creating a custom view in Android which method is responsible for displaying that view on the screen?

Creating custom views. By extending the View class or one of its subclasses you can create your custom view. For drawing view use the onDraw() method. In this method you receive a Canvas object which allows you to perform drawing operations on it, e.g. draw lines, circle, text or bitmaps.

What is view tag in Android XML?

This class represents the basic building block for user interface components. A View occupies a rectangular area on the screen and is responsible for drawing and event handling. Views are used for Drawing Shapes like Circles,Rectangles,Ovals etc . Just Use View with background and apply a Shape using Custom Drawable.


1 Answers

(Does the xml layout file need to use <myWebView>? Can it?) How do I use this the correct way?

Yes:

<your.package.name.myWebView
    android:layout_with="blah"/>

Well, that works if the myWebView is a public class. If it's an inner one:

<view class="your.package.name.myWebView.YourActivity$myWebView" />
like image 198
Cristian Avatar answered Nov 15 '22 11:11

Cristian