0

I have the following code:

public class MainActivity extends ActionBarActivity {

    EditText userfield;
    EditText passfield;
    Button logbtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        userfield = (EditText)findViewById(R.id.editText);
        passfield = (EditText)findViewById(R.id.editText2);

        logbtn = (Button)findViewById(R.id.button);

        GifWebView view = new GifWebView(this, "file:///android_asset/watg.gif");

        view.getSettings().setLoadWithOverviewMode(true);
        view.getSettings().setUseWideViewPort(true);

        setContentView(view);

        userfield.bringToFront();
        userfield.invalidate();
        passfield.bringToFront();
        passfield.invalidate();
        logbtn.bringToFront();
        logbtn.invalidate();

    }

As you can see the GifWebView (extends WebView) is created programatically after the onCreate()

The button and EditTexts are made in the Activity.xml but are brought to front programatically in the Java file.

Why doesnt the above code work?

EDIT - Frame Layout Problem

My XML Code

<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>

The Error:

enter image description here

Greg Peckory
  • 7,700
  • 21
  • 67
  • 114

1 Answers1

1

You are calling setContentView(R.layout.activity_main) and again later setContentView(view). setContentView will overwrite the layout, this will in effect, replace it with a new layout. I suggest using a FrameLayout in XML file... Webview will be your bottom layer. On top of that Put a Linear/RelativeLayout with the Edittexts and Button... Simple and clean.

You can add your webview in XML like:

<com.package.GifWebView android:id="@+id/GWebView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>

In code, access this as:

GifWebView gWView = (GifWebView) findViewById(R.id.GWebView);
gWView = new GifWebView(this, "file:///android_asset/watg.gif");
Arjun Mathew Dan
  • 5,240
  • 1
  • 16
  • 27
  • Makes sense. But my webView is not created inside the xml file. So how would I do this. Notice I am using GifWebView, not WebView – Greg Peckory Jun 24 '15 at 13:17
  • Thanks for the detailed answer, I will EDIT my answer to show you the problem I am having with Frame Layout – Greg Peckory Jun 24 '15 at 13:40
  • hi, i need to see your GifWebView class contents... then i can test.. BTW, u can unaccept the answer for now as this question is unanswered still.. :-) – Arjun Mathew Dan Jun 25 '15 at 08:08
  • I have made some progress/changes. See here for all code: http://stackoverflow.com/questions/31045159/extending-a-custom-layout-class-constructor-never-used – Greg Peckory Jun 26 '15 at 22:39