Android में रंग को इंटीजर (ARGB) से हेक्साडेसिमल स्ट्रिंग (#RRGGBB या #AARRGGBB) में बदलने का तरीका।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट
res/layout/activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
चरण 3: रूपांतरण लॉजिक
src/MainActivity.java:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.textView);
int intColor = -16895234; // उदाहरण: नीला रंग
String hexColor = Integer.toHexString(intColor);
// ARGB फॉर्मेट में 8 हेक्स डिजिट होते हैं
// पहला 2 डिजिट अल्फा (FF = अपारदर्शी)
// substring(2) से अल्फा हटाकर #RRGGBB प्राप्त करें
String hexWithoutAlpha = hexColor.substring(2).toUpperCase();
textView.setText("#" + hexWithoutAlpha);
}
}
रंग इंटीजर फॉर्मेट
Android में रंग 32-बिट इंटीजर होता है: 0xAARRGGBB
- AA - अल्फा (00=पारदर्शी, FF=अपारदर्शी)
- RR - लाल
- GG - हरा
- BB - नीला
उपयोगी हेल्पर मेथड्स
// इंटीजर से #RRGGBB (अल्फा के बिना)
public static String toHexNoAlpha(int color) {
return String.format("#%06X", (0xFFFFFF & color));
}
// इंटीजर से #AARRGGBB (अल्फा के साथ)
public static String toHexWithAlpha(int color) {
return String.format("#%08X", color);
}
// हेक्स स्ट्रिंग से इंटीजर
public static int fromHex(String hex) {
return Color.parseColor(hex);
}
// Color क्लास उपयोग (API 1+)
int color = Color.rgb(255, 0, 0); // लाल
int colorWithAlpha = Color.argb(255, 255, 0, 0); // अपारदर्शी लाल
// कंपोनेंट निकालें
int red = Color.red(colorInt);
int green = Color.green(colorInt);
int blue = Color.blue(colorInt);
int alpha = Color.alpha(colorInt);
उदाहरण आउटपुट
Color.RED(-65536) →#FF0000Color.BLUE(-16776961) →#0000FFColor.WHITE(-1) →#FFFFFFColor.TRANSPARENT(0) →#00000000
चरण 4: मैनिफेस्ट
मानक मैनिफेस्ट।