Friday, 29 January 2016

Android Tutorials - Linear Layout

Linear layout is a container for andoid ui components.  It helps us to arrange components in either horizontal (default) or vertical direction.

Frequently used attributes in a linear layout are the following.  A fair understanding of them will help us create a gui easily and use them at the appropriately.

android:orientation - horizontal (default )  or vertical

android:weightSum - This attribute works with android:layout_weight in the child components.  This attribute contains the sum of the layout_weight attributes in its children. The range is (0.0 to 1.0).  eg. if weightSum in the linearlayout parent has 1.0 and it has two child buttons, and each of them have a layout_weight value of 0.5.  They will be allocated equal space in its parent.  If the two have values (0.25 & 0.75) respectively and its parent has 100 pixels of horizontal space, the children will be allocated 25 pixels and 75 pixels respectively.  

android:gravity: This attribute decides where the child components will placed in the rectangular space that it has.  The possible values are ( top, left, right, bottom, center_horizontal, center_vertical, center, fill, fill_horizontal, fill_vertical, start, end ... ).  

center = center_horizontal + center_vertical.

Component placed in the top left corner should have value as( top | left). 

The following is a layout resource file for a sample activity which I created to help understand these basic attributes.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/topLevelContainer"
    android:weightSum="1.0">
    <LinearLayout
        android:id="@+id/firstChildContainer"
        android:layout_weight="0.5"
        android:gravity="center_horizontal|center_vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:text="Button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:text="Button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/secondChildContainer"
        android:layout_weight="0.5"
        android:gravity="center_vertical|right|center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:text="Cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:text="OK"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>



The above example contains 3 LinearLayouts.  A root container with two child containers and each of them have two buttons respectively. 

Top Level Linear Layout ( which illustrates android:orientation and android:weightSum).
The First & Second  child container  ( which illustrates android:gravity ).

The following screen shot says it all.




Thursday, 7 May 2015

JDK registry problem / issue solved in Windows 7

How to run a lower version of java when we have multiple jdk versions installed.


Please change the files in the following directory.

Step 1: Change directory to C:\ProgramData\Oracle\Java\javapath
Step2: Here you will find 3 exe files ( java.exe and javaw.exe and javaws.exe).  All three files are symbolic links to the actual distribution of java
Step3: Take a back up of these files in a directory of your choice.
Step4: delete all three files present in "C:\ProgramData\Oracle\Java\javapath"
Step5: Create new symbolic links for all 3 files with your distributed version.

In my case, I had installed jdk1.6, jdk1.7 and jdk1.8.  After lot of digging around I found that it is these 3 files which are actually getting referred.  So I created 3 symbolic links for the following files.\

The syntax is    mklink <linkname>  <actual exe name>

From under c:\ProgramData\Oracle\Java\javapath directory execute the following commands.

mklink java.exe c:\program Files(x86)\java\jdk1.6.0_24\bin\java.exe
mklink javaw.exe c:\program Files(x86)\java\jdk1.6.0_24\bin\javaw.exe
mklink javaws.exe c:\program Files(x86)\java\jdk1.6.0_24\bin\javaws.exe


Now go to any directory and type java -version, it will return only the version which you have linked ( in my case it is jdk1.6.0_24).

Thanks
Edwin.


Wednesday, 8 October 2014

Java - Varargs

Varargs is a very good feature introduced in Java 1.5.  This feature allows programmers to pass variable number of argument of same type to a method.  Some may confuse it with overloading.  But it is actually different.  We will learn the differences between overloading and varargs below.

Difference between method overloading and varargs

Overloading.
1. Method signature may contain arguments of different data types.
Eg. public long add(int i,long l)



Varargs
1. varargs parameter in method signature will be of same data type.
    public long add(long l1, long ... numbers)

2. The varargs parameter should be last of the arguments to the method. If a method has only one parameter of varargs type, it is allowed.

Eg 1. Please see the above example.
Eg 2. Invalid code.
       public long add(long ... numbers, long l1)

Eg. 3 public long add(long ... numbers)


Code Sample:



public class VarargsDemo {
    
    /*
      The following method uses varargs parameter (numbers).
      Java considers varargs parameter as an array.
      So the variable numbers can be used as an array of integers.
    */

public int add(int n1)
{
System.out.println("Calling add method with 1 argument : ");
return n1;
}

public int add(int n1,int n2)
{
System.out.println("Calling add method with 2 arguments : ");
return n1 + n2;
}

public int add(int n1,int n2,int n3)
{
System.out.println("Calling add method with 3 arguments : ");
return n1 + n2 + n3;
}

    public int add(int n1,int ... numbers)
{
System.out.println("Calling add method varargs as argument : ");
int sum=n1;
for(int i=0;i<numbers.length;i++) {
sum = sum + numbers[i];
}
return sum;
}

public double average(double n1, double ... numbers)
{
double sum=n1;
for(int i=0;i<numbers.length;i++) {
sum = sum + numbers[i];
}
return sum/(numbers.length+1);
}

public static void main(String[] args)
{
VarargsDemo vd=new VarargsDemo();
// Sample usage of add Method
System.out.println("The sum of all numbers is : "+vd.add(1,2));
System.out.println("The sum of all numbers is : "+vd.add(1,2,3,4));
System.out.println("The sum of all numbers is : "+vd.add(1));
System.out.println("The sum of all numbers is : "+vd.add(10,11,12,1000,12,11,5000,12000));

// Sample usage of average method which uses varargs
System.out.println("The average of all numbers is : "+vd.average(1.25,2.75));
System.out.println("The average of all numbers is : "+vd.average(1.0,2.0,3.5,4.75));
System.out.println("The average of all numbers is : "+vd.average(1.0));
System.out.println("The average of all numbers is : "+vd.average(10.0,11.12,12.23,1000.34,12.45,11.56,5000.0,12000.0));
}
}


Output: 


Calling add method with 2 argument : 
The sum of all numbers is : 3
Calling add method varargs argument : 
The sum of all numbers is : 10
Calling add method with 1 argument : 
The sum of all numbers is : 1
Calling add method varargs argument : 
The sum of all numbers is : 18056
The average of all numbers is : 2.0
The average of all numbers is : 2.8125
The average of all numbers is : 1.0
The average of all numbers is : 2257.2125


Learning:

1. When we have two methods a. overloaded method and b. varargs method,  when we invoke a method with number of arguments which can fit into the overloaded method, then  the overloaded method will be called.  

Eg. In the above code, both the following methods can be used to invoke add() with 2 arguments.  If we invoke add(10,20), the method (a) will be called instead of method (b).

a. add(int n1,int n2) 
b. add(int n1,int ...numbers) 

Mysql - Alter/Remove default value of a column

Mysql Tech Byte: 
We can remove just the default values of a column(s) in Mysql using the following command. 

Single Column: 
ALTER TABLE < TableName > ALTER < ColumnName > DROP DEFAULT; 

If we execute the above statement, the default value for that particular column will be dropped. 

Multiple Columns: 
ALTER TABLE < TableName > ALTER < ColumnName1 > DROP DEFAULT, ALTER < ColumnName2 > DROP DEFAULT; 

Modify a default value: 

ALTER TABLE < TableName > ALTER < ColumnName1 > DROP DEFAULT, ALTER < ColumnName2 > SET DEFAULT 100; 

This statement removes a default value for ColumnName1 and alters the DEFAULT value of ColumnName2 from its previous value to the new value 100.

Wednesday, 3 October 2012

Language & Country in C#

1. To get the language in full Text like (English/Dutch) use the following code.

CultureInfo.CurrentCulture.Parent.DisplayName

2. To get language in two letter form like (en/ja . .) use the following code.

CultureInfo.CurrentCulture.Parent.Name;

3. To get Country in full Text like "United States/Japan" use the following Code

RegionInfo.CurrentRegion.DisplayName;

4. To get Country in two letter format like (US/JP) use the following code.

RegionInfo.CurrentRegion.Name;

Hope this helps someone.

Have a nice day.
Edwin.

Friday, 23 March 2012

MFC Dialog - Simulate a Mouse Click


Beginner Series.

Sometimes when you are trying to select a row in a listbox (CListBox) which is place in your by name <YourDialogName>, you might do all that should be done to select a row programatically.  But when nothing else works, here is a short cut.  You might notice that if you click on an entry in a list box, you selchanged method will be called.  But when you do a "SetCurSel", it might not be called, because your row will not get selected.  In that case the following short cut works.  You may vary the time interval given in Sleep method based on your mouse configuration.

void <YourDialogName>::SelectRow(int row)
{
     //COMMENT: listBox_URLis the name of the list box, whose row should be selected.


     RECT r; 

     listBox_URL.GetItemRect(row,&r);

     int x=r.left + 10, y= r.top+5;
     const int lParam=y<<16 | x;  // Infuse the X & Y co-ordinates into the lParam
     const int wParam=0;
     listBox_URL.SendMessage(WM_LBUTTONDOWN,wParam,lParam);
     Sleep(20);
     listBox_URL.SendMessage(WM_LBUTTONUP,wParam,lParam); 
}

MFC Dialog - Escape/Enter key disable

Beginner Series


When we start to program using an MFC Dialog, handling the Escape key and Enter key seems to be a challenge.  One way is to override the PreTranslateMessage of the Dialog.
Following is a code snippet


Please replace the <DialogName> with the actual name of the dialog which you have created.  


1. In the header file <DialogName>.h file add the following method, under the protected access modifier. 


virtual BOOL PreTranslateMessage(MSG* msg);


2. In the <DialogName>.cpp file add the following code



BOOL <DialogName>::PreTranslateMessage(MSG* msg)
{
       //COMMENT: Get the ID from which this message is received.
         // If the control matches, then see whether the Windows message is KEYDOWN. 
         // Then check whether the key pressed is Enter key.  Return false.  
         // By returning false, we are instructing the dialog to avoid the default behavior, which might 
         // be to close the dialog.


UINT ctrlID=GetDlgCtrlID(msg->hwnd);
if(ctrlID==<YOUR_CONTROL_ID> )  
{
if(msg->message == WM_KEYDOWN)
{
if(msg->wParam==VK_ENTER)
{
return FALSE;
}
}
}

if(msg->message == WM_KEYDOWN)
{
if(msg->wParam == VK_ESCAPE)
{
return FALSE;
}
}
      // For all the others, please go ahead with the default behavior.  

      return CDialog::PreTranslateMessage(msg);
}