Not able to generate apk

If you are not able to build your project even after downloading and installing ADT properly then you must have missed Downloading Build tools from Android SDK Manager.

Open your Android SDK Manager and look for Android SDK Build-tools 

Image

 

If Android SDK Buil-tools is not installed then this is what blocking you from generating apk. Install it and it and everything should work fine.  Make sure that there is no compilation issues in your resource folder.

Possible errors while running test cases

While writing functional or unit test cases through Robotium or  InstrumentationTestCase if you get

TestCase class com.example.test.WelcomeActivityText is missing a public constructor with no parameters or a single String parameter

at run time then this must be because of your test project dint include a default constructor. That is

import com.example.WelcomeActivity;
import com.jayway.android.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;

public class WelcomeActivityText extends ActivityInstrumentationTestCase2<WelcomeActivity> {

    private Solo mSolo;
    public WelcomeActivityText() {
        super(WelcomeActivity.class);
    }
    
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mSolo = new Solo(getInstrumentation());
    }
    
    public void testWelcomeActivity(){
        assertEquals(WelcomeActivity.class, getActivity().getClass());
        mSolo.assertCurrentActivity("WelcomeActivity is not shown", WelcomeActivity.class);
    }

}

If you create a default constructor like the above code you can get ride of this error.

Invalid Package: ” could not be found or has no tests”

If you get this error means then please do check you have started your test case function with test as prefix (testWelcomeActivity).

Problem While triggering Notification

To create a notification refer this link.

Mostly everything should work perfectly. Suppose if you get the bellow error

Suppressing notification from package com.example.notify by user request  then it means that notification is blocked by the Android Framework since the Show notifications check box in Application Manager is unchecked. By default this check box will be checked but there are possibilities that this may get unchecked (I have seen its getting unchecked in one Samsung Nexus S device). Just checking the check box will solve your problem. Show Notifications is available inside Setting->Application Manager ->Downloads->Your Application

Application Manager App

Application Manager App

Restructuring DatePickerDialog

If you want to restrict your user not to enter date lesser then the current date from DatePickerDialog then you can use the below code as such :

try {
            DateTime dt = new DateTime(desiredDate, DateTimeZone.UTC);

            DatePickerDialog selectDatePicker = new DatePickerDialog(this, mDateSelectedListener, dt.getYear(),
                    dt.getMonthOfYear() - 1, dt.getDayOfMonth());
            java.lang.reflect.Field mDatePickerField = selectDatePicker.getClass().getDeclaredField("mDatePicker");
            mDatePickerField.setAccessible(true);
            DatePicker mDatePicker = (DatePicker) mDatePickerField.get(selectDatePicker);

            mDatePicker.init(dt.getYear(), dt.getMonthOfYear() - 1, dt.getDayOfMonth(),
                    new OnDateChangedListener() {

                        @Override
                        public void onDateChanged(DatePicker view, int year, int monthOfYear,
                                int dayOfMonth) {

                            if (isDateAfter(view)) {
                                Calendar mCalendar = Calendar.getInstance();
                                view.init(mCalendar.get(Calendar.YEAR),
                                        mCalendar.get(Calendar.MONTH),
                                        mCalendar.get(Calendar.DAY_OF_MONTH), this);
                            }
                        }

                        private boolean isDateAfter(DatePicker tempView) {
                            Calendar mCalendar = Calendar.getInstance();
                            Calendar tempCalendar = Calendar.getInstance();
                            tempCalendar.set(tempView.getYear(), tempView.getMonth(),
                                    tempView.getDayOfMonth(), 0, 0, 0);
                            if (tempCalendar.before(mCalendar))
                                return true;
                            else
                                return false;
                        }
                    });
            selectDatePicker.show();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

Motorola Droid2 problem with listview default behaviour

Have you tested your application on all the Android devices ? If you dint then you might be in risk. Some devices in android uses device custom theme to render all its application. Let me explain this with Motorola Droid2 device. I am trying to add a simple ListView in my app. Here is the code which i used to generate it.

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" >
 <ListView 
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:id="@+id/my_list"
 >

</ListView>
</RelativeLayout>

DroidFixes.java

public class DroidFixes extends Activity {
 CustomAlertDialog dialog;
 ListView list;
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 String s[] ={"1","2","3"};
 setContentView(R.layout.activity_main);
 list = (ListView)findViewById(R.id.my_list);
 ArrayAdapter<String> arr = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line,s);
 list.setAdapter(arr);
 }
}

This results in :

PROBLEM :

If you closely observer you would’ve found a grey background in the lower half of the ListView. This will not be seen in other devices are even in the emulator. This is the custom behavior of ListView which is implemented in Motorola Droid2. This happen because in the above xml layout ListView’s height is specified as wrap_content.

SOLUTION :

To avoid this there are many solution

  1. Change the ListView height attribute to wrap_content. Since the list content is small it couldn’t occupy the entire screen so the remaining space is filled with the device theam, setting the listview height attribute to wrap_content will wrap perfectly to the list content without occupying any extra space.
  2. If you insist to go with match_parent for listview height attribute then you can set android:overScrollFooter=”@null” this will remove the drawable in the ListView footer content.
  3. If this dint solve your problem then if you have any inflated custom row for this ListView set a background color to that view.

OUTPUT:

Applying Solution 1

Since i dint use any custom views or layout for this ListView row i am just changing the android:layout_height to “wrap_content” to attain this output.

Applying Solution 2

Things To Note:

Compare the solution 2 with 1. Since ListView’s height is left to match_parent the bottom most divider line is visible if you set it as wrap_content then you wont get the bottom most divider.

Hope this solves your problem :-)

Using this operator in Java

this is always a reference to the object on which the method was invoked. Consider this example

class ex1
{
    int x=1,y=1;
    public ex1(int x,int y)
    {
        this.x=x; //this.x refers to the class variable x not to the location function variable
        this.y=y; //for your better understanding in this place this operator can be considered as (this == e)
    }
    public void dis()
    {
        System.out.println("X: "+x+" Y: "+y+this.getClass().getName()); //getClass().getName() returns the object class name
    }
    public static void main(String args[])
    {
        ex1 e = new ex1(10,5);
        e.dis();
        ex2 e1 = new ex2();
    }
}
class ex2
{
    ex2()
    {
        System.out.println(this.getClass().getName());
    }
}

In this above program this operator is used to get the name of the current objects class. this operator cannot be used inside the static method. this operator can also be used to call other methods in the class.

OUTPUT:

X: 10 Y: 5ex1
ex2

Android Development Tools

Development in Android is made very easy by providing the ADT (Android Developer Tools) plug-in for Eclipse. This ADT plug-in provides GUI access for the command line tools.

Lets see about :

  • Logcat
  • Debugger
  • Drag and drop options

Debugger :

In Android Debug comes as an perspective. Open your .Java file from your project in Eclipse IDE and double click on the line where you want to set a break point.

The line int debug=0 is set as the debug line. This means that the program execution will be stopped when this particular line reaches.  Now press F11 key to start debugging. An Conform perspective switch window will appear requesting you to switch to debug perspective. Hit yes to switch to debug perspective. Press F5 to step into the debugging point.

Debugging point

At this point the debug value is 0 which is denoted in the variables window. Press F6 to go to the next line. Now the debug value changes to 1 because of the increment.

Press F8 to completely Run the code.

Drag and Drop :

In Android front end design can be simply done by dragging and dropping. Instead of typing for each and every Views in a design this helps to finish your work soon. To make use of the drag and drop option first right click on the .xml file in your project. Click on the open with -> Android Layout Editor and select Graphical Layout in the bottom of the screen.

Just drag and drop the views from the left widget and place it over the right side screen. There is an excellent video in the developers site regarding Development Tools in android this will help you to understand the various features which will help you save time in designing.

 

Complete Steps to install Android in Ubuntu

Just go through Installation and Hello world pdf for step by step  procedure for setting up development environment in your Ubuntu operating system.  Since Eclipse is an common IDE most of the steps remains same for other operating systems.

Recommended Software:

  • Java 6
  • 32-bit Eclipse IDE (classic or Java)
  • ADT
  • Android SDK

All the software mentioned above are open source software (*you have open-java) :) .

Go through the pdf for Installation steps.

Using android:layout_weight attribute

As discussed earlier i have found an temporary solution which can be used for your design to occupy your total screen space in a proportionate way. android:layout_weight attribute can be used to allocate the size of the View according to the screen size.  If we use android:layout_width=”100dp” or android:layout_width=”wrap_content” only the specified space is occupied.

Problem:

Consider that you need to place 2 buttons at the end of the screen which occupies the entire bottom part. If you use layout_width=150dp respectively for both the buttons it will look like this in 3.7″ screen and it gives an impression that the screen is occupied wisely.

Run the same code in 7″ screen even though you have used dp for setting the size of the buttons, it wont full fill your current desire. The output will look like

Solution:

Consider the bottom most part as an individual layout and add the layout_weight attribute to scale up the View elements according to the screen size.

To use layout_weight the layout_width or height of the layout must be using fill_parent

In the bellow example android:layout_width=”fill_parent” which denotes that weight is applied only to the width of the particular view not to it height. weightSum attribute take up the entire space of the layout which has to be split in an integer value, in our case we have only 2 buttons which occupies equal width, so weightSum is set to 2 and the layout_weight value of the views is specified as 1 for both the views. Each view occupies 1/2 (layout_weight/weightSum) of the space.

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout
xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
<LinearLayout android:layout_alignParentBottom=”true”
android:orientation=”horizontal”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:baselineAligned=”true” android:weightSum=”2″
>
<Button
android:layout_width=”0dp”
android:layout_height=”wrap_content”
android:text=”Button1″
android:layout_weight=”1″
/>
<Button
android:layout_width=”0dp”
android:layout_height=”wrap_content”
android:text=”Button2″
android:layout_weight=”1″
/>
</LinearLayout>
</RelativeLayout>

Output:

Now you are able to get the desired design across multiple screens with a single xml file. layout_weight will be a useful attribute if you use it wisely.