I have a main activity which should load a small splash xml file for 2 or 3 seconds when the app is opened. I tried this snippet of code in the oncreate before adding it to my major project. Keep in mind, both apps worked seperately but for some reason, i get a null pointer exception when running the app..... help?
MainActivity:
package com.Depauw.dpuhelpdesk;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button knowledgeBase, submitRequest, helpme, faq, technician, call;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
//display the logo during 5 secondes,
new CountDownTimer(2000,1000){
public void onTick(long millisUntilFinished){}
public void onFinish(){
//set the new Content of your activity
MainActivity.this.setContentView(R.layout.activity_main);
}
}.start();
Initialize();
}
private void Initialize(){
knowledgeBase = (Button) findViewById(R.id.knowledgebase1);
submitRequest = (Button) findViewById(R.id.submitrequest1);
helpme = (Button) findViewById(R.id.helpButton);
faq = (Button) findViewById(R.id.faqButton);
technician = (Button) findViewById(R.id.submitrequest2);
call = (Button) findViewById(R.id.callButton);
knowledgeBase.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
Intent knowledgeIntent = new Intent(MainActivity.this, knowledgebase1_activity.class);
startActivity(knowledgeIntent);
}
});
submitRequest.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
Intent requestIntent = new Intent(MainActivity.this, submitRequest_activity.class);
startActivity(requestIntent);
}
});
helpme.setOnClickListener(new OnClickListener(){ //dialog box
public void onClick(View arg0){
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Help");
builder.setMessage("This app allows you to access the IT KnowledgeBase from DePauw's website." + " " +
"If you experience any issues using our app, please send us an email to helpdesk#depauw.edu or call 765-658-4294");
builder.setCancelable(true);
builder.setIcon(R.drawable.ic_launcher);
builder.setPositiveButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
builder.setOnCancelListener(null);
}
});
builder.create().show(); // create and show the alert dialog
}
});
faq.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
Intent requestIntent = new Intent(MainActivity.this, activity_main_faq.class);
startActivity(requestIntent);
}
});
technician.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
Intent requestIntent = new Intent(MainActivity.this, submit_technician_request.class);
startActivity(requestIntent);
}
});
call.setOnClickListener(new OnClickListener(){ //dialog box
public void onClick(View arg0){
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Complete the call?");
builder.setMessage("Click yes to connect your call. Otherwise, click no.");
builder.setCancelable(true);
builder.setIcon(R.drawable.ic_launcher);
//saying yes completes the call. This way, we don't get accidently calls as often
builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:17656584294"));
startActivity(callIntent);
} catch (ActivityNotFoundException activityException) {
Log.e("Calling a Phone Number", "Call failed", activityException);
}
}
});
builder.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = builder.create();
// show it
alertDialog.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.main, menu);
return true;
}
}
splash.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#raw/splash2" />
main_activity_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".basic_activity1"
tools:ignore="MergeRootFrame"
android:background="#000000" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/header"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#EAC117"
android:textSize="35sp" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#raw/firstintentlogo"
android:layout_weight="1" android:contentDescription="#string/headLogoName"/>
</LinearLayout>
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/knowledgebase1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/knowledgebase1"
android:textColor="#EAC117" />
<Button
android:id="#+id/submitrequest1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/submitrequest1"
android:textColor="#EAC117" />
<Button
android:id="#+id/submitrequest2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/submitTechnician"
android:textColor="#EAC117" />
<Button
android:id="#+id/helpButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/helpButton"
android:textColor="#EAC117" />
<Button
android:id="#+id/faqButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/faqButton"
android:textColor="#EAC117" />
<Button
android:id="#+id/callButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/callButton"
android:textColor="#EAC117" />
</LinearLayout>
</ScrollView>
</FrameLayout>
Initialize() is getting called immediately.
This is happening before MainActivity.this.setContentView(R.layout.activity_main) is called.
Related
I need to use a .java file to perform actions for a .xml layout. I have used this layout for a dialog box. I have linked the .java file to that layout but it doesn't seem to work at all.
This is the MainActivity
package com.example.ayush.projectfive;
import android.content.DialogInterface;
import android.content.Intent;
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 {
Button btn;
AlertDialog.Builder alrt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
alrt = new AlertDialog.Builder(MainActivity.this);
alrt.setIcon(R.mipmap.ic_launcher);
alrt.setTitle("Login");
alrt.setCancelable(false);
alrt.setView(R.layout.mylayout);
alrt.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alrt.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alrt.show();
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
}
});
}
}
This is the activity_main
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
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.ayush.projectfive.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EXIT"
android:id="#+id/button"
android:layout_gravity="center_horizontal" />
This is the dialog box layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="LOGIN"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:layout_marginTop="20dp"
android:id="#+id/editText" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:layout_marginTop="15dp"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/editText2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_marginTop="20dp"
android:id="#+id/button3" />
This is the .java file I'd like to link with.
package com.example.ayush.projectfive;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Second extends AppCompatActivity{
EditText et, et2;
Button btn2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
et = (EditText) findViewById(R.id.editText);
et2 = (EditText) findViewById(R.id.editText2);
btn2 = (Button) findViewById(R.id.button3);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String s1 = et.getText().toString();
String s2 = et.getText().toString();
if(s1.equals(s2)){
Toast.makeText(Second.this, "Login Successful", Toast.LENGTH_SHORT).show();
Intent i = new Intent(Second.this,Third.class);
startActivity(i);
}
else{
}
}
});
}
}
This is the .java file I'd like to link with [...]
public class Second extends AppCompatActivity{
Okay... That's what this code does
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
If you are trying to use the views that are contained within the dialog box and respond to the buttons there to login, then you need to remove the button from the activity, add alrt.show() in the onCreate instead of onClick and move the Activity start code into the dialog's onClick handlers.
It's also worth mentioning that the object is a builder, so you can do this
View dialogView = LayoutInflater.from(MainActivity.this).inflate(R.layout.mylayout, null);
final EditText editUsername = dialogView.findViewById(R.id.editText);
// TODO: Get other views from mylayout.xml
new AlertDialog.Builder(MainActivity.this)
.setIcon(R.mipmap.ic_launcher)
.setTitle("Login")
.setCancelable(false)
.setView(dialogView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String username = editUsername.getText().toString();
// TODO: Get more variables
// TODO: Verify credentials
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
}
})
.setNegativeButton("CANCEL", null)
.show();
And, as stated earlier, remove btn.setOnClickListener
just add alrt.show(); in your MainActivity.java e.g. here:
public class MainActivity extends AppCompatActivity {
Button btn;
AlertDialog.Builder alrt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
alrt = new AlertDialog.Builder(MainActivity.this);
alrt.setIcon(R.mipmap.ic_launcher);
alrt.setTitle("Login");
alrt.setCancelable(false);
alrt.setView(R.layout.mylayout);
alrt.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alrt.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alrt.show();
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
}
});
}
}
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();
I am trying to modify the layout of a dialog and then perform some function and close the alert box as my function is over.
Layout File
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:orientation="horizontal"
android:layout_below="#+id/header"
android:paddingTop="15dp"
android:paddingBottom="15dp">
<ImageView
android:id="#+id/cam"
android:layout_width="0dp"
android:layout_height="match_parent"
android:padding="10dp"
android:paddingLeft="10dp"
android:src="#drawable/ic_cam"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:onClick="camera_listener"
/>
<ImageView
android:id="#+id/gal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:padding="10dp"
android:paddingLeft="10dp"
android:src="#drawable/ic_gal"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:onClick="gallery_listener"
/>
</LinearLayout>
Java File
AlertDialog.Builder myAlertDialog; // Variable declared as a class member
private void startDialog() {
LayoutInflater inflater = this.getLayoutInflater();
final View view = inflater.inflate(R.layout.activity_fab, null);
myAlertDialog = new AlertDialog.Builder(this);
myAlertDialog.show();
}
public void gallery_listener(View view) {
pictureActionIntent = new Intent(Intent.ACTION_PICK, null);
pictureActionIntent.setType("image/*");
pictureActionIntent.putExtra("return-data", true);
startActivityForResult(pictureActionIntent, GALLERY_PICTURE);
myAlertDialog.setOnDismissListener();
}
public void camera_listener(View view) {
pictureActionIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
pictureActionIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(pictureActionIntent, CAMERA_REQUEST);
}
I'm displaying two images in dialog and defining function on their click... i want to close the DIALOG as soon as corresponding function is performed.
I tried using dismiss but it is not working..!
try this in your second method,
in Main.java
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class DialogCustom extends Activity {
AlertDialog myAlertDialog;
ImageView ivOne,ivTwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog_custom);
ivOne=(ImageView)findViewById(R.id.cam);
ivTwo=(ImageView)findViewById(R.id.gal);
myAlertDialog = new AlertDialog.Builder(this).create();
ivOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startDialog();
}
});
ivTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
#Override
public void run() {
dismis();
}
});
}
});
}
private void startDialog() {
myAlertDialog.show();
}
public void dismis()
{
myAlertDialog.dismiss();
}
}
and in main.xml file.
<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="120dp"
android:layout_below="#+id/header"
android:orientation="horizontal"
android:paddingBottom="15dp"
android:paddingTop="15dp" >
<ImageView
android:id="#+id/cam"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:onClick="camera_listener"
android:padding="10dp"
android:paddingLeft="10dp"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/gal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:onClick="gallery_listener"
android:padding="10dp"
android:paddingLeft="10dp"
android:src="#drawable/ic_launcher" />
</LinearLayout>
Alternate i found... is first create a alert dialog using builder then assign that to a dialog and use dismiss function with that
Update-
private AlertDialog dialog; // Variable with class scope
private void startDialog() {
LayoutInflater inflater = this.getLayoutInflater();
final View view = inflater.inflate(R.layout.activity_fab, null);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setView(view);
dialog = builder.create();
dialog.show();
}
public void gallery_listener(View view) {
pictureActionIntent = new Intent(Intent.ACTION_PICK, null);
pictureActionIntent.setType("image/*");
pictureActionIntent.putExtra("return-data", true);
startActivityForResult(pictureActionIntent, GALLERY_PICTURE);
dialog.dismiss();
}
This has worked for me... if there is any better method plz share!
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 :)
Here is My Code
package com.dialog;
import java.util.HashMap;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.dialog, null);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText name=(EditText)findViewById(R.id.name);
final EditText contact=(EditText)findViewById(R.id.contact);
final HashMap<String,String> map=new HashMap<String,String>();
Button button_add=(Button)findViewById(R.id.main_add);
final AlertDialog alert;
builder.setCancelable(true)
.setPositiveButton("Add",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
map.put(contact.getText().toString(), name.getText().toString());
//Toast.makeText(Main.this,map.get(contact.getText().toString()), Toast.LENGTH_LONG).show();
dialog.cancel();
}
})
.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setView(textEntryView);
alert=builder.create();
button_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alert.show();
}
});
}
}
Her are my two xml files:
1)main.xml
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/main_add" android:text="Add"></Button>
</LinearLayout>
2)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">
<TextView
android:id="#+id/name_label"
android:text="Name:"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/name"
android:layout_below="#+id/name_label"
android:hint="Enter your name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/contact_label"
android:layout_below="#+id/name"
android:text="Contact:"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/contact"
android:layout_below="#+id/contact_label"
android:hint="Enter the contact number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
Help me!!!
Here's the problem:
final EditText name=(EditText)findViewById(R.id.name);
final EditText contact=(EditText)findViewById(R.id.contact);
You look them up from main layout, not from dialog. Should be:
final EditText name=(EditText)textEntryView.findViewById(R.id.name);
final EditText contact=(EditText)textEntryView.findViewById(R.id.contact);
AlertDialog dialog = new Builder(Main.this.getParent()).create();
dialog.setTitle("Delete");
dialog.setButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
dialog.setButton2("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Whatever you want to do
dialog.dismiss();
}
});
dialog.show();