如何在伊斯兰主题Android应用项目中添加乌尔都语(参考示例样式)
Hey there! Let's break down exactly how to add Urdu language support to your Islamic-themed Android app, including handling the right-to-left (RTL) layout that's critical for matching the reference style you mentioned. Here's a step-by-step guide:
First, you need to create language-specific resource files for Urdu:
- In your Android project, navigate to
res/values - Create a new folder named
values-ur(theurcode is the ISO 639-1 code for Urdu) - Inside this folder, create a
strings.xmlfile and populate it with your app's text translated to Urdu. For example:
<resources> <string name="app_name">اسلامی ایپ</string> <string name="prayer_times">نماز کے اوقات</string> <string name="read_quran">قرآن پڑھیں</string> <string name="dua_section">دعایں</string> </resources>
Urdu is a right-to-left language, so your app needs to support RTL layouts to display content correctly:
- Open your
AndroidManifest.xmland addandroid:supportsRtl="true"to the<application>tag:
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" <!-- Add this line --> android:theme="@style/AppTheme"> <!-- Activities go here --> </application>
- Update your layout files to use start/end instead of left/right for padding, margins, and positioning. For example:
- Replace
android:paddingLeftwithandroid:paddingStart - Replace
android:layout_toRightOfwithandroid:layout_toEndOf
This ensures elements align correctly when the language switches to RTL.
- Replace
If you want users to manually switch to Urdu from your app's settings, here's how to do it in Kotlin:
fun switchToUrdu(context: Context) { val urduLocale = Locale("ur") Locale.setDefault(urduLocale) val resources = context.resources val config = resources.configuration config.setLocale(urduLocale) config.setLayoutDirection(urduLocale) resources.updateConfiguration(config, resources.displayMetrics) // Restart the app to apply changes val intent = context.packageManager.getLaunchIntentForPackage(context.packageName) intent?.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK) context.startActivity(intent) }
You can trigger this function from a button or setting option in your app.
Some default system fonts might not render Urdu script (especially Nastaleeq style) perfectly. For an authentic Islamic theme, use a custom Urdu font like Jameel Noori Nastaleeq or Nafees Nastaleeq:
- Place your font file (e.g.,
jameel_noori_nastaleeq.ttf) inassets/fonts/ - Create a font resource file in
res/font/jameel_noori_nastaleeq.xml:
<?xml version="1.0" encoding="utf-8"?> <font-family xmlns:android="http://schemas.android.com/apk/res/android"> <font android:fontStyle="normal" android:fontWeight="400" android:font="@font/jameel_noori_nastaleeq" /> </font-family>
- Apply the font to TextViews either directly in layout:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/quran_verse" android:fontFamily="@font/jameel_noori_nastaleeq" />
Or define it in a style to apply across your app.
- Use Android Studio's Layout Inspector to preview RTL layouts without changing system settings
- Switch your emulator or test device's system language to Urdu to see how the app behaves in a full RTL environment
- Check for layout issues like misaligned buttons, clipped text, or reversed icons—adjust as needed to match your reference design.
内容的提问来源于stack exchange,提问作者Shakeel Mughal

