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
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
}
});
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
}
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();
}
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>
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
Related
I've created a dialog using a custom DialogFragment and interface as well as a custom layout that passes the click events back to the dialog host (MainActivity). However my problem is that when I attempt to get the text entered in the EditText in the dialog, it comes back as an empty string. Any ideas? I am able to get the text from the TextView's within the dialog, just not the EditText.
add_class_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="#+id/viewClassName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Class: "
android:textSize="25sp"
android:textColor="#color/textColor"/>
<EditText
android:id="#+id/editClassName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter class"
android:textSize="25sp"
android:textColor="#color/textColor"
android:layout_gravity="center"/>
</LinearLayout>
</LinearLayout>
MainActivity.java:
public class MainActivity extends FragmentActivity implements AddClassDialogFragment.AddClassDialogListener {
TextView nameView;
EditText editName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View vi = getLayoutInflater().inflate(R.layout.add_class_dialog, null);
nameView = vi.findViewById(R.id.viewClassName);
editName = vi.findViewById(R.id.editClassCredits);
}
public void addClass(View view) {
AddClassDialogFragment acdf = new AddClassDialogFragment();
acdf.show(getFragmentManager(), "ac");
}
#Override
public void onDialogPositiveClick(DialogFragment dialog) {
Toast.makeText(this, nameView.getText().toString(), Toast.LENGTH_SHORT).show();
Toast.makeText(this, editName.getText().toString(), Toast.LENGTH_SHORT).show();
}
#Override
public void onDialogNegativeClick(DialogFragment dialog) {
Toast.makeText(this, "Cancel", Toast.LENGTH_SHORT).show();
}
}
AddClassDialogFragment.java:
public class AddClassDialogFragment extends DialogFragment {
public interface AddClassDialogListener {
void onDialogPositiveClick(DialogFragment dialog);
void onDialogNegativeClick(DialogFragment dialog);
}
AddClassDialogListener mListener;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (AddClassDialogListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener");
}
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.add_class_dialog, null));
builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
mListener.onDialogPositiveClick(AddClassDialogFragment.this);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
mListener.onDialogNegativeClick(AddClassDialogFragment.this);
}
});
return builder.create();
}
}
The reason you are getting the value of TextView is because you have hardcoded it in your layout. android:text="Class: "
There are a couple of things wrong with your code. If the layout is for the dialog then you should not be inflating it in the Activity. Here's how you'd want to implement it.
You don't need to inflate the layout in the Activity. The layout should be inflated in the Dialog and the Activity should have no knowledge of what that layout has or does. Whatever the Activity needs to know should come through the callback.
package com.soulpatch.stackoverflow;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends FragmentActivity implements AddClassDialogFragment.AddClassDialogListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final AddClassDialogFragment acdf = new AddClassDialogFragment();
acdf.show(getSupportFragmentManager(), "abc");
}
#Override
public void onDialogPositiveClick(TextView textView, EditText editText) {
Toast.makeText(this, textView.getText(), Toast.LENGTH_SHORT).show();
Toast.makeText(this, editText.getText() == null ? "" : editText.getText().toString(), Toast.LENGTH_SHORT).show();
}
#Override
public void onDialogNegativeClick(TextView textView, EditText editText) {
Toast.makeText(this, textView.getText(), Toast.LENGTH_SHORT).show();
Toast.makeText(this, editText.getText() == null ? "" : editText.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
And the DialogFragment will define what needs to go back to the activity through the interface you declare here. I have changed it to take a TextView and an EditText. You may change it as per your requirement.
package com.soulpatch.stackoverflow;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class AddClassDialogFragment extends DialogFragment {
public interface AddClassDialogListener {
void onDialogPositiveClick(TextView textView, EditText editText);
void onDialogNegativeClick(TextView textView, EditText editText);
}
AddClassDialogListener mListener;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (AddClassDialogListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener");
}
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final LayoutInflater inflater = getActivity().getLayoutInflater();
final LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.add_class_dialog, null);
final TextView textView = linearLayout.findViewById(R.id.viewClassName);
final EditText editText = linearLayout.findViewById(R.id.editClassName);
builder.setView(linearLayout);
builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
mListener.onDialogPositiveClick(textView, editText);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
mListener.onDialogNegativeClick(textView, editText);
}
});
return builder.create();
}
}
You just referred wrong id for your Edittext .
public class MainActivity extends FragmentActivity implements AddClassDialogFragment.AddClassDialogListener {
TextView nameView;
EditText editName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View vi = getLayoutInflater().inflate(R.layout.add_class_dialog, null);
nameView = vi.findViewById(R.id.viewClassName);
editName = vi.findViewById(R.id.editClassName);
}
public void addClass(View view) {
AddClassDialogFragment acdf = new AddClassDialogFragment();
acdf.show(getFragmentManager(), "ac");
}
#Override
public void onDialogPositiveClick(DialogFragment dialog) {
Toast.makeText(this, nameView.getText().toString(), Toast.LENGTH_SHORT).show();
Toast.makeText(this, editName.getText().toString(), Toast.LENGTH_SHORT).show();
}
#Override
public void onDialogNegativeClick(DialogFragment dialog) {
Toast.makeText(this, "Cancel", Toast.LENGTH_SHORT).show();
}
}
I don't understand why my "App" has stopped working?
This is my MainActivity.class code:-
package com.apps.nishant.iwillguessyournumber;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btnplay = (Button)findViewById(R.id.cmdPlay);
private Button btnexit = (Button)findViewById(R.id.cmdExit);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnButtonClickListener();
}
public void OnButtonClickListener() {
btnplay.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}
);
btnexit.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
adb.setMessage("Do you really wanna exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
}
}
);
}
}
and my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.apps.nishant.iwillguessyournumber.MainActivity">
<Button
android:id="#+id/cmdPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
tools:layout_editor_absoluteX="147dp"
tools:layout_editor_absoluteY="219dp" />
<Button
android:id="#+id/cmdExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exit"
tools:layout_editor_absoluteX="147dp"
tools:layout_editor_absoluteY="328dp" />
</android.support.constraint.ConstraintLayout>
Whenever I am running this app on Genymotion Android:6.0.0 and API 23, it is showing "Unfortunately, (app name) is not working"
What is the problem in my code/platform?
I've tried restarting my apps, looked up a few questions but couldn't yet get my answer when I open a new Activity using Intent, the second activity can't parse the xml file.
The source of your app crash is the following 2 lines,
private Button btnplay = (Button)findViewById(R.id.cmdPlay);
private Button btnexit = (Button)findViewById(R.id.cmdExit);
The method findViewById() should be called inside the onCreate() after the setContentView(). The following code should work,
package com.apps.nishant.iwillguessyournumber;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btnplay;
private Button btnexit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnplay = (Button)findViewById(R.id.cmdPlay);
btnexit = (Button)findViewById(R.id.cmdExit);
OnButtonClickListener();
}
public void OnButtonClickListener() {
btnplay.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}
);
btnexit.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
adb.setMessage("Do you really wanna exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
}
}
);
}
}
ActivityMain.java
package com.abhinav.random;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
public int count=0;
public int rand_num;
public int n;
EditText edt_rand;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rand_num=(int)(Math.random()*100);
edt_rand=(EditText)findViewById(R.id.edt_rand);
n = Integer.parseInt(edt_rand.toString());
}
public void onclick(View v){
if(count!=5){
if(n>rand_num){
Toast toast=Toast.makeText(this,"Your number is greater", Toast.LENGTH_SHORT);
toast.show();
}
if(n<rand_num){
Toast toast=Toast.makeText(this,"Your number is greater", Toast.LENGTH_SHORT);
toast.show();
}
else{
new AlertDialog.Builder(this)
.setTitle("Task complete")
.setMessage("You Win")
.setNeutralButton("Close", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.show();
}
}
else{
new AlertDialog.Builder(this)
.setTitle("No Chances Left")
.setMessage("You Lose")
.setNeutralButton("Close", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.show();
}
}
}
main_activity.xml
enter code here
<EditText
android:id="#+id/edt_rand"
android:hint="#string/number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"
></EditText>
<Button
android:id="#+id/btn_rand"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/edt_rand"
android:layout_below="#+id/edt_rand"
android:layout_marginTop="23dp"
android:text="#string/click"
android:onClick="onclick"/>
Whats wrong with the above code?
the problem is below line in onCreate() method;
n = Integer.parseInt(edt_rand.toString());
You are actually initializing an edittext and directly fetching a value from it. In such the case the value of n will be null or ""
modify your code like below,
public void onclick(View v){
n = Integer.parseInt(edt_rand.getText().toString()); // write this line here
...
}
i have one function for generate random number.
public String getDynamic()
{
Random r = new Random();
int i1 = r.nextInt(99 - 10) + 10;
String privateString = String.valueOf(i1) ;
return privateString;
}
package com.progme.wallkon;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class NextActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
ImageView im1;
im1 = (ImageView)findViewById(R.id.a_01_b);
im1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
ImageView im2;
im2 = (ImageView)findViewById(R.id.a_02_b);
im2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
ImageView im3;
im3 = (ImageView)findViewById(R.id.a_03_b);
im3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showDialog(1);
}
});
}
#Override
protected Dialog onCreateDialog(int id) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Gmelon");
builder.setMessage("setting?");
builder.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("MyTag" , "Click YES");
}
});
builder.setNegativeButton("NO",
new android.content.DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("MyTag", "Click NO");
}
});
return builder.create();
}
}
I wrote code in activity.java like this..
I want to use dialog in im1, im2, im3, and each have to get another event.
Then, I have to write 3 dialog?
and how I can set [//TODO Auto...] here that I use is like..
first dialog for im1,
second dialog for im2,
third dialog for im3..
Please help..
You can write a private variable for the alert dialog and reuse it, but not at the same time
private AlertDialog mDialog = new AlertDialog.Builder(this)
.setTitle("Gmelon")
.setMessage("setting?")
.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("MyTag" , "Click YES");
}
})
.setNegativeButton("NO",
new android.content.DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("MyTag", "Click NO");
}
}).create();
now you can show the dialog where ever you want in your code.
It looks like you could just use showDialog(x) to me unless there is more to this question.
i don't know what is wrong with that code ..the emulator works but the button does not work with the listener.i did everything i cleaned the project and also i made a listener and it supposed to work.
package com.example.dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
public class MainActivity extends Activity {
CharSequence[] items = { "Goolge", "Apple", "MaC Os" };
boolean[] checkedItems = new boolean[items.length];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Button btn= (Button)findViewById(R.id.dialog);
// btn.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
showDialog(0);
}
protected Dialog onCreatDialog(int id) {
switch (id) {
case 0 :
Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("this is a List of items..");
builder.setNegativeButton("Ok",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(getBaseContext(), "Ok clicked",
Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("Cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(), "cancel cliked",
Toast.LENGTH_SHORT).show();
}
});
builder.setMultiChoiceItems(items, checkedItems,
new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
Toast.makeText(
getBaseContext(),
items[which]
+ (isChecked ? " checked"
: " unchecked"),
Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
return null;
}
}
<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"
tools:context=".MainActivity" >
<Button
android:id="#+id/dialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="#string/click_me" />
</RelativeLayout>
uncomment this code:
Button btn = (Button)findViewById(R.id.dialog);
btn.setOnClickListener(new View.onClickListener(){
#Override
public void onClick(View v) {
//Do your stuff
}
});
If you want to use your Activity as onClickListener, keep "this" on setOnClickListener, but your activity must implements OnClickListener interface, and implements its methods to work.