How to round corners and create a border for a TextView or EditText

Step one 

Create a Android Resource Directory called drawables within the res file if you don’t already have one.

Step two

Make a new xml page called whatever you like. I called mine rounded_corner_background.

Step three

Within the xml page you have just created add:

<?xml version="1.0" encoding="UTF-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffffff"/> <!--Background color-->
    <corners android:radius="5px"/>
    <stroke android:width="1dip" android:color="#000"/> <!--outline stroke -->
    <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>

Beginning SQLite in Android

My first assumption of SQLite in Android was, there is a lot of code to do very little. Once you get started though its quite easy.

I’m going to step through how to set up the database connection initially and insert and delete some data from it.

CLICK HERE FOR COMPLETE PROJECT
Within the complete project I have provided examples of how to use the methods which are created bellow.

SETTING UP DATABASE CLASS

1. Create a database class and make it extend SQLiteOpenHelper

2. Create a variable for the database version, something like this:

private static final int DATABASE_VERSION = 1;

3. Give you database a name:

private static final String DATABASE_NAME = “imAnExample”;

4. Create your tables names. You can have as many as you like. Within this example I am only going to have one table.

private static final String TABLE_VEGETABLES = “vegetables”;

5. Now create your columns for the tables:

private static final String KEY_ID = “id”;
private static final String KEY_VEG_TYPE = “type”;

6. Create the constructor for the class it should look something like:

public DatabaseConnection(Context context)
{
   super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

7. Creating the tables in the onCreate method. This is where we will be setting up the columns to use and and key etc. It should look something like:

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase)
{
   String CREATE_VEGETABLES_TABLE = "CREATE TABLE " + TABLE_VEGETABLES + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_VEG_TYPE + " TEXT" + ")";
   sqLiteDatabase.execSQL(CREATE_VEGETABLES_TABLE);
}

8. If the database is upgraded and you don’t want to keep any of the existing content add in a method similar to this:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
  db.execSQL("DROP TABLE IF EXISTS " + TABLE_VEGETABLES);
  onCreate(db);
}

Before I continue with this class. I am going to first make a class to retain the data before it is inserted into the database. This is just simply a class of setter and getter methods. e.g. for this example there should be one method for getting/setting the id and one for getting/setting the vegetable name. This class can be found within the complete example.

ADDING/REMOVING/RETURNING ALL FROM THE DATABASE

9. Adding a new vegetable to the database. In this example I have not specified an id, by not doing this the database will take care of it.

// Adding new vegetable to the table
void addVegetable(GettingVegetableData vegetableData)
{
  SQLiteDatabase db = this.getWritableDatabase();

  ContentValues values = new ContentValues();
  values.put(KEY_VEG_TYPE, vegetableData.getVegetableName()); // vegetable Name
 
  // Inserting Row
  db.insert(TABLE_VEGETABLES, null, values);
  db.close(); // Closing database connection
}

10. Returning all of the vegetables in a list from the database.


// Getting All vegetables
public List gettingVegetableDataList()
{
     List listOfVegetables = new ArrayList();
     // Select All Query
     String selectQuery = "SELECT  * FROM " + TABLE_VEGETABLES;

     SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

      // looping through all rows and adding to list
      if (cursor.moveToFirst())
      {
           do
           {
               GettingVegetableData vegetableData = new GettingVegetableData();
               vegetableData.setId(Integer.parseInt(cursor.getString(0)));
               vegetableData.setVegetableName(cursor.getString(1));

               // Adding vegetableData to list
               listOfVegetables.add(vegetableData);
           }
           while (cursor.moveToNext());
      }

      // return contact list
      return listOfVegetables;
}

11. There is two possible ways in order to delete a vegetable from the database. By its ID or its name. I have shown the two ways bellow: 

// Deleting single vegetable by id
public void deleteVegetableByID(int id)
{
      SQLiteDatabase db = this.getWritableDatabase();
      db.delete(TABLE_VEGETABLES, KEY_ID + "= " + id, null);
      db.close();
}

// Deleting single vegetable by name
public void deleteVegetableByName(String name)
{
      SQLiteDatabase db = this.getWritableDatabase();
      db.delete(TABLE_VEGETABLES, KEY_VEG_TYPE + "= " + name, null);
      db.close();
}

Back again after a while!

I haven’t managed to make a blog post in months unfortunately! however, now that I am done with University I have more time on my hands and have decided to take up blogging again.

Over the past few months I’ve been doing a range of projects within University, these include 2D and 3D games both made on Unity I’ll post these within a few days. I also completed a web project which was quite simple, the theme was Easter.

I have also began my placement year working at a local app development company within Belfast, which is very exciting.

Bit Manipulation Variable Swapping – Java

A simple program which uses bit manipulation to swap out the variables. A benefit of this solution is that it works for more data types than just integers. The code words by using XORs.

 

public class BitManipulationVariableSwapping 
{
    public static void swap_opt(int a, int b)
    {
        a = a^b;
        b = a^b;
        a = a^b;

        System.out.println("a:" + a);

        System.out.println("B:" + b);

    }//swap

    public static void main(String [] args)
    {
        swap_opt(100, 200);
    }//main
}//class

Matching Brackets in the expression – C++

This was an extra programming exercise within university that I completed not so long ago. It was a Microsoft interview question also. I chose to implement it in c++.

The actual question which I was presented with:

Question

#include <iostream>
#include <string>
#include <stack>

using namespace std;

//setting up the stack
stack<char> stacking;

void printMyInfo()
{
	cout<<"***************************************"<<endl;
	cout<<"* Student Name: Zara McKeown          *"<<endl;
	cout<<"* Registration:                       *"<<endl;
	cout<<"* Course: Data Structures             *"<<endl;
	cout<<"***************************************"<<endl<<endl;
}//printMyInfo

int checkBraces(char *characters)
{
    // Continues to push or pop using a for loop
    // \0 means null termination character
    for (int i = 0; characters[i] != '\0'; i++)
    {
        //seeing if the current character is a ( bracket
        if(characters[i] == '(')
        {
            //pushing the character into the stack
            stacking.push(characters[i]);
        }//if
        
        //seeing if the current character is a { bracket
        else if(characters[i] == '{')
        {
            //pushing the character into the stack
            stacking.push(characters[i]);
        }//else if
        
        //seeing if the current character is a [ bracket
        else if(characters[i]=='[')
        {
            //pushing the character into the stack
            stacking.push(characters[i]);
        }//else if
        
        //Checking to see if the next character is ) bracket
        else if(characters[i] == ')')
        {
            //making sure there is something in the stack already
            //if not it returns false
            if(stacking.empty())
            {
                cout << "0";
                return false;
                
            }//if
            
            //checking to see if there if the last thing added to the stack was an ( bracket
            else if(stacking.top() == '(')
            {
                //because the program has found a match. It now can remove the ( bracket.
                // So it can move onto the next if there is one.
                stacking.pop();
                
            }//else if
            
            //if the ( bracket is not found it will then return false.
            else
            {
                cout << "0";
                return false;
            }//else
        }//else if
        
        
        //Checking to see if the next character is } bracket
        else if(characters[i] == '}')
        {
            //making sure there is something in the stack already
            //if not it returns false
            if(stacking.empty())
            {
                
                cout << "0";
                return false;
            }//if
            
            //checking to see if there if the last thing added to the stack was an { bracket
            else if(stacking.top() == '{')
            {
                //because the program has found a match. It now can remove the ( bracket.
                // So it can move onto the next if there is one.
                stacking.pop();
                
            }//else if
            
            //if the { bracket is not found it will then return false.
            else
            {
                cout << "0";
                return false;
            }//else
        }//else if
        
        
        //Checking to see if the next character is ] bracket
        else if(characters[i] == ']')
        {
            //making sure there is something in the stack already
            //if not it returns false
            if(stacking.empty())
            {
                cout << "0";
                return false;
            }//if
            
            //checking to see if there if the last thing added to the stack was an [ bracket
            else if(stacking.top() == '[')
            {
                //because the program has found a match. It now can remove the [ bracket.
                // So it can move onto the next if there is one.
                stacking.pop();
                
            }//else if
            
            //if the [ bracket is not found it will then return false.
            else
            {
                cout << "0";
                return false;
            }//else
        }//else if
        
    }//for
    
    cout << "1";
    return true;

}//int

int main()
{
    char str1[] = "{1+[2+(3+4)]}";
    
    char str2[] = "{(3+5)-[2*1]}";
    
    char str3[] = "{3+5}-[2+1]";
    
    char str4[] ="(2+[3+5)+1]";
    
    char str5[] ="";
    
    printMyInfo();
    
    cout << "This program checks braces ([{}])" << endl;
    
    cout << endl;

    cout << "Are all the braces matching in the expression " << str1 << "? :";

    checkBraces(str1);
    
    cout << endl;
    
    cout << "Are all the braces matching in the expression " << str2 << "? :";

    checkBraces(str2);
    
    cout << endl;
    
    cout << "Are all the braces matching in the expression " << str3 << "? :";

    checkBraces(str3);
    
    cout << endl;

    cout << "Are all the braces matching in the expression " << str4 << "? :";

    checkBraces(str4);
    
    cout << endl;
    
    cout << "Are all the braces matching in an empty expression " << "? :";
    
    checkBraces(str5);
    
    cout << endl;
    
    cout<<endl<<"Program terminated."<<endl;//have these two lines before "return 0;"
    system("pause");//press any key to close the black screen

    return 0;
}//main

Reversing a string – Java

This is a program which accepts a string which is to be entered in by the user. It will then convert it and print out the outcome.


public class ReversingString
{
    public static String reverse ( String s )
    {
        int length = s.length(), last = length - 1;
        char[] chars = s.toCharArray();

        for ( int i = 0; i < length/2; i++ )
        {
            char c = chars[i];
            chars[i] = chars[last - i];
            chars[last - i] = c;
        }//for
        return new String(chars);
    }//reverse

    public static void main(String [] args)
    {
        Scanner keyboard = new Scanner(System.in);
        String input;

        input = keyboard.nextLine();

        reverse(input);

        System.out.println(reverse(input));


    }//main

}//class

Using the leftshift operator << in c++!

Recently in a test within college I came across the use of the shift operator. We had to work out the answer by hand so I thought I’d do a post about it. I have also written a program to help you understand it.

This was the question I was asked.

What is the value of c after the following?

int a=12
int b=2;

int c=a<<b;

Answer: 48

How to work it

Its actually really simple to work out.

The leftshift operator is the equivalent of moving all the bits of a number a specified number of places to the left:

[variable]<<[number of places]

1. Convert the variable to binary.
12 in binary is 1100.

2. Add the number of places onto the number.
This case it is 2 therefore add on 2 0's.

3. The number is now 110000.

4. count the number of 0's.
This case there is 4 0's

5. Now times the variable by the number of 0's.
12*4 in this case = 48.

8. We have now got our answer.

The code to work it out:

#include <iostream>

using namespace std;

int main()
{
    int firstNum, secondNum;
    
    int result;
    
    cout << "Using the shift operator <<!" << endl;
    
    cout << "Indicates the bits are to be shifted to the left." << endl;
    
    cout << "Please enter your first whole number" << endl;
    
    cin >> firstNum;
    
    cout << "Please enter you second whole number" << endl;
    
    cin >> secondNum;
    
    result = firstNum << secondNum;
    
    cout << result << endl;
    
    return 0;
}//main

Aul Irish Tales – Android Application

What is it:
Built an application in a group with three others using agile and scrum.

– Used Trello to keep track of sprints backlog.
– Childrens’ tale app (Aul Irish Tales).
– Featured 5 tales where the user could choose whether they wanted the story to be read to them or they wanted to read it themselves. This allowed younger users who could not read to also use the app without their parents having to sit and read it to them, they could simply turn it on.
– At the end of the tales there was a simple quiz. This allowed the children to be quizzed on what they had learnt.

This slideshow requires JavaScript.

Continue Statement – C++

What it does:

continue causes the number 4 to be skiped in this case when printed out.

Code:


#include <iostream>

using namespace std;

int main()
{
    for(int count =1; count<=8; count++)
    {
        if(count==4)
        {
            continue; //skips the part code in the loop
        }//if
        
        cout << count << endl;
    }//for
    
    cout << "Used coninue to skip printing 4" << endl;
    
    return 0;
}//main

Calculating Interest – C++

What it does:

Calculates the interest of a balance of 5 years. The interest is set a .05 initially and the bank balance is set at 1000. Prints out in a table with two headers for the year and amount.

Code:



#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
    double amount;
    double initalAmount = 1000.0;
    double rate = .05;
    
    //Headings
    cout << "Year" << setw(21) << "Amount on deposit" << endl;
    
    //setting floating point num
    cout << fixed << setprecision(2);
    
    //caculating the amount for 5 years
    for(int year=1; year<=5; year++)
    {
        //caculating the amount for specified year
        amount = initalAmount * pow(1.0 + rate, year);
        
        //displaying the amount + year
        cout << setw(4) << year << setw(21) << amount << endl;
    }//for
    
    return 0;
}//main