FloatingActionButton sample works in Android Studio but not in Eclipse - android

Long-time Eclipse-ADT user, and I've started looking into Android Studio since most samples on the web are now built with gradle. Now I just recently installed Android Studio on my machine and am trying out the samples from developer.android.com, specifically this one: FloatingActionButton
I imported the project in Android Studio, run it on my Nexus 5, works fine. The FABs are rounded and has drop shadows and all.
Now out of curiosity I tried to build the same project in Eclipse. So I fired up Eclipse, created a new project, copy-pasted everything from the Application folder into their proper destinations, then run the project on the same device.
The buttons are square.
Looking into the code, the following lines are responsible for giving the buttons a round shape are as follows:
Under FloatingActionButton.java
setOutlineProvider(new ViewOutlineProvider() {
#Override
public void getOutline(View view, Outline outline) {
outline.setOval(0, 0, getWidth(), getHeight());
}
});
setClipToOutline(true);
Now question is, why does these snippets of code work on Android Studio but not in Eclipse?

It is most likely related to which dependencies are in your project.
Using Gradle, you can configure wildcards for dependency version numbers, meaning that your project will always have the latest version of that library (for better or for worse)
With Eclipse-ANT you are most likely using manually downloaded and imported libraries, of a static version number. And the particular version you are using is compiling square buttons

Finally got it to work. The issue is that the AndroidManifest.xml that i copied over from the Application/src/main folder does not have a min SDK and a target SDK version -- both of which are defined in the build.gradle file, in the /Application directory. I added both and the FABs are now rounded with shadows and all.
Since I can't give bounty to myself, I'd give it to the answer who can explain further, or at least point to something that documents this change.

Now in AndroidStudio you can specify the most of the manifest configuration in build.gradle it will first refers to the module's build.gradle for this configs it is helpful and required for many things one i know is as follows.
Android studio and gradle gives you many new features one of them is buildtypes and flavours using which you can create a new flavours of your one project with very much less efforts, see in eclipse you need to create a whole new project for another flavour of your project while in android studio you can do it in single project. See when you define another flavour you can also define some of the manifest properties that will apply only to that flavour like you can change project name, package name, min sdk, target sdk, version, permission and so on.
There will only will be one manifest so if you declare multiple flavours and define this kind of changes than build.gradle file will fetch that changes and applies to the manifest specifically for that flavour so your manifest will get updated for that flavour.

Related

How to set up Android Studio project from scratch that allows me to use groovy

Groovy's 2.4 release comes with official support for android app development http://docs.codehaus.org/display/GROOVY/Groovy+2.4+release+notes.
Having previously used eclipse for Android development, with no experience at all with Android Studio nor with Gradle, the existing instructions for setting up groovy android projects with Android Studio (e.g. http://docs.groovy-lang.org/docs/next/html/documentation/tools-groovyc.html#section-android, https://github.com/groovy/groovy-android-gradle-plugin, or http://hosain.net/2015/02/07/getting-started-with-android-development-using-groovy-2.4-and-android-studio.html) are too vague, do not work for me, and the difference in version numbers used by them is confusing.
I'd like to see detailed step-by-step instructions that work for Android Studio 1.2.1.1 for Linux, and explain how to decide on the version numbers.
EDIT: In 2019, almost 4 years after the question and this answer, the method described below does not work anymore with the current Android Studio version (3.3.2). I could not find versions of the dependencies that still work, and Gradle complains about not being able to download some dependencies.
The fact that 4 years later there is still no reproducible setup guide by the Groovy maintainers is just one indicator that Groovy for Android has no future. We should have seen discussions on patterns regarding how Groovy's dynamic abilities simplify Android development long ago, but this has not happened. I'm going to try Kotlin next, which seems similar enough to Groovy on first glance, apart from the required type annotations, but most important: one does not have to fight so hard against Android Studio to use it.
The original answer follows, it was for a very specific Android Studio version, extended with guesses how to adapt versions of dependencies in future Android Studio versions. According to the comments, this has worked until Android Studio 3.0.1.
After learning by myself how to do it, this is the step-by-step description that I would have liked to read when I started. I have used information from a number of sources. None of these sources worked for me, and they differed in version numbers and where to insert things into build scripts. These instructions are the result of trying out various combinations and filling in some blanks.
From the welcome screen, select "Start a new Android Studio project".
Fill in the app name and a "company domain".
No changes necessary on this page.
This example uses the Blank Activity.
This example uses the name GroovyActivity for the main android screen. The new project now gets created. When android studio has finished creating the project, it shows two open files like this:
Ignore the "rendering problem" for now. It will be resolve itself later during compilation. Close the two open files by clicking on the crosses of their tabs.
Open this build.gradle (Module: app) file:
Insert the following code before the first line:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6'
}
}
Let's pause for a while to reflect about the version numbers. The lines
classpath 'com.android.tools.build:gradle:1.1.0'
and
classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6'
contain version numbers. How do you know which version number to insert here? At the time you read these instructions, there might have been new releases and unless you have specific version requirements, you will want to use the latest version of whatever it is that is used here in the gradle script.
How can you learn about the latest available versions? It depends:
Note that the editor has highlighted com.android.tools.build:gradle:1.1.0 with a yellow background color. When you hover the mouse pointer over this text, a tooltip appears:
So it seems that android studio knows about the latest version for this thing and advises you to use it. Heed the advice and update this version number to what your version of android studio indicates.
However, no such tooltip appears for this gradle-groovy-android-plugin thing.
What is happening here, anyway? The way I understand it, these names and version numbers identify names of binary components that are used for your app or for building your app, but neither are they part of android studio, nor are you expected to find and install or compile them yourself. Instead, this build script specifies a repository location where these binary components can hopefully be downloaded from. The function jcenter() seems to return the location of the repository. Currently, this repository is bintray.com.
To learn about the latest version of this gradle groovy android plugin, visit bintray.com with your webbrowser and use its search function. Search for groovy-android-plugin. The beginnig of the result looks like this at the time I write this:
The sheer number of listed results is a bit discouraging. I hope they have some relevance sorting in there. Searching through the first page, I only see 2 relevant matches, and the latest version number is 0.3.6.
Having learned about the latest version numbers (at the time of this writing), the correct start of the gradle script is this code:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6'
}
}
Back to modifying the gradle script, insert the following code after the "apply plugin: 'com.android.application'" line:
apply plugin: 'groovyx.grooid.groovy-android'
Finally, near the end of the gradle script, insert a line
compile 'org.codehaus.groovy:groovy:2.4.1:grooid'
so that it looks like this:
Again, there is a version number here, you can find the latest version on bintray by searching for *grooid.jar (yes, with the star as first character). The result list spans two pages, on the second page I find that 2.4.3 is the latest version:
After changing the gradle file, gradle needs to be "sync"ed. The gradle will sync if you exit android studio completely and restart it, so this is what I've done at this point. This picture shows the syncing after the restart:
Later I found that this icon in the toolbar
triggers the sync.
After the syncing finishes, change the left pane from the "Android" view to the "Project" view with the pull-down menu:
Now build and execute your project in the emulator at least once. I'm not sure what this changes, but if you do not build and execute your project now, while everything is still java sources only, then your groovy sources will not be found after we change to groovy. You can build and run your project using the run icon in the toolbar or with the keyboard shortcut shift-F10.
After executing your project in the emulator, turn back to the left pane, which still shows the "Project" view. In this view, navigate to the app/src/main directory, which currently contains the subdirectories java and res, and the AndroidManifest.xml file.
You will now create an additional "Java Folder" below main, with the help of the context menu:
Be sure to use the menu item New->Folder->Java Folder, and not New->Directory, which would cause problems later on.
In the next screen,
tick the check box "Change Folder Location" and make sure the name in the entry field is src/main/groovy.
If you still have that gradle script open, you will see that it has been adapted and contains the name of the new directory in a line starting with "sourceSets". For good measure, click on the gradle sync icon to make sure everything is properly synced.
Next you want to achieve that the new groovy folder contains the proper package path for the package name of your app. The only way that I have found to achieve this properly, is to create a new, temporary, java class with the context menu of the groovy folder,
and enter the name of this class fully qualified with the package name:
Make sure the spelling of the package name is correct!
The new package will appear in the groovy folder, and inside it, the temporary java class.
All groovy classes and also all java classes that make in some way use of groovy classes have to live below the src/main/groovy directory instead of the src/main/java directory. We can now drag the Activity class from the java directory tree to the same package below the groovy directory:
The temporary class can now be deleted (with the context menu), and the Activity class can be migrated from .java to .groovy by choosing Refactor->Rename File from the context menu.
The file extension .java is simply replaced with a .groovy extension. In the same way, you can later create new java classes and rename their file to .groovy when you actually want to create groovy classes.
You can now change some code in the activity to verify that you can actually have groovy code in an android app. Suggestion:
Give the "Hello World" text view an id in the res/layout/activity_groovy.xml file, like this:
Then, programmatically change the text shown by this view using groovy's string interpolation:
Build and execute in emulator:

Android: Using several version of the same library for different flavors

I'm needing help again with my strange projects ;)
So I'm still building a library, and I need to use libA-1.01.jar and libA-2.1.jar
Of course I don't want them in the same flavor, so I used in my gradle file:
flavorACompile files('libs/libA-1.01.jar')
flavorBCompile files('libs/libA-2.1.jar')
This works fine when compiling with gradlew in command line. But the issue is that my classes don't see both libraries. So when I'm coding I can only import the first one (1.01). So I can't use my IDE to code properly I have to guess emacs style if everything is ok and compile to see...
How can I tell Android Studio that the 2 libraries are present? I tried added them both to compile without flavor (just to force android studio to use them) but it didn't work.
I'm sure there is a simple way to do this, but I can't seem to find it :(

How to install a custom datepicker in Android

I have download this custom datepicker:
https://github.com/flavienlaurent/datetimepicker
I'm new to Eclipse and Android.
How should I use this?
When I import, I get an error at res folder.
http://i.stack.imgur.com/Dh7df.jpg
Please help me.
Sorry for asking a noob question.
Thanks
I've imported the datepicker to my Eclipse and I can to build it without any error.
Have you downloaded all the necessary Android SDK files using the Android SDK Manager? I see your 'MainActivity' is having compile error too.
Edit:
From the errors, seems like the attributes buttonBarStyle were only defined starting in API Level 11.
There are a few ways to solve this.
1) Set your app and the library project minSdkVersion in AndroidManifest.xml file to "11", this will make your app not be able to use on older Android versions though.
2) Move this particular layout file to res/layout-v11/ folder, and create another version of the same layout file in res/layout folder, but doesn't use the buttonBarStyle.
3) Seems like there's a way to create a attr.xml file in res/layout, and define the attributes manually. I have not tried it personally though you might want to try.. refer to error: Error: No resource found that matches the given name: attr 'buttonBarButtonStyle'
Good luck
Edit2:
Changing android:minSdkVersion="11" and android:targetSdkVersion="17" in your datetimepicker AndroidManifest.xml file should work!
From your screenshot, I saw that it doesn't recognize "HONEYCOMB", seems like your ADT version is not the latest, please try update it to the latest first.
After that right click your datetimepicker-library project in Eclipse, click 'Properties', then go to 'Android' tab, you should see API 15 or above checked, for mine I have API 17 checked as shown here http://i.imgur.com/Z3DkRY1.png
I think your eclipse is not configured properly
Just ran in the same problem. In order to use this library make sure that:
Datetimepicker-library references NineOld Androids
Project properties, Java build path, Libraries contains android-support-v4 and order and export has this jar checked
Project properties, Android, project target build is set to API 19 for example
The last one was causing most of the problems. If you set it to a higher target build, that does not mean that it will not run on lower API levels.

Third party library troubles on LayerType attribute even after building it on Android 3.2

So, I coded my entire app in Android 4.0.3. I used AmbilWarna color picker. Then I got this requirement to reduce the SDK requirement for Android 2.3. This is the point when EVERYTHING was working fine and superb
I have downgraded teh minimum SDK requirement. Everything runs fine except as soon as I include Ambil Warna as a library on my project, I start getting the error everywhere R is used, Saying that R cannot be resolved to a variable.
Upon a loot on the Console, this is what seems to be the real cause of the problem:
AmbilWarna\res\layout\ambilwarna_dialog.xml:18: error: No resource identifier found for attribute 'layerType' in package 'android'
I have set the Android SDK to 3.2 for AmbilWarna library project and on my project its at 2.3
Does anyone have any idea?
this is regarding this: http://developer.android.com/reference/android/view/View.html#attr_android:layerType
you are just trying to use a property for android that is not available on that api of the layout parser.
I just checked out the colorpicker code you are trying to use, and it's project.properties says it's written for target API-17 (android 4.2) and i'm tryng to compile it for API-10 (android 2.3.3)
simply remove those parts.
my diff
--- a/AmbilWarna/res/layout/ambilwarna_dialog.xml
+++ b/AmbilWarna/res/layout/ambilwarna_dialog.xml
## -19,7 +19,7 ##
android:id="#+id/ambilwarna_viewSatBri"
android:layout_width="#dimen/ambilwarna_hsvWidth"
android:layout_height="#dimen/ambilwarna_hsvHeight"
- android:layerType="software" />
+ />
and hope it's not mentioned in the code :)
I've had a lot of troubles too with my external libraries. What usually helped are one of these or multiple steps of these connected:
Delete ALL R.java imports in your Classes (there must not be any of them)
In Eclipse: Source => "Clean Up"
In Eclipse: Project => "Clean..." (This will rebuild your project from scratch and therefore regenerate your R.java)
Manually delete all R.java and save (located in /gen/)
Delete your external library project and readd it.
Step 3 usually did the job for me.
Also be aware that your minSdkVersion and targetSdkVersion fit to your library project (and the guidelines which are included)
I had the same problem, I thought I could just build and include the third party jar file and bring this over to my project - this resulted in the same problem as you - AmbilWarna\res\layout\ambilwarna_dialog.xml error: No resource identifier found for attribute 'layerType'.
To fix it I copied over the source files and all resources into my project (images/layouts etc etc), hey presto it worked.
Thanks http://code.google.com/p/android-color-picker/ for a nice clean easy to use colour picker!!

Referencing Android custom library cause runtime class not found error in Application

I am working on a custom Android library. I start off with a java project (in Eclipse) with another UI testing Android project.
In my UI testing project, I reference the library project in build path, and everything is good and I can test the library codes in the UI.
Later on, I decided I need to have the 'Gen' code (for custom styles attributes), so I try to change my library to an Android project (via Android-tools -> convert.. ), and disasters happens! After fixing up all the compile time problems, it finally 'let' me start the UI testing application, and bangs! When I try to create any objects from the library class, it said (in the Log) that Class not found.
Two questions:
Any idea how to fix this? I tried but seems it is for referencing another .apk that need to install separately.
If I just keep it to be java project (not android) for my library, can I use the resource generator? and how?
PS: the dependency (my library) is installed before the UI test application, according to the Console of Eclipse said.
Update: I tried to compile the library project to jar and reference it from the UI testing, and in this way, it works. But I don't want to use this approach since it is very slow in terms of testing.
for those who are doing same mistake like me
when library project is an android project
got to
properties > android > click add button at bottom of the popup inside
library section
and add project, instead of
properties > java build path > project > add
later approach is for pure java projects only .
Finally I found the answer to my own question. This is actually mentioned in official documentation.

Categories

Resources