Android Studio ile Intro Slider Yapımı

Slider özelliği, uygulamalarımıza eklediğiniz resimlerin çeşitli efektlerle geçiş yapmasını sağlayan görsel bir uygulamadır. Uygulamalarınızda kuruluğunda intro slider bir kere gösterilirildikten sonra bir daha gösterilmeyecek şekilde nasıl yapabileceğimize basit bir uygulama yaparak anlamaya çalışacağız.
Öncelikle yukardaki gifte ekranın altında bulunan noktaların renklerini projemize tanıtmamız gerekiyor. Noktaların aktif ve bir de pasif renkleri olduğuna dikkat edelim. colors.xml dosyamızı aşağıdaki gibi düzenlememiz gerekiyor.
colors.xml
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 |
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <!-- Ekran Arka Planları--> <color name="slide1">#f64c73</color> <color name="slide2">#20d2bb</color> <color name="slide3">#3395ff</color> <color name="slide4">#c873f4</color> <!-- Noktaların pasif renkleri --> <color name="slide1_nokta_pasif">#d1395c</color> <color name="slide2_nokta_pasif">#14a895</color> <color name="slide3_nokta_pasif">#2278d4</color> <color name="slide4_nokta_pasif">#a854d4</color> <!-- Noktaların aktif renkleri --> <color name="slide1_nokta_aktif">#f98da5</color> <color name="slide2_nokta_aktif">#8cf9eb</color> <color name="slide3_nokta_aktif">#93c6fd</color> <color name="slide4_nokta_aktif">#e4b5fc</color> <array name="array_dot_active"> <item>@color/slide1_nokta_aktif</item> <item>@color/slide2_nokta_aktif</item> <item>@color/slide3_nokta_aktif</item> <item>@color/slide4_nokta_aktif</item> </array> <array name="array_dot_inactive"> <item>@color/slide1_nokta_pasif</item> <item>@color/slide2_nokta_pasif</item> <item>@color/slide3_nokta_pasif</item> <item>@color/slide4_nokta_pasif</item> </array> </resources> |
Uygulamayı çalıştırdığımız zaman yukarda çıkan bilgilendirme barı içinde aşağıdaki styles.xml dosyasına göre düzenlemelerimiz yapalım.
styles.xml
1 2 3 4 5 6 7 8 9 10 11 |
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style> </resources> |
Uygulamamızda intromuz bir defa gözüktükten sonra bir daha gözükmemesi için Shared Preferences kullanmamız gerekiyor. Aşağıdaki gibi PrefManager.java adlı bir sınıf oluşturuyoruz.
PrefManager.java
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 |
package com.example.esatgozcu.introsliderkullanimi; import android.content.Context; import android.content.SharedPreferences; public class PrefManager { /** Program her çalıştığında IntroSlider gözükmemesi için SharedPreferences kullanıyoruz */ SharedPreferences pref; SharedPreferences.Editor editor; Context _context; // shared pref mode int PRIVATE_MODE = 0; // Shared preferences file name private static final String PREF_NAME = "androidhive-welcome"; private static final String IS_FIRST_TIME_LAUNCH = "IsFirstTimeLaunch"; public PrefManager(Context context) { this._context = context; pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE); editor = pref.edit(); } public void setFirstTimeLaunch(boolean isFirstTime) { editor.putBoolean(IS_FIRST_TIME_LAUNCH, isFirstTime); editor.commit(); } public boolean isFirstTimeLaunch() { return pref.getBoolean(IS_FIRST_TIME_LAUNCH, true); } } |
Şimdi hızlı bir şekilde intromuzda göstereceğimiz sayfalarımızı teker teker ekleyelim.
ekran1.xml
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 |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/slide1"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/baslık" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/textView2" android:layout_centerHorizontal="true" android:layout_marginTop="100dp" android:text="PHP" android:textAlignment="center" android:textColor="@android:color/white" android:textSize="80sp" android:textStyle="bold" /> <TextView android:id="@+id/yazi" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/textView6" android:layout_marginTop="40dp" android:paddingLeft="30dp" android:paddingRight="30dp" android:text=" Php 1994 yılında PHP'nin babası olarak nitelendirilen Rasmus Lerdorf tarafından ortaya çıkarıldı." android:textAlignment="center" android:textColor="@android:color/white" android:textSize="24sp" /> </LinearLayout> </RelativeLayout> |
ekran2.xml
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 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/slide2"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/baslik" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/textView2" android:layout_centerHorizontal="true" android:layout_marginTop="100dp" android:text="JAVA" android:textAlignment="center" android:textColor="@android:color/white" android:textSize="80sp" android:textStyle="bold" /> <TextView android:id="@+id/yazi" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/textView6" android:layout_marginTop="40dp" android:paddingLeft="30dp" android:paddingRight="30dp" android:text="Java, Sun Microsystems'den James Gosling tarafından geliştirilen bir programlama dilidir" android:textAlignment="center" android:textColor="@android:color/white" android:textSize="24sp" /> </LinearLayout> </RelativeLayout> |
ekran3.xml
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 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/slide3"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/baslik" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/textView2" android:layout_centerHorizontal="true" android:layout_marginTop="100dp" android:text="PERL" android:textAlignment="center" android:textColor="@android:color/white" android:textSize="80sp" android:textStyle="bold" /> <TextView android:id="@+id/yazi" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/textView6" android:layout_marginTop="40dp" android:paddingLeft="30dp" android:paddingRight="30dp" android:text="Perl, bir dil bilimci olup NASA'da sistem yöneticisi olarak çalışan Larry Wall tarafından geliştirilmiş bir programlama dilidir." android:textAlignment="center" android:textColor="@android:color/white" android:textSize="24sp" /> </LinearLayout> </RelativeLayout> |
ekran4.xml
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 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/slide4"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/baslik" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/textView2" android:layout_centerHorizontal="true" android:layout_marginTop="100dp" android:text="PYTHON" android:textAlignment="center" android:textColor="@android:color/white" android:textSize="80sp" android:textStyle="bold" /> <TextView android:id="@+id/yazi" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/textView6" android:layout_marginTop="40dp" android:paddingLeft="30dp" android:paddingRight="30dp" android:text="Python, Guido Van rossum adlı hollandalı bir programcı tarafından yazılmış bir programlama dilidir." android:textAlignment="center" android:textColor="@android:color/white" android:textSize="24sp" /> </LinearLayout> </RelativeLayout> |
Uygulama ilk çalıştığı zaman karşımıza çıkan MainActivity.java sınıfını aşağıdaki gibi düzenleyelim.
MainActiviy.java
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 |
package com.example.esatgozcu.introsliderkullanimi; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.button); // Butona IntroSlider başlıyor.. button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this,AnaEkran.class)); finish(); } }); } } |
activity_main.xml
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 |
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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" tools:context="com.example.esatgozcu.introsliderkullanimi.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="180dp" android:text="Intro Slider Kullanımı" android:textAlignment="center" android:textColor="@android:color/black" android:textSize="36sp" /> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" android:layout_marginLeft="30dp" android:layout_marginRight="30dp" android:layout_marginTop="23dp" android:background="@android:color/holo_blue_dark" android:paddingBottom="10dp" android:paddingTop="10dp" android:text="BAŞLA" android:textColor="@android:color/white" android:textSize="30sp" /> </LinearLayout> </android.support.constraint.ConstraintLayout> |
Şimdi intromuzda bulunan sayfaların geçişlerini ileri, geç, başla butonlarını ve alt tarafta bulunan noktaların kontrolünü yapacağımız kısma geldik. İlk olarak ana_ekran.xml dosyamızı aşağıdaki gibi dizayn ediyoruz.
ana_ekran.xml
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 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <Button android:id="@+id/btn_skip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:background="@null" android:text="GEÇ" android:textColor="@android:color/white" /> <Button android:id="@+id/btn_next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:background="@null" android:text="İLERİ" android:textColor="@android:color/white" /> <View android:id="@+id/view" android:layout_width="match_parent" android:layout_height="1dp" android:layout_above="@id/layoutDots" android:alpha=".5" android:background="@android:color/white" /> <LinearLayout android:id="@+id/layoutDots" android:layout_width="match_parent" android:layout_height="30dp" android:layout_alignParentBottom="true" android:layout_marginBottom="20dp" android:gravity="center" android:orientation="horizontal"></LinearLayout> </RelativeLayout> |
AnaEkran.java sınıfımızda yapacağımız işlemleri kod satırlarında gerektiği gibi açıklamaya çalıştım.
AnaEkran.java
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
package com.example.esatgozcu.introsliderkullanimi; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.os.Build; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.text.Html; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class AnaEkran extends AppCompatActivity{ private ViewPager viewPager; private MyViewPagerAdapter myViewPagerAdapter; private LinearLayout dotsLayout; private TextView[] dots; private int[] layouts; private Button btnSkip, btnNext; private PrefManager prefManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ana_ekran); viewPager = (ViewPager) findViewById(R.id.view_pager); dotsLayout = (LinearLayout) findViewById(R.id.layoutDots); btnSkip = (Button) findViewById(R.id.btn_skip); btnNext = (Button) findViewById(R.id.btn_next); // Daha önce çalışıp çalışmadığı kontrol ediliyor prefManager = new PrefManager(this); if (!prefManager.isFirstTimeLaunch()) { // IntroSlider daha önce gösterilmiş ise tekrardan gösterilmiyor launchHomeScreen(); finish(); } // SDK versiyonu 21'den büyükse Notification bar'ı transparan hale getiriyoruz if (Build.VERSION.SDK_INT >= 21) { getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); } //Slider sayfalarını ekliyoruz layouts = new int[]{ R.layout.ekran1, R.layout.ekran2, R.layout.ekran3, R.layout.ekran4}; // Noktaları ekliyoruz addBottomDots(0); changeStatusBarColor(); myViewPagerAdapter = new MyViewPagerAdapter(); viewPager.setAdapter(myViewPagerAdapter); viewPager.addOnPageChangeListener(viewPagerPageChangeListener); //Geç butonuna tıklanırsa.. btnSkip.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { launchHomeScreen(); } }); //İleri butonuna tıklanırsa.. btnNext.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Son sayfa olup olmadığı kontrol ediliyor int current = getItem(+1); if (current < layouts.length) { // Son sayfa değilse mevcut sayfa güncelleniyor viewPager.setCurrentItem(current); } else { // Son sayfa ise else bloğu çalışıyor launchHomeScreen(); } } }); } //Mevcut sayfaya göre noktaların aktif ve pasif renklerini ayarlıyoruz private void addBottomDots(int currentPage) { dots = new TextView[layouts.length]; int[] colorsActive = getResources().getIntArray(R.array.array_dot_active); int[] colorsInactive = getResources().getIntArray(R.array.array_dot_inactive); dotsLayout.removeAllViews(); for (int i = 0; i < dots.length; i++) { dots[i] = new TextView(this); dots[i].setText(Html.fromHtml("•")); dots[i].setTextSize(35); dots[i].setTextColor(colorsInactive[currentPage]); dotsLayout.addView(dots[i]); } if (dots.length > 0) dots[currentPage].setTextColor(colorsActive[currentPage]); } // viewPager'daki mevcut sayfayı döndürüyor private int getItem(int i) { return viewPager.getCurrentItem() + i; } // AnaEkran sayfasına geçiş yapıyoruz private void launchHomeScreen() { prefManager.setFirstTimeLaunch(false); startActivity(new Intent(AnaEkran.this, MainActivity.class)); finish(); } // Viewpager listener değişiklik olduğunda harekete geçecek ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() { @Override public void onPageSelected(int position) { addBottomDots(position); // Eğer son sayfa ise.. if (position == layouts.length - 1) { // İleri butonu başla olarak değişecek btnNext.setText("BAŞLA"); btnSkip.setVisibility(View.GONE); } else { btnNext.setText("İLERİ"); btnSkip.setVisibility(View.VISIBLE); } } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } }; /** SDK versiyonuna göre Notification bar'ı transparan hale getiriyoruz */ private void changeStatusBarColor() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Window window = getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.setStatusBarColor(Color.TRANSPARENT); } } /** View pager adapter */ public class MyViewPagerAdapter extends PagerAdapter { private LayoutInflater layoutInflater; public MyViewPagerAdapter() { } @Override public Object instantiateItem(ViewGroup container, int position) { layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = layoutInflater.inflate(layouts[position], container, false); container.addView(view); return view; } @Override public int getCount() { return layouts.length; } @Override public boolean isViewFromObject(View view, Object obj) { return view == obj; } @Override public void destroyItem(ViewGroup container, int position, Object object) { View view = (View) object; container.removeView(view); } } } |
Böylelikle Android Studio’da nasıl Intro Slider yapabileceğimizi anlamış olduk projeninin kaynak kodunu buraya tıklayarak indirebilirsiniz.