Android Create Options Menu for RecyclerView Item using PopupMenu

custom_row.xml

<?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="wrap_content"
    android:orientation="vertical"
    android:paddingBottom="5dp"
    android:paddingTop="5dp">

    <Button
        android:id="@+id/buttonOptions"
        android:layout_width="10dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:background="@android:color/transparent"
        android:gravity="center"
        android:text="&#8942;"
        android:textAppearance="?android:textAppearanceLarge" />

    <TextView
        android:layout_centerInParent="true"
        android:id="@+id/textViewNumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/buttonOptions"
        android:layout_toStartOf="@+id/buttonOptions"
        android:gravity="center"
        android:text="100"
        android:textAppearance="@android:style/TextAppearance.Large" />

</RelativeLayout>

NumberAdapter.java

public class NumberAdapter extends RecyclerView.Adapter<NumberAdapter.NumberViewHolder> {

    private List<Integer> integerList;
    Context context;

    public NumberAdapter(List<Integer> integerList) {
        this.integerList = integerList;
    }

    @Override
    public NumberViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        View view = inflater.inflate(R.layout.custom_row, parent, false);

        NumberViewHolder viewHolder = new NumberViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final NumberViewHolder holder, int position) {

        Integer num = integerList.get(position);

        holder.textViewNumber.setText(String.valueOf(num));

        final Button button = holder.buttonOptions;

        holder.buttonOptions.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu popup = new PopupMenu(context, button);

                popup.inflate(R.menu.custom_menu);

                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.itemDelete:

                                integerList.remove(holder.getAdapterPosition());
                                notifyItemRemoved(holder.getAdapterPosition());
                                return true;
                        }
                        return false;
                    }
                });

                popup.show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return integerList.size();
    }

    public class NumberViewHolder extends RecyclerView.ViewHolder {

        public TextView textViewNumber;
        public Button buttonOptions;

        public NumberViewHolder(View itemView) {
            super(itemView);

            textViewNumber = (TextView) itemView.findViewById(R.id.textViewNumber);
            buttonOptions = (Button) itemView.findViewById(R.id.buttonOptions);
        }
    }
}

References
https://github.com/mhdr/AndroidSamples/tree/master/088
https://www.simplifiedcoding.net/create-options-menu-recyclerview-item-tutorial/

Inflate multiple types of rows inside a RecyclerView on Android

text_row.xml

<?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="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textViewText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:textAppearance="@android:style/TextAppearance.Large"
        android:text="Text" />

</RelativeLayout>

image_row.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"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageViewImage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        app:srcCompat="@mipmap/iran" />

</RelativeLayout>

ViewHolderText.java

public class ViewHolderText extends RecyclerView.ViewHolder {

    public TextView textViewText;

    public ViewHolderText(View itemView) {
        super(itemView);

        textViewText= (TextView) itemView.findViewById(R.id.textViewText);
    }
}

ViewHolderImage.java

public class ViewHolderImage extends RecyclerView.ViewHolder{

    public ImageView imageViewImage;

    public ViewHolderImage(View itemView) {
        super(itemView);

        imageViewImage= (ImageView) itemView.findViewById(R.id.imageViewImage);
    }
}

ObjectAdapter.java

public class ObjectAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final int Text = 0, Image = 1;
    List<Object> objectList;

    public ObjectAdapter(List<Object> objectList) {
        this.objectList = objectList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        RecyclerView.ViewHolder viewHolder = null;

        if (viewType == Text) {
            View view = inflater.inflate(R.layout.text_row, parent, false);
            viewHolder = new ViewHolderText(view);
        } else if (viewType == Image) {
            View view = inflater.inflate(R.layout.image_row, parent, false);
            viewHolder = new ViewHolderImage(view);
        }

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder.getItemViewType()==Text)
        {
            ViewHolderText viewHolder= (ViewHolderText) holder;
            com.example.mahmood.a087.Text obj= (com.example.mahmood.a087.Text) objectList.get(position);

            viewHolder.textViewText.setText(obj.getText());
        }
        else if (holder.getItemViewType()==Image)
        {
            ViewHolderImage viewHolder= (ViewHolderImage) holder;
            com.example.mahmood.a087.Image obj= (com.example.mahmood.a087.Image) objectList.get(position);

            viewHolder.imageViewImage.setImageResource(obj.getImage());
        }
    }


    @Override
    public int getItemViewType(int position) {
        if (objectList.get(position) instanceof Text) {
            return Text;
        } else if (objectList.get(position) instanceof Image) {
            return Image;
        }


        return -1;
    }

    @Override
    public int getItemCount() {
        return objectList.size();
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {


    List<Object> objectList = new ArrayList<>();
    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager layoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Text text1 = new Text();
        text1.setText("Mahmood");

        Text text2 = new Text();
        text2.setText("Javad");

        Text text3 = new Text();
        text3.setText("Hamed");

        Image image1 = new Image();
        image1.setImage(R.mipmap.iran);

        Image image2 = new Image();
        image2.setImage(R.mipmap.canada);

        objectList.add(text1);
        objectList.add(image1);
        objectList.add(text2);
        objectList.add(text3);
        objectList.add(image2);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        recyclerView.addItemDecoration(itemDecoration);

        recyclerView.setHasFixedSize(true);

        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        adapter = new ObjectAdapter(objectList);
        recyclerView.setAdapter(adapter);
    }
}

References
https://github.com/mhdr/AndroidSamples/tree/master/087
https://guides.codepath.com/android/Heterogenous-Layouts-inside-RecyclerView

Android Animate Items in RecyclerView Using RecyclerView Animators Library

build.gradle

compile 'jp.wasabeef:recyclerview-animators:2.2.6'

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Button buttonAdd;
    Button buttonRemove;
    RecyclerView recyclerView;
    List<Integer> integerList = new ArrayList<>();
    RecyclerView.Adapter adapter;
    RecyclerView.LayoutManager layoutManager;

    Random random = new Random();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonAdd = (Button) findViewById(R.id.buttonAdd);
        buttonRemove = (Button) findViewById(R.id.buttonRemove);

        buttonAdd.setOnClickListener(buttonAdd_OnClickListener);
        buttonRemove.setOnClickListener(buttonRemove_OnClickListener);

        for (int i = 0; i < 1000; i++) {
            integerList.add(random.nextInt());
            i++;
        }

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        recyclerView.addItemDecoration(itemDecoration);

        recyclerView.setHasFixedSize(false);

        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        recyclerView.setItemAnimator(new OvershootInRightAnimator());
        recyclerView.getItemAnimator().setAddDuration(1000);
        recyclerView.getItemAnimator().setRemoveDuration(1000);

        adapter = new NumberAdapter(integerList);

        SlideInLeftAnimationAdapter slideAdapter = new SlideInLeftAnimationAdapter(adapter);
        slideAdapter.setDuration(1000);
        recyclerView.setAdapter(slideAdapter);
    }

    View.OnClickListener buttonAdd_OnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Integer num = random.nextInt();
            integerList.add(0, num);

            //adapter.notifyDataSetChanged();
            adapter.notifyItemInserted(0);
            recyclerView.scrollToPosition(0);
        }
    };

    View.OnClickListener buttonRemove_OnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            integerList.remove(0);
            adapter.notifyItemRemoved(0);
        }
    };
}

References
https://raw.githubusercontent.com/mhdr/AndroidSamples/master/086/images/01.gif
https://github.com/wasabeef/recyclerview-animators

Android Working With RecyclerView and CardView

country_row.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="wrap_content"
    android:paddingBottom="1dp"
    android:paddingEnd="10dp"
    android:paddingLeft="5dp"
    android:paddingRight="10dp"
    android:paddingStart="10dp"
    android:paddingTop="5dp">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="5dp"
        app:cardElevation="3dp"
        app:cardUseCompatPadding="true"
        app:contentPadding="10dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/imageViewCountry"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:contentDescription="image"
                app:srcCompat="@mipmap/iran" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_marginLeft="10dp"
                android:layout_marginStart="10dp"
                android:layout_toEndOf="@+id/imageViewCountry"
                android:layout_toRightOf="@+id/imageViewCountry"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/textViewCountryName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Country"
                    android:textAppearance="@android:style/TextAppearance.Large" />

                <TextView
                    android:id="@+id/textViewCountryCapital"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Capital" />
            </LinearLayout>
        </RelativeLayout>
    </android.support.v7.widget.CardView>


</RelativeLayout>

References
https://github.com/mhdr/AndroidSamples/tree/master/085
https://pupli.net/2017/05/21/android-working-with-recyclerview-and-cardview/
https://developer.android.com/training/material/lists-cards.html
https://code.tutsplus.com/tutorials/getting-started-with-recyclerview-and-cardview-on-android–cms-23465
http://www.androidhive.info/2016/05/android-working-with-card-view-and-recycler-view/
http://stackoverflow.com/questions/27599603/cardview-not-showing-shadow-in-android-l
https://www.youtube.com/watch?v=A2_6mI7drVQ

Android Working with Support GridLayout

build.gradle

compile 'com.android.support:gridlayout-v7:25.3.1'

activity_main.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"
    tools:context="ir.mhdr.a084.MainActivity">


    <android.support.v7.widget.GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Left"
            app:layout_column="0"
            app:layout_columnWeight="1"
            app:layout_row="0" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Center"
            app:layout_column="1"
            app:layout_columnWeight="1"
            app:layout_row="0" />


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Right"
            app:layout_column="2"
            app:layout_gravity="fill_vertical"
            app:layout_row="0"
            app:layout_rowSpan="2"
            app:layout_columnWeight="1"/>


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Bottom"
            app:layout_column="0"
            app:layout_columnSpan="2"
            app:layout_columnWeight="1"
            app:layout_gravity="fill_horizontal"
            app:layout_row="1" />
    </android.support.v7.widget.GridLayout>
</RelativeLayout>

References
https://github.com/mhdr/AndroidSamples/tree/master/084
https://developer.android.com/reference/android/widget/GridLayout.html
https://www.youtube.com/watch?v=4bXOr5Rk1dk
http://stackoverflow.com/questions/10016343/gridlayout-not-gridview-how-to-stretch-all-children-evenly

Android Working with ViewFlipper

activity_main.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"
    tools:context="com.example.mahmood.a083.MainActivity">

    <TableLayout
        android:id="@+id/tableLayoutButtons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:stretchColumns="*">

        <TableRow android:orientation="horizontal">

            <Button
                android:id="@+id/buttonPrevious"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:text="Previous" />

            <Button
                android:id="@+id/buttonNext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:text="Next" />
        </TableRow>
    </TableLayout>

    <ViewFlipper
        android:id="@+id/viewFlipper"
        android:layout_above="@+id/tableLayoutButtons"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Start"
            android:gravity="center"/>

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="center"
            app:srcCompat="@mipmap/cat1" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="center"
            app:srcCompat="@mipmap/cat2" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="center"
            app:srcCompat="@mipmap/cat3" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="End"
            android:gravity="center"/>
    </ViewFlipper>
    
</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    ViewFlipper viewFlipper;
    Button buttonPrevious;
    Button buttonNext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewFlipper= (ViewFlipper) findViewById(R.id.viewFlipper);
        //viewFlipper.setInAnimation();
        //viewFlipper.setOutAnimation();
        //viewFlipper.setFlipInterval(1000);
        //viewFlipper.startFlipping();

        buttonPrevious= (Button) findViewById(R.id.buttonPrevious);
        buttonNext= (Button) findViewById(R.id.buttonNext);

        buttonNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewFlipper.showNext();
            }
        });

        buttonPrevious.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewFlipper.showPrevious();
            }
        });
    }
}

References
https://github.com/mhdr/AndroidSamples/tree/master/083
https://developer.android.com/reference/android/widget/ViewFlipper.html
https://www.youtube.com/watch?v=9xGIlaezMAU
https://www.youtube.com/watch?v=x5G2ejthdKM
http://abhiandroid.com/ui/viewflipper

Android Working with RecyclerView

build.gradle

compile 'com.android.support:recyclerview-v7:25.3.1'

Country.java

public class Country {

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCapital() {
        return capital;
    }

    public void setCapital(String capital) {
        this.capital = capital;
    }

    private String name;
    private String capital;
}

country_row.xml

<?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="wrap_content">

    <TextView
        android:id="@+id/textViewCountryName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginEnd="50dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="50dp"
        android:layout_marginStart="10dp"
        android:gravity="center"
        android:text="Country Name"
        android:textStyle="bold"
        android:textAppearance="@android:style/TextAppearance.Large" />

    <TextView
        android:id="@+id/textViewCountryCapital"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@+id/textViewCountryName"
        android:layout_toRightOf="@+id/textViewCountryName"
        android:gravity="center"
        android:text="Capital Name"
        android:textAppearance="@android:style/TextAppearance.Large" />
</RelativeLayout>

CountryAdapter.java

public class CountryAdapter extends RecyclerView.Adapter<CountryAdapter.CountryViewHolder> {

    private List<Country> countryList;

    public CountryAdapter(List<Country> countries)
    {
        this.countryList=countries;
    }

    @Override
    public CountryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        View view = inflater.inflate(R.layout.country_row, parent, false);

        CountryViewHolder viewHolder=new CountryViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(CountryViewHolder holder, int position) {

        Country country=countryList.get(position);

        holder.textViewCountryName.setText(country.getName());
        holder.textViewCountryCapital.setText(country.getCapital());
    }

    @Override
    public int getItemCount() {
        return this.countryList.size();
    }

    public class CountryViewHolder extends RecyclerView.ViewHolder{

        public TextView textViewCountryName;
        public TextView textViewCountryCapital;

        public CountryViewHolder(View itemView) {
            super(itemView);

            textViewCountryName= (TextView) itemView.findViewById(R.id.textViewCountryName);
            textViewCountryCapital= (TextView) itemView.findViewById(R.id.textViewCountryCapital);
        }
    }
}

main_activity.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"
    tools:context="ir.mhdr.a082.MainActivity">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    List<Country> countryList=new ArrayList<>();
    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager layoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        generateCountries();

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        recyclerView.setHasFixedSize(true);

        // use a linear layout manager
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        // animate items with DefaultItemAnimator
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        // specify an adapter (see also next example)
        adapter = new CountryAdapter(countryList);
        recyclerView.setAdapter(adapter);
    }

    private void generateCountries()
    {
        Country country1=new Country();
        country1.setName("Iran");
        country1.setCapital("Tehran");

        Country country2=new Country();
        country2.setName("Netherlands");
        country2.setCapital("Amsterdam");

        Country country3=new Country();
        country3.setName("Turkey");
        country3.setCapital("Ankara");

        Country country4=new Country();
        country4.setName("Ukraine");
        country4.setCapital("Kiev");


        Country country5=new Country();
        country5.setName("Russia");
        country5.setCapital("Moscow");

        Country country6=new Country();
        country6.setName("Chile");
        country6.setCapital("Santiago");

        Country country7=new Country();
        country7.setName("United States");
        country7.setCapital("Washington, D.C.");

        countryList.add(country1);
        countryList.add(country2);
        countryList.add(country3);
        countryList.add(country4);
        countryList.add(country5);
        countryList.add(country6);
        countryList.add(country7);
    }
}

References
https://github.com/mhdr/AndroidSamples/tree/master/082
https://developer.android.com/training/material/lists-cards.html
https://developer.android.com/guide/topics/ui/layout/recyclerview.html
https://guides.codepath.com/android/using-the-recyclerview
http://www.androidhive.info/2016/01/android-working-with-recycler-view/

Disable swipe in a ViewPager on Android

public class CustomViewPager extends ViewPager {

private boolean enabled;

public CustomViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.enabled = true;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (this.enabled) {
        return super.onTouchEvent(event);
    }

    return false;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    if (this.enabled) {
        return super.onInterceptTouchEvent(event);
    }

    return false;
}

public void setPagingEnabled(boolean enabled) {
    this.enabled = enabled;
}
}
CustomViewPager mViewPager = (CustomViewPager) findViewById(R.id.pager);
mViewPager.setPagingEnabled(false);

References
http://stackoverflow.com/questions/31000076/how-to-make-swipe-disable-in-a-view-pager-in-android

BottomNavigationView is not full width on Android

The BottomNavigationView is not displayed as full width because it is not supposed to.
If you want to fill the space behind the BottomNavigationView, you can set the background color of the view to be the same color as the items background:

<android.support.design.widget.BottomNavigationView
     android:background="@color/bottom_view_color"
     app:itemBackground="@color/bottom_view_color"

     // .... />

References
http://stackoverflow.com/questions/41432902/bottomnavigationview-is-not-full-width/