28 Eylül 2017 Perşembe

Fragment Sınıfı

Fragment Nedir?
Fragment Activity nesnesinin modüler bir  parçasıdır. Kendi yaşam döngüsü vardır ve kendi event'lerini alır.

Ana XML
Fragment main_activity.xml'de şöyle gösterilir.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/homebuttons_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="200dp" />
Kalıtım
Şöyle yaparız.
public class UsersView extends Fragment {

  View myView;

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
    @Nullable Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.users, container, false);

    ...
    // The End
    return myView;
  }

}
Fragment Değiştirme
Bir fragment başka bir fragment ile değiştirilebilir.
public void replaceFragment(Fragment someFragment) {
  FragmentTransaction transaction = getFragmentManager().beginTransaction();
  transaction.replace(R.id.frame_container, someFragment);
  transaction.addToBackStack(null);
  transaction.commit();
}
Şöyle çağırırız.
Fragment fragment = new AboutFragment();
replaceFragment(fragment);
OnCreateView metodu
Şöyle yaparız.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

  View view = inflater.inflate(R.layout.fragment_home, container, false);
  ....
  return view;
}
View yaratıldıktan sonra içindeki nesneler elde edilir. Şöyle yaparız.
myButton = (Button) view.findViewById(R.id.button);
getContext metodu
Bu metod fragment'i içeren activity'nin context'ini döndürür. Fragment kendini içeren activity nesnesini bilir.
// Activity this fragment is attached to.
FragmentHostCallback mHost;
Metodun içinden de host nesnesinin kullanıldığı görülebilir.
/**
 * Return the {@link Context} this fragment is currently associated with.
 */
public Context getContext() {
    return mHost == null ? null : mHost.getContext();
}
getActivity metodu
Fragment içinde Toast yaratmak istersek şöyle yaparız
Toast.makeText(getActivity(), "...", Toast.LENGTH_LONG).show();


Looper Sınıfı

getMainLooper metodu
Şöyle yaparız.
Looper.getMainLooper();

27 Eylül 2017 Çarşamba

PopupMenu Sınıfı

constructor
Açıklaması şöyle. Anchor denilen şey ikinci parametre
Android Popup Menu displays the menu below the anchor text if space is available otherwise above the anchor text. It disappears if you click outside the popup menu.
Şöyle yaparız.
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu (MainActivity.this, button1);
getMenuInflater metodu
Örnek
popup_menu.xml şöyledir.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item  
    android:id="@+id/Stats"  
    android:title="Stats"/>  

  <item  
    android:id="@+id/Reset"  
    android:title="Reset"/>  

  <item  
    android:id="@+id/three"  
    android:title="Three"/>  
</menu>
Şöyle yaparız.
//Inflating the Popup using xml file
popup.getMenuInflater ()
  .inflate (R.menu.popup_menu, popup.getMenu());
Örnek
popup_menu.xml şöyledir.
<menu xmlns:androclass="http://schemas.android.com/apk/res/android" >  

  <item  
    android:id="@+id/one"  
    android:title="One"/>  

  <item  
    android:id="@+id/two"  
    android:title="Two"/>  

  <item  
    android:id="@+id/three"  
    android:title="Three"/>  

</menu>  

setOnMenuItemClickListener metodu
Örnek
Şöyle yaparız.
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new      

  PopupMenu.OnMenuItemClickListener() {
    public boolean onMenuItemClick(MenuItem item) {
      ...
      return true;
    }
});
Örnek
Şöyle yaparız
menuButton.setOnClickListener(new View.OnClickListener() {  
  public void onClick(View v) {    
    PopupMenu popup = new PopupMenu(MainActivity.this, menuButton);  
    popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());  

    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {  
      public boolean onMenuItemClick(MenuItem item) {  
        ...
        return true;  
      }  
    });  

    popup.show(); 
  }  
});
onMenuItemClick metodunda tıklanan menu nesnesi id ile bulunabilir.
switch (item.getItemId()) {
    case R.id.Stats:
        doStuff();
        return true;
    case R.id.Reset:
        ...
        return true;
}
show metodu
Şöyle yaparız.
popup.show(); //showing popup menu





26 Eylül 2017 Salı

Context.startActivity metodu

Giriş
Intent için verilen birinci parametre context, ikinci parametre başlatılacak Activity sınıfı olmalı.

Yanlış Kullanım
Şu kod ikinci parametre sınıf olmadığı için yanlış.
Intent enableLocIntent = new Intent(MainActivity.this, EnableLocationActivity.this);
Örnek
Şöyle yaparız
Intent intent = new Intent (context, NewActivity.class);
context.startActivity (intent);