1

bellow you can see a script that displays the data from a SQLite database in a popup window. My question is that how can I implement a button inside that window to accomplish some tasks

here is the code:

package com.example.asus.sqlliteproject;

import android.app.AlertDialog;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    DataBaseHelper myDB;
    EditText Name,LastName,Grades;
    Button AddData,ViewData;


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

        myDB = new DataBaseHelper(this);
        Name = (EditText)findViewById(R.id.name);
        LastName = (EditText)findViewById(R.id.lastName);
        Grades = (EditText)findViewById(R.id.Grades);
        AddData = (Button)findViewById(R.id.addDataButton);
        ViewData = (Button)findViewById(R.id.view);
        addData();

    }
    public void addData () {
        AddData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean inserted = myDB.insertData(Name.getText().toString(),
                        LastName.getText().toString(),
                        Grades.getText().toString());
                if (inserted) {
                    Toast.makeText(MainActivity.this, "Text Inserted!", Toast.LENGTH_SHORT).show();
                } else
                    Toast.makeText(MainActivity.this, "Unsuccessful!", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void viewAll () {
        ViewData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Cursor res = myDB.getAllData();
                if (res.getCount() ==0){
                    // show some message
                    showMessage("Error", "Nothing found");
                    return;
                }
                StringBuffer buffer = new StringBuffer();
                while (res.moveToNext()) {
                    buffer.append("ID :"+ res.getString(0)+"\n");
                    buffer.append("Name :"+ res.getString(1)+"\n");
                    buffer.append("LastName :"+ res.getString(2)+"\n");
                    buffer.append("Grade :"+ res.getString(3)+"\n\n");
                }
                // show all data here
                showMessage("Data",buffer.toString());

            }
        });

    }

    public void showMessage (String title, String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.show();
    }

}

and here is a screen shot of the app and what I want to implement:

Click here for image

dadadodo
  • 349
  • 2
  • 15

5 Answers5

0

try this,

builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
               //Your code

            }
        });

        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            //Your code

            }
        });
Pradeep Gupta
  • 1,770
  • 1
  • 9
  • 23
0

http://developer.android.com/guide/topics/ui/dialogs.html

     builder.setPositiveButton("buttontext", new  DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {           
       //do some shit
    }
DenseCrab
  • 1,273
  • 11
  • 22
0

it's simple.

public void showMessageDialog(String title , String Message)
{

    AlertDialog dialog = new AlertDialog.Builder(Main3Activity.this)
            .setTitle(title)
            .setMessage(Message)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    //add your code here...

                }
            })

            .show();

}
Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
0

If you want to imlement your custom button - in left corner with custom properties, you have to use "custom layout" for AlertBox.

Something like this:

View tmLayout = View.inflate(ctx, R.layout.myalert, null);
            TextView header= (TextView) tmLayout.findViewById(R.id.header);
            Button btn= (Button) tmLayout.findViewById(R.id.btn);

            btntzsetcur.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //callback
                }
            });
            header.setText("header);
            btn.setText("btn");

            alerboxTimeZone = new AlertDialog.Builder(ctx).create();
            alerboxTimeZone.setView(tmLayout);

Of course, you have to use XML file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
            <TextView
                    android:id="@+id/header"
                    style="@style/whitetext"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/empty" />

             <LinearLayout
                android:id="@+id/wrap"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" 
                android:orientation="vertical" >

            <Button
                android:id="@+id/btn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/empty"
                android:textSize="20sp" />

            </LinearLayout>


</LinearLayout>
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
0

You can make a custom dialog with a custom view.

Dialog dialog = new Dialog(_context);
dialog.setContentView(YOUR_CUSTOM_VIEW); // put your xml file instead of YOUR_CUSTOM_VIEW

put buttons or whatever you need in your xml file and reach your buttons like this :

Button button = dialog.findViewById(BUTTON_ID); // R.id.YOUR_BUTTON

and then you can set onClickListener and whatever stuff you want for the component

Alikbar
  • 685
  • 6
  • 20