Friday, 16 January 2015

get Views actual Height and width at run time.


view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
viewHeightvisibleOpenBtn.getMeasuredHeight();
viewWidthvisibleOpenBtn.getMeasuredWidth();



/**
 * Calculate actual width height of views
 */
Final ViewTreeObserver observer = view.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
viewWidth = view.getWidth();
}
});

Wednesday, 14 January 2015

Simple date Format converter


Example 
formateDateFromstringUpdated("yyyy-MM-dd HH:mm:ss", "M/d/yyyy hh:mm aa", "2015-01-15 06:30:00");


public static String formateDateFromstringUpdated(String inputFormat, String outputFormat, String inputDate){

Date parsed = null;
String outputDate = inputFormat;

SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, java.util.Locale.getDefault());
SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, java.util.Locale.getDefault());

try {
parsed = df_input.parse(inputDate);
outputDate = df_output.format(parsed);

catch (ParseException e) { 
Log.e("Date Formatting ""ParseException - dateFormat");
}

return outputDate;

}



Monday, 12 January 2015

Get width of button in layout at runtime.

 Simple code snippet Get width of button in layout at runtime. 

final ViewTreeObserver observer = openButton.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
widthButton = openButton.getWidth();
tXWidth = width - widthButton;
frameLayout.setTranslationX(tXWidth - leftMargin);
}
});

Set Margin on scal Metrics of devices dynamically.

Simple code snippet to Set Margin on scal Metrics of devices dynamically.

MarginLayoutParams marginParams = new MarginLayoutParams(
frameLayout.getLayoutParams());
final float scale = getResources().getDisplayMetrics().density;
leftMargin = (int) (leftMargin * scale );
marginParams.setMargins(leftMargintopMargin, 0, 0);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
marginParams);
layoutParams.gravity = Gravity.NO_GRAVITY// This is the new line
frameLayout.setLayoutParams(layoutParams);

Happy Codding