Browsing the Android category

com.android.ddmlib.AdbCommandRejectedException: device unauthorized. This adb server’s $ADB_VENDOR_KEYS is not set Try ‘adb kill-server’ if that seems wrong.

I got the above error in Android Studio when trying to put my app on a real phone.

The solution: unplug and re-plug the phone back in.  There will be a prompt for “Always trust this computer?”  Check “yes” and then proceed.  I guess I somehow avoided that dialog box the first time I plugged it in.

Read the article

android cannot resolve method ‘startactivity(android.content.intent)’

I had a webview in Android and I was trying to have it open links in a new browser window. I was getting the error:

android cannot resolve method ‘startactivity(android.content.intent)’

 

The solution:

I added the part in bold:

 view.getContext().startActivity(intent);

 

View complete source code on GitHub

Read the article

Android Webview sample code

Modified slightly from my previous post on “Basic Android Studio webview example” – this removes the “bounce effect”, which I was able to turn off with:

webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);

 

MainActivity.java

activity_main.xml

AndroidManifest.xml

Read the article

Do things right the first time

I used Adobe’s PhoneGap to make my initial apps – and it was great – but I got an email from Google, saying I had a vulnerable version of Apache Cordova.  Apple had emailed me about a year ago with a similar message (at the time, Google didn’t).  So I had to re-do my apps then (in PhoneGap).  Thinking this might happen again,  I decided to do it in Android Studio.  Lesson learned: don’t take shortcuts!

Here’s my new Android Studio template to display a basic website.  This accounts for NOT displaying the navigation/address bar.

Now, load the local file.

Android Studio: right click on “app” and hit “Show in Explorer”.  In the “app” folder – go to “src” then “main”.  You’ll see a folder for “java” and “res”.  Make a folder “assets”

http://screenshots.stevesohcot.com/1379l

http://screenshots.stevesohcot.com/15307

Place all the HTML/CSS files in there.

Then when you load up the page, use:

webview.loadUrl(“file:///android_asset/index.html”);

 

I was using jQuery mobile, and there was an error.  I opened it up locally on my computer (Windows, using Chrome), and saw:

Uncaught SecurityError: Failed to execute ‘replaceState’ on ‘History’: A history state object with URL

Solution was found on  Stack Overflow .  Then I had another issue:

Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

I guess the “real” solution would be to make the website on a remote server, but I didn’t want to do that.  Instead I just changed all my modals (that was apparently using Ajax to display content) into:

<a href=”” onClick=”window.location=’…'”>link</a>

Remember to move over the icons (“show in Explorer” for “mipmap” and place the icons – individually – in there, renaming where applicable).

The other downside from having to convert from PhoneGap to my own version was there’s no longer a splash screen. I previously wrote about it when you’re calling a remote page, but I’m sure there’s a better way for local files.

Read the article

Basic Android Studio webview example

This is (another) example of a basic Android Studio webview example.  It won’t show the navigation/address bar.

 

MainActivity.java

package com.diamondium.conjecture;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {

private WebView webview ;
@Override

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

webview =(WebView)findViewById(R.id.webView);

webview.setWebViewClient(new WebViewClient());
webview .getSettings().setJavaScriptEnabled(true);
webview .getSettings().setDomStorageEnabled(true);
webview.loadUrl(“http://www.google.com”);
}

}

 

activity_main.xml

<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”
tools:context=”.MainActivity”>

<WebView
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:id=”@+id/webView”
android:layout_alignParentTop=”true”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_alignParentBottom=”true”
android:layout_alignParentRight=”true”
android:layout_alignParentEnd=”true” />

</RelativeLayout>

 

And in the manifest.xml file, add in:

<uses-permission android:name=”android.permission.INTERNET” />

As a child to manifest (I put it above “application”).

Read the article

I just switched from an Android to an iPhone

The first thing I wanted to do was set up my Contacts.  I had managed my contact in Gmail/Google Contacts.  I put people into “Groups” (labels).  I ignored the “My Contacts” (default label) and created two custom ones: “Show” and “Hide”.  Obviously “Show” is people I want to see in my phone; “Hide” is for people who I want to keep their number, incase they text me it would display a name – but I don’t want to see those people all the times when I compose a new message / make a phone call.
I couldn’t figure it out.  I ended up re-setting my iPhone because in the “setup” process  there is a step for “Import from Android” (that I missed seeing the first time).
What I ended up doing was going into the Google Contact Manager and exporting two separate lists in the vCard format.  Then (on my dekstop), I went to iCloud and create the two groups, and imported the two lists (separately).
Then in the iPhone settings, I went into Contacts and will manage which group appears under “iCloud” – not under “Gmail.”

Read the article

android studio load local html in webview

The trick was in Explorer (I’m on a Windows computer), navigate to your project’s path, then go to: app\src\main

In the “Main” folder, create a folder “assets” (plural).  Then in that folder, put the HTML content.

My MainActivity.java is as follows:

package com.diamondium.keepintouch;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {

private WebView webview;

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

webview =(WebView)findViewById(R.id.webView);
webview.setWebViewClient(new WebViewClient());
webview .getSettings().setJavaScriptEnabled(true);
webview .getSettings().setDomStorageEnabled(true);
webview.loadUrl(“file:///android_asset/index.html”);
}
}

Read the article

android webview screen goes blank when loading new page

For my Android app, where it’s basically just a webview, the screen was going blank when a new page was loading.

The cause was that I had a splash screen in the Android app that was toggling the visibility of the entire webview.
The solution: add a variable to detect the “first time it’s run” – and only set the visibility on the first time.

public class MainActivity extends Activity {
    String ShowOrHideWebViewInitialUse = “show”;
    private WebView webview ;
…..
private class MyWebViewClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView webview, String url, Bitmap favicon) {
            // only make it invisible the FIRST time the app is run
            if (ShowOrHideWebViewInitialUse.equals(“show”)) {
                webview.setVisibility(webview.INVISIBLE);
            }
            super.onPageStarted(webview, url, favicon);
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            ShowOrHideWebViewInitialUse = “hide”;
…..

 

Read the article