Sunday, August 21, 2016

Android SDK Manager not opening (Solution)

Well, been a crazy week! 5 whole week days, trying to fix the SDK manager bug. Being spatted with the error, "Failed to execute android.bat" and "Cannot find file android.bat". and finally, possibilities come to my rescue.

Here is the solution:

1. Locate "android.bat" in your "..............\Android\sdk\tools" folder
2. Right-click and edit on text editor
3. Change set java_exe=   to set java_exe=%JAVA_HOME%\bin\java.exe
4. Delete for /f "delims=" %%a in ('"%java_exe%" -jar lib\archquery.jar') do set swt_path=lib\%%a. Replace it with set swt_path=lib\x86_64. 86_64, for a 64-bit os.
5. Open terminal cmd and type android. Should open up the SDK manager tool window.

Good luck

Tuesday, May 3, 2016

Dialog Animations in Android

Been working on a project for my client. A value add implementation of  dialog sliding from bottom to middle of screen literally made my day. Too good not to share and so here it goes. For, confidentiality,i'm not sharing the screen shot. But, i can assure you that this code will smooth glide into your layouts and class file elements.

Here is my pusedo code:
1. Create dialog layout and position it over but at bottom of parent screen.
2. The dialog needs to smoothly glide from bottom to middle of the screen.

Here is how it is done:
1. Create a anim folder in res (res/anim).
2. Create two animation resources
2a. Slide_up_dialog
<pre class="brush: csharp">
xml version="1.0" encoding="utf-8"?> <
set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate       
      android:duration="@android:integer/config_mediumAnimTime"     
       android:fromYDelta="0%p"       
    android:interpolator="@android:anim/accelerate_interpolator"     
     android:toXDelta="0" /> 
</set>
</pre>
2a. Slide_up_dialog
<pre class="brush: csharp">

xml version="1.0" encoding="utf-8"?><
set xmlns:android="http://schemas.android.com/apk/res/android" >
 <translate      
    android:duration="@android:integer/config_mediumAnimTime"     
     android:fromYDelta="0%p"      
     android:interpolator="@android:anim/accelerate_interpolator"      
    android:toYDelta="25%p" />
</set>
</pre>
3. Create Style resources in Styles.xml
<pre class="brush: csharp">

<style name="DialogAnimation">
    <item name="android:windowEnterAnimation">@anim/slide_up_dialog</item>
    <item name="android:windowBackground">@null</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowNoTitle">true</item>
</style>
</pre>
4. Extend the DialogAnimation style into your DialogTheme Style (on Same Page)
<pre class="brush: csharp">
<style name="DialogSlideAnim" parent="@android:style/Theme.Dialog">
    <item name="android:windowAnimationStyle">@style/DialogAnimation</item>
    <item name="android:windowExitAnimation">@anim/slide_out_down</item>
</style>
</pre>
4. Java Code for Animating XML elements of Dialog
<pre class="brush: csharp">
//First the Dialog object creation
final Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.calendar_dialog);
//Second implement the following code to implement the animation
Window window = dialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.windowAnimations = R.style.DialogAnimation;
//to keep the dialog window to the bottom of the parent screen
wlp.gravity = Gravity.BOTTOM;
wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);
//Finally
dialog.show();
</pre>
Sit back and treat yourself for the dialog animation you've just created ;-)

Saturday, February 27, 2016

Calculate Age from Date of Birth (Java/Android SourceCode)

Here is the concept for calculating Age from a user defined or selected Date of Birth. I've used Ecplise IDE to develop an android application. But, the MainActivity.class source code can also be used in Java.

Steps in Android:

A. Create XML layout with
  • DataPicker (pre-defined data object)
  • Button (onClick that will handle setting values within respective text view objects)
  • TextView (object to get DOB)
  • TextView (object to show Age)
B. Create Package-Class called MainActivity

0. Import the necessary widgets
  • import java.util.Calendar;
  • import android.app.Activity;
  • import android.os.Bundle;
  • import android.view.Menu;
  • import android.view.View;
  • import android.view.View.OnClickListener;
  • import android.widget.Button;
  • import android.widget.DatePicker;
  • import android.widget.TextView;
  • import com.example.datedemo.R;
1. Initiate DataPicker, TextView elements by importing declaring and importing pre-defined respective classes
  •  DatePicker picker;
  • TextView tvdob;
  • TextView tvage;
2. Override onCreate bundle and setContentView
  • setContentView(R.layout.date);
3. Allocate Memory for initiated objects within onCreate bundle
  • btnsubmit=(Button)findViewById(R.id.button1);
  • picker =(DatePicker) findViewById(R.id.datePicker1);
  • tvdob=(TextView)findViewById(R.id.tvdob);
  • tvage=(TextView)findViewById(R.id.tvage);


  • 4. Create OnClick event handler within onCreate bundle
  • btnsubmit.setOnClickListener(new OnClickListener() {
  • @Override
    public void onClick(View view) {
    // TODO Auto-generated method stub
    //getDateofBirth() receives values from the custom method declared public below
    tvdob.setText(getDateofBirth());
    //currentDate() receives values from the custom method declared public below
    tvage.setText(currentDate());

    }
    });
    5. Create custom method to receive Date of Birth from the picker object values selected:
    • public String getDateofBirth(){
      StringBuilder builder=new StringBuilder();
    builder.append("DOB: ");
    builder.append((picker.getMonth() + 1)+"/");//month is 0 based
    builder.append(picker.getDayOfMonth()+"/");
    builder.append(picker.getYear());
    return builder.toString();
        }
    5. Create custom method to receive Current Date Values from Calendar Object (CALENDAR WIDGET SHOULD BE IMPORTED):
    • public String currentDate(){
    StringBuilder todaydate=new StringBuilder();
    //get today's date
    Calendar today=Calendar.getInstance();
    //calculate age by subtracting today's date year from the user's (picker object) selected date year 
    int age=today.get(Calendar.YEAR)-picker.getYear();
    if (today.get(Calendar.MONTH) < picker.getYear()) {
     age--;  
    } else if (today.get(Calendar.MONTH) == picker.getYear()
       && today.get(Calendar.DAY_OF_MONTH) < picker.getYear()) {
     age--;  
    }
    todaydate.append("Age: ");
    todaydate.append(String.valueOf(age));
    return todaydate.toString();
    }
    C. Declare the manifest file with the the and details.
    D. Run the application and sip on a cup of coffee, while you take pride in the new bit code you executed and learnt :-)

    ________________________________________________________

    Just in case, the lazy boy in you wants just to copy/paste the code and perform a quick fix, here following is the full source code for MainActivity.class:

    //source beings here
    package com.datepick; //your Gen package name

    import java.util.Calendar;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.DatePicker;
    import android.widget.TextView;

    import com.example.datedemo.R; //your package name

    public class MainActivity extends Activity{
    //Create Instance 
    DatePicker picker;
    TextView tvdob;
    TextView tvage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.date);
    //Allocate memory
    Button btnsubmit=(Button)findViewById(R.id.button1);
    picker =(DatePicker) findViewById(R.id.datePicker1);
    tvdob=(TextView)findViewById(R.id.tvdob);
    tvage=(TextView)findViewById(R.id.tvage);
    btnsubmit.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
    // TODO Auto-generated method stub
    tvdob.setText(getDateofBirth());
    tvage.setText(currentDate());
    }
    });
    }
    public String getDateofBirth(){
      StringBuilder builder=new StringBuilder();
    builder.append("DOB: ");
    builder.append((picker.getMonth() + 1)+"/");//month is 0 based
    builder.append(picker.getDayOfMonth()+"/");
    builder.append(picker.getYear());
    return builder.toString();
        }
    public String currentDate(){
    StringBuilder todaydate=new StringBuilder();
    Calendar today=Calendar.getInstance();
    int age=today.get(Calendar.YEAR)-picker.getYear();
    if (today.get(Calendar.MONTH) < picker.getYear()) {
     age--;  
    } else if (today.get(Calendar.MONTH) == picker.getYear()
       && today.get(Calendar.DAY_OF_MONTH) < picker.getYear()) {
     age--;  
    }
    todaydate.append("Age: ");
    todaydate.append(String.valueOf(age));
    return todaydate.toString();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
    }

    }
    //Code Ends

    Make sure to create xml layout elements and Manifest file declarations before running the application.