Reputation: 8068
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:
Upvotes: 0
Views: 1046
Reputation: 5298
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");
Upvotes: 1