-1

I have a layout with a TextView labelled as Camera. On the click of that TextView I have to show camera. The layout is set to a dialog. The following is the code i have done

public class SelectCameraOrGalleryAlertDialog extends Dialog {

    public SelectCameraOrGalleryAlertDialog(Context context) {
        super(context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.alerttoselectcameraorgallery);

        final View txtCamerabutton = (View) findViewById(R.id.txtcamerabutton);

        txtCamerabutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            startActivity(intent);
        }
    });

    }

}

Here error shows as The method startActivity(Intent) is undefined for the type new View.OnClickListener(){}

This is the layout

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="70dp"
        android:gravity="center"
        android:background="#e29567">

        <TextView
            android:layout_width="126dp"
            android:layout_height="fill_parent"
            android:drawableLeft="@drawable/addressicon"
            android:drawablePadding="10dp"
            android:gravity="center_vertical"
            android:text="Add Photo"
            android:textSize="19sp" 
            android:textColor="#3a3a3a"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="90dp"
        android:layout_gravity="center"
        android:orientation="horizontal"
        android:background="#3a3a3a" >

        <TextView
            android:id="@+id/txtcamerabutton"
            android:layout_width="94dp"
            android:layout_height="fill_parent"
            android:paddingTop="5dp"
            android:drawableTop="@drawable/editicon"
            android:gravity="center"
            android:scaleX=".5"
            android:scaleY=".5"
            android:text="Camera" />

        <TextView
            android:id="@+id/txtgallerybutton"
            android:layout_width="94dp"
            android:layout_height="fill_parent"
            android:paddingTop="5dp"
            android:drawableTop="@drawable/replaceicon"
            android:gravity="center"
            android:scaleX=".5"
            android:scaleY=".5"
            android:text="Gallery" />

    </LinearLayout>


</LinearLayout>

How to solve this?

2 Answers2

1

Try below Code

public class SelectCameraOrGalleryAlertDialog extends Dialog {
    private Context mCtx;
            public SelectCameraOrGalleryAlertDialog(Context context) {
                super(context);
                mCtx = context;
            }

            @Override
            public void onCreate(Bundle savedInstanceState) {
                requestWindowFeature(Window.FEATURE_NO_TITLE);
                setContentView(R.layout.alerttoselectcameraorgallery);

                final View txtCamerabutton = (View) findViewById(R.id.txtcamerabutton);

                txtCamerabutton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                    mCtx. startActivity(intent);
                }
            });

            }
Anjali
  • 1
  • 1
  • 13
  • 20
0

Inside Button's onClick instead of startActivity(intent) use startActivityForResult(intent, 0),

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0); 

And add Camers Uses Permission in your manifest file.

<uses-permission android:name="android.permission.CAMERA"></uses-permission>

For more information check this link.

Community
  • 1
  • 1
Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
  • I have done this. But error showing as The method startActivityForResult(Intent, int) is undefined for the type new View.OnClickListener(){}. I think this error is showing because i extends this SelectCameraOrGalleryAlertDialog from dialog. how to solve this? Do i have to add anything on the constructor? – jafeena_thaha Jan 01 '15 at 12:39