인트로 슬라이드 화면 구성하기

프로그래밍/Android (Java)|2019. 4. 19. 12:56
반응형

[출처] https://www.javatpoint.com/android-introduction-slider-example

소스는 잘 된다. 빌드를 하면 이미지가 두개 없다고 나오는데, 아무거나 가져다 놓고 빌드하면 화면을 볼수 있다.

 

activity_main.xml

 

  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  

  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  

  4.     xmlns:tools="http://schemas.android.com/tools"  

  5.     android:layout_width="match_parent"  

  6.     android:layout_height="match_parent"  

  7.     android:background="@color/bg_mainactivity"  

  8.     tools:context="example.javatpoint.com.introonetimefirsttime.MainActivity">  

  9.   

  10.   

  11.     <TextView  

  12.         android:layout_width="wrap_content"  

  13.         android:layout_height="wrap_content"  

  14.         android:layout_marginEnd="8dp"  

  15.         android:layout_marginStart="8dp"  

  16.         android:layout_marginTop="176dp"  

  17.         android:textSize="18dp"  

  18.         android:text="This is your MainActivity or Home Page"  

  19.         android:textColor="@android:color/white"  

  20.         app:layout_constraintEnd_toEndOf="parent"  

  21.         app:layout_constraintHorizontal_bias="0.503"  

  22.         app:layout_constraintStart_toStartOf="parent"  

  23.         app:layout_constraintTop_toTopOf="parent" />  

  24.   

  25.     <Button  

  26.         android:id="@+id/btn_click"  

  27.         android:layout_width="wrap_content"  

  28.         android:layout_height="wrap_content"  

  29.         android:layout_marginBottom="96dp"  

  30.         android:layout_marginEnd="8dp"  

  31.         android:layout_marginStart="8dp"  

  32.         android:text="Button"  

  33.         android:onClick="btn_Click"  

  34.         app:layout_constraintBottom_toBottomOf="parent"  

  35.         app:layout_constraintEnd_toEndOf="parent"  

  36.         app:layout_constraintHorizontal_bias="0.501"  

  37.         app:layout_constraintStart_toStartOf="parent" />  

  38.   

  39. </android.support.constraint.ConstraintLayout>  


Create an activity_welcome.xml file and add the following code. It is used for the layout of slider.

activity_welcome.xml

 

  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  

  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  

  4.     xmlns:tools="http://schemas.android.com/tools"  

  5.     android:layout_width="match_parent"  

  6.     android:layout_height="match_parent"  

  7.     tools:showIn="@layout/activity_welcome">  

  8.   

  9.   

  10.     <android.support.v4.view.ViewPager  

  11.         android:id="@+id/view_pager"  

  12.         android:layout_width="match_parent"  

  13.         android:layout_height="match_parent" />  

  14.   

  15.     <LinearLayout  

  16.         android:id="@+id/layoutDots"  

  17.         android:layout_width="match_parent"  

  18.         android:layout_height="@dimen/dots_height"  

  19.         android:layout_alignParentBottom="true"  

  20.         android:layout_marginBottom="@dimen/dots_margin_bottom"  

  21.         android:gravity="center"  

  22.         android:orientation="horizontal">  

  23.   

  24.     </LinearLayout>  

  25.   

  26.     <View  

  27.         android:layout_width="match_parent"  

  28.         android:layout_height="1dp"  

  29.         android:alpha=".5"  

  30.         android:layout_above="@id/layoutDots"  

  31.         android:background="@android:color/white" />  

  32.   

  33.     <Button  

  34.         android:id="@+id/btn_next"  

  35.         android:layout_width="wrap_content"  

  36.         android:layout_height="wrap_content"  

  37.         android:layout_alignParentBottom="true"  

  38.         android:layout_alignParentRight="true"  

  39.         android:background="@null"  

  40.         android:text="@string/next"  

  41.         android:textColor="@android:color/white" />  

  42.   

  43.     <Button  

  44.         android:id="@+id/btn_skip"  

  45.         android:layout_width="wrap_content"  

  46.         android:layout_height="wrap_content"  

  47.         android:layout_alignParentBottom="true"  

  48.         android:layout_alignParentLeft="true"  

  49.         android:background="@null"  

  50.         android:text="@string/skip"  

  51.         android:textColor="@android:color/white" />  

  52.   

  53. </RelativeLayout>  


Now create the layout for the welcome sliders as welcome_slide1.xml and welcome_slide2.xml in layout directory.

welcome_slide1.xml

 

  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  

  3.     android:layout_width="match_parent"  

  4.     android:layout_height="match_parent"  

  5.     android:background="@color/bg_screen1">  

  6.   

  7.     <LinearLayout  

  8.         android:layout_width="wrap_content"  

  9.         android:layout_height="wrap_content"  

  10.         android:layout_centerInParent="true"  

  11.         android:gravity="center_horizontal"  

  12.         android:orientation="vertical">  

  13.   

  14.         <ImageView  

  15.             android:layout_width="@dimen/img_width_height"  

  16.             android:layout_height="@dimen/img_width_height"  

  17.             android:src="@drawable/jtp_logo" />  

  18.   

  19.         <TextView  

  20.             android:layout_width="wrap_content"  

  21.             android:layout_height="wrap_content"  

  22.             android:text="@string/slide_1_title"  

  23.             android:textColor="@android:color/white"  

  24.             android:textSize="@dimen/slide_title"  

  25.             android:textStyle="bold" />  

  26.   

  27.         <TextView  

  28.             android:layout_width="wrap_content"  

  29.             android:layout_height="wrap_content"  

  30.             android:layout_marginTop="20dp"  

  31.             android:paddingLeft="@dimen/desc_padding"  

  32.             android:paddingRight="@dimen/desc_padding"  

  33.             android:text="@string/slide_1_desc"  

  34.             android:textAlignment="center"  

  35.             android:textColor="@android:color/white"  

  36.             android:textSize="@dimen/slide_desc" />  

  37.   

  38.     </LinearLayout>  

  39. </RelativeLayout>  


welcome_slide2.xml

 

  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  

  3.     android:layout_width="match_parent"  

  4.     android:layout_height="match_parent"  

  5.     android:background="@color/bg_screen2">  

  6.     <LinearLayout  

  7.         android:layout_width="wrap_content"  

  8.         android:layout_height="wrap_content"  

  9.         android:layout_centerInParent="true"  

  10.         android:gravity="center_horizontal"  

  11.         android:orientation="vertical">  

  12.   

  13.         <ImageView  

  14.             android:layout_width="@dimen/img_width_height"  

  15.             android:layout_height="@dimen/img_width_height"  

  16.             android:src="@drawable/image" />  

  17.   

  18.         <TextView  

  19.             android:layout_width="wrap_content"  

  20.             android:layout_height="wrap_content"  

  21.             android:text="@string/slide_2_title"  

  22.             android:textColor="@android:color/white"  

  23.             android:textSize="@dimen/slide_title"  

  24.             android:textStyle="bold" />  

  25.   

  26.         <TextView  

  27.             android:layout_width="wrap_content"  

  28.             android:layout_height="wrap_content"  

  29.             android:layout_marginTop="20dp"  

  30.             android:paddingLeft="@dimen/desc_padding"  

  31.             android:paddingRight="@dimen/desc_padding"  

  32.             android:text="@string/slide_2_desc"  

  33.             android:textAlignment="center"  

  34.             android:textColor="@android:color/white"  

  35.             android:textSize="@dimen/slide_desc" />  

  36.   

  37.     </LinearLayout>  

  38.   

  39. </RelativeLayout>  


colors.xml

 

  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <resources>  

  3.     <color name="colorPrimary">#3F51B5</color>  

  4.     <color name="colorPrimaryDark">#303F9F</color>  

  5.     <color name="colorAccent">#FF4081</color>  

  6.     <color name="bg_mainactivity">#d4e6e3</color>  

  7.     <!-- Screens background color-->  

  8.     <color name="bg_screen1">#16c266</color>  

  9.     <color name="bg_screen2">#90c2bb</color>  

  10.   

  11.     <!-- dots inactive colors -->  

  12.     <color name="dot_dark_screen1">#39d1ba</color>  

  13.     <color name="dot_dark_screen2">#14a895</color>  

  14.   

  15.     <!-- dots active colors -->  

  16.     <color name="dot_light_screen1">#8de7f9</color>  

  17.     <color name="dot_light_screen2">#8cf9eb</color>  

  18.   

  19.    <array name="array_dot_active">  

  20.         <item>@color/dot_light_screen1</item>  

  21.         <item>@color/dot_light_screen2</item>  

  22.     </array>  

  23.   

  24.     <array name="array_dot_inactive">  

  25.         <item>@color/dot_dark_screen1</item>  

  26.         <item>@color/dot_dark_screen2</item>  

  27.     </array>  

  28. </resources>  


strings.xml

 

  1. <resources>  

  2.     <string name="app_name">IntroOneTimeFirstTime</string>  

  3.   

  4.     <string name="next">NEXT</string>  

  5.     <string name="skip">SKIP</string>  

  6.     <string name="start">GOT IT</string>  

  7.   

  8.     <string name="slide_1_title">Welcome to Javatpoint!</string>  

  9.     <string name="slide_1_desc">Javatpoint is passionate to offer better technical content to the world.</string>  

  10.   

  11.     <string name="slide_2_title">Android</string>  

  12.     <string name="slide_2_desc">Android is a mobile operating system developed by Google.</string>  

  13.   

  14. </resources>  


dimens.xml

 

  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <resources>  

  3.     <!-- Default screen margins, per the Android Design guidelines. -->  

  4.     <dimen name="activity_horizontal_margin">16dp</dimen>  

  5.     <dimen name="activity_vertical_margin">16dp</dimen>  

  6.     <dimen name="fab_margin">16dp</dimen>  

  7.     <dimen name="dots_height">30dp</dimen>  

  8.     <dimen name="dots_margin_bottom">20dp</dimen>  

  9.     <dimen name="img_width_height">120dp</dimen>  

  10.     <dimen name="slide_title">30dp</dimen>  

  11.     <dimen name="slide_desc">16dp</dimen>  

  12.     <dimen name="desc_padding">40dp</dimen>  

  13.   

  14. </resources>  


Create a PrefManager.java class and add the following code. In the class, we use SharedPreferences class that keeps the preference name and a Boolean state true if the app is launched for the first time.

PrefManager.java

 

  1. package example.javatpoint.com.introonetimefirsttime;  

  2.   

  3. import android.content.Context;  

  4. import android.content.SharedPreferences;  

  5.   

  6. public class PrefManager {  

  7.     SharedPreferences pref;  

  8.     SharedPreferences.Editor editor;  

  9.     Context _context;  

  10.     // shared pref mode  

  11.     int PRIVATE_MODE = 0;  

  12.   

  13.     // Shared preferences file name  

  14.     private static final String PREF_NAME = "welcome";  

  15.     private static final String IS_FIRST_TIME_LAUNCH = "IsFirstTimeLaunch";  

  16.   

  17.     public PrefManager(Context context) {  

  18.         this._context = context;  

  19.         pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);  

  20.         editor = pref.edit();  

  21.     }  

  22.   

  23.     public void setFirstTimeLaunch(boolean isFirstTime) {  

  24.         editor.putBoolean(IS_FIRST_TIME_LAUNCH, isFirstTime);  

  25.         editor.commit();  

  26.     }  

  27.   

  28.     public boolean isFirstTimeLaunch() {  

  29.         return pref.getBoolean(IS_FIRST_TIME_LAUNCH, true);  

  30.     }  

  31. }  


In the WelcomeActivity.java class file, add the following code.

In this class, we are performing the following tasks:

  • Checking the first time launch of the application using prefManager.isFirstTimeLaunch() method, if it returns true then the file MainActivity.java will be launched.

  • Adding the slider with Skip and Next buttons.

WelcomeActivity.java

 

  1. package example.javatpoint.com.introonetimefirsttime;  

  2.   

  3. import android.support.v4.view.ViewPager;  

  4. import android.support.v7.app.AppCompatActivity;  

  5. import android.os.Bundle;  

  6. import android.content.Context;  

  7. import android.content.Intent;  

  8. import android.graphics.Color;  

  9. import android.os.Build;  

  10. import android.support.v4.view.PagerAdapter;  

  11. import android.text.Html;  

  12. import android.view.LayoutInflater;  

  13. import android.view.View;  

  14. import android.view.ViewGroup;  

  15. import android.view.Window;  

  16. import android.view.WindowManager;  

  17. import android.widget.Button;  

  18. import android.widget.LinearLayout;  

  19. import android.widget.TextView;  

  20. public class WelcomeActivity extends AppCompatActivity {  

  21.   

  22.     private ViewPager viewPager;  

  23.     private MyViewPagerAdapter myViewPagerAdapter;  

  24.     private LinearLayout dotsLayout;  

  25.     private TextView[] dots;  

  26.     private int[] layouts;  

  27.     private Button btnSkip, btnNext;  

  28.     private PrefManager prefManager;  

  29.   

  30.     @Override  

  31.     protected void onCreate(Bundle savedInstanceState) {  

  32.         super.onCreate(savedInstanceState);  

  33.   

  34.         // Checking for first time launch - before calling setContentView()  

  35.         prefManager = new PrefManager(this);  

  36.         if (!prefManager.isFirstTimeLaunch()) {  

  37.             launchHomeScreen();  

  38.             finish();  

  39.         }  

  40.   

  41.         // Making notification bar transparent  

  42.         if (Build.VERSION.SDK_INT >= 21) {  

  43.             getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);  

  44.         }  

  45.   

  46.         setContentView(R.layout.activity_welcome);  

  47.   

  48.         viewPager = (ViewPager) findViewById(R.id.view_pager);  

  49.         dotsLayout = (LinearLayout) findViewById(R.id.layoutDots);  

  50.         btnSkip = (Button) findViewById(R.id.btn_skip);  

  51.         btnNext = (Button) findViewById(R.id.btn_next);  

  52.   

  53.   

  54.         // layouts of welcome sliders  

  55.         layouts = new int[]{  

  56.                 R.layout.welcome_slide1,  

  57.                 R.layout.welcome_slide2  

  58.         };  

  59.   

  60.         // adding bottom dots  

  61.         addBottomDots(0);  

  62.   

  63.         // making notification bar transparent  

  64.         changeStatusBarColor();  

  65.   

  66.         myViewPagerAdapter = new MyViewPagerAdapter();  

  67.         viewPager.setAdapter(myViewPagerAdapter);  

  68.         viewPager.addOnPageChangeListener(viewPagerPageChangeListener);  

  69.   

  70.         btnSkip.setOnClickListener(new View.OnClickListener() {  

  71.             @Override  

  72.             public void onClick(View v) {  

  73.                 launchHomeScreen();  

  74.             }  

  75.         });  

  76.   

  77.         btnNext.setOnClickListener(new View.OnClickListener() {  

  78.             @Override  

  79.             public void onClick(View v) {  

  80.                 // checking for last page if true launch MainActivity  

  81.                 int current = getItem(+1);  

  82.                 if (current < layouts.length) {  

  83.                     // move to next screen  

  84.                     viewPager.setCurrentItem(current);  

  85.                 } else {  

  86.                     launchHomeScreen();  

  87.                 }  

  88.             }  

  89.         });  

  90.     }  

  91.   

  92.     private void addBottomDots(int currentPage) {  

  93.         dots = new TextView[layouts.length];  

  94.   

  95.         int[] colorsActive = getResources().getIntArray(R.array.array_dot_active);  

  96.         int[] colorsInactive = getResources().getIntArray(R.array.array_dot_inactive);  

  97.   

  98.         dotsLayout.removeAllViews();  

  99.         for (int i = 0; i < dots.length; i++) {  

  100.             dots[i] = new TextView(this);  

  101.             dots[i].setText(Html.fromHtml("?"));  

  102.             dots[i].setTextSize(35);  

  103.             dots[i].setTextColor(colorsInactive[currentPage]);  

  104.             dotsLayout.addView(dots[i]);  

  105.         }  

  106.   

  107.         if (dots.length > 0)  

  108.             dots[currentPage].setTextColor(colorsActive[currentPage]);  

  109.     }  

  110.   

  111.     private int getItem(int i) {  

  112.         return viewPager.getCurrentItem() + i;  

  113.     }  

  114.   

  115.     private void launchHomeScreen() {  

  116.         prefManager.setFirstTimeLaunch(false);  

  117.         startActivity(new Intent(WelcomeActivity.this, MainActivity.class));  

  118.         finish();  

  119.     }  

  120.   

  121.     //  viewpager change listener  

  122.     ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {  

  123.   

  124.         @Override  

  125.         public void onPageSelected(int position) {  

  126.             addBottomDots(position);  

  127.   

  128.             // changing the next button text 'NEXT' / 'GOT IT'  

  129.             if (position == layouts.length - 1) {  

  130.                 // last page. make button text to GOT IT  

  131.                 btnNext.setText(getString(R.string.start));  

  132.                 btnSkip.setVisibility(View.GONE);  

  133.             } else {  

  134.                 // still pages are left  

  135.                 btnNext.setText(getString(R.string.next));  

  136.                 btnSkip.setVisibility(View.VISIBLE);  

  137.             }  

  138.         }  

  139.   

  140.         @Override  

  141.         public void onPageScrolled(int arg0, float arg1, int arg2) {  

  142.   

  143.         }  

  144.   

  145.         @Override  

  146.         public void onPageScrollStateChanged(int arg0) {  

  147.   

  148.         }  

  149.     };  

  150.   

  151.     // Making notification bar transparent  

  152.        

  153.     private void changeStatusBarColor() {  

  154.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {  

  155.             Window window = getWindow();  

  156.             window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);  

  157.             window.setStatusBarColor(Color.TRANSPARENT);  

  158.         }  

  159.     }  

  160.   

  161.     /** 

  162.      * View pager adapter 

  163.      */  

  164.     public class MyViewPagerAdapter extends PagerAdapter {  

  165.         private LayoutInflater layoutInflater;  

  166.   

  167.         public MyViewPagerAdapter() {  

  168.         }  

  169.   

  170.         @Override  

  171.         public Object instantiateItem(ViewGroup container, int position) {  

  172.             layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);  

  173.   

  174.             View view = layoutInflater.inflate(layouts[position], container, false);  

  175.             container.addView(view);  

  176.   

  177.             return view;  

  178.         }  

  179.   

  180.         @Override  

  181.         public int getCount() {  

  182.             return layouts.length;  

  183.         }  

  184.   

  185.         @Override  

  186.         public boolean isViewFromObject(View view, Object obj) {  

  187.             return view == obj;  

  188.         }  

  189.   

  190.   

  191.         @Override  

  192.         public void destroyItem(ViewGroup container, int position, Object object) {  

  193.             View view = (View) object;  

  194.             container.removeView(view);  

  195.         }  

  196.     }  

  197. }  


In the MainActivity.java class, add the following code. This class checks the state returned by the SharedPreferences.

MainActivity.java

 

  1. package example.javatpoint.com.introonetimefirsttime;  

  2.   

  3. import android.content.Intent;  

  4. import android.support.v7.app.AppCompatActivity;  

  5. import android.os.Bundle;  

  6. import android.view.View;  

  7. import android.widget.Toast;  

  8.   

  9. public class MainActivity extends AppCompatActivity {  

  10.   

  11.     @Override  

  12.     protected void onCreate(Bundle savedInstanceState) {  

  13.         super.onCreate(savedInstanceState);  

  14.         setContentView(R.layout.activity_main);  

  15.         PrefManager prefManager = new PrefManager(getApplicationContext());  

  16.         if(prefManager.isFirstTimeLaunch()){  

  17.             prefManager.setFirstTimeLaunch(false);  

  18.             startActivity(new Intent(MainActivity.this, WelcomeActivity.class));  

  19.             finish();  

  20.         }  

  21.     }  

  22.     protected void btn_Click(View view){  

  23.         Toast.makeText(MainActivity.this"clicked on button", Toast.LENGTH_LONG).show();  

  24.     }  

  25. }  


AndroidMenifest.java

 

  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  

  3.     package="example.javatpoint.com.introonetimefirsttime">  

  4.   

  5.     <application  

  6.         android:allowBackup="true"  

  7.         android:icon="@mipmap/ic_launcher"  

  8.         android:label="@string/app_name"  

  9.         android:roundIcon="@mipmap/ic_launcher_round"  

  10.         android:supportsRtl="true"  

  11.         android:theme="@style/AppTheme">  

  12.   

  13.         <activity android:name=".WelcomeActivity">  

  14.             <intent-filter>  

  15.                 <action android:name="android.intent.action.MAIN" />  

  16.   

  17.                 <category android:name="android.intent.category.LAUNCHER" />  

  18.             </intent-filter>  

  19.         </activity>  

  20.         <activity  

  21.             android:name=".MainActivity"/>  

  22.     </application>  

  23.   

  24. </manifest>  

Output:

 

 

 

 

 

[출처] https://www.javatpoint.com/android-introduction-slider-example

반응형

댓글()