2 Haziran 2016 Perşembe

BaseAdapter Sınıfı

Giriş
Şu satırı dahil ederiz.
import android.widget.BaseAdapter;
Bu sınıf bir ListView ile beraber kullanılır. Sınıfı şöyle kalıtırız
public class MyCustomAdapter extends BaseAdapter {
  Context context;
  ArrayList<String> brands;

  public AdapterListView(ArrayList<String> brands, Context context) {
    this.brands = brands;
    this.context = context;
  }

  @Override
  public int getCount() {
    return brands.size();
  }

  @Override
  public Object getItem(int i) {
    return null;
  }

  @Override
  public long getItemId(int i) {
    return 0;
  }

  @Override
  public View getView(int i, View view, ViewGroup viewGroup) {
    ...
    return view;
  }
}
getCount metodu
Şöyle yaparız.
@Override
public int getCount() {
  return list.size();
}
getItem metodu
Şöyle yaparız.
@Override
public Object getItem(int position) {
  return list.get(position);
}
getItemId metodu
Şöyle yaparız.
@Override
public long getItemId(int position) {
  return position;
}
getViewMetodu
1. Metoda geçilen view null ise yeni bir nesne yaratılır.
@Override 
public View getView(int position, View view, ViewGroup parent){

  if (view == null){
    //Burada LayoutInflater ile bir view nesnesi yarat
  }
  
  return view;
}
Yeni bir nesne yaratmak için şu iki yöntem kullanılabilir.
LayoutInflater inflater = getLayoutInflater();
view = inflater.inflate(R.layout.row, null);
veya
view= LayoutInflater.from(context).inflate(R.layout.activity_listview,null);
2. Daha sonra view nesnesine veri atanır. Atama işlemi için view nesnesi gerçek nesneye cast edilir.
Satır için kullanılan xml'in şöyle bir şey olduğunu varsayalım.
/*********************row.xml **************/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:padding="10dip" >

  <TextView android:id="@+id/Name"
        android:layout_height="wrap_content" android:layout_width="match_parent"
        android:layout_marginLeft="25dp" />

</LinearLayout>
Bu durumda her satırda Name isimli bir TextView olacaktır.
TextView tv=(TextView)view.findViewById(R.id.Name);
tv.setText(brands.get(i));
Bir başka örnek
((TextView) view.findViewById(R.id.Name)).setText("...");
3. Eğer satır bir çok bileşenden oluşuyorsa bir holder kullanılır. Önce bir holder tanımlarız. Şöyle yaparız.
public class ViewHolder{
  TextView tvId,tv1,tv2,tv3,tv4,tv5;
}
Daha sonra yeni satır için holder yaratılır. Mevcut satır için holder alınır. Holder doldurulur ve view ile döndürülür. Şöyle yaparız.
@Override
public View getView(int position, View v, ViewGroup parent) {
  final  ViewHolder holder;
  if (v == null){
    holder = new ViewHolder();
    v = layoutInflater.inflate(R.layout.data_item, null);
    holder.tvId = (TextView) v.findViewById(R.id.txtID);
    holder.tv1 = (TextView) v.findViewById(R.id.txt1);
    holder.tv2 = (TextView) v.findViewById(R.id.txt2);
    holder.tv3 = (TextView) v.findViewById(R.id.txt3);
    holder.tv4 = (TextView) v.findViewById(R.id.txt4);
    holder.tv5 = (TextView) v.findViewById(R.id.txt5);
    v.setTag(holder);
    }else{
      holder = (ViewHolder) v.getTag();
    }

    holder.tvId.setText(actorsList.get(position).getId());
    holder.tv1.setText(list.get(position).get1());
    holder.tv2.setText(list.get(position).get2());
    holder.tv3.setText(list.get(position).get3());
    holder.tv4.setText(list.get(position).get4());
    holder.tv5.setText(list.get(position).get5());
    return v;
}
4. Eğer istenirse arka plan rengi de değiştirilebilir.
if (view == null) {
  view = layoutInflater.inflate(R.layout.list_ld_requirements, null);
  ...
}
view.setBackgroundColor(Color.parseColor("#E5E5E5"));

getTag metodu
getTag çok sütunlu listeler için kullanılıyorsa her bir sütuna erişmek için kullanılır. Şöyle bir sınıf tanımlarız.
static class ViewHolder {
    TextView headlineView;
    TextView reporterNameView;
    TextView reportedDateView;
    public TextView ldstatusView;
    public TextView ldtype;
    public TextView ldsource;
    public TextView req;
}
Daha sonra bu sınıfı setTag metodu ile her satıra atarız.
public View getView(int position, View view, ViewGroup parent) {
  ViewHolder holder;

  if (view == null) {
    view = layoutInflater.inflate(R.layout.list_ld_requirements, null);
    holder = new ViewHolder();
    holder.headlineView = (TextView) convertView.findViewById(R.id.sbu);
    holder.reporterNameView = (TextView) convertView.findViewById(R.id.bu);
    holder.reportedDateView = (TextView) convertView.findViewById(R.id.sbuu);
    holder.ldstatusView = (TextView) convertView.findViewById(R.id.sc);
    holder.ldtype=(TextView)convertView.findViewById(R.id.ssc);
    holder.ldsource=(TextView)convertView.findViewById(R.id.rq);
    view.setTag(holder);
  } else {
    holder = (ViewHolder) view.getTag();
  }

  holder.headlineView.setText("...");
  holder.reportedDateView.setText("...");
  holder.ldstatusView.setText("...");
  holder.ldtype.setText("...");
  holder.ldsource.setText("...");
  return convertView;
}


30 Mayıs 2016 Pazartesi

Google-Maps GoogleMap.OnMapClickListener Arayüzü

Şöyle yaparız.
GoogleMap.OnMapClickListener clickListener=new GoogleMap.OnMapClickListener() {
  @Override
  public void onMapClick(final LatLng pos) {...}

};
Bu nesneyi şöyle ekleriz.
map.setOnMapClickListener(clickListener);

Google-Maps GoogleMap.OnMarkerDragListener Arayüzü

Şöyle yaparız.
// we define the object to invoke when the marker is dragged
map.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener()
{
  @Override
  public void onMarkerDragStart(Marker arg0)
  {...}
    
  @Override
  public void onMarkerDragEnd(final Marker pos)
  {...}

  @Override
  public void onMarkerDrag(Marker arg0)
  {...}
     
});

15 Mayıs 2016 Pazar

CircleOptions

Giriş
Bu sınıf fluent interface şeklinde kullanılır. Şöyle yaparız.
LatLng center =...; double radius = ...;

circle = mMap.addCircle(new CircleOptions()
             .center(center)
             .radius(radius)
             .strokeWidth(2)
             .strokeColor(mStrokeColor)
             .fillColor(mFillColor));
Constructor
Şöyle yaparız
new CircleOptions();
center metodu
Şöyle yaparız
LatLng center = ...;
center(center)
fillColor metodu
Şöyle yaparız
fillColor(mFillColor)
radius metodu
Şöyle yaparız
double radius = ...;
radius(radius)
strokeColor metodu
Şöyle yaparız
strokeColor(mStrokeColor)
strokeWidth metodu
Şöyle yaparız
strokeWidth(2)


20 Mart 2016 Pazar

Ringtone Sınıfı

constuctor
Şöyle yaparız.
Uri ntfy = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), ntf);
play metodu
Şöyle yaparız.
r.play();



14 Mart 2016 Pazartesi

Toast Sınıfı

Giriş
Şu satırı dahil ederiz.
import android.widget.Toast;
Toast bilgilendirme metinleri göstermek için kullanılır. Toast nesnesi make() metodlarından birisini kullanarak yaratılır. Daha sonra ekranda göstermek için show() metodu çağrılır.

En Basit Kullanım Şekli
Toast şöyle kullanılır. 3. parametre olan LENGTH_LONG bilgilendirme penceresinin ne kadar süre gösterileceğini belirtir.

Toast.makeText(MainActivity.this,"Checking...",Toast.LENGTH_LONG).show();
Bir başka örnek
Toast.makeText(getApplicationContext(),
                        "Passwords do not match!",Toast.LENGTH_LONG)
                        .show();
Başka Thread
Toast normalde GUI thread'inden çağrılır. Başka bir thread içinden çağırmak için şöyle yaparız.
@Override
public void onXYZ(...) {
  runOnUiThread(new Runnable() {
    public void run() {
      Toast.makeText(...).show();
    }
  });
}
constructor
Şöyle yaparız.
Toast toast = new Toast(getApplicationContext());
cancel metodu
Ekranda birden fazla toast göstermek iyi değil. Öncekini iptal etmek gerekir. Bu durumda toast nesnesini saklamamız lazım gelir. Önceli toast nesnsi önce cancel() ile iptal edilir. Yeni toast nesnesi yaratılır ve show() çağrılır.
void toast(final String message) {
  mToast.cancel();
  mToast = Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT);
  mToast.show();
}
setDuration metodu
Şöyle yaparız.
toast.setDuration(Toast.LENGTH_LONG);
setGravity metodu
Toast penceresinin hangi konumda açılacağını belirtmemize yarar. Normalde Toast penceresi belirli bir konumda açılır. Eğer bir başka bileşene göre konumu ayarlamak istersek şöyle yaparız. Önce bileşenin konumunu buluruz.
buttonView.getLocationOnScreen(location);
Daha sonra Toast pencereminizin konumunu ayarlarız.
toast.setGravity(Gravity.TOP|Gravity.LEFT,buttonView.getRight()+5, 
location[1]-10);
setView metodu
Şöyle yaparız.
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
                (ViewGroup) findViewById(R.id.toast_layout_root));
...
toast.setView(layout);
show metodu
Şöyle yaparız.
toast.show();



6 Mart 2016 Pazar

MediaStore Sınıfı

Media Sınıfı
Bu sınıfa şöyle erişiriz.
MediaStore.Images.Media
insertImage metodu
Elimizde bir dosya ismi olsun.
String fileName = ...;
Şöyle yaparız.
Media.insertImage(getContentResolver(), fileName, "Screen", "screen");