The button works when its alone without the WebView but once I add that only the WebView works and clicking the button does nothing.
Here's my MainActivty.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.myWebView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://");
}
Intent intentCall;
public void onClick(View v){
intentCall.setAction("android.intent.action.CALL");
intentCall.setData(Uri.parse("tel:"));
startActivity(intentCall);
finish();
}}
Here's my Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.yellowribbon.evindrake.yellowribbonv3" >
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Activity_Main.xml
<?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:id="#+id/activity_main"
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="org.yellowribbon.evindrake.yellowribbonv3.MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/myWebView"
android:layout_alignParentBottom="true" />
<Button
android:text="#string/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Push-buttons can be pressed, or clicked, by the user to perform an action!
A typical use of a push-button in an activity would be the following:
.
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
}
}
However, instead of applying an OnClickListener to the button in your activity, you can assign a method to your button in the XML layout, using the android:onClick. You are trying to achieve that without this attribute!
Now, when a user clicks the button, the Android system calls the activity's myClickMethod(View)(that i created) method. In order for this to work, the method must be public and accept a View as its only parameter. For example:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickMethod"
android:text="button" />
and in code
public void myClickMethod(View view) {
// Perform action on click after identify the view
}
Also you are getting a null pointer because of Intent intentCall
use your onClick(View v) like below,
public void onClick(View v) {
String number = "18002738255";
Uri call = Uri.parse("tel:" + number);
Intent surf = new Intent(Intent.ACTION_CALL, call);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(surf);
}
Add code to inside button component in activity_main.xml file
android:onClick="onClick"
Related
I've used AlarmManager ,so it works every minute
every minute, service class is called
and it keeps working even after it's turned off
it's got a sound in the back of the device
the idea works when i run the app inside Android Studio on device 5.1.1
but i noticed when i disconnected the device from the cable.
alerts are irregular and audio works
other times.
//
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(getApplicationContext(), RingAlarm.class);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),
12345, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime(),
1 * 60 * 100 , pendingIntent);
}
}
// ---
public class RingAlarm extends Service {
MediaPlayer sound2;
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getApplicationContext(), "الحمد لله", Toast.LENGTH_LONG).show();
sound2 = (MediaPlayer) MediaPlayer.create(RingAlarm.this,R.raw.a2);
sound2.start();
return START_STICKY;
}
#Override
public IBinder onBind(Intent arg0){
// TODO Auto-generated method stub
return null;
}
}
//
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ahmedco.testcode">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".RingAlarm" />
</application>
</manifest>
this is an example to take the picture.
Why button 3 does not INVISIBLE after shooting?
What is the correct way to hide the button after taking a picture?
Please help with thanks.
Note: I TRY IN galaxy 3 mini (18190) >> does not work
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
public class MainActivity extends ActionBarActivity {
private static final int ACTIVITY_START_CAMER_APP = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void showButton1(View view) {
EditText editText1 = (EditText) findViewById(R.id.editText);
editText1.setText("1", TextView.BufferType.EDITABLE);
showhideButton();
}
public void hideButton2(View view) {
EditText editText1 = (EditText) findViewById(R.id.editText);
editText1.setText("0", TextView.BufferType.EDITABLE);
showhideButton();
}
public void showhideButton0(View view) {
showhideButton();
}
public void showhideButton() {
EditText editText1 = (EditText) findViewById(R.id.editText);
Button button3 = (Button) findViewById(R.id.button3);
int val = Integer.parseInt(editText1.getText().toString().trim());
if (val == 1) {
Toast.makeText(this, "hide Button3", Toast.LENGTH_SHORT).show();
button3.setVisibility(View.INVISIBLE);}
else if (val ==0) { Toast.makeText(this, "show Button3", Toast.LENGTH_SHORT).show();
button3.setVisibility(View.VISIBLE);
}
}
public void takePhoto(View view) throws IOException {
Intent callCameraApplicationIntent = new Intent();
callCameraApplicationIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException e) {
e.printStackTrace();
}
callCameraApplicationIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(callCameraApplicationIntent, ACTIVITY_START_CAMER_APP);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
EditText txt4 = (EditText) findViewById(R.id.editText);
if (requestCode == ACTIVITY_START_CAMER_APP && resultCode == RESULT_OK) {
txt4.setText("1", TextView.BufferType.EDITABLE);
}
else { return;}
showhideButton();
}
File createImageFile() throws IOException {
File folder = new File(Environment.getExternalStorageDirectory() + "/image1");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
File folder2 = new File(Environment.getExternalStorageDirectory() + "/image1/Original_Images");
boolean success2 = true;
if (!folder2.exists()) {
success2 = folder2.mkdir();
}
File image = new File(Environment.getExternalStorageDirectory() + "/image1/Original_Images/1.jpg");
return image;
}
}
<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">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="show Button3"
android:id="#+id/button1"
android:layout_alignParentBottom="true"
android:layout_marginBottom="122dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="hideButton2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hide Button3"
android:id="#+id/button2"
android:layout_above="#+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="showButton1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button3"
android:id="#+id/button3"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_below="#+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="58dp"
android:inputType="number"
android:text="0" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="takePhoto"
android:id="#+id/button"
android:onClick="takePhoto"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="showhideButton"
android:id="#+id/button4"
android:onClick="showhideButton0"
android:layout_above="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
I'm getting the following error in Android Studio logcat:
I/chromium﹕ [INFO:CONSOLE(1)] "Uncaught ReferenceError: callJS is not defined"
I have researched on stackoverflow and tried the suggested answers on similar post with no luck. here
I'm trying to execute javascript from a local html file that is loaded into a WebChromeClient by clicking on a android button. The code is taken from the Chapter 10 example in The Pragmatic Programmers "Hello Android Introducing Google's Mobile development Platform" fourth Edition book.
index.html
<html>
<head>
<script language="JavaScript">
function callJS(arg){
document.getElementById('replaceMe').innerHTML = arg;
{
</script>
</head>
<body>
<h2>WebView</h2>
<p>
Display JavaScript alert
</p>
<p>
Call Android from JavaScript
</p>
<p id="replaceMe"></p>
</body>
</html>
MainActivity
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String TAG = "LocalBrowser";
private final Handler handler = new Handler();
private WebView webView;
private TextView textView;
private Button button;
/** Object exposed to JavaScript */
private class AndroidBridge {
#JavascriptInterface // Required in Android 4.2+
public void callAndroid(final String arg) { // must be final
handler.post(new Runnable() {
public void run() {
Log.d(TAG, "callAndroid(" + arg + ")");
textView.setText(arg);
}
});
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the Android controls on the screen
webView = (WebView) findViewById(R.id.web_view);
textView = (TextView) findViewById(R.id.text_view);
button = (Button) findViewById(R.id.button);
// Rest of onCreate follows...
// Turn on JavaScript in the embedded browser
webView.getSettings().setJavaScriptEnabled(true);
// Expose a Java object to JavaScript in the browser
webView.addJavascriptInterface(new AndroidBridge(),
"android");
// Set up a function to be called when JavaScript tries
// to open an alert window
webView.setWebChromeClient(new WebChromeClient() {
#Override
public boolean onJsAlert(final WebView view,
final String url, final String message,
JsResult result) {
Log.d(TAG, "onJsAlert(" + view + ", " + url + ", "
+ message + ", " + result + ")");
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
result.confirm();
return true; // I handled it
}
});
// Load the web page from a local asset
webView.loadUrl("file:///android_asset/index.html");
// This function will be called when the user presses the
// button on the Android side
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Log.d(TAG, "onClick(" + view + ")");
if (android.os.Build.VERSION.SDK_INT < 19) {
webView.loadUrl("javascript:callJS('Hello from Android')");
}else{
webView.evaluateJavascript("javascript:callJS('Hello from Android')", null);
}
}
});
}
}
activity_main
<LinearLayout
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="fill_parent"
android:layout_weight="1.0"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1.0"
android:padding="5sp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="#string/textView"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:text="#string/call_javascript_from_android"
android:textSize="18sp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/text_view"
android:textSize="18sp"/>
</LinearLayout>
</LinearLayout>
Any help is greatly appreciated! Thanks in advance!
I have an Android app, running a WebView that loads a certain page, and also part of the app
I want to generate one of button onclick() event inside the WebView page
How to load JavaScript file to WebView page which is inside the Android assets?
Thanks.
Finally I found the answer...
webView.loadUrl("javascript:(function(){document.getElementById('buttonClick').click();})()");
here is the complete source code
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Huddle extends Activity {
private WebView wv;
EditText editText;
TextView textView;
Button button,buttonClick;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText)findViewById(R.id.titlebar);
textView = (TextView)findViewById(R.id.txt_html);
button = (Button)findViewById(R.id.button);
buttonClick = (Button)findViewById(R.id.buttonClick);
wv = (WebView) findViewById(R.id.webview);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setUserAgentString("Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
wv.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
wv.setWebChromeClient(new MyChromeClient());
wv.loadUrl("file:///android_asset/www/index.html");
wv.requestFocus();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String txt = editText.getText().toString();
wv.loadUrl("javascript:sayHelloFromJS('"+txt+"')");
}
});
buttonClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
wv.loadUrl("javascript:(function(){document.getElementById('buttonClick').click();})()");
}
});
}
class MyJavaScriptInterface
{
#SuppressWarnings("unused")
public void showHTML(final String html)
{
runOnUiThread(new Runnable() {
#Override
public void run() {
Huddle.this.textView.setText(html);
}
});
}
}
class MyChromeClient extends WebChromeClient{
}
class MyWebClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
}
index.html
add this index.html to assets\www\index.html
<html>
<script language="JavaScript">
function sayHelloFromJS(value) {
document.getElementById("namefromAndroid").value = value;
}
function setHtml(){
var HTML = ""+document.getElementById("namefromjs").value;
window.HTMLOUT.showHTML(HTML);
}
function doAlert() {
alert("JavaScript says: Hello Android !!! How are you?");
}
</script>
</head>
<body>
<p> Enter your name here <input type="text" id="namefromjs" />
<p> <input type="button" onclick= "setHtml()" value="Set Name in Android">
<p> Name from Android is <input type="text" id="namefromAndroid" />
<p> Button Click <input type="button" value="Click me" id="buttonClick" onclick= "doAlert()" />
</body>
</html>
res\layouts\main.xml
<?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"
>
<TextView
android:layout_width="184dp"
android:layout_height="22dp"
android:id="#+id/txt_html" android:layout_gravity="left|center_vertical"/>
<EditText
android:layout_width="161dp"
android:layout_height="wrap_content"
android:id="#+id/titlebar" android:layout_gravity="left|center_vertical"/>
<Button
android:layout_width="129dp"
android:layout_height="wrap_content"
android:text="Add text to web"
android:id="#+id/button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Event"
android:id="#+id/buttonClick" android:layout_gravity="left|center_vertical"/>
<WebView android:id="#+id/webview" android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
this is for kotlin. its working for java add (;) line end
wv.loadUrl("javascript:(function() {document.querySelectorAll('.class')[0].click();})()")
I did a web to show different videos in HTML5,and the web works.
Now I would like to do a google tv app and show this web and work with it.
I try with a webView component but the part with javascript doesnt work and show like a plain text($function...) and the rest appear without any format.
This is my Activity:
public class Test extends Activity {
WebView mWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vid);
mWebView = (WebView) findViewById(R.id.web);
mWebView.getSettings().setJavaScriptEnabled(true);
if (savedInstanceState != null) {
mWebView.restoreState(savedInstanceState);
} else {
mWebView.loadUrl("http://192.168.2.103:8080/VirtualSTBJSF/vid.xhtml");
mWebView.setWebViewClient(new verMiWeb());
}
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mWebView.saveState(outState);
}
#Override
public void onStop() {
super.onStop();
mWebView.stopLoading();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
mWebView.goBack();
}
return super.onKeyDown(keyCode, event);
}
}
class verMiWeb extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
and the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
android:id="#+id/web"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Any idea?
Thanks for the answers.