Set Styles programmatically in Android

Styles in Android are a very useful kind of resources which allow us to easily configure a set of components. Styles can be created as resources in an xml file. Styles can also be set to a component in its xml declaration, but sometimes it is necessary to set the styles programmatically.
Suppose we have a big list of locations to be added as radio buttons. The easiest way to add them is doing it programmatically. When creating a new radio button, the first approach would be to use the “new RadioButton” sentence. The constructor receives the style ID as one of its parameters.

// Get the list
String[] locationArray = getResources().getStringArray(R.array.location);

RadioGroup radioGroupLocations = (RadioGroup) findViewById(R.id.filtro_radioGroup_locations);
RadioButton radioButtonLocation;

// For each location, create a radio button
for (String string : locationArray) {
   radioButtonLocation = new RadioButton(this, null, R.style.RadioButton_style);
   radioButtonLocation.setText(string);
   
   // Different IDs to get the expected behaviour as radio group
   radioButtonLocation.setId(radioGroupLocations.getChildCount());  

   // Add the button to the group 
   radioGroupLocations.addView(provincia);
}

The result is not the expected one. Every radio button should have the style of the first one.

Radio buttons before

The solution is not to use the constructor, but to use the “getLayoutInflater” method. This solution requires to create a layout with just the radio button as its content (template_radiobutton.xml):

<?xml version="1.0" encoding="utf-8"?>
<RadioButton style="@style/RadioButton_style" />

The code is now:

// Get the list
String[] locationArray = getResources().getStringArray(R.array.location);

RadioGroup radioGroupLocations = (RadioGroup) findViewById(R.id.filtro_radioGroup_locations);
RadioButton radioButtonLocation;

// For each location, create a radio button
for (String string : locationArray) {
   radioButtonLocation = (RadioButton) getLayoutInflater().inflate(R.layout.template_radiobutton, null);
   radioButtonLocation.setText(string);

   // Different IDs to get the expected behaviour as radio group
   radioButtonLocation.setId(radioGroupLocations.getChildCount());
   
   // Add the button to the group 
   radioGroupLocations.addView(provincia);
}

And the result:

Radio buttons after

Read More

Android curious methods

Working with Android library I found some curious methods to make it funny.

class Log

public static int wtf (String tag, String msg)

What a Terrible Failure: Report a condition that should never happen. The error will always be logged at level ASSERT with the call stack. Depending on system configuration, a report may be added to the DropBoxManager and/or the process may be terminated immediately with an error dialog.

class ActivityManager

public static boolean isUserAMonkey()

Returns “true” if the user interface is currently being messed with by a monkey.

Read More

Vulnerability, exposure, threat and risk terms

A threat is a potential cause of an unwanted impact to a system or organization (ISO 13335-1). Threats fall into two categories: vulnerabilities and exposures.

A vulnerability, according to MITRE’s CVE Terminology, is a mistake in software that can be used by a hacker to gain access to a system. A vulnerability:

  • Allows an attacker to execute commands as another user.
  • Allows an attacker to access data that is contrary to the specified access restrictions for that data.
  • Allows an attacker to pose as another entity.
  • Allows an attacker to conduct a denial of service

An exposure is defined by MITRE’s CVE Terminology as a system configuration issue or a mistake in software that allows access to information or capabilities that can be used by a hacker as a stepping-stone into a system or network. An exposure:

  • Allows an attacker to conduct information gathering activities.
  • Allows an attacker to hide activities.
  • Includes a capability that behaves as expected, but can be easily compromised.
  • Is a primary point of entry that an attacker may attempt to use to gain access to the system or data.
  • Is considered a problem according to some reasonable security policy.

A risk according to the ISO 31000 definition is the effect of uncertainty upon objectives where an effect is a deviation from the expected, positive or negative. ISO 31000 notes that risk can be regarded in terms of:

  • Likelihood of an event occurring.
  • Impact of the event if it occurs.

Read More