<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sagar Malhotra</title>
    <description>The latest articles on DEV Community by Sagar Malhotra (@sagar0_0).</description>
    <link>https://dev.to/sagar0_0</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F715574%2Fae44bea0-9861-422f-8f2a-38390201cfbd.jpg</url>
      <title>DEV Community: Sagar Malhotra</title>
      <link>https://dev.to/sagar0_0</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sagar0_0"/>
    <language>en</language>
    <item>
      <title>DP vs SP vs DPI vs PX in Android</title>
      <dc:creator>Sagar Malhotra</dc:creator>
      <pubDate>Sun, 28 Jul 2024 15:47:30 +0000</pubDate>
      <link>https://dev.to/sagar0_0/dp-vs-sp-vs-dpi-vs-px-in-android-4j9i</link>
      <guid>https://dev.to/sagar0_0/dp-vs-sp-vs-dpi-vs-px-in-android-4j9i</guid>
      <description>&lt;p&gt;We use some standard Units of measurement to make sure everyone is on the same table when we are talking about sizes, in real world, and in the Android world.&lt;/p&gt;

&lt;p&gt;Even if you are not aware of a single term here, don’t worry I will start from the basics and explain the actual difference between all these Size-related Units used in Android.&lt;/p&gt;

&lt;p&gt;Let’s understand the basics:&lt;/p&gt;

&lt;p&gt;Screen Size:&lt;/p&gt;

&lt;p&gt;Screen size is the diagonally calculated size of your screen. This is generally calculated in Inches, which is a real-world Unit of measurement for sizes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy8mamirrhhl4kmgu0fcs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy8mamirrhhl4kmgu0fcs.png" alt="Image description" width="746" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PX or Pixels:&lt;/p&gt;

&lt;p&gt;These are the smallest units of display on a screen. Each pixel represents a single point of color, or you can also consider it as a dot that represents a single color.&lt;br&gt;
There are hundreds of dots present on your screen that show some specific color patterns to form an image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ve0w31587g7jf5ct9tm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ve0w31587g7jf5ct9tm.png" alt="Image description" width="800" height="700"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Screen Resolution:&lt;/p&gt;

&lt;p&gt;Screen resolution is the number of pixels a display can show in each dimension (vertical and horizontal).&lt;/p&gt;

&lt;p&gt;You might have heard a screen resolution is 1920 × 1080 pixels or 720 × 1280 pixels. That simply means 1920 pixels are there on the screen vertically and 1080 pixels are there horizontally.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnb6yb0yb5b24j8dqdlec.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnb6yb0yb5b24j8dqdlec.png" alt="Image description" width="780" height="1040"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: More Resolution does not always mean more screen size.&lt;br&gt;
Suppose there is a device with just 720*1280 resolution with 6 inches of screen size and another device with 1920*1080 resolution with the same 6 inches of screen size. So, we will see a significant difference in the quality of screens on both devices because of the terms called Density and PPI.&lt;/p&gt;

&lt;p&gt;Density:&lt;/p&gt;

&lt;p&gt;In general: Density is essentially a measurement of how tightly matter is packed together.&lt;br&gt;
Same here, density means how many no. of pixels are present in a particular area on your screen. &lt;br&gt;
The more the no. of pixels present in an area more will be the density and screen quality.&lt;br&gt;
The screen with high resolution has comparatively smaller pixel sizes so that there can be more density(more pixels tightly packed together).&lt;/p&gt;

&lt;p&gt;PPI: Pixel per inch.&lt;/p&gt;

&lt;p&gt;Density ∝ PPI ∝ Screen Quality ∝ (1/Pixel)&lt;/p&gt;

&lt;p&gt;That is why a 6-inch screen with a higher resolution of 1920*1080 will have more pixels in a small area and hence more colors can be generated in a small area of the screen and hence better the quality.&lt;/p&gt;

&lt;p&gt;DPI or Dots per Inch:&lt;/p&gt;

&lt;p&gt;To measure the above-mentioned density of the screen, we use this DPI.&lt;/p&gt;

&lt;p&gt;It simply means how many no. of pixels are present in one inch of the size.&lt;/p&gt;

&lt;p&gt;The base DPI is 160 dpi which corresponds to lower resolution screen devices and we call it MDPI.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs1x3wp80p1gdamvuuv44.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs1x3wp80p1gdamvuuv44.png" alt="Image description" width="800" height="304"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;DP or DIP or Density-Independent Pixels:&lt;/p&gt;

&lt;p&gt;As the name suggests, is a virtual pixel unit that’s used as a unit of measurement to preserve the visible size of a user interface (UI) on screens with different densities.&lt;/p&gt;

&lt;p&gt;It will remain the same from the user's perspective for different screen sizes but for different screen sizes, there will be different densities that correspond to different PX values.&lt;/p&gt;

&lt;p&gt;That also means there is a standard formula that we can use to calculate the DP and PX relation based on the current screen DPI.&lt;/p&gt;

&lt;p&gt;Formula:&lt;br&gt;
PX = DP * (dpi/160)&lt;/p&gt;

&lt;p&gt;For MDPI devices: dpi values is 160, put the value here:&lt;/p&gt;

&lt;p&gt;PX = DP * (160/160)&lt;/p&gt;

&lt;p&gt;PX = DP&lt;/p&gt;

&lt;p&gt;1 PX is equal to 1 DP in MDPI devices.&lt;/p&gt;

&lt;p&gt;Now, you can also calculate how many pixels correspond to 1 DP in different screen sizes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqmnkih30gseu2igjutb2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqmnkih30gseu2igjutb2.png" alt="Image description" width="800" height="505"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;SP or Scalable Pixels or Scale-Independent Pixels:&lt;/p&gt;

&lt;p&gt;It’s like DP for Fonts. The default value of an sp is the same as the default value for a dp&lt;/p&gt;

&lt;p&gt;It is the same as DP but it just takes care of the User system settings for Font-Sizes and scales the Fonts size if the user decides to increase/decrease the Font size from settings.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmtutpe9vu5kpadtijz5f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmtutpe9vu5kpadtijz5f.png" alt="Image description" width="800" height="320"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope this gives you the clarity on what you need to know and what you should use for displaying anything in the UI.&lt;/p&gt;

&lt;p&gt;Use DP for any components, so that they won’t show different sizes to user for different screen sizes and resolutions.&lt;/p&gt;

&lt;p&gt;Always use SP for Text components, so that it can respect the user system settings along with the resolution of screen.&lt;/p&gt;

&lt;p&gt;Follow Sagar Malhotra for more such Android related content.&lt;/p&gt;

</description>
      <category>android</category>
      <category>programming</category>
      <category>development</category>
    </item>
    <item>
      <title>Add Multilingual support (Multiple Languages) to your Android App</title>
      <dc:creator>Sagar Malhotra</dc:creator>
      <pubDate>Sat, 09 Mar 2024 13:55:53 +0000</pubDate>
      <link>https://dev.to/sagar0_0/add-multilingual-support-multiple-languages-to-your-android-app-3ihf</link>
      <guid>https://dev.to/sagar0_0/add-multilingual-support-multiple-languages-to-your-android-app-3ihf</guid>
      <description>&lt;p&gt;Supporting multiple languages is important to scale up your application and reach the masses. Around 25% of India and 64% of Europe's working adult population are multilingual, and even the USA saw a ~194% increase in multilingual population. (Source)&lt;/p&gt;

&lt;p&gt;Also, 65%+ consumers prefer to consume content in their preferred language, so this should be considered an important functionality that already comes with most of the TOP applications like Amazon, WhatsApp, Facebook, etc.&lt;/p&gt;

&lt;p&gt;So, let’s build for multilingual world!&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Organize your string resources.
&lt;/h2&gt;

&lt;p&gt;Do not use hardcoded string values anywhere in your code&lt;/p&gt;

&lt;h2&gt;
  
  
  Do
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Text(stringResources(R.string.follow_me))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Don’t
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Text("Follow me")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using strings.xml, we will have a common place for all our string resources and we can then support multiple languages by adding more strings.xml files.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: If you are having some complex string calculations/concatenations then still you can store them in strings.xml and format accordingly, maybe using MessageFormat&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  2. Add Multiple Languages
&lt;/h2&gt;

&lt;p&gt;You already have one place for all your string resources, now you just have to translate every string resource to all other supported languages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Example of hindi strings.xml file
&amp;lt;resources&amp;gt;
    &amp;lt;string name="subscribe_to_sagar_malhotra"&amp;gt;सागर मल्होत्रा की सदस्यता लें&amp;lt;/string&amp;gt;
    &amp;lt;string name="language"&amp;gt;हिन्दी&amp;lt;/string&amp;gt;
&amp;lt;/resources&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I am using an Android Studio Plugin named “AndroidLocalize” to perform this operation.&lt;/p&gt;

&lt;p&gt;The translated values might need some changes, but it can help you save a lot of time.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Trigger language change
&lt;/h2&gt;

&lt;p&gt;Whatever UI you are using for your language picker(here ExposedDropDownmenu), you have to trigger an onClick event to notify your OS that you have changed the locale of your application, so that the OS can also switch to that specific language’s strings.xml file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;onClick = {
    // set app locale given the user's selected locale
    AppCompatDelegate.setApplicationLocales(
        LocaleListCompat.forLanguageTags(
            "hi"//ISO for hindi
        )
    )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You need to pass the ISO-639 code of your language to send and notify your OS of changed language preferences.&lt;/p&gt;

&lt;h2&gt;
  
  
  3.1 Fixing the issue
&lt;/h2&gt;

&lt;p&gt;This might not work for your application, as currently this method only works for AppCompatActivity, check if you are extending ComponentActivity for your specific Activity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MainActivity : AppCompatActivity() { ... }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After extending AppCompatActivity, you also need to make changes to your supported theme for the specific activity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;style name="Theme.MultilingualApp" parent="Theme.AppCompat.Light.NoActionBar" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Save Locale Preferences&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For Android 12 or below, you have to either manually store the selected language preference value or just use this configuration in your AndroidManifest to allow AndoridX to handle the locale preferences itself.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;service//Inside application tag
    android:name="androidx.appcompat.app.AppLocalesMetadataHolderService"
    android:enabled="false"
    android:exported="false"&amp;gt;
    &amp;lt;meta-data
        android:name="autoStoreLocales"
        android:value="true" /&amp;gt;
&amp;lt;/service&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. Android OS Per-App Language Preferences
&lt;/h2&gt;

&lt;p&gt;In Android 13 and above, the Android OS also supports changing the Per-App Language preference from system settings.&lt;/p&gt;

&lt;p&gt;To notify your OS that your application also supports multiple languages, add the necessary configuration in your AndroidManifest file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Add this to your application tag
android:localeConfig="@xml/locale_config"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Define all your supported languages in the local_config.xml file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&amp;lt;locale-config xmlns:android="http://schemas.android.com/apk/res/android"&amp;gt;
    &amp;lt;locale android:name="en" /&amp;gt;
    &amp;lt;locale android:name="gu" /&amp;gt;
    &amp;lt;locale android:name="hi" /&amp;gt;
    &amp;lt;locale android:name="ar-AE" /&amp;gt;
&amp;lt;/locale-config&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, your application will also support changing the language from system settings too.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Avoid Activity Recreation
&lt;/h2&gt;

&lt;p&gt;You might have noticed that your activity is recreating when you are changing your application locale, that is because changing the locale is also a kind of configuration change and by default your activity will be recreated whenever a configuration change occurs.&lt;/p&gt;

&lt;p&gt;You can prevent this default behavior by informing your OS using AndroidManifest.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Add this to your specific activity tag
android:configChanges="layoutDirection|locale"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Video:&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/ObgmK3BywKI"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;I hope this feature helps you reach a wider audience, make sure to Follow me for more such useful content.&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>androiddev</category>
    </item>
  </channel>
</rss>
