solutions to beginners problem when starting android development

If your going to start developing android application, you will definitely face several issues. Could be lack of examples on android developers page or outdated on-line tutorials.

The follow are the problems that I have faced during the development of my application https://play.google.com/store/apps/details?id=com.my.trial  .

Problem: OnItemClickListener event does not gets called, When you have a checkBox, radioBox  inside listview.
Solution: Set the following properties in your checkBox, radioBox

android:focusable="false"
android:focusableInTouchMode="false"

Problem: UI Elements take their space, while their visibility is set Hidden
Solution: Set the visibility to Gone, it wont take space anymore

Problem: Trying to create a Dialog, you will receive this exception  android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an application
Solution: You need to use the Activity Context new Dialog(this) or new Dialog(YourActivity.this) instead of new Dialog(getApplicationContext);

Problem: Calendar month returns wrong value . It display 1 month lesser
Solution: Is to increment it by 1. The Calendar Month is Zero based it starts from 0 this is why you get 1 month lesser than current month. January = 0, February = 1 ..

Problem: The last item of listview does not have the separator line at the bottom
Solution: Set the layout_height to “match_parent

Request.executeMeRequestAsync Depreciated in Facebook SDK 3.7 Android

The Request.executeMeRequestAsync, is depreciated in Facebook SDK 3.7 . Even though in the Getting started tutorials of Facebook Developer’s website they use it, you should avoid using it!
https://developers.facebook.com/docs/android/getting-started/
You should replace it with Request.newMeRequest(..){}.executeAsync

Request.newMeRequest(session, new Request.GraphUserCallback() {
.....
}).executeAsync();

More details: https://developers.facebook.com/docs/reference/android/current/class/Request/

Using Venn diagrams in C++ to write better codes

How can we use a Venn diagram to program? Well, you might be wondering what am I going to write? Venn diagram shows the possible relations between a finite collection of sets (according to wikipedia). Yes, perfect. The part of Venn diagram that is interesting to us as programmers is the intersection ∩ .

Recently I had to write a socket program which as we know involves a server and multiple clients. I looked at the functions that are general between making a server and client socket connections, and ended up drawing this Venn diagram.

venn diagram socket c++

We can clearly conclude, that the intersection methods between them can be a single class, and will have a server socket and client socket class as-well.

It has become a habit, to draw a Venn digram and write the methods that are common between different classes and write them into one. It is an easy way to share the method with your colleagues.

Will create an abstract class, to place the generic functions in it
Note: As it involves creating a connection it should be singleton. We need only one instance of this class. We do not want to create multiple sockets!!!!
The constructor receives socket connection, in-case the child class might need to do extra functionality can use the socket connection handler


/************************************************************************/
/*	  Base Abstract class
	  handles the creation and initialization of sockets
/************************************************************************/
#pragma once
#ifndef ABSTRACTSOCKET_H
#define ABSTRACTSOCKET_H
#include <iostream>
#include <windows.h>
#include <stdexcept>
#include <stdlib.h>
#pragma comment(lib,"ws2_32.lib")

class AbstractSocket
{
public:
	AbstractSocket(SOCKET &socketConnection);
protected:
	// sending data
	virtual bool send(void *data) = 0;
	// receiving data
	virtual bool receive(void *data) = 0;

	virtual void bindSocket() =  0;

	// starts the windows socket
	void initialiseWinsock() ;
	// creates a socket
	void createSocket(SOCKET &socketConnection);
	WSADATA wsa;
	struct sockaddr_in conStruct;

private:
	AbstractSocket(){};
};
#endif

Server Socket
Our server code will have just binding the socket, send and receive. The same will be for client class, except the binding
I prefer to implement the bind socket in the header file and the send and receive in the .cpp file. To keep the code cleaner and simple :)

#include "AbstractSocket.h"
class ServerSocket:public AbstractSocket{

public:
	ServerSocket::ServerSocket(): AbstractSocket(socketConnection){};
	virtual void bindSocket();
	virtual bool send(void *data) ;
	virtual bool receive(void *data);
	SOCKET socketConnection;
	struct sockaddr_in myAddress, si_other;
};

Share your feedbacks :)

amazon linux php gd error php54-common conflicts

While installing php-gd  (amazon linux php gd) on an ec2 instance, you might get an error:

The fix arhp54-common conflicts with php-common-5.3.38-1.2.amzn1.x86_64″

php54-cmmon conflicts linux ami amazon

The solution is  to remove the php-common and re-install it again. but you need to install php and httdp as-well again.

yum remove php-common
yum install php-common
yum install php-gd
yum remove httpd httpd24-tools
yum install httpd
yum install php

Note: You need to install extensions again

Perforce java socket time out large projects

Recently I had to import a large perforce project via Eclipse plugin. Unfortunately, Eclipse throws java socket time out. After  searching over the net I stumble across various websites but none helped.

The perforce server is hosted on free Ec2, not a powerful server. So importing a large project could take a long time or most of the time it will throw the silly java socket time out error.

The solution which I came up with is, when it throw the error, it displays 3 options

  1. Work Offline
  2. Edit Settings
  3. Cancel
  • Select the first one Work offline.
  • Go to PHP Perspective, select one of your project folder, go to team -> get latest version
  • If it fails, try getting the latest version per file rather than folder.

Keep performing the get latest version till your revisions are up-to date

define vs const in c plus plus

Majority of  C++ programmers’ , when it comes to using  constants they declare it via #define. It is easy to just declare it using #define , but  the ideal way is to declare it using constants via the const keyword

Advantages of declaring constants via const keyword

  1.  Compile Time Error
  2.  Type safety
  3. They can be viewed in debugging  ( Very handy in large projects), in other words you can watch them

How to create const variables

We will create a Student class, which has one method setMark, and a constant MAX_MARK, we can set mark of  a student only if its less then the value of MAX_MARK

Header File .h


#include

class Student
{
public:
	Student();
	void setMark(int a);
protected:
	const int MAX_MARK;   // OUR CONSTANT
	int mark;
};

Source File .cpp


#include "Student.h"

// we initialize the variables in our constructor best practice
Student::Student() :mark(0), MAX_MARK(100){}

void Student::setMark(int a)
{
	if (a < MAX_MARK)
	{
		mark = a;
		std::cout << "SET" << std::endl;
	}
	else
	{
		std::cout << "NOT SET" << std::endl;
	}
}

Our main

#include
#include "Student.h"

int main()
{
	Student myStudent;
	myStudent.setMark(120);
}

If you pass  a value  less than 100, the mark gets set, else not.
Using the const keyword will help you  to watch the variable during debugging!  You will c know what value has been set and it’s type.

how ever if  you declare it via #define  you wouldn’t be able to watch the variable  during debugging!.

We can view the const variable in debugging

We can view the const variable in debugging

Fisher Yates Modern Shuffle

Fisher Yates Modern Shuffle is an algorithm which randomly changes the position of elements in a list.

so if you have an array of numbers from 1 to 10 in order

array = {1,2,3,4,5,6,7,8,9,10}

After applying Fisher Yates Modern shuffle, their locations will changed randomly

array = {5,4,7,10,2,9,1,8,3,6}

The code below is my implementation in C++, please feel free to suggest any improvements.

#include <iostream>
#include <vector>
#include <time.h>
using namespace std;

int main()
{
	srand(time(NULL));
	vector<int> elements;

	for (int i = 1; i < 9; ++i)
	{
		elements.push_back(i);
	}
	
	// Before SHUFFLE
	for (auto i = elements.begin(); i != elements.end(); i++)
	{
		cout << *i << endl;
	}
 
	int currentIndexCounter = elements.size();
	for (auto currentIndex = elements.rbegin(); currentIndex != elements.rend() - 1; currentIndex++ , --currentIndexCounter)
	{
		int randomIndex = rand() % currentIndexCounter;
		if (*currentIndex != elements.at(randomIndex))
		{
			swap(elements.at(randomIndex), *currentIndex);
		}
	}
	
    // After SHUFFLE
	for (auto i = elements.begin(); i != elements.end(); i++)
	{
		cout << *i << endl;
	}

	return 0;
}