Bu dersimizde android support library kütüphanesini incelemeye devam ediyoruz. Bu videoda snackbarın ne olduğundan ve nasıl uygulanması gerektiğini anlatıyoruz.
Snackbarın ve flaoting action button’ın birbirlerine uyumlu yani bir scroll işleminde birbirlerine uygun hareket etmesi için kök root viewgroup’u CoordinatorLayout seçmemiz gerekiyor. Aksi takdirde snackbarımız floating action button’ın üzerine geliyor ve bu durum önerilen bir durum değil.
Snack bar toast mesajların daha görsel ve efektif hali diyebiliriz. Uygulaması da çok kolay:
Snackbar.make(view, “Simple Snackbar”, Snackbar.LENGTH_SHORT).show() diyerek ekranda belirmesini sağlayabiliriz. Burdaki view snackbarın görüleceği layoutun root elementidir.
Snackbar’da bilgi ile beraber bir action butonu da oluşturabiliriz. Bunun için snackbar.setAction methodunu kullanmamız gerekiyor…
Snackbarımızı kişiselleştirmemiz de mümkün. Bunun için snackbar.getView() methodundan gelen değeri bir View nesnesine atayım gerekli değişiklikleri yapabiliriz.
Fab ve snack bar anlatımlarında kullanılan projeyi indirmek için lütfen burayı tıklayınız…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
public class MainActivity extends AppCompatActivity { CoordinatorLayout coordinatorLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); coordinatorLayout= (CoordinatorLayout) findViewById(R.id.rootLayout); FloatingActionButton fab= (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getBaseContext(), "Fab", Toast.LENGTH_LONG).show(); } }); } public void simpleSnackBar(View view){ Snackbar.make(coordinatorLayout, "Simple Snackbar Örneği", Snackbar.LENGTH_SHORT).show(); } public void snackBarwithAction(View v){ Snackbar snackbar=Snackbar.make(coordinatorLayout, "Hata Oluştu", Snackbar.LENGTH_SHORT); snackbar.setAction("TEKRAR DENE", new View.OnClickListener() { @Override public void onClick(View v) { Snackbar.make(coordinatorLayout, "Bu sefer oldu", Snackbar.LENGTH_SHORT).show(); } }); snackbar.show(); } public void snackBarCustomize(View v){ Snackbar snackbar=Snackbar.make(coordinatorLayout, "Kişisel snackbar örneği", Snackbar.LENGTH_SHORT); View snackBarLayout=snackbar.getView(); snackBarLayout.setBackgroundColor(Color.RED); TextView tv= (TextView) snackBarLayout.findViewById(android.support.design.R.id.snackbar_text); tv.setTextColor(Color.YELLOW); snackbar.show(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rootLayout" tools:context="com.example.emrealtunbilek.fabvesnackbarexample.MainActivity"> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@android:drawable/ic_dialog_email" android:layout_gravity="bottom|right" android:layout_margin="16dp" app:fabSize="normal" android:backgroundTint="@color/colorAccent" app:rippleColor="@color/colorPrimaryDark" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="vertical" > <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Simple Snack Bar" android:onClick="simpleSnackBar" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="snackBarwithAction" android:text="Snack Bar with Action" /> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="snackBarCustomize" android:text="Snack Bar Kişiselleştirme" /> </LinearLayout> </android.support.design.widget.CoordinatorLayout> |