i'm trying to make a webview app which can display two website in one screen. but when i run my app it display one website and open on new browser. how can i display two website and it doesn't work on new browser?
this is my WebViewActivity class:
package com.mkyong.android;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class WebViewActivity extends Activity {
private WebView webView1, webView2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView1 = (WebView) findViewById(R.id.webView1);
webView1.getSettings().setJavaScriptEnabled(true);
webView1.loadUrl("http://www.google.com");
webView2 = (WebView) findViewById(R.id.webView2);
webView2.getSettings().setJavaScriptEnabled(true);
webView2.loadUrl("http://www.google.com");
}
}
this is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView
android:id="#+id/webview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.39" />
<WebView
android:id="#+id/webview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.53" />
</LinearLayout>
I suggest you use framelayout and try to set webviewClient to your webview
this function shouldOverrideUrlLoading (in the declaration of the webviewClient)
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
Log.i("Page", url);
view.setWebViewClient(new MyWebViewClient());
view.setWebChromeClient(new MyWebChromeClient());
view.loadUrl(url);
return true;
}
will override url loaded by the webview and force it to be loaded in the webview not in the new browser
Related
I want to use Data Binding Library & ViewModel
to identify position of clicked item in List.
To know on which view the click happened so that I can trigger different Intent.
Here is my XML file (Currently onclick is on RelativeLayout)
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="orgmembvm"
type="com.support.android.designlibdemo.include.OrgMemb" />
<variable name="orgmembhandlers"
type="com.support.android.designlibdemo.View.MyOrgMembHandlers"/>
</data>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp">
<RelativeLayout
android:id="#+id/org_memb_rec"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="#{orgmembhandlers::onClickOpenEditRec}"
android:padding="8dp">
//Inner Child element
</RelativeLayout>
BindViewHolder of Adaptor
#Override
public void onBindViewHolder(#NonNull OrgMembViewHolder holder, int
position) {
holder.mBinding.setOrgmembvm(orgMembArrayList.get(position));
holder.mBinding.setOrgmembhandlers(new MyOrgMembHandlers());
holder.mBinding.executePendingBindings();
}
Code in Activity Class for getting viewModel attached to Activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
......
......
userModel = ViewModelProviders.of(this).get(OrgMembViewModel.class);
userModel.getAllLive().observe(this, orgMembList -> {
mAdapter.updateorgmemblist(orgMembList);
});
}
How can this be achieved?
1. Take position variable in layout
<variable
name="position"
type="int"/>
2. set position from adapter
#Override
public void onBindViewHolder(#NonNull OrgMembViewHolder holder, int position) {
holder.mBinding.setPosition(position);
}
3. Put int position in onClickOpenEditRec param
public class MyOrgMembHandler {
public void onClickOpenEditRec(int position) {
}
}
4. Pass position from layout
android:onClick="#{()->orgmembhandlers.onClickOpenEditRec(position)}"
Check this answer to see ways of setting click in data binding.
I have two Fragments. First Fragment is named as Fragment1 and Second Fragment named as physics. First Fragment have Two Buttons. I would Like to show a local html page ( say A.html when Button1 is pressed and B.html when Button2 is Pressed ) in Second Fragment i.e in physics.java
My Fragment.java contains following code
package com.nepalpolice.mnemonics;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* Created by Sagar on 2017/09/23.
*/
public class Fragment1 extends Fragment implements View.OnClickListener{
public Button button;
public Fragment1() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
button = (Button) view.findViewById(R.id.button1);
button = (Button) view.findViewById(R.id.button2);
return view;
}
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity().getApplicationContext(), physics.class);
switch (v.getId()) {
case R.id.button1:
intent.putExtra("url", "file:///android_asset/B.html");
startActivity(intent);
break;
case R.id.button2:
intent.putExtra("url", "file:///android_asset/A.html");
startActivity(intent);
break;
default:
break;
}
}
}
and My SecondFragment contains following code
package com.nepalpolice.mnemonics;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
/**
* Created by Sagar on 2017/09/23.
*/
public class physics extends Fragment {
WebView myWebView;
public physics() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_fragment2, container, false);
String url = getActivity().getIntent().getStringExtra("url");
myWebView=(WebView)rootView.findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl(url);
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
return rootView;
}
}
and main fragment_fragment1.xml contains
<FrameLayout 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"
tools:context="com.nepalpolice.mnemonics.Fragment1">
<LinearLayout 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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignParentTop="true"
android:text="button01"
android:onClick="onClick"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignParentTop="true"
android:text="button02"
android:onClick="onClick"/>
</LinearLayout>
</FrameLayout>
and Secondfragment.xml has
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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" >
<WebView
android:id="#+id/webview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
But my is Crashing with following Error.
java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button1'
I'm almost Done with my project. If it could be done...It would mean a lot for me.
I have created widget. I am trying to update the widget in background each 5 seconds (For testing purpose 5 second). But it is not getting update.Is any mistake on my code.
Provider class
package com.widget.widgetapplication;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
import java.util.Random;
public class MyWidget extends AppWidgetProvider {
private int randomNumber = 0;
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Toast.makeText(context, "Receive", Toast.LENGTH_SHORT).show();
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
Toast.makeText(context, "Update", Toast.LENGTH_SHORT).show();
// Get all ids
ComponentName thisWidget = new ComponentName(context,
MyWidget.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
// create some random data
randomNumber = new Random().nextInt(100);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
Log.w("WidgetExample", String.valueOf(randomNumber));
// Set the text
remoteViews.setTextViewText(R.id.time, String.valueOf(randomNumber));
appWidgetManager.notifyAppWidgetViewDataChanged(widgetId, R.id.time);
// Register an onClickListener
Intent intent = new Intent(context, MyWidget.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.watch, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
}
widget info xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minHeight="100dp"
android:minWidth="100dp"
android:previewImage="#drawable/analog_watch"
android:initialLayout="#layout/widget_layout"
android:updatePeriodMillis="5000">
</appwidget-provide
Registered receiver in manifest
<receiver android:name=".MyWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="#xml/widget_info"/>
</receiver>
Widget layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent">
<AnalogClock
android:id="#+id/watch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:textStyle="bold"
android:text="Time is : "
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"/>
</LinearLayout>
YES. you are doing it wrong.
the minimum available value for updatePeriodMillis is 30min (1800000 millis).
official document :
Note: Updates requested with updatePeriodMillis will not be delivered more than once every 30 minutes.
I have a listview in which I have a textview, an image and a button in a row in Xamarin using c#. by clicking on button I need to open a new layout:
this is axml for listview: called "Friends"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="378.2dp"
android:id="#+id/listView1" />
</LinearLayout>
this is axml for each row in listview: called "Row1"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="81.1dp"
android:id="#+id/linearLayout1">
<ImageView
android:src="#drawable/Icon"
android:layout_width="91.7dp"
android:layout_height="match_parent"
android:id="#+id/imageView5" />
<TextView
android:text="Text"
android:layout_width="184.9dp"
android:layout_height="35.6dp"
android:id="#+id/textView1"
android:gravity="center"
android:textSize="20dp" />
<Button
android:text="Chat"
android:layout_width="62.3dp"
android:layout_height="37.8dp"
android:id="#+id/chat1"
android:layout_marginTop="32dp" />
</LinearLayout>
</LinearLayout>
and this is the activity for listview: called "FriendsActivity"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace EJIK_Network
{
[Activity (Label = "FriendsActivity")]
public class FriendsActivity : Activity
{
ListView listview;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.Friends);
listview = FindViewById<ListView> (Resource.Id.listView1);
View rootInAnotherLayout = this.LayoutInflater.Inflate (Resource.Layout.Row1,null);
Button chat = rootInAnotherLayout.FindViewById<Button> (Resource.Id.chat1);
chat.Click += (sender, e) => {
var CA = new Intent(this, typeof(ChatingActivity));
StartActivity (CA);
};
}
}
}
The cod run but when I clicked on chat button it didn't open the new layout wich I defined in "ChatingActivity"??????
You have to create a Custom Adapter and inside the GetView method:
public override View GetView(int position, View convertView, ViewGroup parent) {
var view = (convertView ??
this._context.LayoutInflater.Inflate(
Resource.Layout.Row1,
parent,
false)) as LinearLayout;
Button chat = view.FindViewById<Button> (Resource.Id.chat1);
chat.Click += (sender, e) => {
var CA = new Intent(this, typeof(ChatingActivity));
StartActivity (CA);
};
}
So clicking on the chat button of each list item, the ChatingActivity will start.
I have 2 layouts,in first layout I have EditText,Button and in second layout I have TextView. What I want is when i write something in EditText to copy that in second layout when you press button,but i get crash every time. Is there any way to use components from two layouts or i am missing something. If this is imposible another way would be good. Thx
main activity
public void onCreate(Bundle ivansad) {
super.onCreate(ivansad);
setContentView(R.layout.activity_main);
Button Start = (Button) findViewById(R.id.bStart);
Start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.numbers);
Intent Changeto2 = new Intent (MainAct.this, Numgen.class);
startActivity(Changeto2);
}
});
}
second activity
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.numbers);
Button Next = (Button)findViewById(R.id.bNext);
final EditText text = (EditText)findViewById(R.id.broj1);
final TextView edit = (TextView)findViewById(R.id.lb1);
Next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
edit.setText(text.getText());
}
});
}
First layout
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 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"
tools:ignore="Deprecated"
android:background="#drawable/grey_bg" >
<Button
android:id="#+id/bNext"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_x="-1dp"
android:layout_y="405dp"
android:background="#000000"
android:text="Dalje"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
<EditText
android:textColor="#ffffff"
android:id="#+id/broj1"
android:layout_width="154dp"
android:layout_height="50dp"
android:layout_x="164dp"
android:layout_y="6dp"
android:background="#drawable/text_bg"
android:ems="10"
android:gravity="center"
android:hint="1-49"
android:inputType="number"
tools:ignore="HardcodedText" >
</AbsoluteLayout>
Second Layout
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 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:background="#drawable/grey_bg"
android:orientation="vertical">
<TextView
android:id="#+id/lb1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_x="80dp"
android:layout_y="107dp"
android:gravity="center"
android:text=" "
android:textSize="18sp"
tools:ignore="HardcodedText" />
</AbsoluteLayout>
What you want to do is have two separate activities, one to edit the text, and one to display it.
The first one you want to set the layout to layout_main, and set a callback for the button. Inside the callback you want:
final EditText text = (EditText)findViewById(R.id.broj1);
Intent intent = new Intent(MainAct.this, NumGen.class);
Bundle bundle = new Bundle();
bundle.putString("mString",text.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
Then inside your second activity (NumGen?) you can get that string you saved by doing this:
Bundle data = getIntent().getExtras();
String mString="";
if(data.containsKey("mString")){ mString=data.getString("mString"); }
One of the main issues it looks like you might be having is the ids defined in your XML files aren't the same as the ids in your code. After that, read up on how Activities work and pass information.