Android uygulamanızda bir TextView‘in metin rengini değiştirmek için birkaç yöntem bulunmaktadır. Bu işlemi programlama veya XML dosyası üzerinden yapabilirsiniz.
XML Üzerinden Metin Rengini Değiştirme
TextView
‘in metin rengi XML dosyasından şu şekilde ayarlanabilir.
1 2 3 4 5 6 7 8 9 | <TextView android:id="@+id/textViewExample" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Örnek Metin" android:textColor="@android:color/holo_red_dark" /> |
android:textColor
özelliği ile rengi tanımlayabilirsiniz.- Renk için:
@android:color/holo_red_dark
gibi sistem renklerini kullanabilirsiniz.@color/
renk1 diyerek kendi tanımladığınız renkleri kullanabilirsiniz.
2. Programlama (Java/Kotlin) Üzerinden Metin Rengini Değiştirme
TextView’in metin rengini kod içinde değiştirmek için setTextColor
metodunu kullanabilirsiniz.
Java
1 2 3 4 | TextView textViewExample = findViewById(R.id.textViewExample); textViewExample.setTextColor(getResources().getColor(R.color.renk1)); |
Kotlin
1 2 3 4 | val textViewExample: TextView = findViewById(R.id.textViewExample) textViewExample.setTextColor(resources.getColor(R.renk1, null)) |
3. Renk Tanımı Eklemek
Kendi özel renklerinizi res/values/colors.xml
dosyasında tanımlayabilirsiniz.
1 2 3 4 5 | <resources> <color name="renk1">#FF5733</color> </resources> |
Bu renk daha sonra XML veya kod içinde kullanılabilir.
- XML:
@color/renk1
- Java/Kotlin:
R.color.renk1
4. Örnek: Dinamik Renk Değiştirme
Eğer bir butona tıklayınca TextView’in rengini değiştirmek isterseniz, şu şekilde yapabilirsiniz.
Java
1 2 3 4 5 6 7 8 | Button buttonChangeColor = findViewById(R.id.buttonChangeColor); TextView textViewExample = findViewById(R.id.textViewExample); buttonChangeColor.setOnClickListener(v -> { textViewExample.setTextColor(getResources().getColor(R.color.renk1)); }); |
Kotlin
1 2 3 4 5 6 7 8 | val buttonChangeColor: Button = findViewById(R.id.buttonChangeColor) val textViewExample: TextView = findViewById(R.id.textViewExample) buttonChangeColor.setOnClickListener { textViewExample.setTextColor(resources.getColor(R.color.renk1, null)) } |
Örnek Renk Değiştirme Projesi
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = findViewById(R.id.textViewExample); Button button = findViewById(R.id.buttonChangeColor); button.setOnClickListener(v -> { // Rengi kırmızıya çevir textView.setTextColor(getResources().getColor(android.R.color.holo_red_dark)); }); } } |