Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

Android Studio in Ubuntu Linux ? Why not ?

Recently, I tried to replace the Eclipse Android Development Environment in my computers with the Android Studio Developed by Google. But, unfortunately ended up getting so many errors on Linux Ubuntu.

After some digging on the internet and many failed attempts to change permissions in directories of the Operating System, I finally managed to get things working and thought that I could write this down in the Notes, So, that in the  future there won't be any inconveniences. 




First I downloaded Android Studio from the official developer Web Site here.


After downloading the .zip file. I extracted the whole thing By Just Right Click-> Extract Here.

Inside the Extracted android-studio/bin Directory there's 'studio.sh' which is the 'Self Extracting Archive' is the file to be executed, to do that I just launched my terminal. (Ctrl + T)


$ cd Downloads/android-studio/bin
$ ./studio.h



It took a quite a bit of time and ended up, showing an error for the versions. That was completely annoying and it kept coming. 

So, initially, what I thought was that It could be a problem with the Java versions and I tried to understand which version of it that had at the moment.


$ java -version
$ dpkg --list | grep -i jdk


It showed me that I had OpenJDK 6. So, I decided to Update the Java Version. 

$ sudo apt-get update   
[ To Download the package lists from repositories. ]
$ sudo add-apt-repository ppa:webupd8team/java 
[ Add the Repositories if it doesn't exist ]
$ sudo apt-get update && sudo apt-get install oracle-jdk7-installer
$ update-alternatives --display java

At the end of this line, I had to opt the Required version and I typed #3 which was the required OpenJDK1.7.0. And I tried running the './studio.h' again and guess what I ended with the, Error message saying "tools.jar is not in android studio class path Please ensure JAVA_HOME points to JDK rather than JRE". 

As usual when it comes to settings related to default paths, what comes to our mind usually is, '.bashrc'. So I ran the following commands.

$ cd ~
$ vi .bashrc


I added the following lines at the end of the '.bashrc' having typed the comments for future references. Comments are shown after '#' in shell scripting.


# comment : the .bashrc is eddited to add java_home by Harsha
JAVA_HOME="/usr/lib/jvm/java-7-openjdk-i386"
export JAVA_HOME
PATH=$PATH:$JAVA_HOME

I Saved the file, and ran, 


$ source ~/.bashrc
$ echo $JAVA_HOME



Above is to test that it echoes the Java path. Then , I tried run the '/studio.h'. Still I kept getting the Error "tools.jar is not in  android studio class path Please ensure JAVA_HOME points to JDK rather than JRE". 

After digging the Internet for around 10 minutes, I found out the way of doing this. So, Here's the whole thing. Exporting the JDK path in the '~/.bashrc' won't give us the right answer. So do the following.


$ sudo apt-get install openjdk-7-jdk  [ If you don't have it ]
$ sudo vi /etc/environment



Add the line. 

JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-i386 


Save & Close. You probably would require a reboot of the System. After the Reboot is made, run the '/.studio.h' and you'd love using Android Studio which is a better application by the way.



Thank You.

Creating First Android App with Eclipse

This is the start of the many of the Simplified android tutorials and it starts with a very simple android application.To make things easier this whole tutorial follows a set of steps.


  1. First, Create a Project Named ‘Application01’ by going to File->New->Android Application. Provide the Minimum and Maximum Android Version Requirements as you wish. Keep other project settings as usual and Click ‘Finish’.





  2. Go inside the Application01->res->layout->activity_main.xml and start adding form items. You can either copy the XML code I’ve provided below or add button yourself.


  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.buttonclick.MainActivity"
        tools:ignore="MergeRootFrame" >
    
        <TextView
            android:id="@+id/textView_01"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="The Total Value Is : " 
            android:layout_marginTop="10dp"
            android:textColor="#ff0000"
            android:textStyle="bold"
            />
    
        <Button
            android:id="@+id/btn_incrementer"
            android:layout_width="fill_parent"
            android:layout_height="82dp"
            android:text="Increment" 
            android:layout_marginTop="40sp"/>
    
        <TextView
            android:id="@+id/textView_02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/textView_01"
            android:layout_alignBottom="@+id/textView_01"
            android:layout_centerHorizontal="true"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceSmall" />
    
        <Button
            android:id="@+id/btn_decrementer"
            android:layout_width="fill_parent"
            android:layout_height="82dp"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/btn_incrementer"
            android:text="Decrement" />
    
    </RelativeLayout>
    
    


  4. Finally your app front end should look like below.



  5. Then, start Changing Main_Activity.java which is inside Application01->src->com.example.application01->Main_Activity.java. You can either copy or paste the code that I’ve provided below or write the code yourself.


    package com.example.application01;
    
    import android.app.Activity;
    import android.app.ActionBar;
    import android.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.widget.Button;
    import android.widget.TextView;
    import android.os.Build;
    
    public class MainActivity extends Activity {
    
     Button dec,inc;
     TextView dis;
     int count=0;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            if (savedInstanceState == null) {
                getFragmentManager().beginTransaction()
                        .add(R.id.container, new PlaceholderFragment())
                        .commit();
            }
            
            /* MyCode Starts Here */
            
            dec = (Button) findViewById(R.id.btn_dec);
            inc = (Button) findViewById(R.id.btn_inc);
            dis = (TextView)findViewById(R.id.display_value);
            
            dec.setOnClickListener(new View.OnClickListener() {
       
       @Override
       public void onClick(View arg0) {
        // TODO Auto-generated method stub
        
        count++;
        dis.setText(String.valueOf(count));
        
        
       }
      });
            
            inc.setOnClickListener(new View.OnClickListener() {
       
       @Override
       public void onClick(View v) {
        // TODO Auto-generated method stub
        
        count--;
        dis.setText(String.valueOf(count));
       }
      });
              
            /* My Code Ends Here */
        }
    
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    
        /**
         * A placeholder fragment containing a simple view.
         */
        public static class PlaceholderFragment extends Fragment {
    
            public PlaceholderFragment() {
            }
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_main, container, false);
                return rootView;
            }
        }
    
    }

  6. If you intend to write code, make sure to add your Code inside protected void onCreate(Bundle savedInstanceState) method. Declare all the Variables inside public class MainActivity { } class.


  7. You should cast the Android Widget Button dec to XML Button like, dec = (Button) findViewById(R.id.btn_dec);


  8. Write the Button Click Event like follows.
dec.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

count++;
dis.setText(String.valueOf(count));


}
});




Faster Android Emulator?

If you ever found out that your Android Emulator is slower then, this might be the solution for you.



1. First, you've to install virtual box in your Computer. (Linux: Ubuntu in my case). You can use Ubuntu Software Center to do that.


2. After installation, you've to install Eclipse IDE with Android SDK. (Find adt-bundle & install)

3. Then, Download Android Operating System ISO, Here.

4. Now, Run Virtual Box Software & Create a new Virtual Machine. (Ctrl+N)



5. In the next Screen, Give your Virtual Machine a Base Memory Minimum of 512 MB. (RAM) The rest of the steps don't make any significant impact except Virtual Hard Disk setting where You should set it as Start-up Disk. Others, proceed as you please.




6. After completing VM Creation. Change the Settings of  the Virtual Machine by Machine->Settings. You've to set the IDE Controller. Give the Exact location of your downloaded Android generic ISO.



7. In order to enable network connection to the Virtual Machine, Set Network: Attached to: Bridged Adapter. So that, Virtual Machine will share network connection with Host.



8. Then, Start Virtual Machine. Select 'Run Android without Installation' option in the Android Live Installation Menu.


After few minutes, you'll be in your own Android Environment. Try to Practice Mouse Capture (Integration) with your Virtual Android Environment.



9. Then, Press Alt+F1 inside the Virtual Machine. You'll be seeing a screen similar to Terminal or Command Prompt. Then, Type netcfg command in order to get the Emulator's IP address. (Alt+F7 to Set it to Previous State)


Copy down the IP address. Take your terminal out & Type the following command.

$ adb connect "Your IP Address"

if it returns connected, then you're correct up to now.

10. Create a dummy Project to test this whole thing & go to Run-> Run Configurations. Then, Click Android Application-> New Configuration.

11. Inside the, Target tab. Under Target Tab set Deployment Target Selection Mode: Always Prompt to Pick Device.


12. Now, You can Click Apply & Run. You'll be prompted to choose emulators Select the Virtual Box.


Finally, you're done. Thanks for coming.