The Blog

➤ How to add footer to NavigationView

28 Ago 15

NavigationView was recently added by the Android support team to help developers deliver consistent NavigationDrawer Menu across apps. It’s usage is super simple thanks to its support of native menu’s xml resource files.

Such simplicity comes with a drawback. You can add menu items, an header but not a footer. After investigating a little I saw the NavigationView actually uses a ListView to display the menu and the given header is added through the ListView.addHeaderView method.

Read More

How to avoid Fragments overlapping due to backstack nightmare in android

16 Ago 15

Recognize this mess above?? That WTF moment that happens when you’re trying to do nothing special?

This screenshot was taken after pressing the back button after a specific set of preconditions have occurred:

  • You committed a FragmentTransaction without “addToBackStack”
  • You did not set the background color to the rootview of your fragments (Good boy)
  • you tried to go back just after adding the above mentioned transaction.

Let me explain:

Lets assume we’ve 3 fragments: A, B, C. and you’d like to have the following behavior: User navigates in the following direction A -> B -> C; Suddenly you realize you’d like to skip “B” from the back stack letting the user jump from “C” to “A” when pressing the back button. What would you do?

Read More

Android – Validate an email inside an EditText ( and more )

26 Mag 12

Input validation could be very painful. Building forms, in every technology, is a boring and painful job.

Fortunately on the web many libraries have born in order to facilitate this task to web developers. In android there are, built-in, some best practices you can use to facilitate the job but you’ll always have to take care about:

  • in-depth data validation
  • mandatory fields check
  • error presentation

Bored about giving birth another painful form I decided to write an opensource library that does almost everything for me. The library is called FormEditText and you’ll be able to browse/download the sourcecode here in github.

The basic usage of the library will allow the developer to get an edit text validated through xml attributes.

In this snippet of xml code we have a lot of good tech ( both from Android and my library ):

  • line #3:  we define a new namespace. You’ll need to modify the package name ( com.andreabaccega.edittextformexample in the code ) with yours
  • line #9: we provide an hint for the user so that he’ll know what he should write inside the field
  • line #10: we provide a value for the stock android:inputType attribute.
  • line #12: we tell the library that the field should be a valid email address

The last thing we’ll need to do is to ask the library to check the edittext validity:

This piece of code, performed when the user clicks the “submit” button, will ask the library to validate the content. Here two things could happen:

  1. the input is valid: in this case the library will just return true ( obviously )
  2. the input is not valid: in this case the library will return false and will set an error message that will look like the image on the top of the page.

When in the first case the library won’t do anything except returing “true” on line #3 of the previous snippet.

Further readings:

I’ll suggest to take a look at the github project. If you want to try the library I also setted up an example app that you can download from the market.


android sviluppo applicazioni

Link AppBrain | Link Android Market

Introducing Flatterizor

09 Mag 12

Flatterizor is a PHP library developed in order to issue a very specific problem.

Sometimes you have a complex object and you need to store it into your database. Most cases the serialize function does work. Unfortunately sometimes it does not ( not because serialize is broken/bugged ).

The Library is useful only if the followings are true:

  • You want to store your object inside a database
  • Your object is not “Flat”  ( meaning you’ve at least a value in your object that is an array or an object itself )
  • You want to use your Database SQL to Search & filter objects with a particular value.

The last sentence is actually the most appealing. Lets take, for example, the following objects:

Read More

How to backup your web server data [rsnapshot + rsync + ssh]

23 Apr 12

You start doing some research about this topic when one of the following events occurs:

  • You lose your data
  • You start hosting third party data

Read More

Android: How to execute some code only on first time the application is launched

12 Apr 12

It happens you need to execute some piece of code only on the first time the user start using your app.

Lets say you want to show a quick tutorial to the user just once — Indeed, when the user open your app for the first time.

Well, an easy solution would be to use a SharedPreference to store the info we need to accomplish this “task”.

Below, a snippet with a simple method ( to be included inside your activity class ) that will “solve” the problem.

Lost password reset software design pattern

07 Apr 12

When you build a new Service-Website from scratch you’ll probably have to face with this. Passwords are the most sensitive data we have and your application should behave securely enough to maintain user’s data private!

Some time ago you it was a common pattern to store the user password as plain text ( or with some Symmetric-key algorithm ). This let the user to retrieve their password withouth setting a new one.

From this pattern a fork was born which required to answer a predefined question in order to make the application send the password back to the user.

It seems these patterns are not used anymore in favor of the actual one which involves a more structured application/user flow in order to let the user regain access to the application. I’m talking about password reset.

The new way

Read More

Serve landing page while building a new wordpress site

05 Apr 12

If you’re a web developer you confronted by this at least once. If you don’t own a development webserver you’ll need to make your developments in the production webserver. This means that everyone could see your work-in-progress easily.

A common pattern is to create an index.html file ( which gets served – almost ever – before index.php ) which will contain a specific landing page with an “under construction” text.

How to serve the index.html file to “regular people” while you work on wordpress ( index.php ) ?

It’s quite easy using .htaccess and mod_rewrite of apache. The basic concepts are:

  • WordPress logged in people will see the “work in progress” website
  • Not logged in people will see the “site under construction” page

In order to achieve that, you’ve to prepend the following lines to your .htaccess file  ( placed in your document root ) :

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} index.html$
  RewriteCond %{HTTP_COOKIE} ^.*wordpress_logged_in.*$ [NC]
  RewriteRule . /index.php [L]
</IfModule>

As you can see we only override the normal web-flow only if the browser is going to request the index.html file and the user has the wordpress_logged_in cookie setted.

Whenever you need to see your “real website” you just need to point your browser to http://example.com/wp-admin , login and then you’ll be able to see the website you’re creating and not the “under construction” page.

Note: This is only a partial solution and people could be smart enough to emulate the wordpress_logged_in cookie and see your work in progress anyway. In my case-scenarios this was more than enough.

 

Avoiding Xss injection without sacrifying some tags in PHP

23 Mar 12

Sooner or later I always remember I’ve a blog. This time the topic is XSS.

If you don’t have a clue of what XSS is maybe you should read this before reading this article.

Read More

onChange event on EditText in Android

09 Ott 10

Sooner or later you’ll have to deal with it. If you’re an html developer and you write also in javascript you’ll surely know the onchange event.

Unfortunately it’s a little bit tricky to find the same event on android.

The onChange event is helpful when you’ve to deal with the following things:

  • Let the user know (in realtime) how many characters he typed.
  • Let the user know (in realtime) how many remaining characters he is allowed to type.
  • Make realtime processing of the content ( like sending it online and fetch some partial results of the partial typed edittext )

You’ve to implement your own instance of TextWatcher and let the edittext know that you want to be notified at each change by calling the method EditText.addTextChangedListener.

Below i will give you a simple example ( it’s written on the fly but you’ll understand the idea )

[sourcecode lang=”java”]
((EditText)findViewById(R.id.et_testo)).addTextChangedListener(new TextWatcher() {

public void afterTextChanged(Editable s) {
((TextView)findViewById(R.id.numcaratteri)).setText(String.format(getString(R.string.caratteri), s.length()));

}

public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub

}

});

[/sourcecode]