Introduction Slider
Please add below dependency to app level build.gradle file
compile 'me.relex:circleindicator:1.2.2@aar'
Create new activity named - IntroductionActivity.java
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import java.util.ArrayList;
import me.relex.circleindicator.CircleIndicator;
public class IntroductionActivity extends AppCompatActivity implements View.OnClickListener {
private static ViewPager mPager;
private static final Integer[] LayoutList = {R.layout.slider_layout1, R.layout.slider_layout2, R.layout.slider_layout3,
R.layout.slider_layout4,R.layout.slider_layout5, R.layout.slider_layout6};
private ArrayList<Integer> LayoutArray = new ArrayList<Integer>();
private TextView btnSkip, btnDone;
boolean isFromSettings = false;
Typeface FontType;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_introduction);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
init();
}
private void init() {
FontType = CustomUtils.getFontTypeForTitle(IntroductionActivity.this);
btnSkip =(TextView) findViewById(R.id.btnSkip);
btnSkip.setTypeface(FontType);
btnDone =(TextView) findViewById(R.id.btnDone);
btnDone.setTypeface(FontType);
btnSkip.setOnClickListener(this);
btnDone.setOnClickListener(this);
for (int i = 0; i < LayoutList.length; i++)
LayoutArray.add(LayoutList[i]);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(new MySliderAdapter(IntroductionActivity.this,LayoutArray));
CircleIndicator indicator = (CircleIndicator) findViewById(R.id.indicator);
indicator.setViewPager(mPager);
// Auto start of viewpager
/*final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == XMEN.length) {
currentPage = 0;
}
mPager.setCurrentItem(currentPage++, true);
}
};
Timer swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(Update);
}
}, 2500, 2500);*/
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
@Override
public void onClick(View view) {
Intent intent;
switch (view.getId()) {
case R.id.btnSkip:
Handle your operation here
break;
case R.id.btnDone:
Handle your operation here
break;
}
}
}
------------------------------------------
Create new java file named - MySliderAdapter.java
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class MySliderAdapter extends PagerAdapter {
private ArrayList<Integer> layout;
private LayoutInflater inflater;
private Context context;
public MySliderAdapter(Context context,ArrayList<Integer> layout) {
this.context = context;
this.layout = layout;
inflater = LayoutInflater.from(context);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return layout.size();
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
View myImageLayout = inflater.inflate(layout.get(position), view, false);
ImageView myImage = (ImageView) myImageLayout.findViewById(R.id.image);
view.addView(myImageLayout,0);
return myImageLayout;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
}
------------------------------------------
Create layout file for above activity
activity_introduction.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@color/colorPrimary"
tools:context="com.introduction.IntroductionActivity">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true" />
<me.relex.circleindicator.CircleIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_alignParentBottom="true" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<TextView
android:id="@+id/btnSkip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/Margin20dp"
android:textSize="18sp"
android:text="@string/skip" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<TextView
android:id="@+id/btnDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/Margin20dp"
android:textSize="18sp"
android:text="@string/done_label" />
</LinearLayout>
</RelativeLayout>
----------------------------------------
Create layout file for each slider page in layout dir, i am creating only one.
slider_layout1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/colorPrimary">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/topicon" />
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/comfortaa_bold_font"
android:paddingTop="30dp"
android:text="Welcome to Chambu"
android:textColor="@color/colorWhite"
android:textSize="18sp" />
<TextView
android:id="@+id/tvTitle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/comfortaa"
android:paddingTop="5dp"
android:text="Your unique id is"
android:textColor="@color/colorWhite"
android:textSize="16sp" />
</LinearLayout>
</RelativeLayout>
Comments
Post a Comment