Android: How to open the webview in a new screen - android

I've got four Image Buttons in screen1.xml.
I've defined the webview in screen3.xml.
If i click the ImageButton1 or other buttons, the repective URL (in webview) should load in screen3.xml.
I tried to do this, but it throws forceclose error. But If I try the same this with webview defined in screen1.xml, it works. But the problem is the Image Buttons and the webview appears in the same page and they are overlapped which looks awful!
Please help me out to open the webview in a new screen.
BTW, can addView be used for this purpose?
public class click extends Activity
{
private WebView webView;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.screen1);
webView = (WebView) findViewById(R.id.web_view);
ImageButton i1 = (ImageButton) findViewById(R.id.imagebutton1);
ImageButton i2 = (ImageButton) findViewById(R.id.imagebutton2);
ImageButton i3 = (ImageButton) findViewById(R.id.imagebutton3);
ImageButton i4 = (ImageButton) findViewById(R.id.imagebutton4);
}
private void openBrowser1()
{
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("myurl");
}
public void myClickHandler1(View view)
{
openBrowser1();
}
Screen1.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="#+id/web_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0" />
<ImageButton android:id="#+id/imagebutton1" android:clickable="true" android:onClick="myClickHandler1"
android:layout_width="130dip" android:background="#null" android:layout_height="130dip" android:layout_x="21dip"
android:layout_y="61dip" ></ImageButton>
<ImageButton android:id="#+id/imagebutton2" android:clickable="true" android:onClick="myClickHandler2"
android:layout_width="130dip" android:background="#null" android:layout_height="130dip" android:layout_x="171dip"
android:layout_y="61dip" ></ImageButton>
<ImageButton android:id="#+id/imagebutton3" android:clickable="true" android:onClick="myClickHandler3"
android:layout_width="130dip" android:background="#null" android:layout_height="130dip" android:layout_x="21dip"
android:layout_y="241dip" ></ImageButton>
<ImageButton android:id="#+id/imagebutton4" android:clickable="true" android:onClick="myClickHandler4"
android:layout_width="130dip" android:background="#null" android:layout_height="130dip" android:layout_x="171dip"
android:layout_y="241dip" ></ImageButton>
</AbsoluteLayout>
screen3.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView
android:id="#+id/web_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0" />
</AbsoluteLayout>

I think you need some fix to your code...
public class click extends Activity {
private WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen1);
webView = (WebView) findViewById(R.id.web_view);
ImageButton i1 = (ImageButton) findViewById(R.id.imagebutton1);
ImageButton i2 = (ImageButton) findViewById(R.id.imagebutton2);
ImageButton i3 = (ImageButton) findViewById(R.id.imagebutton3);
ImageButton i4 = (ImageButton) findViewById(R.id.imagebutton4);
WebSettings webSettings = webView.getSettings();
webSettings.setSavePassword(false);
webSettings.setSaveFormData(false);
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(false);
webView.loadUrl("http://www.reforma.com");
//Add a listener to ImageButton1
i1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openBrowser1();
}
});
}
private void openBrowser1()
{
//this intent is used to open other activity wich contains another webView
Intent intent = new Intent(click.this,SecondActivity.class);
startActivity(intent);
}
}
screen1.xml
<WebView
android:id="#+id/web_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ImageButton android:id="#+id/imagebutton1" android:clickable="true"
android:layout_width="130dip" android:layout_height="130dip" android:layout_x="21dip"
android:layout_y="61dip" ></ImageButton>
<ImageButton android:id="#+id/imagebutton2" android:clickable="true"
android:layout_width="130dip" android:layout_height="130dip" android:layout_x="171dip"
android:layout_y="61dip" ></ImageButton>
<ImageButton android:id="#+id/imagebutton3" android:clickable="true"
android:layout_width="130dip" android:layout_height="130dip" android:layout_x="21dip"
android:layout_y="241dip" ></ImageButton>
<ImageButton android:id="#+id/imagebutton4" android:clickable="true"
android:layout_width="130dip" android:layout_height="130dip" android:layout_x="171dip"
android:layout_y="241dip" ></ImageButton>
</AbsoluteLayout>
Add a new activity wich contains your webview where the webpage should be load!.
public class SecondActivity extends Activity {
private WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen3);
webView = (WebView) findViewById(R.id.web_view);
WebSettings webSettings = webView.getSettings();
webSettings.setSavePassword(false);
webSettings.setSaveFormData(false);
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(false);
webView.loadUrl("http://www.reforma.com");
}
}
screen3.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView
android:id="#+id/web_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</AbsoluteLayout>
Add to your AndroidManifest.xml your second Activity
<activity android:name=".SecondActivity"
android:label="SecondActivity"/>
and you must have Internet permission to load the web page...
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
any doubts???
Jorgesys

It doesn't look like you're inflating and loading the screen3.xml layout anywhere, so attempts to reference the WebView it contains will probably generate an exception. Sounds like what you really want to do is define another Activity for the webview layout and swap to it when the user pushes one of the buttons. See the 2nd part of the Notepad sample for a description of how to add additional Activities to an application and swap between them:
http://developer.android.com/resources/tutorials/notepad/notepad-ex2.html

Related

how to show a website within the same activity?

I have created an Application where on Button click the Website should open within the same Activity.
Button 1 -> should load website = >"google"
Button 2 -> should load website = >"google play"
Button 3 -> should load website = >"you tube"
This is How i want it to look: https://i.stack.imgur.com/yzDF9.png
Sample code of activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/linear1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<ScrollView
android:id="#+id/Srcollview1"
android:layout_width="100dp"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="program 1"
></Button>
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="program 2"
></Button>
<Button
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="program 3"
></Button>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webviewing"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</WebView>
</LinearLayout>
</LinearLayout> </LinearLayout>
Sample code for MainActivity.java
Button b1,b2,b3;
WebView webView;
LinearLayout l1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.btn1);
b2 = (Button)findViewById(R.id.btn2);
b3 = (Button)findViewById(R.id.btn3);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
webView = (WebView) findViewById(R.id.webviewing);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.google.com");
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
webView = (WebView) findViewById(R.id.webviewing);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://play.google.com/");
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
webView = (WebView) findViewById(R.id.webviewing);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.youtube.com/");
}
});
}
Your code works fine, except you set the listener for b2 twice. Typo maybe?
If you are facing connection errors, just add the Internet permission to AndroidManifest.xml.
<uses-permission android:name="android.permission.INTERNET"/>

Issue with using an image button in Android Studio

I'm creating a Soundboard app which uses buttons currently to make different sounds. However I want to change the buttons to image buttons, having tried this I get the following error:
Unexpected Cast to ImageButton: Layout tag was imageView.
I've tried looking around the internet but can't find a fix. The code in the java class looks like this.
public class SecondActivity extends AppCompatActivity {
Button b,b2;
MediaPlayer nice,burp,fancy;
ImageButton IMG;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
b = (Button) findViewById(R.id.button);
b2 = (Button) findViewById(R.id.button3);
nice = MediaPlayer.create(this, R.raw.nice);
burp = MediaPlayer.create(this, R.raw.burp);
fancy = MediaPlayer.create(this, R.raw.fancy);
configureImageButton();
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
nice.start();
}
}
);
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
fancy.start();
}
}
);
}
private void configureImageButton() {
ImageButton IMG = (ImageButton) findViewById(R.id.IMG);
IMG.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
burp.start();
}
}
);
}
The XML code looks like this:
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.oliver.olliessoundofmusic.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nice"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fancy"
android:id="#+id/button3"
android:layout_alignTop="#+id/button"
android:layout_toEndOf="#+id/button" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/IMG"
android:background="#drawable/robert"
android:layout_toEndOf="#+id/button3" />
</RelativeLayout>
I just want imageButtons to work with action listeners the same way a normal button would, to make the app more visually appealing.
Thanks in advance.
In your xml change ImageView tag to ImageButton
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nice"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fancy"
android:id="#+id/button3"
android:layout_alignTop="#+id/button"
android:layout_toEndOf="#+id/button" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/IMG"
android:background="#android:color/transparent"
android:src="#drawable/robert"
android:layout_toEndOf="#+id/button3" />
Please notice that I made the background to a transparent color, and made the drawable to be in src property
Cast this to an ImageView instead, then since that is what you have in the XML
ImageButton IMG = (ImageButton) findViewById(R.id.IMG);
The OnClickListener will still work as expected.
Also, beware of variable shadowing. You'd want line this instead.
IMG = (ImageButton) findViewById(R.id.IMG);
Declare IMG as ImageView in you activity or use imagebutton widget in the layout file.

How can I enable jumping to linked page if user clicks TextView?

XML Layout
<LinearLayout style="#style/behindMenuScrollContent"
android:paddingTop="25dp" >
<TextView
style="#style/behindMenuItemTitle"
android:text="People" />
<LinearLayout
android:id="#+id/iconViewLink4"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<jp.fureco.IconView
android:id="#+id/iconViewItem4"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:textSize="20dp"/>
<TextView
android:layout_marginLeft="10dp"
style="#style/behindMenuItemLabel"
android:text="Visitor" />
</LinearLayout>
</LinearLayout>
Then I have myWebView just like this
WebView myWebView = (WebView)findViewById(R.id.webView1);
When a user clicks on iconViewLink4, I want it jump to the url "http://example.com/messsage"
How can I code that?
Yes you can. Just use android:onClick="methodName" in <LinearLayout> or add it programatically by using setOnClickListener like
LinearLayout linLay = (LinearLayout)findViewById(R.id.iconViewLink4);
linLay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// the method or code that will take control to your URL
}
}
Make it clickable and give it an OnClickListener (you can do this on any View)
<LinearLayout
android:id="#+id/iconViewLink4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true">
findViewById(R.id.iconViewLink4).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something here
}
});

How to setting listener for button inside the Crouton Toast and clear that Crouton Toast using that button?

I have an app, which shows toast below the actionbar. I'm using Crouton library for displaying toast. Here I'm placing custom layout for toast, that layout contains one textview and one button (for close option). So Toast appears with button. If I click that it should close the toast but nothing happens.. Below Code for this example. Any Help Appreciated
MainActivity.java
public class MainActivity extends SherlockActivity implements OnClickListener {
private View customToastView;
private TextView customToastMessageView;
private Button customToastCloseButton;
private Button doneButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
doneButton = (Button) findViewById(R.id.done_button);
customToastView = getLayoutInflater().inflate(R.layout.toast_custom_layout, null);
customToastMessageView = (TextView) customToastView.findViewById(R.id.messages);
customToastCloseButton = (Button) customToastView.findViewById(R.id.close_button);
customToastCloseButton.setOnClickListener(this);
doneButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.done_button:
Crouton.cancelAllCroutons();
customToastMessageView.setText("Message");
Crouton.show(this, customToastView);
break;
case R.id.close_button:
Crouton.cancelAllCroutons();
break;
}
}
}
toast_custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/messages"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="26dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/close_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="23dp"
android:text="Button" />
</RelativeLayout>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/done_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="146dp"
android:text="Button" />
</RelativeLayout>

Webview is displaying its content below the main layout

i have created a webview for my android application , inicially i am tying to take application simpler as possible to understand things . i have created a webview to display the different page in it . i simply created a button which load url into webview which is working perfectly fine ,but the webview is loading the urls below the button . it is not overriding the main layout or not opening the url in newpage. i see the webpage in webview and the button above it . how can get rid of this button. thanks in advance. here is my java and xml code
activity
public class TestinglinkActivity extends Activity {
final Activity activity = this;
WebView webview;
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new MyWebViewClient());
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
Button btn_login = (Button) findViewById(R.id.btn_click_login);
btn_login.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
webview.loadUrl("http://www.google.com.pk");
webview.goBack();
webview.goForward();
}
}
);
}
}
xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:isScrollContainer="true"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarStyle="outsideInset"
android:scrollbars="vertical"
android:scrollbarSize="10dp"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/btn_click_login"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="2"
android:text="GO"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_marginRight="5dp"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:lines="4"
/>
<ImageView android:id="#+id/imageBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:padding="5dp"
android:contentDescription="#string/hello"
android:layout_alignParentLeft="true"
/>
</RelativeLayout>
<TextView
android:id="#+id/date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:gravity="center_vertical"
android:lines="4"
/>
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</ScrollView>
It sounds to me like you want to start a new Activity when the button is pressed. The reason your WebView appears below the other layout is because that's exactly what your XML describes. If you want your WebView to appear full screen, you need to move the WebView to a separate Activity which is started when your button is clicked.
You probably will want to pass the URL in the Extra of the Intent used to start the new Activity.

Categories

Resources