16 July 2014

Make a calling Android Application

Here we will learn the steps / code to make a call from your android application .

What are we going to do ?
-> We will make a button and when we press the button we will be able to call on the desired number


What we need ?
  1. A Relative layout
  2. A Button
  3. The number you desire to call
Step 1 : Make a button on your xml page (activity_main.xml) in res folder as shown below .

The code for the above layout is given 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"
    tools:context=".MainActivity"
    android:background="#FF0000" >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="#F0F0FF"
        android:padding="10dp"
        android:textSize="30dp"
        android:text="Make a Call to 0000" />
</RelativeLayout&gt

Step 2 : Make changes in the MainActivity.java file in the src folder .

MainActivity.java
package com.mia.makecall;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity {

    Button callbutton;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        callbutton = (Button) findViewById(R.id.button1);
        
callbutton.setOnClickListener(new OnClickListener(){
            
            public void onClick(View arg0){
                Intent i1 = new Intent(android.content.Intent.ACTION_DIAL,Uri.parse("tel:0000"));
                startActivity(i1);
            }
        });
    }
}

You can change the number (marked in green) to the number you desire to call

Step 3 : Add CALL permission in AndroidManifest.xml file .

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mia.makecall"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />
      

<uses-permission android:name="android.permission.CALL_PHONE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.mia.makecall.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 4 : Run the project

When you click the button you see a keypad with the number we used in our code . You can call the number pressing the call button  .

NOTE :If you want to directly call the desired number without letting the keypad to be seen , just change the word ACTION_DIAL with DIAL in your java file (MainActivity.java) .

  • In the xml file (activity_main.xml) we just made a button with id button1 which when clicked performs an action of calling a particular number .
Button callbutton;
  • In the java file MainActivity.java we made an instance of a Button ,then we linked it with the button in activity_main.xml .
callbutton = (Button) findViewById(R.id.button1);
  • By call the setOnClickListener() with the instance of our button we provide an action of our choice to the button .
callbutton.setOnClickListener(new OnClickListener(){
  public void onClick(View arg0){
  • Intent is an used to perform a specialized function . A Uri is used to take input from its parameters and display it to the specific place . The Uri takes the number as the input and displays it on the dialpad using tel function .
Intent i1 = new Intent(android.content.Intent.ACTION_DIAL,Uri.parse("tel:0000"));
                startActivity(i1
  • We have to include CALL_PHONE permission in AndroidManifest.xml which gives us access to the components in the phone (call opertion inbuild in the phone)
<uses-permission android:name="android.permission.CALL_PHONE"/>

Stay tuned with Made In Android

Published By:
Yatin Kode
on 16.7.14

8 July 2014

R.java has been deleted !

You may have a problem sometimes . The generated file R.java can be deleted automatically. What to do then ?

You may have encountered some errors and to prevent it you may have cleaned it by pressing Clean from Project in your Menu-bar .
project clean

You have a file in your gen folder R.java
gen folder

It may go missing and you may get lots of errors in your classes .(like : R cannot be resolved to a type) . Do not try to correct it by importing R .
r not resolved
  1. Just check that there are no errors in your layouts (xml files) from res .
  2. There may be some problem in the style.xml or some API level problem in AndroidMainfest.xml
  3. Build your project by going to Project in your Menu-bar and click on Build All .
build all

 .You may see that all your errors corresponding to R.java are been disappeared .

Stay Tuned with Made In Android

Published By:
Yatin Kode
on 8.7.14

6 July 2014

Making use of TableLayout in Android ( PART - 2 )

We learnt to make table with two rows and single column in the previous post. Now we will learn how to split the row into columns .
If you followed the previous tutorial to make a Table with single column , then making a Table with multiple columns is simple .

Step 1 : We just have to include two or more TextViews in a single TableRow . We will make a Table as shown in the below screenshot .
Add the code to activity_main.xml in res->layout .The code for the above screenshot is given below :
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

  <TableRow 
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:gravity="center_horizontal" >

                    <TextView
                        android:id="@+id/TextView04"
                        android:layout_weight="1"
                        android:background="#00FF00"
                        android:gravity="center"
                        android:padding="5dip"
                        android:text="HEADER 1"
                        android:textColor="#000000"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/TextView04"
                        android:layout_weight="1"
                        android:background="#00FF00"
                        android:gravity="center"
                        android:padding="5dip"
                        android:text="HEADER 2"
                        android:textColor="#000000"
                        android:textStyle="bold" />
                </TableRow>

                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal" >

                    <TextView
                        android:id="@+id/TextView04"
                        android:layout_weight="1"
                        android:background="#b0b0b0"
                        android:gravity="center"
                        android:padding="5dip"
                        android:text="ELEMENT 1"
                        android:textColor="#000000" />

                    <TextView
                        android:id="@+id/TextView04"
                        android:layout_weight="1"
                        android:background="#a09f9f"
                        android:gravity="center"
                        android:padding="5dip"
                        android:text="ELEMENT 2"
                        android:textColor="#000000" />
                </TableRow>

                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal" >

                    <TextView
                        android:id="@+id/TextView04"
                        android:layout_weight="1"
                        android:background="#b0b0b0"
                        android:gravity="center"
                        android:padding="5dip"
                        android:text="ELEMENT 3"
                        android:textColor="#000000" />

                    <TextView
                        android:id="@+id/TextView04"
                        android:layout_weight="1"
                        android:background="#a09f9f"
                        android:gravity="center"
                        android:padding="5dip"
                        android:text="ELEMENT 3"
                        android:textColor="#000000" />
                </TableRow>        
            </TableLayout>

Step 2 : The file MainActivity.java remains the same as in the previous post Step 3 : Just Run the project .


Stay Tuned with Made In Android

Published By:
Yatin Kode
on 6.7.14

5 July 2014

Making Use of TableLayout in Android (PART - 1)

A Table Layout is used to make tables in your application. Which application will u like : One with lots of text paragraphs (boring one) OR the one with short tables providing the same information in tabular format (the interesting one).
People probably used huge paragraphs before but now the scenario has changed .

What do we need ?
  1. A Relative / Linear Layout .
  2. A TableLayout
  3. A TableRow (depends on the number of rows needed) .
  4. TextViews
What are we going to do ?
We wiil prepare a Table with 1 row & 1 column .

Step 1 : Drag a TableLayout from Layouts in Palette .
 
Step 2 : Drag Some TableRow elements from Layouts again and.The TableRow can be dragged as many times as the number of rows you require in your project. (I will drag 2 TableRows as I will display two rows in my Table ) .

Step 3 : Now alter the code of   the activity_main.xml file to the code given below .
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

         <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:shrinkColumns="*"
            android:stretchColumns="*" >

            <!-- Row 1 with single column -->
            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:gravity="center_horizontal" >
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="2dp"
                    android:layout_span="3"
                    android:padding="5dip"
                    android:text="HEADER"
                    android:textColor="#000"
                    android:textSize="18dp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="2dp"
                    android:layout_span="3"
                    android:background="#FFFF00"
                    android:padding="5dip"
                    android:text="CONTENT OF TABLE"
                    android:textColor="#000000"
                    android:textSize="18dp" />
            </TableRow>

            </TableLayout>

    </LinearLayout>
You will now find the Graphical Layout of activity_main.xml as shown below .
This looks boring . Now we will change the look & feel of the HEADER so that it actually looks like a Header of a Table .

Step 4 : Make a New Folder named drawable in the res folder in your project . Right-click on res from Package Explorer .Then Select New->Folder. Give the name as drawable and its made .

Step 4 : Now Right-click on the newly made drawable folder . Select New->Android XML File .

Step 5 : Give a name (header_shape) to the XML File and select shape as Root Element. Then click on Finish . This File will be used to give a custom shape to our Table header .

Step 6 : Now change the default code present in header_shape.xml to the code given below .

header_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >
    <solid  android:color="#C0C0C0"/>
    <!-- color of the border -->
    <stroke android:width="2dip"
            android:color="#453"/>
    <!-- for round corner -->
    <corners android:topLeftRadius="7dp"
        android:topRightRadius="7dp"
        android:bottomLeftRadius="7dp"
        android:bottomRightRadius="7dp"/>
    <padding android:left="5dip"
        android:top="5dip"
        android:right="5dip"
        android:bottom="5dip"/>   
</shape>

Step 7 : Add the line given below to the TextView of the HEADER to get the desired color as shown in the below screenshot .
android:background="@drawable/header_shape"

You will see the Graphical Layout of activity_main.xml as shown below .

Step 8 : Make changes in MainActivity.java found in src folder from your project .

MainActivity.java

package com.mia.tableproject;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

}

Step 9 : Run the Project .

This type of table format was used in the app Engineering Colleges by Smart Daemon . The screenshot for the app is shown below .


In the next post we will make table with two rows and two columns .

Published By:
Yatin Kode
on 5.7.14

30 June 2014

Grid Layout In Android v2.3 (API level 8)

We have seen in a previous post on layouts that Grid layout is applicable to only API greater than 14 . Lets use a trick that allows A grid like structure in API 1-13

What we need ?
  1. Just Lots of Linear Layout .
  2. ImageButtons
What are we going to do ?
Implement a Grid layout like structure shown in screenshot below which can also be deployed in Android v (1 - 3) .
grid of images


We are going to make a Grid like structure using Linear Layouts

We will make a structure as shown in the diagram below :
grid diagram

First we save the image used in ImageButton . I have used the image shown below, you can also use the same for practice .
a.png

The code for the layout is :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="268dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_marginBottom="20dp"
            android:layout_marginLeft="3dp"
            android:layout_row="0"
            android:orientation="vertical" >

            <ImageButton
                android:id="@+id/list"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:background="@drawable/a"
                android:gravity="center" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_marginLeft="60dp"
            android:layout_row="0"
            android:orientation="vertical" >

            <ImageButton
                android:id="@+id/notation"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:background="@drawable/a"
                android:gravity="center" />

        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="268dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_marginLeft="3dp"
            android:layout_row="1"
            android:orientation="vertical" >

            <ImageButton
                android:id="@+id/info"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_gravity="center"
                android:background="@drawable/a"
                android:gravity="center" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_marginRight="0dp"
            android:layout_marginLeft="60dp"
            android:layout_row="1"
            android:orientation="vertical" >

            <ImageButton
                android:id="@+id/about"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_gravity="center"
                android:background="@drawable/a"
                android:gravity="center" />

        </LinearLayout>
    </LinearLayout>

</LinearLayout>

Explanation of the code:
  • The Layouts outerupper and outerbelow are horizontally oriented which hold the buttons horizontally i.e. aside each other .
  • The layout outer is vertically oriented , hence it holds the two layouts(outerupper and outerbelow) vertically i.e. one below each other .

Stay Tuned with Made In Android

Published By:
Yatin Kode
on 30.6.14

Previous Page Next Page Home
Top