2

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());
    }
}    
Community
  • 1
  • 1
Greg Peckory
  • 7,700
  • 21
  • 67
  • 114

1 Answers1

5

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

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Thanks for the answer, could you expand on this a small bit, assume I know very little as I am only a beginner. What do you mean by 'non-default parameters' or a 'custom attribute for my custom `View`' Would this fix my problem or is it just a good habit? Should those constructor methods be used? Why aren't mine? – Greg Peckory Jun 25 '15 at 08:50
  • Ok I think I get what you're saying. So I create a `attrs.xml` file in the values folder. I then create a new layout inside that. I use this layout in my main xml file and I can add styling and custom attributes. I then access those attributes from the Class. I have 2 questions. How do I specify that the new layout corresponds to the GifWebView? And do I keep all 3 constructors and put the `loadURL()` method in all of them. Thanks a lot for being patient with me, 'll be over the moon if this works – Greg Peckory Jun 25 '15 at 09:20
  • Oh I figured out the first question, but have no idea about the second one. – Greg Peckory Jun 25 '15 at 09:22
  • Do I keep all 3 constructors and put the loadURL() method in all of them? – Greg Peckory Jun 25 '15 at 09:39
  • Thanks, and I can't seem to figure out what to replace 'myapp' with. – Greg Peckory Jun 25 '15 at 09:41
  • Great! I have 2 more questions. I almost have it. 1) In the MainActivity do I call `gWView = new GifWebView(this);`. If so, do I leave the `loadUrl()` method out of the corresponding constructor, as it has no parameter for `attrs`, hence not possible ? 2) My image is located in the 'assets' folder. In my xml I cannot access like this: `myapp:fpath = "@assets/watg.gif`. What is the alternative? Thanks so much for all the help! – Greg Peckory Jun 25 '15 at 09:54
  • yes to the first question, but if you declare it on your layout, you don't to instantiate it programmatically. Use string as type for myapp:fpath, and provide only the name. Retrieve the name from the attribute and use the AssetManager to load it – Blackbelt Jun 25 '15 at 09:57
  • I still get white blank screen. Is it possible that accessing the path from xml is causing issues. See EDIT above. All is there. Thanks for this. – Greg Peckory Jun 25 '15 at 10:09
  • `loadUrl(s.toString());` is wrong. It should be `loadUrl"file:///android_asset/s.toString());`, if your attribute is `myapp:myattribute="watg.gif"` – Blackbelt Jun 25 '15 at 10:54
  • I tried `loadUrl("file:///android_asset/"+s.toString());` ... still a blank screen. – Greg Peckory Jun 25 '15 at 11:00
  • do you have .gif at the end ? Can you print the content of `s` on the log and check the results? – Blackbelt Jun 25 '15 at 11:01
  • My logcat is empty and adb logs says `DeviceMonitor: Adb rejected connection to client '1813': closed` I tried `Log.e("mytag", s.toString());` and get nothing in the logs – Greg Peckory Jun 25 '15 at 11:13
  • try to make it work (the logcat) and post the result here – Blackbelt Jun 25 '15 at 11:14
  • The logcat is working fine. But it seems placing `Log.e` in any of the constructor methods produces nothing. I have tried `Log.e` elsewhere in the project and it worked fine. This would suggest that the constructor methods arent even being run at all !! – Greg Peckory Jun 25 '15 at 11:35