23 January 2015

Installing Phonegap for Android without command-line

PhoneGap is used to make Android,iOS,Windows etc. Apps using html or css. We will learn to install PhoneGap in adt-bundle to develop android app using html, javascript and css.

What will we do?
We will make PhoneGap application in adt-bundle.

What we will need?
Step 1: Download Java jdk from here .If you already have it no need to re-install.

Step 2 : Download adt-bundle(android-sdk). If present already do not re-install it. Check in here for download

Step 3 : Download Apache ant from


Step 4 : Make a folder as ant in C:/ drive . Decompress the apache-ant . Copy the content of apache-ant to C:/ant.

Step 5 : Download Phonegap 2.2.0 from
We will use Phonegap 2.2.0 since it is stable compared to other versions.


 Step 6 : Decomress the rar/zip file to Desktop.

Step 7 : Go to Start->Right-click on Computer->Properties

Step 6: Go to Advanced System Settings-> Environment Variables->double-click PATH in User variables for User If PATH not there create New (Variable Name- PATH).Value for PATH is given below.
Note down the Path for 
1. platform-tools in adt-bundle (C:\adt-bundle-windows-x86\sdk\platform-tools\)
 2. tools in adt-bundle (C:\adt-bundle-windows-x86\sdk\tools\)
3.bin in Java (C:\Program Files\Java\jdk1.8.0_11\bin\)
4. bin in ant (C:\ant\bin)

NOTE : Add semi-colon(;) between each path and at the end

Paste the above whole path from the Notepad to the value in PATH variable.

Path for my Computer would be:
Variable name: PATH
Variable path :  C:\adt-bundle-windows-x86\sdk\platform-tools\;C:\adt-bundle-windows-x86\sdk\tools\;C:\Program Files\Java\jdk1.8.0_11\bin\;C:\ant\bin;

Click OK

Step 7 : Click on New under System Variables and and create a variable with
Variable name : ANT_HOME
Variable value : C:\ant

Do the same for another variable
Variable name : JAVA_HOME
Variable value : C:\Program Files\Java\jdk1.8.0_11\
You'll get the below result.
Continued to the next post.
Stay Tuned with Made In Android

Published By:
Yatin Kode
on 23.1.15

20 January 2015

Zoom Controls for Android


What we will do ?
-> We will choose an image and apply zoom-in and zoom-out function on it.

What we will need?
  • ImageView(with Image)
  • Zoom Controls
  • Java Code in MainActivity.java
Step 1: Create a new Project by clicking on File->New->Android Application Project.
file new android application project
Step 2 : Give a name to the application (ZoomDemo) and also a package name(mia).Then click on Next ..Next and then click Finish.
ZoomDemo com.mia.zoomdemo

Step 3 : Drag an ImageView into the layout from Palette as shown below.
drag imageview from palette
It will ask for an image source. Click on ic_launcher and then OK.
ic_launcher OK

Step 4 : Also drag ZoomControls to the layout as shown below.
drag zoomcontrols
The code for activity_main.xml is shown below.
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher" />

    <ZoomControls
        android:id="@+id/zoomControls1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="50dp" />

</RelativeLayout>

Step 5 : Code for MainActivity.java found in src->com.mia.zoomdemo folder is shown below.
MainActivity.java
package com.mia.zoomdemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ZoomControls;

public class MainActivity extends Activity {
    
    ImageView img;
    ZoomControls zoom;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        img = (ImageView) findViewById(R.id.imageView1);
        zoom = (ZoomControls) findViewById(R.id.zoomControls1);
        
        zoom.setOnZoomInClickListener(new View.OnClickListener() {
            
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            
            int w = img.getWidth();
            int h = img.getHeight();
            
            RelativeLayout.LayoutParams params = 
                new RelativeLayout.LayoutParams(w + 10, h +10);
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
            
            img.setLayoutParams(params);
        }
    });
        
        zoom.setOnZoomOutClickListener(new View.OnClickListener() {
            
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            
            int w = img.getWidth();
            int h = img.getHeight();
            
            RelativeLayout.LayoutParams params = 
                new RelativeLayout.LayoutParams(w - 10, h -10);
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
            
            img.setLayoutParams(params);
        }
    });
    }

}

Step 6 : Run the Project
zoom controls output
zoom controls output

Explanation of the Code:

Explanation from activity_main.xml:
  • We have added an image using ImageView with id as imageView1 and a ZoomControls with id as zoomControls1
<ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher" />

    <ZoomControls
        android:id="@+id/zoomControls1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="50dp" />

Explanation from MainActivity.java
  • We create variables of type ImageView and ZoomControls as img and zoom.
ImageView img;
ZoomControls zoom;
  •  We link the ImageView from activity_main.xml to the variable and same for ZoomControls using method findViewById().
img = (ImageView) findViewById(R.id.imageView1);
        zoom = (ZoomControls) findViewById(R.id.zoomControls1);
zoom.setOnZoomInClickListener(new View.OnClickListener() {
            
        @Override
        public void onClick(View v) {
  • We define value of integer variables w and h using getWidth() and getHeight() methods.
int w = img.getWidth();
            int h = img.getHeight();
  •  LayoutParams is used to play with size of elements in the RelativeLayout.
RelativeLayout.LayoutParams params = 
                new RelativeLayout.LayoutParams(w + 10, h +10);
  • addRule() method is used for placing the image in center of the page while zooming in or out.
params.addRule(RelativeLayout.CENTER_IN_PARENT); 
            img.setLayoutParams(params);
  • Same methods are used for Zoom Out control.
Github download
 Stay Tuned with Made In Android

Published By:
Vijay Mukhiya
on 20.1.15

15 January 2015

ScrollView-Large number of items in single page.

What if you have to fit a large story in a page or fit 20-30 buttons, check-boxes in a page? Use a ScrollView to fit it there.

What we will do?
-> We will prepare an application with a page with lot of items and which scrolls down.

What we will need?
  • Adding ScrollView in activity_main.xml along with existing items.
  • Others remain same.
Step 1 : Create a new Project. Click on File->New_>Android Application Project.
file new

Step 2 : Give a specific name to your Project and also a package name(optional).Here I prefer it to be ScrollDemo. Then click Next... till Finish and click finish.
app name

Step 3 : In the activity_main.xml (res->layout folder), drag the ScrollView from the Palette as shown below. Then add different elements of your choice inside it.
drag scrollview

activity_main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="240dp"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="252dp"\
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Android is a mobile operating system (OS) based on the Linux kernel and currently developed by Google. With a user interface based on direct manipulation, Android is designed primarily for touchscreen mobile devices such as smartphones and tablet computers, with specialized user interfaces for televisions (Android TV), cars (Android Auto), and wrist watches (Android Wear). "
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Android is the most widely used mobile OS and, as of 2013, the highest selling OS overall. Android devices sell more than Microsoft Windows, iOS, and Mac OS X devices combined,[13][14][15][16][17] with sales in 2012, 2013 and 2014[18] close to the installed base of all PCs.[19] As of July 2013 the Google Play store has had over 1 million Android apps published, and over 50 billion apps downloaded.[20] A developer survey conducted in April–May 2013 found that 71% of mobile developers develop for Android.[21] At Google I/O 2014, the company revealed that there were over 1 billion active monthly Android users, up from 538 million in June 2013.[22]"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.11"
        android:text="Click Me" /> 

</LinearLayout>

</ScrollView>

Dont make any changes in other files.

Step 4 : Run your Project.
run project

Explanation of the Code:
A ScrollView Element covers all the elements under it.
diagram explain

Stay Tuned with Made In Android

Published By:
Yatin Kode
on 15.1.15

8 January 2015

Use WebView to make Online apps in Android

 What will we do?
-> We will make an app which will open google.com directly.

What will we need?
  • WebView
  • Changes in MainActivity.java
  • Changes in AndroidManifest.xml
Step 1 : Create a New Android Application by clicking on File->New->Android Application Project.
file new android app project

Step 2 : Give a name to your Application(WebDemo) and a suitable package name of your choice. Then Click on Next...till Finish.
WebDemo

Step 3 : Drag WebView form Palette in activity_main.xml layout.
drag webview from palette
Code for activity_main.xml is given below.

activity_main.xml 


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">"
    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
</LinearLayout>


Step 4 : Open MainActivity.java from src folder in Project Explorer and write the below code inside it.

MainActivity.java 


package com.example.webdemo;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;

@SuppressLint("SetJavaScriptEnabled") public class MainActivity extends Activity {

    WebView wb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        wb=(WebView)findViewById(R.id.webView1);
        String url="https://www.google.com";
    wb.loadUrl(url);
    wb.getSettings().setJavaScriptEnabled(true);
    }

}


Step 5 : Add INTERNET permission in AndroidMainfest.xml

AndroidManifest.xml 


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.webdemo"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.webdemo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Step 6 : Run the Project.
webview output

Explanation of the Code:

Explanation from activity_main.xml:
  • We have just added A WebView to the Layout having the default id as webView1.
<WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

Explanation from MainActivity.java:
  • We first declare a variable of type WebView called as wb.
WebView wb;
  •  We use method findViewById() to create a connection between variable wb and webView1 form activity_main.xml.
wb=(WebView)findViewById(R.id.webView1);
  • We define a String named url which defines the website to be launched and then load it in the WebView using method loadUrl().
String url="https://www.google.com";
    wb.loadUrl(url);
  • We use the method getSettings().setJavaScriptEnabled(true) to allow javascript changes to appear in our webpage.
wb.getSettings().setJavaScriptEnabled(true);

Explanation from AndroidManifest.xml
  • We add permission for our application to access the Internet service.
uses-permission android:name="android.permission.INTERNET"
Github download
Stay Tuned with Made In Android

Published By:
Yatin Kode
on 8.1.15

1 January 2015

RadioButton with RadioGroup in Android

Have you ever seen a traffic signal. Its color changes(red, yellow, green) in few seconds one by one. It takes only one color at a time if its working properly.

What we will do?
-> We will make a mini traffic signal in our phone with using radio buttons. Right now we are not concentrating on the timing of the color change.

What we need?
  • A LinearLayout.
  • 3 RadioButtons 
Step 1 : Create a New Android Application by selecting File->New->Android Application Project.
file new

Step 2 : Give a name and a package name to your application, then click on Next till Finish.Name can be RadioButtonDemo.
give app name

Step 3 : Drag Three RadioButtons in The activity_main.xml from the Palette.
drag radio button
activity_main.xml    
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:id="@+id/linearl">"

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Mini Traffic Signal"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    <RadioGroup android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" android:id="@+id/rgOpinion">
        
        <RadioButton
            android:id="@+id/red"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Stop" />

        <RadioButton
            android:id="@+id/yellow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Drive Slow" />
        
        <RadioButton
            android:id="@+id/green"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Go" />
</RadioGroup>
    </LinearLayout>

Step 4 : The code for MainActivity.java file present in src folder from Project Explorer is given below.

MainActivity.java 
package com.mia.radiobuttondemo;

import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Toast;
import android.app.Activity;
import android.graphics.Color;

public class MainActivity extends Activity {

    RadioButton red,yellow,green;
    LinearLayout linearl;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        red = (RadioButton)findViewById(R.id.red);
        yellow = (RadioButton)findViewById(R.id.yellow);
        green = (RadioButton)findViewById(R.id.green);
        linearl = (LinearLayout)findViewById(R.id.linearl);
       
        red.setOnCheckedChangeListener(
                new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
                        if(red.isChecked()==true)
                            linearl.setBackgroundColor(Color.RED); 

                    }});
       
        yellow.setOnCheckedChangeListener(
                new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
                        if(yellow.isChecked()==true)
                            linearl.setBackgroundColor(Color.YELLOW); 

                    }}); 
       
        green.setOnCheckedChangeListener(
                new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
                        if(green.isChecked()==true)
                            linearl.setBackgroundColor(Color.GREEN); 

                    }});   
    }   
}

Step 5 : Run the Project.
red coloryellow colorgreen color
Explanation of the code :

Explanation from activity_main.xml
  • We define an id for the Linear Layout as linearl which consists all our elements.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:id="@+id/linearl">
  • Then we make a RadioGroup which is used to collect all RadioButtons under one Group or Title (e.g. Countries, Cars).The id of RadioGroup is not essential when we have a single RadioGroup.
<RadioGroup android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" android:id="@+id/rgOpinion">
  • Make a RadioButton for each of our option (red,yellow,green) and give them specific ids like red,yellow,green respectively.
<RadioButton
            android:id="@+id/red"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Stop" />

        <RadioButton
            android:id="@+id/yellow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Drive Slow" />
        
        <RadioButton
            android:id="@+id/green"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Go" />

Explanation for MainActivity.java
  • We declare variables of respective types for the three radio buttons and the linear layout.
RadioButton red,yellow,green;
    LinearLayout linearl;
  • We direct the declared variables to their specific elements from activity_main.xml using method findViewById().
red = (RadioButton)findViewById(R.id.red);
        yellow = (RadioButton)findViewById(R.id.yellow);
        green = (RadioButton)findViewById(R.id.green);
        linearl = (LinearLayout)findViewById(R.id.linearl);
  • We define onCheckedChangeListener() method for all our radio buttons, so that we can take a proper action when they are checked(clicked).
red.setOnCheckedChangeListener(
                new CompoundButton.OnCheckedChangeListener() {

Note : We can also define setOnCheckedChangeListener from class RadioGroup. The above statement would become...

red.setOnCheckedChangeListener(
                new RadioGroup.OnCheckedChangeListener() { 
  • By applying an if condition we check that the particular radio-button is checked by isChecked() method.
if(red.isChecked()==true)
  • In the if parenthesis we define the code to change the color of our layout by using the method setBackgroudColor().
linearl.setBackgroundColor(Color.RED);

Github download
Stay Tuned with Made In Android

Published By:
Yatin Kode
on 1.1.15

Previous Page Next Page Home
Top