Categories
discuss

Build tools update breaks my compile

I updated Android Studio 2.0 Preview from 4 to 5 this morning. If I continue to build with:

dependencies {
    classpath 'com.android.tools.build:gradle:2.0.0-alpha3'
}

this will still compile successfully. But if I move up to -alpha5 I get this error:

Error:Execution failed for task ‘:app:compileReleaseJavaWithJavac’. java.io.FileNotFoundException: C:….intermediatesexploded-aarcom.google.android.gmsplay-services8.4.0jarsclasses.jar (The system cannot find the path specified)

Answer

From now on, I think Google expects us to not use this:

'com.google.android.gms:play-services:8.4.0'

But rather add the services you need specifically. Find a list of them here

compile 'com.google.android.gms:play-services-analytics:8.4.0'
compile 'com.google.android.gms:play-services-appinvite:8.4.0'
compile 'com.google.android.gms:play-services-maps:8.4.0'
compile 'com.google.android.gms:play-services-location:8.4.0'
compile 'com.google.android.gms:play-services-plus:8.4.0'
compile 'com.google.android.gms:play-services-drive:8.4.0'

Also, you need to apply play service plugin in last line of your application’s gradle:

apply plugin: 'com.google.gms.google-services'
Categories
discuss

android data binding with a custom view

The Android data binding guide discusses binding values within an activity or fragment, but is there a way to perform data binding with a custom view?

I would like to do something like:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.mypath.MyCustomView
        android:id="@+id/my_view"
        android:layout_width="match_parent"
        android:layout_height="40dp"/>

</LinearLayout>

with my_custom_view.xml:

<layout>

<data>
    <variable
        name="myViewModel"
        type="com.mypath.MyViewModelObject" />
</data>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{myViewModel.myText}" />

</LinearLayout>

</layout>

While it appears possible to do this by setting custom attributes on the custom view, this would quickly become cumbersome if there’s a lot of values to bind.

Is there a good way to accomplish what I’m trying to do?

Answer

In your Custom View, inflate layout however you normally would and provide a setter for the attribute you want to set:

private MyCustomViewBinding mBinding;
public MyCustomView(...) {
    ...
    LayoutInflater inflater = (LayoutInflater)
        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mBinding = MyCustomViewBinding.inflate(inflater);
}

public void setMyViewModel(MyViewModelObject obj) {
    mBinding.setMyViewModel(obj);
}

Then in the layout you use it in:

<layout xmlns...>
    <data>
        <variable
            name="myViewModel"
            type="com.mypath.MyViewModelObject" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.mypath.MyCustomView
            android:id="@+id/my_view"
            app:myViewModel="@{myViewModel}"
            android:layout_width="match_parent"
            android:layout_height="40dp"/>

    </LinearLayout>
</layout>

In the above, an automatic binding attribute is created for app:myViewModel because there is a setter with the name setMyViewModel.

Categories
discuss

Changing zoom in D3.js from mousewheel to control+mousewheel

From this question I know how to disable mousewheel zoom in D3.js. How can I remap it to holding control button down plus mousewheel? Thanks.

Answer

I found a solution that does not need to modify the D3.js source.

I based my solution on this question: How to temporarily disable the zooming in d3.js

Instead of clicking on a button to enable/disable zooming, I used keydown/keyup events:

svg.call(zoom = d3.behavior.zoom().on('zoom', redrawOnZoom)).on('dblclick.zoom', null);

var zooming = false;

function redrawOnZoom() {
  if (zooming) {
    svgContainer.attr('transform', 'translate(' + zoom.translate() + ')' + ' scale(' + zoom.scale() + ')');
  }
};

d3.select("body").on("keydown", function () {
    zooming = d3.event.ctrlKey;
});

d3.select("body").on("keyup", function () {
     zooming = false;
});

Here is a complete fiddle: https://jsfiddle.net/tabacof/dcbor8eL/3/

Categories
discuss

Android TabLayout overlapping the Fragment’s content

I’m trying to make an activity with TabLayout and Viewpager.
My problem is that the TabLayout overlaps the top of the Viewpager, so the full content isn’t visible.

The original image
The result

<android.support.design.widget.CoordinatorLayout 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"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:tabMode="fixed"
            app:tabGravity="fill"/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_add_white_24dp"/>

</android.support.design.widget.CoordinatorLayout>

What can I do to make my fragment begin right below the tabs?

Answer

You should wrap your layout with a surrounding LinearLayout:

<?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"
   android:orientation="vertical">

 <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        app:tabGravity="fill"
        app:tabMode="fixed"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"/>
 </android.support.design.widget.AppBarLayout>

 <android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>
Categories
discuss

How to redirect back to Android app?

I need to open a Custom Chrome tab that brings the user to my website, and then redirects the user back to the app. Its similar to launching a page for OAuth.

Here is what the Android side looks like…

AndroidManifest.xml (adds intent filter to activity for listening to URI)

<activity android:name=".app.MyActivity"
        android:label="@string/title"
        android:theme="@style/AppTheme"
        android:launchMode="singleTask"
        android:screenOrientation="portrait">

        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="mytest" />
        </intent-filter>

    </activity>

Activity.java (launches custom chrome tab)

private void goToSite() {
    CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
    int toolbarColor = getResources().getColor(R.color.primary);
    intentBuilder.setToolbarColor(toolbarColor);vice_key(), shared().getAccessToken());
    intentBuilder.build().launchUrl(this, Uri.parse("https://example.com/some-page"));
}

On my server, the user completes a form and is eventually brought to a page that is supposed to redirect them back into the app…

server.html

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Redirecting...</title>
<script>
    var redirectToApp = function() {
        setTimeout(function appNotInstalled() {
            window.location.replace("http://example.com/app-not-installed");
        }, 100);
        window.location.replace("mytest://justanexample");
    };
    window.onload = redirectToApp;
</script>
</head>
<body></body>
</html>

But I am not being redirected to the app. I suspect something is wrong with my server code because a similar setup (the manifest and activity code) works when dealing with OAuth pages. What is wrong with my server code? How can I troubleshoot this?

EDIT

The Android side appears to be correct – Changing the server code to <a href="mytest://other/parameters/here">GO TO APP</a> appears to work but I need a way that does not involve clicking a link

EDIT 2

The code above actually works :/

Because I was in a Chrome custom tab I was redirecting to the page I was already at (because the Chroms custom tab activity is considered to be in-app). This made me think no redirect was occurring. Tristan’s comment helped me realize this

Answer

You can simply use Javascript as explained in This answer.

Basically you’ll be making a link with an

<a href="intent://example.com#Intent;scheme=http;package=com.example.mypackage;end">

in which package = your package name

For more information, see this question Open android application from a web page

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..