Android Create Article Stepper
1. Project Structure
2. Gradle
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.article.stepper"
minSdkVersion 19
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'com.balysv:material-ripple:1.0.2'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.mikhaellopez:circularimageview:3.2.0'
}
3. Gradle Properties
android.enableJetifier=true
4. Main Activity
package com.article.stepper;
import android.app.Dialog;
import android.content.Context;
import android.graphics.PorterDuff;
import android.os.Bundle;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.article.stepper.utils.Tools;
public class MainActivity extends AppCompatActivity {
private static final int MAX_STEP = 4;
private ViewPager viewPager;
private MyViewPagerAdapter myViewPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_article_stepper);
// adding bottom dots
bottomProgressDots(0);
initComponent();
Tools.setSystemBarColor(this, R.color.grey_5);
Tools.setSystemBarLight(this);
}
private void bottomProgressDots(int current_index) {
LinearLayout dotsLayout = (LinearLayout) findViewById(R.id.layoutDots);
ImageView[] dots = new ImageView[MAX_STEP];
dotsLayout.removeAllViews();
for (int i = 0; i < dots.length; i++) {
dots[i] = new ImageView(this);
int width_height = 10;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(width_height, width_height));
params.setMargins(10, 10, 10, 10);
dots[i].setLayoutParams(params);
dots[i].setImageResource(R.drawable.shape_circle);
dots[i].setColorFilter(getResources().getColor(R.color.grey_20), PorterDuff.Mode.SRC_IN);
dotsLayout.addView(dots[i]);
}
if (dots.length > 0) {
dots[current_index].setImageResource(R.drawable.shape_circle);
dots[current_index].setColorFilter(getResources().getColor(R.color.grey_80), PorterDuff.Mode.SRC_IN);
}
}
private void initComponent() {
viewPager = (ViewPager) findViewById(R.id.view_pager);
myViewPagerAdapter = new MyViewPagerAdapter();
viewPager.setAdapter(myViewPagerAdapter);
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
bottomProgressDots(position);
}
});
((ImageButton) findViewById(R.id.bt_comments)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showCommentsDialog();
}
});
((ImageButton) findViewById(R.id.bt_back)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
}
private void showCommentsDialog() {
final Dialog dialog = new Dialog(this, R.style.BaseTheme);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // before
Tools.setSystemBarColorDialog(this, dialog, R.color.grey_5);
Tools.setSystemBarLightDialog(dialog);
dialog.setContentView(R.layout.dialog_article_comments);
dialog.setCancelable(true);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
((ImageButton) dialog.findViewById(R.id.bt_close)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
dialog.getWindow().setAttributes(lp);
}
/**
* 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(R.layout.item_article_stepper, container, false);
TextView text = (TextView) view.findViewById(R.id.text);
if (position > 0) {
text.setVisibility(View.VISIBLE);
(view.findViewById(R.id.lyt_article_cover)).setVisibility(View.GONE);
if (position % 2 == 0) {
text.setText(R.string.long_lorem_ipsum_2);
} else {
text.setText(R.string.long_lorem_ipsum);
}
}
container.addView(view);
return view;
}
@Override
public int getCount() {
return MAX_STEP;
}
@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);
}
}
}
5. Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey_5"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/spacing_medium"
android:layout_alignParentBottom="true"
android:background="@drawable/bg_gradient_very_soft" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/grey_3"
android:orientation="horizontal">
<ImageButton
android:id="@+id/bt_back"
android:layout_width="?android:attr/actionBarSize"
android:layout_height="?android:attr/actionBarSize"
android:background="?attr/selectableItemBackgroundBorderless"
android:tint="@color/grey_60"
app:srcCompat="@drawable/ic_arrow_back" />
<LinearLayout
android:id="@+id/layoutDots"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" />
<ImageButton
android:id="@+id/bt_comments"
android:layout_width="?android:attr/actionBarSize"
android:layout_height="?android:attr/actionBarSize"
android:background="?attr/selectableItemBackgroundBorderless"
android:tint="@color/grey_60"
app:srcCompat="@drawable/ic_chat_outline" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
6. Value
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:ns1="urn:oasis:names:tc:xliff:document:1.2">
<style name="Base.TextAppearance.AppCompat.Subhead" parent="android:TextAppearance.Material.Subhead"/>
<style name="TextAppearance.AppCompat.Body1" parent="Base.TextAppearance.AppCompat.Body1"/>
<style name="TextAppearance.AppCompat.Body2" parent="Base.TextAppearance.AppCompat.Body2"/>
<style name="TextAppearance.AppCompat.Small" parent="Base.TextAppearance.AppCompat.Small"/>
<style name="TextAppearance.AppCompat.Medium" parent="Base.TextAppearance.AppCompat.Medium"/>
<style name="TextAppearance.AppCompat.Subhead" parent="Base.TextAppearance.AppCompat.Subhead"/>
<color name="grey_3">#f7f7f7</color>
<color name="grey_5">#f2f2f2</color>
<color name="grey_10">#e6e6e6</color>
<color name="grey_20">#cccccc</color>
<color name="grey_40">#999999</color>
<color name="grey_60">#666666</color>
<color name="grey_80">#37474F</color>
</resources>