0

I'm making an android app and I have a button that won't react when I press it. The button is supposed to bring up an alertdialog box. I'm very confused as I have an OnClickListener and the dialog box is instantiated.

Here is the code from the Java file:

public class tab1Expenses extends Fragment {

    Button btnEx;

    public class button extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.tab1expense);
            btnEx = (Button)findViewById(R.id.btnEx);

            btnEx.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {
                    View view = LayoutInflater.from(button.this).inflate(R.layout.add_ex, null);

                    AlertDialog.Builder add = new AlertDialog.Builder(button.this);
                    add.setView(view);

                    add.setCancelable(true)
                        .setTitle("Enter Expense:")
                        .setMessage("Expense Name:")
                        .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });

                    Dialog dialog = add.create();
                    dialog.show();
                }
            });
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.tab1expense, container, false);
        return rootView;
    }

}

And here is my XML file that is related to the java file:

<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: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="com.example.ojemz.expensetracker.tab1Expenses$button"
android:background="@android:color/darker_gray">

<Button
    android:text="Add Expense"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btnEx"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:width="800dp"
    android:textAppearance="@style/TextAppearance.AppCompat.Widget.Switch"
    android:background="@android:color/black"
    android:textColor="@android:color/holo_green_light" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="17dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <!-- TextView and other stuff -->

    </LinearLayout>
</ScrollView>

Corpse
  • 107
  • 1
  • 11

1 Answers1

1

You're doing it wrong!

The button class extending Activity has no reason to exist here.

If you simply want to get some onClick callbacks just do the following:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab1expense, container, false);
    btnEx = (Button)rootView.findViewById(R.id.btnEx);
    btnEx.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                View view = LayoutInflater.from(button.this).inflate(R.layout.add_ex, null);
                AlertDialog.Builder add = new AlertDialog.Builder(tab1Expenses.this.getActivity());
                add.setView(view);
                add.setCancelable(true)
                    .setTitle("Enter Expense:")
                    .setMessage("Expense Name:")
                    .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    });
                Dialog dialog = add.create();
                dialog.show();
            }
        });
    return rootView;
}

Don't forget to remove your "button" inner class.

Side note

You should rename your tab1Expenses Fragment class to Tab1Expenses in order to distinguish between instances and classes.

Please refer to Java naming conversions.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
  • that seems good except for in the LayoutInflater and Alerdialog declaration I put: ' View view = LayoutInflater.from(tab1Expenses.this).inflate(R.layout.add_ex, null); AlertDialog.Builder add = new AlertDialog.Builder(tab1Expenses.this); ` and it says it cant be applied – Corpse Jan 26 '17 at 02:11
  • Builder (android.content.Context) in Builder cannot be applied to (com.example.ojemz.expensetracker.tab1Expenses) – Corpse Jan 26 '17 at 02:15
  • I replaced tab1Expenses.this with rootview.getContext() and it worked! thank you so much – Corpse Jan 26 '17 at 02:27
  • To get the context you can use this: tab1Expenses.this.getActivity() – Anis LOUNIS aka AnixPasBesoin Jan 26 '17 at 02:28