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.

Advertisements

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).

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 🙂