android : navigation drawer and upgrading libraries methodology - android

Im following headfirst methodology to learn to code in androidstudio.
they provide some code for building a simple navigation drawer app.
in order to be compatible with previous android version, they advise to import :
import android.content.Intent;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
but in my last version of android studio this doesnt work : some of them are greyed and alt+enter does not import the correct library when i put the java code provided by my methodology.
so i replaced a few imports with the newer versions. for example :
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.ActionBarDrawerToggle ;
import android.widget.Toolbar;
however, i did not find anything for to import "NavigationView" and com.google.android.material.navigation.NavigationV iew is grayed when i try to import it.
So my question is basic: how do i know which lib to import when android studio does not suggest anything with alt+enter and when the libraries have evolved? the question could be asked for many programs i guess ..is there a standard methodology to upgrade the code?

You first need to include your dependencies in your build.gradle (app) file.
For example, your NavigationView is a part of Google's Material library. This library does not come included in Android SDK when you create the project and therefore you need to add the library under 'dependencies' in your build.gradle (app) file in order to use it:
dependencies {
implementation 'com.google.android.material:material:1.3.0-alpha01'
}
This way, upon the build of the project, Gradle will download the library and include it in your project so you can use the Material components. You can usually find the gradle dependency you need to include with a quick Google search.
Another thing to note is that the legacy support libraries have been replaced with AndroidX as you can read here:
https://developer.android.com/topic/libraries/support-library
https://developer.android.com/jetpack/androidx
And you can find the full list of AndroidX support libraries in here:
https://developer.android.com/jetpack/androidx/explorer

Related

How to customize Android Studio's import ordering in Kotlin to ignore whether they are "static" imports?

Using the "Optimize Imports" in Android Studio 3.4.1, the imports are ordered similar to this:
import com.walla.walla
import com.willy.willy
import org.koin.android.ext.android.get
import org.koin.androidx.viewmodel.ext.android.viewModel
import kotlin.concurrent.thread // <-- note this line
import kotlin.random.Random
As you can see above, function (a.k.a. "static" import in Java) imports like kotlin.concurrent.thread and kotlin.random.Random are put under other imports.
It is not consistent with the Android Kotlin style guide:
Import statements for classes, functions, and properties are grouped together in a single list and ASCII sorted.
I couldn't find a way to make it such that Android Studio order imports irrespective of whether the import is a class or a function. Is there an option to make it so?
This seems to be a misunderstanding. In fact, kotlin.concurrent.thread is a function. Therefore, it should be grouped together with the other classes.
UPDATE: I do see that the latest version of IntelliJ 2019.1 (and Android Studio) might not be able to conform to the Android Kotlin style guide. If you have these import statements, then IntelliJ does not sort strictly by ASCII:
import org.apache.commons.lang3.StringUtils
import java.util.Base64
import kotlin.concurrent.thread
Instead, IntelliJ orders them as:
Third-party
Java
Kotlin
I do not see a way to configure IntelliJ or Android Studio to sort them this way:
import java.util.Base64
import kotlin.concurrent.thread
import org.apache.commons.lang3.StringUtils
Perhaps you should submit some feedback to IntelliJ or the authors of the Android Kotlin style guide.

Why used import statement is specified as "unused import statement" in Android Studio?

I'm using Android Studio and build as "Generate Signed APK...". And I faced the error, "Unused import statement" like following.
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity; // <- specified as unused ERROR
import android.support.v4.app.FragmentManager; // <- specified as unused ERROR
import android.support.v4.view.ViewPager; // <- specified as unused ERROR
import android.util.Log;
public class Main extends FragmentActivity {
...
}
But I can easily see FragmentActivity is used in code. How can I fix it? This error happens all over my codes...
This is because you have "Optimize imports on the fly" enabled, which automatically removes unused imports. You likely also have "Add unambiguous imports on the fly" selected, which automatically adds imports you need. The solution is to write the code first, and watch your imports be added automatically, and manually add any ambiguous ones, as they become needed.
https://www.jetbrains.com/idea/help/creating-and-optimizing-imports.html?search=optim
In the event this does not work for you, or if you simply prefer to enter your own import statements, then simply disable the features in settings > Editor > Auto Import.
When I updated the android studio to 3.3.1 I go this error.
Solved it by updating the libraries and compileSdkVersion to the latest version that is 27. Hope it will help someone else.
I meet the problem in the Android studio 3.2.1. Invalidate and delete ./idea does not work. It disappear when I upgrade 3.4.1.

import com.google.android.gms.common.api.GoogleApiClient; cannot be resolved

I want to make a login with Google+ login button like this Tutorial
I have copied the codes, set google_play_services libraries, added package name and SHA1 key in google api console, but there are some import errors :
This import is not an error :
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.plus.model.people.Person;
But this import an error (cannot be resolved) :
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.plus.Plus;
Anybody have a solution ?
Add this in yourbuild.gradle(app) inside dependency
compile 'com.google.android.gms:play-services:8.1.0'
[Solved]
I try to download new version of google-play-services library and import error is gone.
So, always update/download newest version of google-play-services library..
You need to add the dependency to your app.
Instructions for eclipse here
http://developer.android.com/google/play-services/setup.html
You can also do this by right clicking on your app (if you're using Android Studio) selecting Open Module Settings, going to the dependency tab. Then Clicking the "plus" at the lower left, select library dependency, then find play-services

Android Studio: Select min sdk version not working?

I was trying to select min sdk version to 11. After I finish creating the project, the main activity still shows I am importing v7 ActionBarActivity:
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
...
Did I configure something wrong?
Thanks
Open your build.gradle file and make sure that minSdkVersion is set to 11.
Click the Sync Gradle Files button on the toolbar to the left of the SDK manager
the Gradle files will now be automatically re-imported
Clean and Rebuild your project
For those using Android Studio 0.8.6 and having the same issue while following Google's tutorials, what worked for me was to add this line under "dependecies" in your module build.gradle file:
dependencies {
compile 'com.android.support:appcompat-v7:20.0.0'
}
it worked for me!
:)
No, android studio import it automatically because there are some features that worked since version 14.
For example: when you set the actionbar in your XML document and you want to hide / show some item you use "android:showAsAction" attribute, but the compiler generates an error, because that works only on version 14 and later.
So, the compiler tells you that you have to change it in "yourapp:showAsAction" (because you declared to support version 11) where "yourapp" is a namespace that you've imported in your xml file:
xmlns:yourapp="http://schemas.android.com/apk/res-auto
Remember that "yourapp" can be every name you want! Well, now if you test your app you find out that show / hide functionality doesn't work properly, and this is because your activity doesn't extends ActionBarActivity. This is why androidStudio automatically extends ActionBarActivity when you declare a minSdk of 11.
Hope this post was useful, bye!

Android SDK Fragment Support

Ok i'm just starting to get into Android programming now, and I'm following the android "TabActivity" tutorial: http://developer.android.com/reference/android/app/TabActivity.html.
Everythings working but it can't seem to find some of the support classes, see the code below that is generating the errors.
mTabManager.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
FragmentStackSupport.CountingFragment.class, null);
mTabManager.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
LoaderCursorSupport.CursorLoaderListFragment.class, null);
mTabManager.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
LoaderCustomSupport.AppListFragment.class, null);
mTabManager.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
LoaderThrottleSupport.ThrottledLoaderListFragment.class,null);
For FragmentStackSupport/LoaderCursorSupport/LoaderCustomSupport/LoaderThrottleSupport, it says for all of them cannot be resolved to a type. I've added the latest support library to a folder in the root directory named "libs" and also coppied it into "C:/Eclipse/v4/" directory. My import files are:
import java.util.HashMap;
import android.R;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;
import android.widget.TabHost;
import cowdawg.hello_tab.namespace.R.layout;
import cowdawg.hello_tab.namespace.R.id;
Could someone please offer me some advice on how to solve this, thanks :).
It's probably because the following classes are not part of the standard Android API (or the support library), but only exist in the support demos sample code for demonstration purposes:
FragmentStackSupport.CountingFragment
LoaderCursorSupport.CursorLoaderListFragment
LoaderCustomSupport.AppListFragment
LoaderThrottleSupport.ThrottledLoaderListFragment
You will need to add these classes to your own project in order to be able to use them. Here you can find the source files under the subsection 'Files'.
FragmentStackSupport
LoaderCursorSupport
LoaderCustomSupport
LoaderThrottleSupport
On Eclipse IDE: Select your project and then right click on the project -> Android Tools -> Add Support Library.
Required support library will be added on your project build path. You will found this library jar under Java Build Path -> Libraries tab -> Android Dependencies.
Enjoy exploring!
Have you added the library to your build path? Right click on the jar in eclipse -> Build Path -> Add to Build Path.
Try adding by right clicking on the project and opening properties window
Instead of these missing classes you can create your own by subclassing Fragment class (or its subclasses like DialogFragment, ListFragment etc). More info is provided by Android here.

Categories

Resources