Friday, November 25, 2011

Android radio buttons example

In Android, you can use “android.widget.RadioButton” class to render radio button, and those radio buttons are usually grouped by android.widget.RadioGroup. If RadioButtons are in group, when one RadioButton within a group is selected, all others are automatically deselected.

In this tutorial, we show you how to use XML to create two radio buttons, and grouped in a radio group. When button is clicked, display which radio button is selected.

1. Custom String

Open “res/values/strings.xml” file, add some custom string for radio button.
File : res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MyAndroidAppActivity!</string>
    <string name="app_name">MyAndroidApp</string>
    <string name="radio_male">Male</string>
    <string name="radio_female">Female</string>
    <string name="btn_display">Display</string>
</resources>

2. RadioButton

Open “res/layout/main.xml” file, add “RadioGroup“, “RadioButton” and a button, inside the LinearLayout.
File : res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <RadioGroup
        android:id="@+id/radioSex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
 
        <RadioButton
            android:id="@+id/radioMale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_male" 
            android:checked="true" />
 
        <RadioButton
            android:id="@+id/radioFemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_female" />
 
    </RadioGroup>
 
    <Button
        android:id="@+id/btnDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_display" />
 
</LinearLayout>
Radio button selected by default.
To make a radio button is selected by default, put android:checked="true" within the RadioButtonelement. In this case, radio option “Male” is selected by default.

3. Code Code

Inside activity “onCreate()” method, attach a click listener on button.
File : MyAndroidAppActivity.java
package com.mkyong.android;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
 
public class MyAndroidAppActivity extends Activity {
 
  private RadioGroup radioSexGroup;
  private RadioButton radioSexButton;
  private Button btnDisplay;
 
  @Override
  public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 
 addListenerOnButton();
 
  }
 
  public void addListenerOnButton() {
 
 radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
 btnDisplay = (Button) findViewById(R.id.btnDisplay);
 
 btnDisplay.setOnClickListener(new OnClickListener() {
 
  @Override
  public void onClick(View v) {
 
          // get selected radio button from radioGroup
   int selectedId = radioSexGroup.getCheckedRadioButtonId();
 
   // find the radiobutton by returned id
          radioSexButton = (RadioButton) findViewById(selectedId);
 
   Toast.makeText(MyAndroidAppActivity.this,
    radioSexButton.getText(), Toast.LENGTH_SHORT).show();
 
  }
 
 });
 
  }
}

4. Demo

Run the application.
1. Result, radio option “Male” is selected.
android radio button demo1
2. Select “Female” and click on the “display” button, the selected radio button value is displayed.
android radio button demo2

Sunday, November 20, 2011

Easy and Fully Configurable Laptop Battery Manager For Windows 7/Vista

We have previously covered power tools that allows you to keep a check on your laptop battery, among them so far our favorite has been Battery Bar. But now a new contender has arisen that aims to be totally flexible and fully configurable, and yes it also supports Aero Glass.

BattCursor is a free battery management tool that aims to replace the default Power functionality that comes in Windows 7 and Vista. Everything is customizable, from Cursor, Tray Symbol, and Colorization to Battery Tweaks, Power Profiles, and Notifications.



Unlike Windows, where the Advanced Power Settings is too complicated to use, BattCursor makes it easy to tweak everything from a single window. You can perform battery tweaks and create a custom power profile to suit your needs or you can simply customize the look and feel. The profile management section is quite impressive since it allows you to automatically change the power plan according to the battery. For example in my case, if the battery is above 70%, the profile would be High Performance, if battery is above 50% then Balanced, and so on.


When the battery reaches the warning level, your cursor will become yellow and so will your battery along with the superbar/taskbar and Windows Explorer. The same will happen when the battery reaches the critical level, in this case, everything will become red.


Note: This color change will only take place when Windows Aero Glass is enabled.


The screenshots taken above are from developer’s product webpage

These colors are just the default, but can be changed to anything of your liking. Not only this, but you can also set when to warn you and which percentage would mean the battery is critical, etc. Just like you can change the Power profile in Windows from the system tray, you can do the same with this tool. Simply right-click the system tray icon and go to Profiles.


There are tons of other features that you can explore. Even if you are comfortable with your current laptop battery power management, you should still give it a try since it drastically helps you in further reducing the power consumption. Since BattCursor has a less system memory footprint, it can also come handy if you are running Windows 7 on a Netbook.


It works with Windows 7 and Windows Vista, both 32 and 64-bit OS are supported. Enjoy!

Wednesday, November 16, 2011

How To Solve Laptop From Overheating [Windows 7]?

A few months back, I bought a new Core i3 laptop, hoping to run more resource intensive applications and games with better performance. While most of the applications seem to run without any issues, I have been dealing with a few thermal heating issues while playing games or running encoding tasks. For some odd reason, even reducing the affinity of the application or the game, does not eliminate the heating problem. I even bought a cooling pad, and tried switching to lower graphic options, but that, too, has resulted in minimum heat reduction. After going through many methods of reducing system heat up, it appears that there is only one true method that may be more effective than others (courtesy of our former editor, Ghaus Iftikhar), i.e., to reduce the maximum processor state from the laptop’s power settings. In this post, we will tell you how to prevent your system from heating up by adjusting the power options for your processor state.

This tip might come in handy not just for gamers and people who wish to run resource intensive application in the wake of overheating laptops, but also if, for some reason, you are unable to return a laptop with a heating issues (perhaps due to warranty constraints), or are dealing with an old laptop, which won’t take the brunt any more.

Before explaining the method of reducing the maximum processor state, let us take a look at the logic behind reducing the maximum processor state to prevent overheating of your laptop. Reducing the maximum processor state for your laptop (both when it is on battery or when the power cable is plugged in), reduces the processor’s performance a notch (depending on your settings) and prevents it from being used at optimum potential by an application or game, which will reduce thermal heating. For example, if you are playing a game that is consuming 100% of your processor’s capacity, then it may also result in heating up your system, whereas reducing the battery power state to, say 80%, can resolve this problem, and also result in battery power conservation.

To change these settings, go to Power Options from the Control Panel or the system tray menu.




Now go to Change Plan Settings –> Change Advanced Power Settings.



In the new window that pops-up, expand Processor Power management –> Maximum processor state, and reduce the on battery and plugged in power settings. The settings you keep will depend on your preferences and the capability of your system.

Based on our experience, we have noticed that that the laptop overheats when the processor is running at 100% processor state. Reducing the processor state by a few notches results in the reduction of temperate by 10-20 C, which results in a minor performance dip. We used Speccy to check for temperate during testing, based on which the processor running on 95% processor state gave the same performance (barely noticeable dip), with a drop of 10-20 C. You can keep an even lower processor state (such as 80-85%) to make sure that your laptop heats up even less.


The above mentioned tip should easily prevent your laptop from heating up; however, make sure that the processor state is not reduced very low in order to prevent reducing your processor’s performance to an undesirably low level.