Show/Convert pixels to dp in android programmatically

How to calculate and show pixels into dp inside android application dynamically on button click.

In this tutorial we are converting pixels into dp format. We are getting pixels directly from app user using EditText and after clicking on button it will convert them and display on screen using TextView. So here is the complete step by step tutorial for Show/Convert pixels to dp in android programmatically.

android-project-download-code-button

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

Code for MainActivity.java file.

 package com.convertpixelstodp_android_examples.com;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
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 = 0;
 float value2 = 0;
 String TestValue;
 Context context;
 @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);
 context = getApplicationContext();
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 TestValue = edittext.getText().toString();
 
 value1 = Integer.parseInt(TestValue);
 
 value2 = PixelsToDp(context, value1);
 
 textview.setText(Float.toString(value2) + " - DP");
 }
 });
 }
 
 public float PixelsToDp(Context context, int pixels){
 
 Resources resource = context.getResources();
 
 float dp = pixels/(resource.getDisplayMetrics().densityDpi/160f);
 return dp;
 }
 
}

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.convertpixelstodp_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 pixels to dp in android programmatically" />

</RelativeLayout>

Screenshot:

pixel-2-dp

 

Show/Convert pixels to dp in android programmatically

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

Leave a Reply

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