adding dots indicators to View-pager - android

How to add dots indicators to my project.
I tried many tutorials and solutions, but all solutions required to add fragments to my project which i don't want.
SlideAdapter.java
public class SlideAdapter extends PagerAdapter {
Context context;
LayoutInflater inflater;
public int[] lst_images = {
R.drawable.iconslide1,
R.drawable.iconslide2,
R.drawable.iconslide3,
R.drawable.iconslide2
};
public String[] lst_title = {
"COSMONAUT",
"SATELITE",
"GALAXY",
"ROCKET"
} ;
public String[] lst_description = {
"a",
"b",
"c",
"d"
};
public int[] lst_backgroundcolor = {
Color.rgb(55,55,55),
Color.rgb(239,85,85),
Color.rgb(110,49,89),
Color.rgb(1,188,212)
};
public SlideAdapter(Context context) {
this.context = context;
}
#Override
public int getCount() {
return lst_title.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return (view==(LinearLayout)object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.slide , container , false);
LinearLayout layoutslide = (LinearLayout) v.findViewById(R.id.slidelinearlayout);
ImageView imgslide = (ImageView) v.findViewById(R.id.slideimg);
TextView txttitle= (TextView) v.findViewById(R.id.txttitle);
TextView description = (TextView) v.findViewById(R.id.txtdescription);
layoutslide.setBackgroundColor(lst_backgroundcolor[position]);
imgslide.setImageResource(lst_images[position]);
txttitle.setText(lst_title[position]);
description.setText(lst_description[position]);
container.addView(v);
return v;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}}
and there what i did in my main_Slider java class
main_Slider.java
private ViewPager viewPager;
private SlideAdapter myadapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slide_main);
viewPager = (ViewPager) findViewById(R.id.viewpager);
myadapter = new SlideAdapter(this);
viewPager.setAdapter(myadapter);}
so i need modify my codes to add dots indicators.

Related

How to remove/disable pages in viewpager adapter?

I have struggled a lot with my ViewPagerAdapter for remove/disable pages. But nothing happens. Is there any ways to remove pages from a ViewPagerAdapter? Here is my code-
As you can see, I have three positions there. I just want to remove/disable position 1(paragraph 1).
public class ViewPagerAdapter2 extends PagerAdapter
{
private Context context;
private LayoutInflater layoutInflater;
String strings[] = {
"paragraph 0",
"paragraph 1",
"paragraph 2"
};
public ViewPagerAdapter2 (Context context) {
this.context = context;
}
#Override
public int getCount()
{
// TODO: Implement this method
return strings.length;
}
#Override
public boolean isViewFromObject(View view, Object object)
{
// TODO: Implement this method
return view == object;
}
#Override
public Object instantiateItem(ViewGroup container, int position)
{
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.view_pager_lyrics,null);
final TextView textView = view.findViewById(R.id.view_pager_lyricsTextView);
textView.setText(Html.fromHtml("<!DOCTYPE html><html><head></head><body><div style='text-align: center'><p>"+strings[position]+"</p></div></body></html>"));
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object)
{
ViewPager vp = (ViewPager) container;
View view = (View) object;
vp.removeView(view);
}
}

How to close viewAdapter with button on last image?

what I am trying to do is close the image adapter at the click of a button placed in the third view.
I am setting the views following this tutorial and it works fine: https://www.journaldev.com/10096/android-viewpager-example-tutorial
Now what I am trying to do is close the adapter with a botton in the last view. I get an error that says that points the button: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
I think this is because the button is not on the main layout but on the view layout. so how can I acces it correctly to hide/close the adapter
this is the main
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView3 = findViewById(R.id.textView3);
textView3.setVisibility(View.INVISIBLE);
btnClose=findViewById(R.id.btnClose);
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new CustomPagerAdapter(this));
viewPager.setPageTransformer(true, new ZoomOutPageTransformer());
btnClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
viewPager.setVisibility(View.INVISIBLE);
textView3.setVisibility(View.VISIBLE);
}
});
}
This is the adapter
public class CustomPagerAdapter extends PagerAdapter {
private Context mContext;
public CustomPagerAdapter(Context context) {
mContext = context;
}
#Override
public Object instantiateItem(ViewGroup collection, int position) {
ModelObject modelObject = ModelObject.values()[position];
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), collection, false);
collection.addView(layout);
return layout;
}
#Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
#Override
public int getCount() {
return ModelObject.values().length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public CharSequence getPageTitle(int position) {
ModelObject customPagerEnum = ModelObject.values()[position];
return mContext.getString(customPagerEnum.getTitleResId());
}
}
This is the model object class
public enum ModelObject {
RED(R.string.red, R.layout.view_red),
BLUE(R.string.blue, R.layout.view_blue),
GREEN(R.string.green, R.layout.view_green);
private int mTitleResId;
private int mLayoutResId;
ModelObject(int titleResId, int layoutResId) {
mTitleResId = titleResId;
mLayoutResId = layoutResId;
}
public int getTitleResId() {
return mTitleResId;
}
public int getLayoutResId() {
return mLayoutResId;
}
}
Just to know how it works I want to hide the adapter and show a textview. Again I think the problem is the button being on another layout, so how can I call it?
Implement your listener in main class & pass it to viewpager. Attach the listener on demand, only the review view.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView3 = findViewById(R.id.textView3);
textView3.setVisibility(View.INVISIBLE);
viewPager = (ViewPager) findViewById(R.id.viewpager);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View arg0) {
viewPager.setVisibility(View.INVISIBLE);
textView3.setVisibility(View.VISIBLE);
}
}
viewPager.setAdapter(new CustomPagerAdapter(this), listener);
viewPager.setPageTransformer(true, new ZoomOutPageTransformer());
}
public class CustomPagerAdapter extends PagerAdapter {
private Context mContext;
private View.OnClickListener mListener;
public CustomPagerAdapter(Context context, View.OnClickListener listener) {
mContext = context;
mListener = listener;
}
#Override
public Object instantiateItem(ViewGroup collection, int position) {
ModelObject modelObject = ModelObject.values()[position];
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), collection, false);
collection.addView(layout);
if (position == 2) {
View btnClose = layout.findViewById(R.id.btnClose);
btnClose.setOnClickListener(mListener);
}
return layout;
}
#Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
#Override
public int getCount() {
return ModelObject.values().length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public CharSequence getPageTitle(int position) {
ModelObject customPagerEnum = ModelObject.values()[position];
return mContext.getString(customPagerEnum.getTitleResId());
}
}

Auto slider images using ViewPager

I'm trying to replace colors by images from Drawable in this code.
I found this tutorial on how we can make an auto images slider by using viewpager on Android Studio, but this code is to switch colors images how can I replace this colors by images?
Here is my SliderAdapter.java
public class SliderAdapter extends PagerAdapter {
private Context context;
private List<Integer> color;
private List<String> colorName;
public SliderAdapter(Context context, List<Integer> color, List<String> colorName) {
this.context = context;
this.color = color;
this.colorName = colorName;
}
#Override
public int getCount() {
return color.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item_slider, null);
TextView textView = (TextView) view.findViewById(R.id.textView);
LinearLayout linearLayout = (LinearLayout)
view.findViewById(R.id.linearLayout);
textView.setText(colorName.get(position));
linearLayout.setBackgroundColor(color.get(position));
ViewPager viewPager = (ViewPager) container;
viewPager.addView(view, 0);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
ViewPager viewPager = (ViewPager) container;
View view = (View) object;
viewPager.removeView(view);
}
}
here is my MainActivity
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
TabLayout indicator;
List<Integer> color;
List<String> colorName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
viewPager=(ViewPager)findViewById(R.layout.viewPager);
indicator=(TabLayout)findViewById(R.layout.indicator);
color = new ArrayList<>();
color.add(Color.RED);
color.add(Color.GREEN);
color.add(Color.BLUE);
colorName = new ArrayList<>();
colorName.add("RED");
colorName.add("GREEN");
colorName.add("BLUE");
viewPager.setAdapter(new SliderAdapterDemo(this, color, colorName));
indicator.setupWithViewPager(viewPager, true);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new SliderTimer(), 4000, 6000);
}
private class SliderTimer extends TimerTask {
#Override
public void run() {
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
if (viewPager.getCurrentItem() < color.size() - 1) {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
} else {
viewPager.setCurrentItem(0);
}
}
});
}
}
}

Refresh list inside fragments, inside a ViewTab, pressing a button tha'ts in the adapter

As you can see from the title I'm a little confused on how to resolve my problem. I have an activity that use Tabs to switch between two fragments that contain one list eache one. The list have the same adapter, and in every list there's a button that refresh the two lists.
Here's my MainActivity.class:
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
public static TreeSet<Task> tasksTODO = new TreeSet<>();
public static TreeSet<Task> tasksDONE = new TreeSet<>();
private TabLayout tabs;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = findViewById(R.id.container);
tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(mViewPager);
tabs.setSelectedTabIndicatorColor(Color.parseColor("#FFFFFF"));
addTasks();
}
#Override
protected void onResume() {
super.onResume();
addTasks();
}
private void setupViewPager(ViewPager vp) {
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mSectionsPagerAdapter.addFragment(new ShowTodoTasks(), "todo");
mSectionsPagerAdapter.addFragment(new ShowDoneTasks(), "done");
vp.setAdapter(mSectionsPagerAdapter);
}
public void addTasks() {
tasksDONE.add(new Task("Number 1", "pepin", "", "Description 1", 1));
tasksDONE.add(new Task("Number 2", "pepin", "", "Description 2", 1));
tasksDONE.add(new Task("Number 3", "pepin", "", "Description 3", 1));
tasksTODO.add(new Task("Number 4", "pepin", "", "Description 4", 1));
setupViewPager(mViewPager);
}
}
Here's my TaskAdapter:
public class TaskAdapter extends BaseAdapter {
protected Context context;
protected ArrayList<Task> items;
private ImageView edit;
private CheckBox checked;
Task dir;
private TextView title, comment, description, date;
public TaskAdapter(Context context, ArrayList<Task> items) {
this.context = context;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(final int position, View view, ViewGroup viewGroup) {
LayoutInflater inf = (LayoutInflater) viewGroup.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inf.inflate(R.layout.task_item_template, null);
edit = (ImageView) view.findViewById(R.id.item_edit);
checked = (CheckBox) view.findViewById(R.id.item_done);
final View finalView = view;
dir = items.get(position);
int priority = dir.getPriority();
if (priority == 1) {
view.setBackgroundResource(R.drawable.major_prior_task_border);
}
if (priority == 2) {
view.setBackgroundResource(R.drawable.medium_prior_task_border);
}
if (priority == 3) {
view.setBackgroundResource(R.drawable.minor_prior_task_border);
}
title = view.findViewById(R.id.item_title);
title.setText(dir.getTitle());
comment = view.findViewById(R.id.tv_comment);
comment.setText(dir.getComment());
description = view.findViewById(R.id.item_description);
description.setText(dir.getDescription());
date = view.findViewById(R.id.item_date);
date.setText(dir.getCreationDate());
return view;
}
}
And here are my Fragments:
public class ShowDoneTasks extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_show_done_tasks, container, false);
setup(MainActivity.tasksDONE,v);
return v;
}
#Override
public void onStart() {
super.onStart();
}
private void setup(TreeSet<Task> tasks, View v) {
ListView lv = v.findViewById(R.id.lv_tasks_done);
ArrayList<Task> tareasDone = new ArrayList<>(tasks);
TaskAdapter adapter = new TaskAdapter(v.getContext(), tareasDone);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
And the other one:
public class ShowTodoTasks extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_show_todo_tasks, container, false);
setup(MainActivity.tasksTODO,v);
return v;
}
private void setup(TreeSet<Task> tasks, View v) {
ListView lv = v.findViewById(R.id.lv_tasks_todo);
ArrayList<Task> tareasTodo = new ArrayList<>(tasks);
TaskAdapter adapter = new TaskAdapter(v.getContext(), tareasTodo);
adapter.notifyDataSetChanged();
lv.setAdapter(adapter);
}
}
What I want is this: when you click on the checkBox that's in TaskAdapter i want to refresh the list. I tried using adapter.notifyDataSetChanged() in the fragments but it dosn't seem to work.
Any ideas?

OnPageChangeListener in ViewPager adapter?

I have a regular ViewPager with an adapter:
public class MyDialogFragment extends DialogFragment {
private ViewPager viewPager;
private MyViewPagerAdapter myViewPagerAdapter;
private City city;
public static MyDialogFragment newInstance() {
MyDialogFragment fragment = new MyDialogFragment();
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_layout, container, false);
viewPager = (ViewPager) v.findViewById(R.id.viewPager);
city = Parcels.unwrap(getArguments().getParcelable("city"));
myViewPagerAdapter = new MyViewPagerAdapter(getContext());
viewPager.setAdapter(myViewPagerAdapter);
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// Page changed
}
});
return v;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public class MyViewPagerAdapter extends PagerAdapter {
private Context context;
private LayoutInflater layoutInflater;
public MyViewPagerAdapter(Context context) {
this.context = context;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.city_layout, container, false);
ImageView imageView = (ImageView) view.findViewById(R.id.cityImage);
Glide.with(context)
.load(city.getImage())
.into(imageView);
}
container.addView(view);
return view;
}
#Override
public int getCount() {
return city.size();
}
#Override
public boolean isViewFromObject(View view, Object obj) {
return view == ((View) obj);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
}
As you can see, I have a page change listener set on my ViewPager. But I need to be able to notify the adapter when the page has changed so that I can modify the adapter's ImageView. How can I do this?
Add a method to your adapter class
public class MyViewPagerAdapter extends PagerAdapter {
...
public void pageChanged(int position) {
// Do something
}
...
}
After that in your OnPageChangeListener use that method
#Override
public void onPageSelected(int position) {
myViewPagerAdapter.pageChanged(position);
}
One approach is to create a data structure in your adapter to map the pager view to its position. A SparseArray is most efficient:
private SparseArray<View> pageMap = new SparseArray<>();
Then in the adapter's instantiateItem() method, add the view to the SparseArray:
public Object instantiateItem(ViewGroup container, int position) {
...
pageMap.put(position, view);
...
}
Now in your OnPageChangeListener you can do the following:
#Override
public void onPageSelected(int position) {
View view = adapter.getView(pos);
if (view != null) {
ImageView iv = (ImageView) view.findViewById(R.id.your_image_view);
// You now have the image view
}
}
Make sure you remove views from the SparseArray in the adapter's destroyItem() method:
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
pageMap.remove(position);
...
}
You can use the constructor with imagelist to pass and that automatically called on next page..In your code will be modified like :::
ArrayList imageList=new ArrayList<>();
imageList.add("ImagePath here");
myViewPagerAdapter = new MyViewPagerAdapter(getContext(),imageList);
viewPager.setAdapter(myViewPagerAdapter);
Then your adapter will be like ::
public class MyViewPagerAdapter extends PagerAdapter {
private Context context;
private ArrayList<String> imageList
private LayoutInflater layoutInflater;
public MyViewPagerAdapter(Context context,ArrayList<String> imageList) {
this.context = context;
this.imageList = imageList;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.city_layout, container, false);
ImageView imageView = (ImageView) view.findViewById(R.id.cityImage);
Glide.with(context)
.load(imageList.get(position)) // This will be changes like
.into(imageView);
}
container.addView(view);
return view;
}
#Override
public int getCount() {
return city.size();
}
#Override
public boolean isViewFromObject(View view, Object obj) {
return view == ((View) obj);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
That done ..
After this, there are some suggestion that
- please use arraylist while using the viewpager ..
Now you just taking the parceble object from bundle..
you can do the same for the arraylist,
just receive the arraylist from the same bundle by passing the whole arraylist at once.
so you dont need to find the page change event..viewpager will automatically handle that..
Here's How to send & receiving the parceable arraylist
// while sending the list
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("citylist", new ArrayList<City>());
// while receiving the list
ArrayList<City> citylist = getIntent().getParcelableArrayListExtra("citylist");
After this just get the all image and add in the imagelist array
for (int i = 0; i < citylist.size(); i++) {
imageList.add(citylist.get(i).getImage());
}
And pass this imageList in
myViewPagerAdapter = new MyViewPagerAdapter(getContext(),imageList);
viewPager.setAdapter(myViewPagerAdapter);
Tell me if needed more assitance.

Categories

Resources