activity_main. xml:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/dialog_page_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/> </LinearLayout>
PageIndicatorFragment:
package com.example.dialogwithpageindicator;
import android.os.Bundle;
import androidx.annotation.NonNull; import
androidx.annotation.Nullable; import
androidx.core.content.ContextCompat;
import androidx.fragment.app.DialogFragment; import
androidx.fragment.app.Fragment; import
androidx.viewpager2.widget.ViewPager2;
import android.view.LayoutInflater; import android.view.View; import
android.view.ViewGroup; import android.widget.AutoCompleteTextView;
import android.widget.ImageView; import android.widget.TextView;
public class PageIndicatorFragment extends DialogFragment {
private static final String ARG_SHOW_AS_DIALOG = "PageIndicatorFragment";
private AutoCompleteTextView txt;
private ImageView imageView;
ViewPager2 viewPager;
public static PageIndicatorFragment newInstance(boolean showAsDialog){
PageIndicatorFragment pageIndicatorFragment=new PageIndicatorFragment();
Bundle args=new Bundle();
args.putBoolean(ARG_SHOW_AS_DIALOG,showAsDialog);
pageIndicatorFragment.setArguments(args);
return pageIndicatorFragment;
}
public static PageIndicatorFragment newInstance(){
return newInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_page_indicator, container,false);
return v;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
txt=(AutoCompleteTextView) view.findViewById(R.id.texttitle);
imageView=(ImageView)view.findViewById(R.id.img);
viewPager=(ViewPager2)view.findViewById(R.id.pager);
ViewPagerLatest viewPagerLatest=new ViewPagerLatest(getActivity());
viewPager.setAdapter(viewPagerLatest);
viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
if(position==0){
txt.setText("1st pager");
ContextCompat.getDrawable(getContext(),R.drawable.black);
}
if(position==1){
txt.setText("2 nd pager");
ContextCompat.getDrawable(getContext(),R.drawable.white);
}
if(position==2){
ContextCompat.getDrawable(getContext(),R.drawable.red);
}
}
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
}
@Override
public void onPageScrollStateChanged(int state) {
super.onPageScrollStateChanged(state);
}
});
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args=getArguments();
if(args!=null){
setShowsDialog(args.getBoolean(ARG_SHOW_AS_DIALOG, true));
}
}
}
fragment_page_indicator. xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="600dp"
tools:context=".PageIndicatorFragment">
<RelativeLayout
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="100dp">
<AutoCompleteTextView
android:id="@+id/texttitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_marginTop="20dp"
android:layout_below="@+id/title">
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp" />
</RelativeLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
Viewpager2:
package com.example.dialogwithpageindicator;
import androidx.annotation.NonNull; import
androidx.fragment.app.Fragment; import
androidx.fragment.app.FragmentActivity; import
androidx.viewpager2.adapter.FragmentStateAdapter;
public class ViewPagerLatest extends FragmentStateAdapter {
public static final int DIALOG_SIZE=3;
public ViewPagerLatest(@NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
@NonNull
@Override
public Fragment createFragment(int position) {
return new PageIndicatorFragment();
}
@Override
public int getItemCount() {
return DIALOG_SIZE;
}
}
MainActivity:
package com.example.dialogwithpageindicator;
import androidx.appcompat.app.AppCompatActivity; import
androidx.fragment.app.DialogFragment; import
androidx.fragment.app.FragmentManager; import
androidx.recyclerview.widget.RecyclerView; import
androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showDialog();
}
private void showDialog() {
FragmentManager fragmentManager=getSupportFragmentManager();
PageIndicatorFragment pageIndicatorFragment=PageIndicatorFragment.newInstance(false);
pageIndicatorFragment.show(fragmentManager,"viewpager2");
//fragmentManager.beginTransaction().add(R.id.dialog_page_indicator,pageIndicatorFragment).commit();
}
}
activity_main. xml:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/dialog_page_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>