Showing posts with label Basics. Show all posts
Showing posts with label Basics. Show all posts

Wednesday, March 5, 2014

Migrating from Eclipse to Android Studio

I had a very difficult time to import an eclipse project to Android Studio.  So finally I found a method to do that.

Step 1 : Open up eclipse and export the project that need to import to Android Studio.


Step 2 : A window will be appeared. Select "Generate Gradle build files"


Step 3: Select the project you want to export


Step 4 : Now a window will be appeared like below. Click "Finish" button.


Step 5 :  Again click "Finish" in next window as well.


Step 6 : Now if all the things worked fine, a file named "build.gradle" has been generated inside your selected project folder.


Step 7 : This is the gradle version that is used to build the project. Open that build.gradle file to check your gradle version.



Step 8 :  Now open up the Android Studio. Select File->Open. Select that build.gradle  file. Press "Ok"



Step 9 : Then a message box will pop up like this. Select "Yes"


Step 10 : Now a new window will be appeared like below. I have installed gradle 1.9 version on my PC. So I select "Use local gradle distribution" option. If you want you can download  latest gradle version from here. Also you can select the "recommended" option as well.




Step 10 : Select "Ok". It will start to build the project and finally import into Android Studio.



Here is the final output.


Other references : https://www.youtube.com/watch?v=b84t7p9YuX8&index=11&list=LL7iK14mNE71scGEhxKuWNlw

Friday, February 28, 2014

How to set an activity dynamically for up-button in android

This is done by implementing getSupportParentActivityIntent() method  and using it we can dynamically set the activity to up-button in android. Here how I have achieved it.

Assume we have two activities. Activity A and B. A is the parent activity and B is the child.

So A need to create an intent to start B. It should pass an extra data which is the name of the parent activity. Here in our example it should be 'A'. Here is the code,

    Intent intent = new Intent();
    intent.putExtra("ParentClassName","A");
    startActivity(intent.setClass(A.this, B.class)); //we are starting activity 'B'

Now in activity B we need to override getSupportParentActivityIntent() and it should look like this,

     @Override
     public Intent getSupportParentActivityIntent() {
           Intent parentIntent= getIntent();

           //getting the parent class name
           String className = parentIntent.getStringExtra("ParentClassName");

           Intent newIntent=null;

           try {
                //you need to define the class with package name
                newIntent = new Intent(B.this,Class.forName("com.myapplication."+className));
          } catch (ClassNotFoundException e) {
                e.printStackTrace();
           }
           return newIntent;
      }