Calculate/Convert dp to pixels in android programmatically

How to get dp(Density-independent Pixels) from EditText and change to normal pixels in android app dynamically on button click.

In this tutorial we are converting the entered dp value into pixels format. The value which you want to convert is easily entered using EditText box and after it we simply retrieve the typed value from EditText and convert it into Pixels format and show on screen using TextView. So here is the complete step by step tutorial for Calculate/Convert dp to pixels in android programmatically.

android-project-download-code-button

How to Calculate/Convert dp to pixels in android programmatically.

Code for MainActivity.java file.

 package com.convertdptopixels_android_examples.com;

import java.text.NumberFormat;
import java.text.ParseException;

import android.app.Activity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

 TextView textview;
 Button button;
 EditText edittext;
 int value1,value2 = 0;
 String TestValue;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 button = (Button)findViewById(R.id.button1);
 textview = (TextView)findViewById(R.id.textView1);
 edittext = (EditText)findViewById(R.id.editText1);
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 TestValue = edittext.getText().toString();
 
 try {
 
 value1 = ((Number)NumberFormat.getInstance().parse(TestValue)).intValue();
 
 } catch (ParseException e) {
 
 // TODO Auto-generated catch block
 
 e.printStackTrace();
 }
 
 value2 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
 value1, getResources().getDisplayMetrics());
 
 textview.setText(Integer.toString(value2) + " - Pixels");
 
 }
 });
 }

}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.convertdptopixels_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:text="Result Show Here"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <EditText
 android:id="@+id/editText1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="25dp"
 android:ems="10"
 android:inputType="number"
 android:gravity="center"
 android:hint="Type Value To Convert" >

 </EditText>

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/editText1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="22dp"
 android:text="Click Here to Convert dp to pixels in android programmatically" />

</RelativeLayout>

Screenshots:

Calculate/Convert dp to pixels in android programmatically

dp-2

Click here to download Calculate/Convert dp to pixels in android programmatically project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *