1 Mayıs 2016 Pazar

-Android Custom Component 1-

Merhabalar,
Android uygulama geliştirirken genellikle standart componentler(bileşenler) kullanıyoruz. 
Ancak sözkonusu görsel tasarım ve/veya daha kapsamlı işlevlere sahip bileşenlere ihtiyaç duyulduğunda,ücretli veya kendi ihtiyacımıza yönelik component geliştirme seçeneklerini ele alıyoruz.
Bu makalede çeşitli özellikler kazandırılmış bir TextView bileşeni geliştirilmiştir.
Proje ile ilgili kaynak dosyaların içerikleri aşağıda yer almaktadır. Gerekli açıklamalar yapılmıştır.

1. Adım: "res/values/attrs.xml" dosyası oluşturunuz ve aşağıdaki içeriği uygulayınız.
-attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name ="MyTextView">
        <!-- gölgeleme: -->
        <attr name="fancyText" format="boolean"/>
        <!-- arka plan renk: -->
        <attr name="textBackColor" format="string" />
        <!-- metin rengi: -->
        <attr name="textColor" format="string"/>
        <!-- boyutu: -->
        <attr name="fontSize" format="float"/>
        <!-- ayırıcı(tarih,telefon no vb gibi kullanımlar için: -->
        <attr name="delimiter" format="string"/>
        <!-- tarih: -->
        <attr name="dateMod" format="boolean" />
         <!-- telefon no: -->
   <attr name="phoneNumberMod" format="boolean" />
    </declare-styleable>
</resources>


2.Adım: TextView bileşeni kullanılacak arayüz(layout) içeriği:
-activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res/com.example.androidtest1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidtest1.MainActivity" >

    <!--  
    //özel nitelik kapsamında geliştirdiğimiz bileşenleri kullanabilmemiz-erişebilmemiz 
    için bu satırı(xmlns:custom)  satırı ilgili layout'da kullanmalıyız.
    Aksi taktirde "com.paketadi.KontrolIsmi" şeklinde kullanımı mümkün olmaz.
    xmlns:custom="http://schemas.android.com/apk/res/com.example.androidtest1"
    -->
    <!-- TextView'dan extends edilen MyTextView nesnesi(paket yoluna dikkat!): -->
    <com.example.androidtest1.MyTextView
        android:id="@+id/myTextView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        custom:dateMod="true"
        custom:delimiter="/"
        custom:fancyText="true"
        custom:textBackColor="red" />
</RelativeLayout>


3.Adım: Nitelik(attribute) kazandırılacak bileşene(TextView) ait içerik.
-MyTextView.java:


package com.example.androidtest1;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.TextView;
import android.widget.Toast;

public class MyTextView extends TextView{
 public String delimiter;
 public boolean fancy_text;
 public String text_back_color;
 public String text_color;
 public boolean text_bold;
 public float text_size;
 public int font_size;
 public boolean date_mod;
 public boolean phone_mod;
 
public MyTextView(Context context) {
super(context);
}

public MyTextView(Context context ,AttributeSet attrs){
super(context,attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTextView );
     int index_c = typedArray.getIndexCount();
     for (int i = 0; i < index_c; ++i) 
     {
        int attr = typedArray.getIndex(i);
        switch (attr)
        {
        
          //tarih ayıraç için mod:
           case R.styleable.MyTextView_dateMod:
            date_mod = typedArray.getBoolean(attr, false);
            break;
           
           //telefon numarası içeriği için ayırac modu:
           case R.styleable.MyTextView_phoneNumberMod:
            phone_mod = typedArray.getBoolean(attr, false);
            break;
           
          //tarih,telefon no vb. gibi işlemlerde ayırıcı(sperate) olarak kullanmak istenirse:
           case R.styleable.MyTextView_delimiter:
           delimiter = typedArray.getString(attr);
           Toast.makeText(getContext(), "delimiter", 1000).show();
           break;

           //içerik gölgeleme:
           case R.styleable.MyTextView_fancyText:
           fancy_text = typedArray.getBoolean(attr, false);
           fancyText();
           break;
           
           case R.styleable.MyTextView_textBackColor:
            text_back_color = typedArray.getString(attr);
            customBackColor();
            break;
           case R.styleable.MyTextView_textColor:
            text_color = typedArray.getString(attr);
            customTextColor();
            break;
          
        }
     }
     dateNow();
              phoneNumber();
     typedArray.recycle();
}//Constructur

private void dateNow() {
if(date_mod){
     SimpleDateFormat dateFormat = 
     new SimpleDateFormat("dd" + delimiter + "MM" + delimiter + "yyyy");
     String today = dateFormat.format(Calendar.getInstance().getTime());
     this.setText(today);  
}
  }
private void phoneNumber(){
//..
//string formatında delimiter kullan..
if(phone_mod){
//formatlama..
}
}

  private void fancyText() {
     if(fancy_text){
        this.setShadowLayer(12, 5, 5, Color.rgb(44, 125, 12));
     }
  }
  private void customBackColor(){
  Color c = new Color();
  int color = c.parseColor(text_back_color);
 this.setBackgroundColor(color);
  }
  private void customTextColor(){
  Color c = new Color();
  int color = c.parseColor(text_color);
  this.setTextColor(color);
  }
}


MyTextView bileşenine erişmek ve kullanmak standart şekilde olduğu için tekrardan yazmaya ihtiyaç duyulmamıştır.
İyi çalışmalar.


Hiç yorum yok:

Yorum Gönder