Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Viewpager in Dialog?

I'm trying to have a dialog where you can click a "next" button to swipe right to the next screen. I am doing that with a ViewPager and adapter:

  final Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.voicedialog);
        dialog.setCanceledOnTouchOutside(false);

        MyPageAdapter adapter = new MyPageAdapter();
        ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
        pager.setAdapter(adapter);

However, I get a NullPointerException saying that pager is null. Why is this happening? Here is the Page Adapter class:

public class MyPageAdapter extends PagerAdapter {
    public Object instantiateItem(ViewGroup collection, int position) {

        int resId = 0;
        switch (position) {
            case 0:
                resId = R.id.voice1;
                break;
            case 1:
                resId = R.id.voice2;
                break;
        }
        return collection.findViewById(resId);
    }

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

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }
}

Here's my layout for the DIALOG:

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

Let me know on how to avoid this situation.

PS: Each of the layouts that should be in the view pager look like this, just diff. text:

<RelativeLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/voice2"
    xmlns:android="http://schemas.android.com/apk/res/android">

        <TextView
            android:text="Slide 1!"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView2"
            android:layout_gravity="center"
            android:textSize="50sp" />

    </RelativeLayout>
like image 362
Ruchir Baronia Avatar asked Jan 04 '23 20:01

Ruchir Baronia


1 Answers

Without Using Enum Class

You should call findViewById on dialog. so for that you have to add dialog before findViewById..

Like this,

ViewPager pager = (ViewPager) dialog.findViewById(R.id.viewpager);

After solving your null pointer exception the other problem's solution here, if you wont use enum class you can use below code...

MainActivity.java

package demo.com.pager;

import android.app.Dialog;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


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

        btn= (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Dialog dialog = new Dialog(MainActivity.this);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.voicedialog);
                dialog.setCanceledOnTouchOutside(false);

                MyPageAdapter adapter = new MyPageAdapter(MainActivity.this);
                ViewPager pager = (ViewPager) dialog.findViewById(R.id.viewpager);
                pager.setAdapter(adapter);
                dialog.show();
            }
        });


    }
}

activity_main.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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="demo.com.pager.MainActivity">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

MyPageAdapter.java

package demo.com.pager;

import android.app.FragmentManager;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by rucha on 26/12/16.
 */

public class MyPageAdapter extends PagerAdapter {

    Context mContext;
    int resId = 0;

    public MyPageAdapter(Context context) {
        mContext = context;
    }


    public Object instantiateItem(ViewGroup collection, int position) {

      /*  int resId = 0;
        switch (position) {
            case 0:
                resId = R.id.voice1;
                break;
            case 1:
                resId = R.id.voice2;
                break;
        }

        return collection.findViewById(resId);*/

        LayoutInflater inflater = LayoutInflater.from(mContext);
        switch (position) {
            case 0:
                resId = R.layout.fragment_blue;
                break;
            case 1:
                resId = R.layout.fragment_pink;
                break;
        }
        ViewGroup layout = (ViewGroup) inflater.inflate(resId, collection, false);
        collection.addView(layout);
        return layout;
    }

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

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }
}

FragmentBlue.java

package demo.com.pager;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import android.support.v4.app.Fragment;

public class FragmentBlue extends Fragment  {

    private static final String TAG = FragmentBlue.class.getSimpleName();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_blue, container, false);
        return view;
    }


}

fragment_blue.xml

<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="match_parent"
    android:background="#4ECDC4">

</RelativeLayout>

voicedialog.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

Please check and reply.

like image 54
Rucha Bhatt Joshi Avatar answered Jan 07 '23 19:01

Rucha Bhatt Joshi