Categories
discuss

Android Studio – how does setImageDrawable work?

I’ve been trying to teach myself how to use android studio but I’ve hit a road block, whats wrong with this code? It wont let me use a draw-able to be set as the image?

    public void obama(){
    Switch s = (Switch)findViewById(R.id.obamaswitch);
    s.setOnClickListener(new View.OnClickListener() {
        @Override
                public void onClick(View v) {
            ImageView p = (ImageView)findViewById(R.id.obamahere);
            p.setImageDrawable(R.drawable.brock);
        }
    });
}

Answer

You need to know what is a parameter and the difference between setImageDrawable and setImageResource.

Different methods requires different parameters. And by that I mean different types of parameters. In this case, setImageDrawable requires a parameter of type Drawable but you gave it a parameter of type int. So that’s why it doesn’t work.

I don’t know if you know this. All the resource ids (i.e. the R.xxx.xxx thingy) in Android are integers! So when you try to pass an integer when it actually needs a Drawable, it fails to do so, of course.

What you need is to either

1) Find a method that accepts an integer as a parameter. or;

2) Use the resource id to get a Drawable object.

For 1), you can use the setImageResource method. Just replace the word Drawable with Resource and change nothing else!

If you prefer 2), you can use this method to get the drawable using a resource id.

Drawable myDrawable = getResources().getDrawable(<insert your id here>);

And then you can pass myDrawable as the parameter:

p.setImageDrawable(myDrawable);
Categories
discuss

How does AlarmManager.AlarmClockInfo’s PendingIntent work?

I am trying to use AlarmManager.AlarmClockInfo to set an alarm.

The constructor to this takes the time and a PendingIntent which is described in the docs as:

an intent that can be used to show or edit details of the alarm clock.

and then setAlarmClock( ) also takes in a pending intent which is described in the docs as:

Action to perform when the alarm goes off

I understand the use of the PendingIntent by setAlarmClock( ), however, how is the PendingIntent used by AlarmClockInfo and how do I use it to edit the details of the alarm clock?

Answer

however, how is the PendingIntent used by AlarmClockInfo and how do I use it to edit the details of the alarm clock?

Quoting myself from this book:

The biggest issue with setAlarmClock() is that it is visible to the user:

  • The user will see the alarm clock icon in their status bar, as if they had set an alarm with their device’s built-in alarm clock app

  • The user will see the time of the alarm when they fully slide open their notification shade

Notification Shade, Showing Upcoming Alarm

  • Tapping on the alarm time in the notification shade will invoke the PendingIntent that you put into the AlarmClockInfo object

So, given this code…:

  static void scheduleAlarms(Context ctxt) {
    AlarmManager mgr=
      (AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
    Intent i=new Intent(ctxt, PollReceiver.class);
    PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0, i, 0);
    Intent i2=new Intent(ctxt, EventDemoActivity.class);
    PendingIntent pi2=PendingIntent.getActivity(ctxt, 0, i2, 0);

    AlarmManager.AlarmClockInfo ac=
      new AlarmManager.AlarmClockInfo(System.currentTimeMillis()+PERIOD,
        pi2);

    mgr.setAlarmClock(ac, pi);
  }

(from this sample project)

…when the user taps on the time in the notification shade, EventDemoActivity will appear. The idea is that you should supply an activity here that allows the user to cancel or reschedule this alarm.

Categories
discuss

How can I clone a JavaScript object except for one key?

I have a flat JS object:

{a: 1, b: 2, c: 3, ..., z:26}

I want to clone the object except for one element:

{a: 1, c: 3, ..., z:26}

What’s the easiest way to do this (preferring to use es6/7 if possible)?

Answer

If you use Babel you can use the following syntax to copy property b from x into variable b and then copy rest of properties into variable y:

let x = {a: 1, b: 2, c: 3, z:26};
let {b, ...y} = x;

and it will be transpiled into:

"use strict";

function _objectWithoutProperties(obj, keys) {
  var target = {};
  for (var i in obj) {
    if (keys.indexOf(i) >= 0) continue;
    if (!Object.prototype.hasOwnProperty.call(obj, i)) continue;
    target[i] = obj[i];
  }
  return target;
}

var x = { a: 1, b: 2, c: 3, z: 26 };
var b = x.b;

var y = _objectWithoutProperties(x, ["b"]);
Categories
discuss

Error in launching AVD – The filesystem is already 140800 (4k) blocks long – Nothing to do

I got the error resize2fs 1.42.13 (17-May-2015)
The filesystem is already 140800 (4k) blocks long. Nothing to do!
when I try to run the AVD.

I’m using a Mac and I’ve done all the necessary updates to the SDK Manager.

Does anyone know what might be?

enter image description here

Answer

Looks like a bug has been assigned and might be in the next release: (Although for Ubuntu, base might be the same)

Issue 189030: Emulator claims it doesn’t launch in Ubuntu (but it does!)

And maybe related info here

Issue 196283: Emulator boot is flaky – Could not bind or listen to socket

Categories
discuss

Renaming a fat jar with Maven

When I create a jar file I want to fit inside my dependencies. For that, I use maven-assembly-plugin such as follows:

<build>
    ...
    <plugins>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <finalName>${project.artifactId}-GUI</finalName>
                <archive>
                    <manifest>
                        <mainClass>gui.MyMainClass</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <!-- <appendAssemblyId>false</appendAssemblyId>-->
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
 </build>

This code works OK and it does what it’s expected to do. However, this creates a new jar called myjar-GUI-jar-with-dependencies.jar. I would like to eliminate that “jar-with-dependencies” ending. Does anybody knows how to do that?

I have used that commented line you can see on my code, but that produces the following warning that I don’t know how to solve it:

[WARNING] Configuration options: 'appendAssemblyId' is set to false, and 'classifier' is missing.
Instead of attaching the assembly file: [myJar-GUI].jar, it will become the file for main project artifact.
NOTE: If multiple descriptors or descriptor-formats are provided for this project, the value of this file will be non-deterministic!
[WARNING] Replacing pre-existing project main-artifact file: [myJar].jar with assembly file: [myJar-GUI].jar

EDITED

After the solution the user Tunaki suggested, I used a different pluggin and maven works as I want it to do it. The code is as follows:

            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.2</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <transformers>
                    <transformer
                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>gui.SparkISGUI</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </plugin>

Answer

First, you need to understand why you are getting this warning.

The Maven convention is that one project should create a single main artifact. For a project of packaging jar, the main artifact is the result of the maven-jar-plugin. This plugin will package as a JAR the classes contained in your project only.

One project can eventually generate additional artifacts that will be distinguished from the main one by their classifier:

Beside the main artifact there can be additional files which are attached to the Maven project. Such attached filed can be recognized and accessed by their classifier.

The classifier is an identifier than will be appended to the main artifact name.

So what happens when you want to create an uber-jar? Somehow, your project needs to generate two jars. The main one will be a JAR containing the classes of your project and the second one will be the uber-jar resulting of maven-assembly-plugin. To distinguish this secondary additional artifact from the main one, the classifier jar-with-dependencies is added.

So when you remove the classifier, you effectively replace the main artifact with the uber-jar. maven-assembly-plugin will emit a warning in this case, and that’s the warning you are having. You can ignore it completely: it just reminds you that you are replacing the main artifact of the project by an additional artifact.


Besides the maven-assembly-plugin, do note that you can also generate an uber-jar with the maven-shade-plugin:

This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade – i.e. rename – the packages of some of the dependencies.

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..