Greg Peckory
Greg Peckory

Reputation: 8068

Extending a custom layout class - Constructor never used

I am using the GifWebView given here

I know the Main Activity etc. is working fine, as I tested it before implementing in XML for Frame Layout. But now that I am using Frame Layout, I am encountering problems.

I will just provide you with my code and a quick explanation:

XML

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.example.dencallanan.giftestapp.GifWebView
        android:id="@+id/GWebView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        />

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:layout_marginBottom="153dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:hint="USERNAME" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText2"
            android:layout_marginTop="58dp"
            android:layout_alignTop="@+id/editText"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignLeft="@+id/editText"
            android:layout_alignStart="@+id/editText"
            android:hint="PASSWORD" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Log In"
            android:id="@+id/button"
            android:layout_below="@+id/editText2"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="25dp" />
    </RelativeLayout>


</FrameLayout>

GifWebView

public class GifWebView extends WebView {

    public GifWebView(Context context, String path) {

        super(context);
        loadUrl(path);
    }

    public GifWebView(Context context, AttributeSet attrs, String path) {

        super(context, attrs); 

    }

    public GifWebView(Context context, AttributeSet attrs, int defStyle, String path) {

        super(context, attrs, defStyle);

    }
}

I think my problem lays here. I saw online that I should add the second two constructor methods seen above. But it seems the first one is the only one being used, i.e - the second two constructors give warning methods never used.

Maybe I'm wrong but supposedly you need these constructors to implement the layout class in XML, and these constructors are never being used... why?

The following two lines are in my MainActivity just to help you see what's going on.

    gWView = (GifWebView) findViewById(R.id.GWebView);
    gWView = new GifWebView(this, "file:///android_asset/watg.gif");

I'd really appreciate any help given, this problem has been at me for hours.

Problem

Nothing shows on Android screen... just blank

EDIT FOR BLACKBELT

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.example.dencallanan.giftestapp.GifWebView
        android:id="@+id/GWebView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        myapp:fpath="watg"  <!-- also tried "watg.gif" -->

        />

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:layout_marginBottom="153dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:hint="USERNAME" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText2"
            android:layout_marginTop="58dp"
            android:layout_alignTop="@+id/editText"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignLeft="@+id/editText"
            android:layout_alignStart="@+id/editText"
            android:hint="PASSWORD" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Log In"
            android:id="@+id/button"
            android:layout_below="@+id/editText2"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="25dp" />
    </RelativeLayout>


</FrameLayout>

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="GifWebView">
        <attr name="fpath" format="string" />
    </declare-styleable>
</resources>

GifWebView.java

public class GifWebView extends WebView {

    public GifWebView(Context context) {

        super(context);
    }

    public GifWebView(Context context, AttributeSet attrs) {

        super(context, attrs);

        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.GifWebView);
        CharSequence s = a.getString(R.styleable.GifWebView_fpath);
        loadUrl(s.toString());
    }

    public GifWebView(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);

        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.GifWebView);
        CharSequence s = a.getString(R.styleable.GifWebView_fpath);
        loadUrl(s.toString());
    }
}    

Upvotes: 2

Views: 783

Answers (1)

Blackbelt
Blackbelt

Reputation: 157467

you shouldn't overload the View's constructor. Use

 public GifWebView(Context context, AttributeSet attrs) {
        super(context, attrs); 
 }

and

public GifWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
}

and provide not-default-parameters, programmatically or define a custom attribute for your custom View

Upvotes: 5

Related Questions