Pages

Monday, 25 June 2012

To get the android device dimentions


public class DeviceDimentions {

Context context;
Display display;

public DeviceDimentions(Context context) {
this.context = context;
display = ((Activity) context).getWindowManager().getDefaultDisplay();
}

public int getWidth() {
return display.getWidth();
}

public int getHeight() {
return display.getHeight();
}
}

Tuesday, 12 June 2012

Dynamic radio buttons and checkbox sample code


LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(btn);

TextView view = new TextView(this);
view.setText("1. Select the correct statement in the following options :");
layout.addView(view);
int n = 5;
for (int i = 0; i < n; i++) {
  CheckBox checkedTextView=new CheckBox(this);
checkedTextView.setText(String.valueOf(i));
checkedTextView.setId(i);
checkedTextView.setOnCheckedChangeListener(this);
            layout.addView(checkedTextView);
}

LinearLayout layout2 = (LinearLayout) findViewById(R.id.ll);
layout2.addView(layout);
TextView view1 = new TextView(this);
view1.setText("2. Select one correct statement in the following options :");
layout.addView(view1);

RadioButton[] radioButtons=new RadioButton[5];
RadioGroup group=new RadioGroup(this);
group.setOrientation(RadioGroup.HORIZONTAL);
for (int i = 0; i < n; i++) {
 
radioButtons[i]=new RadioButton(this);
group.addView(radioButtons[i]);
radioButtons[i].setText(String.valueOf(i));
radioButtons[i].setId(i);
}
   layout.addView(group);
   group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

System.out.println("the checked radio button id is :"+checkedId);

}
});

}

@Override
public void onCheckedChanged(CompoundButton cb,boolean isChecked) {

Toast.makeText(getApplicationContext(),String.valueOf(cb.getId()) , Toast.LENGTH_LONG).show();
}

Thursday, 7 June 2012

Date comparision


DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");

Date d1 = df.parse("2012-01-24 12:30:00 PM");
Date d2 = df.parse("2012-01-24 02:30:00 PM");

int hoursDifference = (int)((d2.getTime() - d1.getTime()) / 3600000L);
System.out.println("Difference in hours: " + hoursDifference);