Categories
discuss

How to use onAppWidgetOptionsChanged() in a widget?

I would like to update my widget every time it gets resized. I figured out that this is done in:

onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions)

But I can’t find out how to update the widget in it. I tried to redraw the widget by calling everything that I put in onUpdate(), but it’s not working. How can I make use of the bundle?

Answer

I would like to update my widget every time it gets resized.

Cool!

I figured out that this is done in onAppWidgetOptionsChanged()

More accurately, if you have the right actions in your <intent-filter>, on Android 4.1+ devices, you will find out about resize events via onAppWidgetOptionsChanged().

But I can’t find out how to update the widget in it.

You update it the same way you update it in onUpdate(). Call updateAppWidget() on the AppWidgetManager with an appropriate RemoteViews.

I tried to redraw the widget by calling everything that I put in onUpdate(), but it’s not working.

“it’s not working” is not a particularly effective description of your symptoms.

How can I make use of the bundle?

For a Bundle named newOptions, you can find out your new size range via:

newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH)
newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH)
newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT)
newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT)

For example, this sample project contains an AppWidgetProvider that simply pours those values into a string and uses that to update a TextView. The result looks like:

Resize Widget, During Resize Operation

Categories
discuss

Android: change app name in home screen

My app displays the name of its starting activity on the Home screen. I’ve tried to change the name by setting the android:label attribute in the manifest, under <application> to no avail. What is the correct way to accomplish this?

Answer

Change the name of the launcher activity using android:label. Seems to do the trick for me.

Categories
discuss

Why does 0x80000000 >> 1 in JavaScript produce a negative value?

Doing some tests with bitwise operations / shifting with JavaScript

0x80000000 >> 1 // returns -1073741824 (-0x40000000)

I would expect that to return 0x40000000 since

0x40000000 >> 1 // returns 0x20000000
0x20000000 >> 1 // returns 0x10000000

Answer

Its an arithmetic shift that’s why the sign is preserved, to do a logical shift use >>>

0x80000000 >>> 1 // returns 1073741824 (0x40000000)
Categories
discuss

Javascript Pass Parameter to Callback or Set Variable Value in DistanceMatrixStatus

I have been playing a little bit with Google’s DistanceMatrixService. The code below works, but, how can I pass another parameter to the callback function or grab one of the values out of the callback?

For example: I have two divs that I want to show different results in (Results1 and Results2), so I am thinking I need to either
pass another value to the GoogleMapDistance function like GoogleMapDistance(YourLatLong,DestLatLong,TheDiv)
or
be able to grab the ResultStr externally outside of the callback document.getElementById(“Results1”).innerHTML = ResultStr;
or
set the innerHTM to the returned value of the function document.getElementById(“Results1”).innerHTML = GoogleMapDistance(YourLatLong,DestLatLong);

I’m stuck. How can I do accomplish this? The way it looks right now is that I am only going to be able to run all this code once and have it only write to one div.

<div id="Results1"></div>
<div id="Results2"></div>

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>

function GoogleMapDistance(YourLatLong,DestLatLong)
{
    var service = new google.maps.DistanceMatrixService();
    service.getDistanceMatrix(
    {
    origins: [YourLatLong],
    destinations: [DestLatLong],
    travelMode: google.maps.TravelMode.DRIVING,
    unitSystem: google.maps.UnitSystem.IMPERIAL,
    avoidHighways: false,
    avoidTolls: false
    }, callback);
}

function callback(response, status)
{
    if (status == google.maps.DistanceMatrixStatus.OK)
    {
    var origins = response.originAddresses;
    var destinations = response.destinationAddresses;
      for (var i = 0; i < origins.length; i++)
      {
          var results = response.rows[i].elements;
          for (var j = 0; j < results.length; j++)
          {
              var element = results[j];
              var from = origins[i];
              var to = destinations[j];
              var distance = element.distance.text;
              var duration = element.duration.text;
              var ResultStr = distance + "&nbsp; (<i>" + duration + "</i>)";
          }
      }
    document.getElementById("Results1").innerHTML = ResultStr;
    }
}

var YourLatLong = "45.4049,-122.797997";
var DestLatLong1 = "47.468893,-122.227978";
var DestLatLong2 = "61.221274,-149.831545";

GoogleMapDistance(YourLatLong,DestLatLong1);

</script>

Answer

You can’t change how Google calls the callback, but you can let it call your own locally function as the callback and then have that (via a closure) call another callback after adding the desired extra argument like this:

function GoogleMapDistance(YourLatLong,DestLatLong, item)
{
    var service = new google.maps.DistanceMatrixService();
    service.getDistanceMatrix(
    {
    origins: [YourLatLong],
    destinations: [DestLatLong],
    travelMode: google.maps.TravelMode.DRIVING,
    unitSystem: google.maps.UnitSystem.IMPERIAL,
    avoidHighways: false,
    avoidTolls: false
    }, function(response, status) {callback(response, status, item)});
}

Or, you could just define your callback inline so it has access to the parent function variables directly:

function GoogleMapDistance(YourLatLong,DestLatLong, item)
{
    var service = new google.maps.DistanceMatrixService();
    service.getDistanceMatrix(
    {
    origins: [YourLatLong],
    destinations: [DestLatLong],
    travelMode: google.maps.TravelMode.DRIVING,
    unitSystem: google.maps.UnitSystem.IMPERIAL,
    avoidHighways: false,
    avoidTolls: false
    }, function callback(response, status)
    {
        // you can access the parent scope arguments like item here
        if (status == google.maps.DistanceMatrixStatus.OK)
        {
        var origins = response.originAddresses;
        var destinations = response.destinationAddresses;
          for (var i = 0; i < origins.length; i++)
          {
              var results = response.rows[i].elements;
              for (var j = 0; j < results.length; j++)
              {
                  var element = results[j];
                  var from = origins[i];
                  var to = destinations[j];
                  var distance = element.distance.text;
                  var duration = element.duration.text;
                  var ResultStr = distance + "&nbsp; (<i>" + duration + "</i>)";
              }
          }
        document.getElementById("Results1").innerHTML = ResultStr;
        }
    }

)}
Categories
discuss

Android: Theme.Holo.Light.NoActionBar vs Theme.Light.NoTitleBar

In res/values-v11/styles.xml, I can’t use Theme.Holo.Light.NoActionBar because it was added in API level 13. Can I use Theme.Light.NoTitleBar instead, with no visual differences? As far as I can tell, they should both have a white background, status and navigation bars, and nothing else.

Answer

Theme.Light.NoTitleBar is slightly different. The easiest way to resolve this and stay compatible back to Honeycomb is to create your own style that extends Theme.Holo.Light, but removes the Action Bar. Just create a style with the following definition:

<style name="MyTheme" parent="android:Theme.Holo.Light">
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
</style>
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..