0

I am just new in Android. I was trying to implement a button in front view at the end of view pager. The button will stay hidden while sliding the images and then the button will be visible if i am at the end of the viewpager.. How do it correct?

I already tried to get the position of the images and inserted an if statement that the button will be visible but it's not working. Please help me...

public class GuestActivity extends Activity {

ViewPager viewPagerguest;
MyPagerAdapter myPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.guestactivity);
    viewPagerguest = (ViewPager)findViewById(R.id.myviewpager);
    myPagerAdapter = new MyPagerAdapter();
    viewPagerguest.setAdapter(myPagerAdapter);

}

private class MyPagerAdapter extends PagerAdapter {

    int NumberOfPages = 5;

    int[] res = {
            R.drawable.meme1,
            R.drawable.meme2,
            R.drawable.meme4,
            R.drawable.meme3,
            R.drawable.meme5
    };

    int[] backgroundcolor = {
            0xFF101010,
            0xFF202020,
            0xFF303030,
            0xFF404040,
            0xFF505050};

    @Override
    public int getCount() {
        return NumberOfPages;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {


        TextView textView = new TextView(GuestActivity.this);
        textView.setTextColor(Color.WHITE);
        textView.setTextSize(30);
        textView.setTypeface(Typeface.DEFAULT_BOLD);
        textView.setText(String.valueOf(position));
        textView.setGravity(View.TEXT_ALIGNMENT_CENTER);

        ImageView imageView = new ImageView(GuestActivity.this);
        imageView.setImageResource(res[position]);
        ViewGroup.LayoutParams imageParams = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        imageView.setLayoutParams(imageParams);

        RelativeLayout layout = new RelativeLayout(GuestActivity.this);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layout.setBackgroundColor(backgroundcolor[position]);
        layout.setLayoutParams(layoutParams);
        layout.addView(textView);
        layout.addView(imageView);

        final int page = position;
        if (page == 3){
            Button btnjoin = (Button) findViewById(R.id.btnJoin);
            btnjoin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intentjoin = new Intent(GuestActivity.this, LoginActivity.class);
                    startActivity(intentjoin);
                }
            });
        }
        layout.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

            }});

        container.addView(layout);
        return layout;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((RelativeLayout)object);
    }

}

}
user2413972
  • 1,355
  • 2
  • 9
  • 25

1 Answers1

-1
@Override
public int getCount() {
    return NumberOfPages + 2;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    RelativeLayout layout = new RelativeLayout(GuestActivity.this);
    ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    layout.setLayoutParams(layoutParams);
    if (position == 0 || position == NumberOfPages) {
        Button btnjoin = (Button) findViewById(R.id.btnJoin);
            btnjoin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intentjoin = new Intent(GuestActivity.this, LoginActivity.class);
                    startActivity(intentjoin);
                }
            });
        layout.addView(btnjoin);
    } else {
        position++;
        TextView textView = new TextView(GuestActivity.this);
        textView.setTextColor(Color.WHITE);
        textView.setTextSize(30);
        textView.setTypeface(Typeface.DEFAULT_BOLD);
        textView.setText(String.valueOf(position));
        textView.setGravity(View.TEXT_ALIGNMENT_CENTER);

        ImageView imageView = new ImageView(GuestActivity.this);
        imageView.setImageResource(res[position]);
        ViewGroup.LayoutParams imageParams = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        imageView.setLayoutParams(imageParams);
        layout.setBackgroundColor(backgroundcolor[position]);
        layout.addView(textView);
        layout.addView(imageView);

        final int page = position;

        layout.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

            }});
    }



    container.addView(layout);
    return layout;
}

But the best for this use RecycleView

Community
  • 1
  • 1
user2413972
  • 1,355
  • 2
  • 9
  • 25
  • i'm getting an error saying.. ` java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.` at line `layout.addView(btnjoin);` – shan025 Oct 03 '15 at 06:20
  • can anyone please help me? – shan025 Oct 06 '15 at 04:01