Tuesday, February 5, 2019

Get Architecture, Version and Screen DPI programmatically

This code get Architecture, Version and Screen DPI of Android device programmatically, using Java.


package com.blogspot.android_er.myandroidinfo;

import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tvMyArch = findViewById(R.id.myArchitecture);
        TextView tvMyRelease = findViewById(R.id.myRelease);
        TextView tvMyDensity = findViewById(R.id.myDensity);

        String arch = System.getProperty("os.arch");
        tvMyArch.setText(arch);

        String release = Build.VERSION.RELEASE;
        tvMyRelease.setText(release);

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int dpi = metrics.densityDpi;
        tvMyDensity.setText(String.valueOf(dpi));

    }
}



<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="My device info"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:textStyle="bold"/>
    <TextView
        android:id="@+id/mylink"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-er.blogspot.com"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/title" />
    <TextView
        android:id="@+id/myArchitecture"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Architecture"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/mylink"
        android:gravity="left"
        android:textSize="30dp"/>
    <TextView
        android:id="@+id/myRelease"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Release"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/myArchitecture"
        android:gravity="left"
        android:textSize="30dp"/>
    <TextView
        android:id="@+id/myDensity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Density"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/myRelease"
        android:gravity="left"
        android:textSize="30dp"/>

</android.support.constraint.ConstraintLayout>

No comments: