Skip to main content

Android: proportionally stretch an ImageView to fit the whole screen width while maintaining its aspect ratio

Proportional image resizing is a fairly common scenario while developing an Android app: there are a number of situations where you might want an image to stretch itself to horizontally fit the whole screen while keeping its original aspect ratio. You might think it should be easy: sadly, it’s not.

The problem

Achieving this result in HTML would be extremely easy: we just need to set our image width to 100% and avoid to set any height, letting the browser doing the whole proportional resize job. Unfortunately, Android wears a different pair of shoes: the ImageView element containing your drawable resource (or downloaded file) requires a width and an height: we can give a fixed number in px, dp or other supported units, use the parent width with the match_parent command or use the actual image width using the wrap_content command: as we can see, proportional resize is not an option. We can achieve some decent results by setting android:layout_widthandroid:layout_height and android:scaleType to make them adapt to the container layout in the following way:

These values could work in your specific scenario, but they tend to give inconsistant results among different devices and, most importantly, among different image sizes/ratios.

The solution

Why should we use a plain ImageView? We can start by taking a look to this interesting post on StackOverflow showing how to extend a View in order to achieve a very similar result: then we can use that knowledge to build our own ImageView extension class. Here’s an example of a ProportionalImageView which automatically scales its height according to its computed width keeping its original aspect ratio intact:


And this is how we can use it inside an XML layout:

Just remember to replace with your app namespace and you should be fine.

Happy coding!


#TwitterAcademy Feb-10 Italian Webinar: TwitterAnalytics, TwitterAds, QuickPromote and more

The first 2015 italian Twitter Webinar hosted by #TwitterAcademy ended a couple hours ago: it was mostly focused on an extensive overview of the two monitoring/advertising platforms granted by Twitter to its business-oriented end-users: and, both of them accessible using our very own standard Twitter account.

The platform is a free web tool allowing the users to effectively analyze the outcome of each of their tweets, as well to measure their interaction rate: - Screenshot 1 è lo strumento gratuito mediante il quale è possibile misurare le visualizzazioni e le interazioni dei nostri tweet.


Besides the starting page, which features an extensive overview of all our recent Twitter activities, we can also access to the Follower dashboard: there, among an historical activity chart, we can obtain some interesting intel about our followers behaviours and habits. We can see their interest, their home country/zone, their gender and, most importantly, an aggregated list of any other Twitter account they’re also following: this could easily be the key we need in order to know better our followers and learn how to pinpoint our tweet contents to better suit their needs.
Analytics: la dashboard dedicata ai Follower


It’s worth to mention that all these data can be easily exported in .CSV format for further analysis using the export button in the main menu.

One of the most interesting features of the Analytics platform is the single tweet analysis, a popup overview contaning a lot of useful details about the selected tweet: views breakdown, answers, retweets, clicks, favs and more. These are all important info to help us to understand if that specific tweet managed to hit the mark or not, being useful to our followers and, most importantly, reaching out new potential users.
Analytics: finestra di dettaglio su un singolo tweet


The Tweet details popup panel also contains the new QuickPromote feature, a commercial tool that allow us to create a minimalistic single-tweet campaign with a single click. It works in a very simple way: you choose a price plan and the tweet will be shown to potential followers until it reaches the expected views/interaction revenue – the more you pay, the most you’ll get. The webinar author suggested to use this feature to maximize the outcome of our “best” tweets in terms of interaction between our followers: meaning that we can use our already faithful user base to find the tweets that are arguably worth to be promoted, filtering out the less catching ones.

In order to use the QuickPromote feature we need to set up a payment option on our account. In order to do that we need to access to the other web platform presented by the webinar:, a business-level web tool we can use to create, manage and promote our commercial campaigns on Twitter : è la piattaforma a pagamento per la pianificazione di campagne promozionali, obiettivi, conversioni etc.


According to the webinar, the advertising platform is the tool we can use if we want to effectively setup a business on Twitter. There we can do campaign planning, metrics & conversions analysis, conversion rate measurements, extensive stats and much more. It shares the same logic – and terminology – used by the Facebook for Business platform, so it shouldn’t be a problem to learn how to effectively use it.

Needless to say, all the ads data shown by the various dashboard can also be exported in .CSV format for further analysis.

Besides the extensive overview of these two platforms, the webinar focused on pointing out some useful key concepts to help anyone interested in using twitter in a more effective way. In case you missed it, here’s a quick summary of what you need to do:

  • Keep an eye on your followers’ interests: if you want to make them happy and boost their interactions you need to write your tweets according to their agenda: that’s the main purpose of the Follower dashboard.
  • Geo-localize your campaigns: this is mostly important for promotions, special offers, commercial-related stuff and every time we need to fire our bullets to actual targets.
  • Observe those followed by our followers: here’s another thing we can do via the the Follower dashboard. We can learn to our “competitors”, see what our followers tend to like/interact most and use these infos to write interesting stuff ourselves.
  • Remember that the followers community constantly evolves. Twitter is a lively, ever-changing experience: just like the web. People interests, behaviour and tendencies are subject to changes, and we need to react accordingly. That’s why we need to constantly keep ourselves up-to-date, and there’s only one way to do so: act like we are our very first follower. As long as we manage to do so, we will be able to write enticing contents that will most likely pay off in terms of interactions / click / views and more.


Here’s a brief list of the questions tweeted by the webinar viewers, followed by the author’s answers:

What’s the “Tweet Interaction Rate” and how is it calculated?
The tweet interaction rate is tue sum of all the interactions (retweets/answers/etc.) divided by its total views number. For example, a Tweet with 30 views and 3 interactions will have an Interaction Rate of 10%.

It’s advisable to use rich media elements (imags, video, cards) in our tweets?
Rich media usage is always advisable, as multimedia content usually grants a substantially higher interaction rate. The content of a tweet containing a nice picture or a catchy video will be probably seen as more interesting by most users: that’s why using media contents, whenever possible, is always encouraged.

What are the Twitter Cards and why I should use them?
They are a rich media content featuring an image, a text and a button in a block placed next to a tweet. A tweet with a card can substantialy boost its interaction rate. For more informations about Twitter Cards you can go to the the official guide. Twitter Cards will also be the main topic of the next Twitter Webinar: be sure to not miss it!

When will the next webinar be held?
The Twitter Cards webinar has not been announced yet. To keep yourself updated on Twitter Italia & Twitter Academy webinars you can use the #TwitterAcademy hashtag.

Where can I find other italian webinars?
All the 2014 Italian Webinars are available from this page of the Twitter blog.

A special thanks to @petrussola, author of the webinar and a valid speaker. See you next time!


Entity Framework Code-First: how to generate an auto-increment ID field

I’ve already covered the practical advantage of the code-first approach featured by Entity Framework 6 in a previous post: the key concept there was all about creating the whole data model  starting from some very standard C# classes: namely, the Entities. The most frequent question asked by readers was: how to define an auto-increment Id field (or primary key) of an Entity? In other words, how can we tell the DB to assign an unique, incremental id number to each and every record by its own?

The answer is quite simple, as long as you’re using MySQLMicrosoft SQL or any other Entity Framework compatible DBMS featuring a native auto-incremental numeric fields support: all we can do is to tell the Entity Framework to properly generate it.

In order to do so we just need to use the DatabaseGeneratedAttribute in the following way:

The ID field will then be set as auto-incremental during the DB generation phase, granting a table-unique ID to each Item record.

That’s about it. Happy coding!


How to disable the On-Screen Touch Virtual Keyboard in Windows 8

You probably already know that Windows 8 has a nice on-screen virtual keyboard just like any other mobile-ready OSes. Once activated, the keyboard will automatically slide-in every time a textbox field gains focus inside a web browser, a desktop application and so on, giving the user the opportunity to tap and/or make handwriting gestures using the touch-screen:


This handy feature comes up and running in most Windows 8 and Windows 8.1 pre-installed environments such as laptops, HTPCs, etc.; needles to say, there’s plenty of scenarios where we just do not need it. For example, when we already have an hardware keyboard.

Question is: how to effectively turn it off?

The answer is quite simple: the virtual keyboard is part of the “Touch screen keyboard and handwriting panel” local system service). In order to shut it down we need to:

  • Press the Windows Key + W (to open the search panel)
  • Type “services” and press ENTER (to open the local services panel)
  • Scroll down to the “Touch screen keyboard and handwriting panel”service.
  • Click/double click to it and select Stop (to shut down the service).
  • If the service startup is Automatic, you should also change it to Manual in order to prevent the system to restart it upon your next reboot.

Please notice that disabling the aforementioned service will also disable the handwriting service, so you’ll lose handwriting gesture recognition. I’m fairly sure that, as long as you have an hardware keyboard, you mosts likely won’t miss it.