## Bridge Design Pattern

Name: Bridge Design Pattern

Type: Structural Design Pattern

Purpose: Decouple abstraction from implementation
(In other words, using composite over inheritance)

Sample Problem and Solution:
Assume that you are required to create some Shapes with two different patterns. Some sample shapes are SolidColorCircle, GradientCircle, SolidColorSquare, and GradientSquare. A possible design is provided below.
public interface Shape {
public void draw();
}
public abstract class Circle implements Shape {

}

@Override
public void draw() {
}
}
public class SolidColorCircle extends Circle {
private String color;

public SolidColorCircle(double radius, String color) {
this.color = color;
}

@Override
public void draw() {
super.draw();
System.out.println("Fill with: " + color);
}
}
public class GradientCircle extends Circle {
private String colorX;
private String colorY;

public SolidColorCircle(double radius, String colorX, String colorY) {
this.colorX = colorX;
this.colorY = colorY;
}

@Override
public void draw() {
super.draw();
System.out.println("Fill with: " + colorX + " & " + colorY);
}
}
Square, SolidColorSquare, and GradientSquare are provided in GitHub.

Even though this tutorial is named as Advanced Android Database, there is nothing advanced in Android database. The last Basic Android Database tutorial explains the quick and dirty way of using SQLite database in your application. This tutorial uses the same problem discussed in the last article, but to explain the recommended way of database access in Android.

The Create, Read, Update and Delete (CRUD) related methods are not explained in this article since those are already explained in the Basic Android tutorial. If you are new to Android Database, please read the Basic Android Database article first and then continue this article in order to learn the best practices.

## Install Apache Tomcat on Ubuntu

Step 1:

Step 2:
Open the Terminal (Ctrl + Alt + T) and enter the following command to change the directory.
cd /opt/

Step 3:
Enter the command given below to extract the Tomcat from  the ~/Downloads directory. If your downloaded file is in any other directory, replace the last parameter by the actual file path.

Step 4: (Optional)
Rename the folder name to apache-tomcat.
sudo mv apache-tomcat-8.0.26/ apache-tomcat/

Step 5:
Tomcat creates some files at the runtime inside this folder (Log files and some other configuration files). If you want to start Tomcat without root privilege, it is required to change the permission of this directory. Enter the following command to change the permission of apache-tomcat folder.
sudo chmod -R 777 apache-tomcat/

Step 6:
An environment variable has to be added to the system. Enter the following command in the terminal to open the /etc/environment in gedit.
sudo gedit /etc/environment

Step 7:
Add the following line at the end of the file. Save and close the gedit.
CATALINA_HOME="/opt/apache-tomcat"

The /etc/environment before the modification.

The /etc/environment after the modification.

Step 8:
Reload the environment variables to the current terminal using this command.
source /etc/environment

Step 9:
Enter the following command to start the Tomcat server.
\$CATALINA_HOME/bin/startup.sh

Step 10:
Visit to the following URL.
http://localhost:8080/

If you get the Apache Tomcat home page, you have successfully installed the Apache Tomcat in your system.

Step 11:
To stop the Tomcat, enter the following command.
\$CATALINA_HOME/bin/shutdown.sh

##### Integrate Tomcat with Eclipse
If you do not have the Eclipse, follow this link and install the Eclipse first.

Step 1:
Open the Eclipse.

Step 2:
Goto Windows → Preferences in the menu bar and select the Runtime Environments under the Server.

Step 3:

Step 4:
Select your Apache Tomcat version under the 'Apache' root and click 'Next'.

Step 5:
Browse and select the Tomcat installation directory.

Step 6:
Click on the 'Finish' button

Step 7:
Click on the 'OK' button to close the Preferences dialog.

Now you are ready to use Apache Tomcat from Eclipse.

## Install the latest Eclipse in Ubuntu

Step 1:

Step 2:
Open the Terminal (Ctrl + Alt + T) and enter the following command to change the directory.
cd /opt

Step 3:
Enter the command given below to extract the Eclipse from ~/Downloads directory. If your downloaded file is in any other directory, replace the last parameter by the actual file path.

Step 4:
Open another Terminal (Ctrl + Alt + T) and enter the following command to create a shortcut file for eclipse.
gedit eclipse.desktop

Step 5:
In the opened gedit, copy and paste the following text.
[Desktop Entry]
Name=Eclipse
Type=Application
Exec=/opt/eclipse/eclipse
Terminal=false
Icon=/opt/eclipse/icon.xpm
Comment=Integrated Development Environment
NoDisplay=false
Categories=Development;IDE;
Name[en]=Eclipse
Name[en_US]=Eclipse

Step 6:
Save and close the gedit.

Step 7:
Enter the following command in the terminal to install the shortcut.
sudo desktop-file-install eclipse.desktop

Now search for Eclipse in the dashboard and open it.

In Ubuntu, the default tooltip background is black, which makes it unreadable in Eclipse. To fix this problem, follow these steps.

Step 1:
Enter the following commands one by one, in a Terminal
sudo gedit /usr/share/themes/Ambiance/gtk-2.0/gtkrc
sudo gedit /usr/share/themes/Ambiance/gtk-3.0/settings.ini
sudo gedit /usr/share/themes/Ambiance/gtk-3.0/gtk-main.css

Change the values of tooltip_fg_color and tooltip_bg_color in all the files, to the following values and save the changes.
tooltip_fg_color:#000000
tooltip_bg_color:#f5f5c5

Step 2:
Restart the Eclipse.

## Factory Design Pattern

Name: Factory Design Pattern

Type: Creational Design Pattern

Purpose:
• Hide object creation logic
• Decouple classes from the clients

Sample Problem and Solution:
Consider a problem where you are required to create a library which provides some 2D Shapes. Other developers will use your library to create some shapes with random measurements. A possible design is provided here.
package com.javahelps.shapes;

public interface Shape {
public void draw();
}
package com.javahelps.shapes;

public class Circle implements Shape {

}

@Override
public void draw() {
System.out.println("Circle");
}
}
package com.javahelps.shapes;

public class Square implements Shape {
private int width;

public Square(int width) {
this.width = width;
}

@Override
public void draw() {
System.out.println("Square");
}
}
package com.javahelps.shapes;

public class Rectangle implements Shape {
private int width;
private int height;

public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}

@Override
public void draw() {
System.out.println("Rectangle");
}
}

## Android Spinner

In Android combo box is called as Spinner. This tutorial shows you, how to use Spinner in your application.

Step 1:
Create an Android project with a project name “Spinner Sample” and a package name “com.javahelps.sample.spinner”.

Step 2:
Modify the activity_main.xml as shown below. Notice that there is a UI component Spinner which is the combo box used to display some names of animals.
<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">

<TextView
android:id="@+id/txtInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Select an animal"
android:textAppearance="?android:attr/textAppearanceMedium" />

<Spinner
android:id="@+id/cmbAnimals"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/txtInfo" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/cmbAnimals">
<ImageView
android:id="@+id/imgAnimal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</RelativeLayout>

</RelativeLayout>
Step 3:

Right click on the drawable folder and select “Paste”.

## Custom Font in Android

This tutorial shows you, how to develop an application in your local language. Since some Android phones have problems with Unicode, you can use custom true-type fonts for your application. The same technique can be used to decorate your application using stylish English fonts as well. This tutorial develops an application to display a welcome message in Tamil. You can use your own font to display whatever you want.

Step 1:
Create an Android application “Custom Font” with a package name “com.javahelps.customfont”.

Step 2:
Add a TextView to the activity_main.xml as shown below.
<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">

<TextView
android:id="@+id/txtMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

Step 3:
Right click on the “res” folder and select NewFolderAssets Folder.

## Immutability Of String

Java defines some objects as immutable objects; developers cannot modify those objects once they are created. String objects and wrapper class objects are the best examples for immutable objects. This article covers the immutable objects and the reason for having immutable objects using String as an example.

Before getting into the actual content, it is required to have a basic knowledge about the equality operator (==). Usually equality operator is used to compare two primitive data types, but it has a different meaning when it is used with references. To compare the content of two objects, equals method is used and the equality operator is used to check whether two references are referring same object.
public class EqualityOperator {
public static void main(String[] args) {
Student stu1 = new Student(100);
Student stu2 = new Student(100);
Student stu3 = stu1;
if (stu1 == stu2) { // false
System.out.println("stu1 == stu2");
}
if (stu1.equals(stu2)) { // true
System.out.println("stu1 equals stu2");
}
if (stu1 == stu3) { // true
System.out.println("stu1 == stu3");
}
}
}
For the complete project click on this link.

In the above example stu1 == stu2 returns false, because they refer two different objects. stu1 == stu3 returns true because both references are referring the same object.

## Android: Simple Calculator

This tutorial guide you to develop a basic Calculator Android application in Android Studio.

NOTE: There is a new article written on How to develop an Android calculator application using Kotlin: Android: Simple Calculator in Kotlin. The new article is the Kotlin version of this article. If you like to start with Kotlin, the recent article is recommended for you.

Step 1:
Create a new Android application project with an application name: “Calculator” and package name: “com.javahelps.calculator”.

Step 2:
By default, Android uses a green Android robot icon. In this project, we are going to use a custom application icon. Therefore, delete the default ic_launcher icon set from the “mipmap” folder.

Step 3:
Get any PNG image file for the application icon. (It is recommended to have a minimum size 256x256 pixels). This icon is used to display in the Google Play as well as in the applications menu of Android devices.

Step 4:
Right click on the “mipmap” folder and select New → Image Asset

Step 5:
Browse and select your icon as the image file and click on Next → Finish buttons. (Make sure that the resource name is: ic_launcher)

Step 6:
Replace the content of the activity_main.xml file by the following code. This code creates a TextView as the calculator number screen and some necessary buttons. TextView is used instead of EditText, in order to prevent manual user input using the default keypad of Android. In this code, some common properties of Buttons are not provided to reduce the length of this tutorial. In your code make sure that you have included these four attributes for all the Buttons.
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="30sp"

Name

Email *

Message *