i need to make a button that opens a custom dialogbox - android

I'm creating an android app for my school project.
I created the interface normaly.
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:background="#drawable/mappic"
android:layout_above="#+id/zoomControls"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ZoomControls
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/zoomControls"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/imageView"
android:layout_alignEnd="#+id/imageView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Obtenir le PCC"
android:id="#+id/button"
android:layout_alignBottom="#+id/zoomControls"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
now I want to make it when I click on the button : Obtenir le PCC this dialog box opens up :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:backgroundTintMode="multiply"
android:backgroundTint="#ff7518ff">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/Suivantbtn"
android:layout_alignLeft="#+id/Suivantbtn"
android:layout_alignStart="#+id/Suivantbtn" />
<TextView
android:id="#+id/textdp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Taper le poit de départ:"
android:textColor="#ff111124"
android:layout_alignBottom="#+id/Suivantbtn"
android:theme="#style/AppTheme"
android:textSize="30dp"
android:layout_below="#+id/image"
android:layout_marginTop="50dp"
android:layout_marginBottom="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="28dp"
android:layout_marginLeft="28dp" />/>
<Button
android:id="#+id/Suivantbtn"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="Suivant "
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_above="#+id/Suivantbtn"
android:layout_marginBottom="56dp"
android:layout_alignLeft="#+id/textdp"
android:layout_alignStart="#+id/textdp"
android:layout_alignRight="#+id/textdp"
android:layout_alignEnd="#+id/textdp"
android:textAlignment="center"
android:backgroundTint="#ff9eadff" />
</RelativeLayout>
and when I click on Suivant ,, the dialog box goes and another dialogbox appears :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:backgroundTintMode="multiply"
android:backgroundTint="#ff7518ff">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/Suivantbtn2"
android:layout_alignLeft="#+id/Suivantbtn2"
android:layout_alignStart="#+id/Suivantbtn2" />
<TextView
android:id="#+id/textar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Taper le poit d'arrivée:"
android:textColor="#ff111124"
android:layout_alignBottom="#+id/Suivantbtn2"
android:theme="#style/AppTheme"
android:textSize="30dp"
android:layout_below="#+id/image"
android:layout_marginTop="50dp"
android:layout_marginBottom="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="28dp"
android:layout_marginLeft="28dp" />/>
<Button
android:id="#+id/Suivantbtn2"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="Suivant "
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_above="#+id/Suivantbtn2"
android:layout_marginBottom="56dp"
android:layout_alignLeft="#+id/textar"
android:layout_alignStart="#+id/textar"
android:layout_alignRight="#+id/textar"
android:layout_alignEnd="#+id/textar"
android:textAlignment="center"
android:backgroundTint="#ff9eadff" />
</RelativeLayout>
the java code :
package artofdev.org.admaps;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Context context = getApplicationContext();
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog);
Button button2 = (Button) dialog.findViewById(R.id.Suivantbtn);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog2 = new Dialog(context);
dialog2.setContentView(R.layout.dialog2);
Button button3 = (Button)
dialog2.findViewById(R.id.Suivantbtn2);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog2.dismiss();
}
});
dialog.dismiss();
dialog2.show();
}
});
dialog.show();
}
});
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And when this appears and I type in and click on next. This dialog box dismisses and another msg box I'll create later shows a msg.
how to do it ?

Here is a fully working code for you:
Context context = getAplicationContext();
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.your_dialog_layout);
Button button2 = (Button) dialog.findViewById(R.id.Suivantbtn);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog2 = new Dialog(context);
dialog2.setContentView(R.layout.your_second_dialog_layout);
Button button3 = (Button) dialog2.findViewById(R.id.Suivantbtn2);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog2.dismiss();
}
});
dialog.dismiss();
dialog2.show();
}
});
dialog.show();
}
});
Hope my answer helped!

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
#Override
//On click function
public void onClick(View view) {
//do something - for example open the dialog box you want
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_alert_dialogue);
dialog.show();
}
});

You should add onClickListener to your buttons. For example (this code is for your Activity class):
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
#Override
//On click function
public void onClick(View view) {
//do something - for example open the dialog box you want
}
});

Related

why custom dialog buttons dont work?

I have a custom dialog in my app but when I click on the buttons of dialog they don't work!
I want to show dialog in one of the items of navigation drawer
and that's code:
dialog = new Dialog(MaterialCardsActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setContentView(R.layout.dialog_mail);
dialog.show();
How can I resolve it?
Custom_Dialog.java
package com.example.material.amirj;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Dialog_Custom extends AppCompatActivity {
private EditText caption;
private Dialog dialog;
private EditText text;
private Button send,cancel;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_mail);
dialog = new Dialog(Dialog_Custom.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setContentView(R.layout.dialog_mail);
caption = (EditText) dialog.findViewById(R.id.txt_caption_dialoge);
text = (EditText) dialog.findViewById(R.id.txt_text_dialoge);
send = (Button) dialog.findViewById(R.id.btn_send_email);
cancel = (Button) dialog.findViewById(R.id.btn_cancel_dialog);
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"titandevelopers98#gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "" + caption.getText());
i.putExtra(Intent.EXTRA_TEXT, "" + text.getText());
try {
startActivity(Intent.createChooser(i, "ارسال ایمیل با ..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(Dialog_Custom.this, "برنامه ای از ازسال ایمیل پشتیبانی نمیکند.", Toast.LENGTH_SHORT).show();
}
}
});
cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
dialog_mail.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="ارتباط با ما - ایمیل"
android:id="#+id/textView2"
android:layout_gravity="right"
android:textColor="#color/colorPrimary"
android:padding="8dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:gravity="center" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ارسال"
android:id="#+id/btn_send_email"
android:textColor="#ffffff"
android:layout_below="#+id/textView2"
android:layout_alignRight="#+id/editText"
android:layout_alignEnd="#+id/editText"
android:layout_marginTop="151dp"
android:theme="#style/MyButton"
android:allowUndo="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="لغو"
android:id="#+id/btn_cancel_dialog"
android:textColor="#android:color/holo_red_light"
style="?android:attr/borderlessButtonStyle"
android:layout_alignTop="#+id/btn_send_email"
android:layout_toLeftOf="#+id/btn_send_email"
android:layout_toStartOf="#+id/btn_send_email" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="#style/MyHintText"
android:id="#+id/view3"
android:layout_below="#+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:focusable="true">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="عنوان"
android:id="#+id/txt_text_dialoge"
android:theme="#style/MyEditText" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="#style/MyHintText"
android:layout_below="#+id/view3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/editText"
android:layout_above="#+id/btn_cancel_dialog">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="متن پیام"
android:id="#+id/txt_caption_dialoge"
android:theme="#style/MyEditText" />
</android.support.design.widget.TextInputLayout>
</RelativeLayout>
final result but buttons don't work
Try this,
dialog_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/buttonClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click To Show Custom Dialog" />
</LinearLayout>
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/imageDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dp" />
<TextView
android:id="#+id/textDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="#+id/imageDialog"/>
<Button
android:id="#+id/okButton"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="Ok"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/textDialog"
android:layout_toRightOf="#+id/imageDialog"
/>
</RelativeLayout>
CustomDialog.java
public class CustomDialog extends Activity {
private Button btnClick;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_dialog_main);
btnClick = (Button) findViewById(R.id.btnClick);
// add listener to button
buttonClick.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Create custom dialog object
final Dialog dialog = new Dialog(CustomDialog.this);
// Include dialog.xml file
dialog.setContentView(R.layout.dialog);
// Set dialog title
dialog.setTitle("Custom Dialog");
// set values for custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.textDialog);
text.setText("Custom dialog");
ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog);
image.setImageResource(R.drawable.image0);
dialog.show();
Button okButton = (Button) dialog.findViewById(R.id.declineButton);
// if decline button is clicked, close the custom dialog
okButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
}
});
}
}
Happy coding..!

Custom popup dialog with input field [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How do I create popup input field for Android?
I need Xml and Java Code.
try to use this custom popup dialog code
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/buttonPrompt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Prompt Dialog" />
<EditText
android:id="#+id/editTextResult"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</EditText>
</LinearLayout>
Custom.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type Your Message : "
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editTextDialogUserInput"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
</LinearLayout>
main.java
package cm.kikani.kalpesh;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
final Context context = this;
private Button button;
private EditText result;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// components from main.xml
button = (Button) findViewById(R.id.buttonPrompt);
result = (EditText) findViewById(R.id.editTextResult);
// add button listener
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// get prompts.xml view
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.custom, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// get user input and set it to result
// edit text
result.setText(userInput.getText());
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
}
You can create a custom dialog class for your case.
your_custom_dialog.xml
<?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="120dp"
android:background="#android:color/black"
android:orientation="vertical" >
<TextView
android:id="#+id/txt_exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="Do you realy want to exit ?"
android:textColor="#android:color/white"
android:textSize="16sp"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#android:color/blue"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_yes"
android:layout_width="100dp"
android:layout_height="30dp"
android:background="#android:color/white"
android:clickable="true"
android:text="Yes"
android:textColor="#android:color/white"
android:textStyle="bold" />
<Button
android:id="#+id/btn_no"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_marginLeft="5dp"
android:background="#android:color/white"
android:clickable="true"
android:text="No"
android:textColor="#android:color/blue"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
and you CustomDialog class must extend Dialog
public class CustomDialogClass extends Dialog {
public Activity activity;
public Dialog dialog;
public Button yes, no;
public CustomDialogClass(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.activity = a;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog);
yes = (Button) findViewById(R.id.btn_yes);
no = (Button) findViewById(R.id.btn_no);
yes.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
activity.finish();
}
});
no.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
dismiss();
}
});
}
}
and you can call it like this
CustomDialogClass customDialog =new CustomDialogClass(activity);
customDialog .show();

Android Calculator App

I'm new to android app development. I was building a basic calculator app in android. I have no compile time errors in the code but when i'm trying to run this app it shows "Unfortunately calculator has stopped working". I have copied XML Layout and .java file one after the other.
`
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/input1"/>
<EditText
android:id="#+id/ed1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/input1"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/input2"/>
<EditText
android:id="#+id/ed3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:hint="#string/input2"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/result"/>
<EditText
android:id="#+id/ed2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/result"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp" >
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/add"/>
<Button
android:id="#+id/btn2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/sub"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/btn3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/mul"/>
<Button
android:id="#+id/btn4"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/div"/>
</LinearLayout>
</LinearLayout>
package com.example.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity
{
public Integer int3;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText input1=(EditText) findViewById(R.id.ed1);
final EditText input2=(EditText) findViewById(R.id.ed2);
final Integer int1=Integer.parseInt(input1.getText().toString());
final Integer int2=Integer.parseInt(input2.getText().toString());
Button add=(Button) findViewById(R.id.btn1);
add.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
int3=int1+int2;
}
});
Button sub=(Button) findViewById(R.id.btn2);
sub.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
int3=int1-int2;
}
});
Button mul=(Button) findViewById(R.id.btn3);
mul.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
int3=int1*int2;
}
});
Button div=(Button) findViewById(R.id.btn4);
div.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
int3=int1/int2;
}
});
final EditText result=(EditText) findViewById(R.id.ed2);
result.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
result.setText(Integer.toString(int3));
}
});
}
}'
Do not Add this on onCreate()
final Integer int1=Integer.parseInt(input1.getText().toString());
final Integer int2=Integer.parseInt(input2.getText().toString()); )
DELETE IT !!
Otherwise, You have to add this convert sentences when you create onClickListener !
you should add these line in every onClick methods like this.
add.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Integer int1=Integer.parseInt(input1.getText().toString());
Integer int2=Integer.parseInt(input2.getText().toString());
int3=int1+int2;
}
});
Button sub=(Button) findViewById(R.id.btn2);
sub.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Integer int1=Integer.parseInt(input1.getText().toString());
Integer int2=Integer.parseInt(input2.getText().toString());
int3=int1-int2;
}
});
There will be no error.
Additionally you should consider how about clicking without edittext value, it can cause just the error you got !Actually, you have to void assigning null(space) value to integer. Your error is putting space to integer. Edittext have space value and you are changing it to integer. Your Error !!

2 button OnClick event on Popup Window in Android

I have 2 buttons in my xml file, onclicklistener open new popup.why this eror? can help me??
My Java code is as follows:
Public class Bab1_b1a extends Activity {
final Context context = this;
private Button hans, logemann;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bab1_b1a);
hans = (Button) findViewById(R.id.hans);
logemann = (Button) findViewById(R.id.logemann);
hans.setOnClickListener(myhandler1);
logemann.setOnClickListener(myhandler2);
}
View.OnClickListener myhandler1 = new View.OnClickListener() {
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.activity_popup_hans);
dialog.setTitle("Hans Kelsen");
TextView text1 = (TextView) dialog.findViewById(R.id.textView1);
text1.setText("Negara ialah");
ImageView image = (ImageView) dialog.findViewById(R.id.imageView1);
image.setImageResource(R.drawable.hans_kelsen);
Button dialogButton = (Button) dialog.findViewById(R.id.back_hans);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
};
View.OnClickListener myhandler2 = new View.OnClickListener() {
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.popup_logemann);
dialog.setTitle("Logemann");`enter code here`
TextView text1 = (TextView) dialog.findViewById(R.id.textView1);
text1.setText("Negara ialah.");
ImageView image = (ImageView) dialog.findViewById(R.id.imageView1);
image.setImageResource(R.drawable.logemann);
Button dialogButton = (Button) dialog.findViewById(R.id.back_logemann);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
};
};
xml :
<LinearLayout 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="#drawable/back"
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=".Bab1_b1a" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/nav_back"
android:contentDescription="#string/skx" />
<LinearLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="#string/h_neg"
android:textSize="13sp" />
<Button
android:id="#+id/hans"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hans_kelsen"
android:textSize="15sp" />
<Button
android:id="#+id/logemann"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/logemann" />
</LinearLayout>
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/nav_forw"
android:contentDescription="#string/skx" />
</LinearLayout>
Can anyone help me know the reason for this problem and how do I solve it. I am a newbie to Android.
Thanks
try this
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt_1 = (Button) findViewById(R.id.bt1);
bt_1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.activity_popup);
dialog.setTitle("Hans Kelsen");
//Her add your textView and ImageView if you want
Button dialogButton = (Button) dialog.findViewById(R.id.bt1_popUP);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
// Same thing for bt_2
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Your Main Activity 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: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=".MainActivity" >
<Button
android:id="#+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BT 1" />
<Button
android:id="#+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/bt1"
android:layout_marginTop="40dp"
android:text="BT 2" />
</RelativeLayout>
Your POP-UP 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="200dp"
android:background="#FFF123"
android:gravity="center" >
<Button
android:id="#+id/bt1_popUP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close" />
</RelativeLayout>
enjoy :)

I get a forcestop occasionally when I press buttons other than the first

I have 3 buttons in total. And it seemed that, if I don't press the first button, and instead second or third button right after I startup, my application will forcestop.
My code for part1 : http://pastebin.com/udkEdF3E
package project.ernest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Part1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button a = (Button) findViewById(R.id.Disclaimer);
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent b = new Intent(getBaseContext(), Part2.class);
Part1.this.startActivity(b);
final Button c = (Button) findViewById(R.id.Orientation);
c.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent d = new Intent(getBaseContext(), Part3.class);
Part1.this.startActivity(d);
final Button e = (Button) findViewById(R.id.Course);
e.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent f = new Intent(getBaseContext(), Part4.class);
Part1.this.startActivity(f);
}
});
};
});
};
});
};
}
my code for main.xml : http://pastebin.com/SxfxfJ4a
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/myColor" android:weightSum="1">
<LinearLayout android:orientation="vertical" android:id="#+id/linearLayout1" android:weightSum="1" android:layout_width="wrap_content" android:layout_height="404dp">
<TextView android:text="#string/HospName" android:id="#+id/textView1" android:textSize="20mm" android:layout_width="324dp" android:layout_height="142dp"></TextView>
<Button android:text="#string/Disclaimer" android:layout_weight="0.37" android:textSize="5mm" android:layout_height="75dp" android:layout_width="fill_parent" android:id="#+id/Disclaimer" android:drawingCacheQuality="high" android:onClick="#string/DisclaimerHandler"></Button>
<Button android:text="#string/Orientation" android:textSize="5mm" android:layout_height="75dp" android:layout_weight="0.37" android:layout_width="fill_parent" android:id="#+id/Orientation" android:onClick="#string/OrientationHandler"></Button>
<Button android:text="#string/Course" android:textSize="5mm" android:layout_height="75dp" android:layout_weight="0.37" android:layout_width="fill_parent" android:id="#+id/Course" android:onClick="#string/CourseHandler"></Button>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:layout_weight="0.19" android:layout_height="wrap_content" android:id="#+id/linearLayout2" android:weightSum="1">
<Button android:layout_weight="0.23" android:text="#string/Search" android:textSize="1.9mm" android:layout_height="75dp" android:layout_width="75dp" android:onClick="#string/SearchHandler" android:id="#+id/Search"></Button>
<Button android:layout_weight="0.23" android:text="#string/Contact" android:textSize="1.9mm" android:layout_height="75dp" android:layout_width="75dp" android:onClick="#string/ContactHandler" android:id="#+id/Contact"></Button>
<Button android:onClick="#string/LinkHandler" android:layout_height="75dp" android:layout_width="75dp" android:layout_weight="0.23" android:textSize="1.9mm" android:text="#string/Link" android:id="#+id/Link"></Button>
<Button android:layout_weight="0.23" android:text="#string/Copyright" android:textSize="1.9mm" android:layout_height="75dp" android:layout_width="75dp" android:onClick="#string/CopyrightHandler" android:id="#+id/Copyright"></Button>
</LinearLayout>
</LinearLayout>
The error I get is either:
java.lang.IllegalStateException: Could not find a method CourseHandler(View) in activity class project.ernest.Part1 or...
java.lang.IllegalStateException: Could not find a method OrientationHandler(View) in activity class project.ernest.Part1 or...
Please help me out!
You've placed initialization of buttons 2 and 3 inside the first button's onClickListener(), means they won't be initialized before you click the first button. Fix your code.
this is the format.
public class Part1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button a = (Button) findViewById(R.id.Disclaimer);
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent b = new Intent(getBaseContext(), Part2.class);
Part1.this.startActivity(b);
}
});
final Button c = (Button) findViewById(R.id.Orientation);
c.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent d = new Intent(getBaseContext(), Part3.class);
Part1.this.startActivity(d);
}
});
final Button e = (Button) findViewById(R.id.Course);
e.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent f = new Intent(getBaseContext(), Part4.class);
Part1.this.startActivity(f);
}
});
}
}

Categories

Resources