Android studio APP has stopped - android

I am new in android learning. Using Android Studio. My app name is Design1. I am learning events so created a event page with two buttons for testing two events, one is toast and another is Alert dialog box.The Toast is working fine but when I click ALERT button to show the alert dialog then a message comes ie "Design1 has stopped".
If I remove the showAlert event from activity_events.xml and events.java files then my works perfect.
events.java
package com.example.borntoflirt.design1;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Camera;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class events extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_events);
}
public void showToast(View v) {
Context context = getApplicationContext(); // OR getBaseContext()
CharSequence text = "Hi Toast";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
public void showAlert(View v) {
Context context1 = getApplicationContext();
AlertDialog.Builder builder = new AlertDialog.Builder(context1);
builder.setMessage("Write your message here.");
builder.setCancelable(true);
builder.setPositiveButton(
"Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.setNegativeButton(
"No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
activity_events.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_red_light"
android:isScrollContainer="false"
android:orientation="vertical"
android:paddingLeft="15dp"
android:paddingRight="15dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight=".30"
android:background="#drawable/rounded_button"
android:text="Toast"
android:textColor="#000"
android:onClick="showToast"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".30"
android:text="Alert"
android:textColor="#000"
android:background="#drawable/rounded_button"
android:layout_margin="2dp"
android:onClick="showAlert"/>
</LinearLayout>
</LinearLayout>

You need to use Activity context. If you use application context, the IllegalStateExpetion will be thrown.
...
new AlertDialog.Buidler(this) // this is a Activity, which is a Context's subclass
....

Use Activity's Context :
AlertDialog.Builder builder = new AlertDialog.Builder(event.this);
Instead Of
Context context1 = getApplicationContext();
AlertDialog.Builder builder = new AlertDialog.Builder(context1);

Try to use getActivity() instead of getApplicationContext().

It has been 7 months but none of the answers worked on me. So this is solution that i found. Try MainActivity.this instead of getApplicationContext() or this.

Related

Android java.lang.NullPointerException at FindViewById in a dialog

I saw many similar questions to this but still cannot resolve mine.
Here is the log:
01-26 00:30:49.714: ERROR/AndroidRuntime(616): FATAL EXCEPTION: main
java.lang.NullPointerException
at com.app.newItemList.onDialogPositiveClick(newItemList.java:39)
at com.app.itemDialog$2.onClick(itemDialog.java:39)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Here is the relevant Java files:
package com.app;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
/**
* Created with IntelliJ IDEA.
* User: Aljarhi
* Date: 1/24/13
* Time: 10:22 PM
* To change this template use File | Settings | File Templates.
*/
public class newItemList extends FragmentActivity
implements itemDialog.NoticeDialogListener{
public void showNoticeDialog() {
// Create an instance of the dialog fragment and show it
DialogFragment dialog = new itemDialog();
dialog.show(getFragmentManager(), "NoticeDialogFragment");
}
// The dialog fragment receives a reference to this Activity through the
// Fragment.onAttach() callback, which it uses to call the following methods
// defined by the NoticeDialogFragment.NoticeDialogListener interface
#Override
public void onDialogPositiveClick(DialogInterface dialog, int id) {
// User touched the dialog's positive button
EditText et = (EditText) findViewById(R.id.editText1);
adapter.add(et.getText().toString());
}
#Override
public void onDialogNegativeClick(DialogInterface dialog, int id) {
// User touched the dialog's negative button
dialog.cancel();
}
ArrayAdapter<String> adapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_list);
ListView lvc = (ListView) findViewById(R.id.listView3);
adapter = new ArrayAdapter<String>(this, R.id.textView2, new ArrayList<String>());
lvc.setAdapter(adapter);
}
public void addItem(View clickedButton)
{
showNoticeDialog();
}
}
package com.app;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.EditText;
/**
* Created with IntelliJ IDEA.
* User: Aljarhi
* Date: 1/25/13
* Time: 3:11 PM
* To change this template use File | Settings | File Templates.
*/
public class itemDialog extends DialogFragment
{
public interface NoticeDialogListener {
public void onDialogPositiveClick(DialogInterface dialog, int id);
public void onDialogNegativeClick(DialogInterface dialog, int id);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_item, null))
// Add action buttons
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mListener.onDialogPositiveClick(dialog, id);
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mListener.onDialogNegativeClick(dialog, id);
}
});
return builder.create();
}
// Use this instance of the interface to deliver action events
NoticeDialogListener mListener;
// Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the host
mListener = (NoticeDialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement NoticeDialogListener");
}
}
}
here is the relevant xml files:
<?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" android:gravity="center_horizontal|top">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mezzo List"
android:id="#+id/textView2" android:layout_gravity="center_horizontal|top"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="263dp"
android:id="#+id/listView3" android:layout_gravity="center_horizontal|top" android:choiceMode="none"/>
<Button
android:layout_width="81dp"
android:layout_height="wrap_content"
android:text="Add Item"
android:id="#+id/button4" android:onClick="addItem"/>
</LinearLayout>
<?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="25dp"
android:text="Mezzo List"
android:id="#+id/textView4" android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Item Name:"
android:id="#+id/textView5" android:layout_gravity="center" android:textSize="20dp"/>
<EditText
android:layout_width="205dp"
android:layout_height="wrap_content"
android:text="New EditText"
android:id="#+id/editText1" android:layout_gravity="center" android:inputType="text"
android:hint="Item Name"
/>
</LinearLayout>
I found the problem to be in the edittext1 id not recognised or as an edittext although it appeared in the list of choices and I selected it form there. I am using intellij Idea 12.0.2. Any help will be appreciated. Thanks in advance.
Actually, I found the solution to my problem.
Inorder to for findViewById to work correctly you have first to select the view which inflates or has its content first set to the corresondong xml file. So, here is the solution:
final View textEntryView = inflater.inflate(R.layout.dialog_item, null);
EditText et = (EditText) textEntryView.findViewById(R.id.editText1);
Thanks all for your help.
In this line:
adapter = new ArrayAdapter<String>(this, R.id.textView2, new ArrayList<String>());
You probably don't want R.id.textView2 there. You generally want to pass in a layout which contains a textview (with the other id parameter). e.g.
adapter = new ArrayAdapter<String>(this, R.layout.somelayout, R.id.someTextViewInThatLayout, new ArrayList<String>());

Multiple TextViews Added in Loop onClick Unexpected Behavior

I have two RelativeLayouts that I am programmatically adding TextViews to from an ArrayList with the intent that, based on the TextView clicked, I can index back into the original ArrayList. For my trivial code example, I'm arbitrarily splitting the items into the two RelativeLayouts.
The code works as expected when the first TextView is clicked. The dialog shows up displaying the correct word and index. However, after closing the dialog, if a different TextView is clicked, the word and index (testWord and testID in my code example) are not updated and only the first TextView's information is displayed. It seems that onClick is only being called on the first click.
Here is an example Java class (I apologize for any formatting errors, it's my first time posting here):
package com.test.test;
import java.util.ArrayList;
import java.util.Scanner;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class AndTestActivity extends Activity {
private final int DIALOG_CASE_ITEM_SELECT=0,CURR_ID=128,P_ID=256,P_HR=512,C_HR=1024;
private final String TEST="This is a test. Only a test.";
private int numPast,testID;
private String testWord;
private ArrayList<String> test;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Scanner s=new Scanner(TEST);
test=new ArrayList<String>();
while(s.hasNext()){
test.add(s.next());
}
int currID=CURR_ID,pID=P_ID,pHrID=P_HR,cHrID=C_HR,tempLen=3;
numPast=0;
RelativeLayout rlP=(RelativeLayout)findViewById(R.id.caseItemListPastLayout);
RelativeLayout rlC=(RelativeLayout)findViewById(R.id.caseItemListCurrLayout);
for(int x=0;x<test.size();x++){
if(x>tempLen){
addToRL(test.get(x),true,rlC,currID,cHrID);
currID++;
cHrID++;
}else{
numPast++;
addToRL(test.get(x),false,rlP,pID,pHrID);
pID++;
pHrID++;
}
}
}
private void addToRL(String sb, boolean current,RelativeLayout rl,int id,int hrID){
TextView citv=new TextView(this);
citv.setText((CharSequence)sb);
citv.setMovementMethod(new ScrollingMovementMethod());
citv.setTextSize(15);
citv.setTextColor(Color.BLACK);
citv.setGravity(Gravity.CENTER_HORIZONTAL);
if(current){
citv.setBackgroundColor(Color.RED);
}else{
citv.setBackgroundColor(Color.WHITE);
}
citv.setPadding(20, 10, 20, 10);
citv.setWidth((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 360, getResources().getDisplayMetrics()));
citv.setId(id);
RelativeLayout.LayoutParams cilp=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if(current&&id!=CURR_ID)cilp.addRule( RelativeLayout.BELOW, hrID-1);
else{
if(id!=P_ID)cilp.addRule( RelativeLayout.BELOW, hrID-1);
}
citv.setLayoutParams(cilp);
citv.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
TextView tv0=(TextView)v;
int tvID=tv0.getId()-P_ID;
if(tvID<0){
tvID=(tv0.getId()-CURR_ID)+numPast;
}
testID=tvID;
testWord=test.get(testID);
showDialog(DIALOG_CASE_ITEM_SELECT);
}
});
rl.addView(citv);
View hr=new View(this);
hr.setBackgroundColor(getResources().getColor(R.color.background));
RelativeLayout.LayoutParams hrlp=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, 5);
hrlp.addRule(RelativeLayout.BELOW,id);
hr.setLayoutParams(hrlp);
hr.setId(hrID);
rl.addView(hr);
}
protected Dialog onCreateDialog(int id) {
Dialog dialog;
AlertDialog.Builder builder;
switch(id){
case DIALOG_CASE_ITEM_SELECT:
builder = new AlertDialog.Builder(this);
builder.setMessage("word: "+testWord+" index: "+testID)
.setCancelable(true)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
dialog = builder.create();
break;
default:
dialog = null;
}
return dialog;
}
}
Here is the associated XML for the layout (nothing special, but I thought I'd include it so there's a fully working example):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="vertical"
android:background="#color/background"
android:screenOrientation="portrait">
<ScrollView
android:id="#+id/caseItemCurrScroll"
android:fillViewport="true"
android:padding="10dp"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<RelativeLayout android:id="#+id/caseItemListCurrLayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</RelativeLayout>
</ScrollView>
<View
android:id="#+id/caseItemScrollSpacer"
android:layout_width="fill_parent"
android:layout_below="#id/caseItemCurrScroll"
android:layout_height="5dp"
android:background="#color/background"/>
<ScrollView
android:id="#+id/caseItemPastScroll"
android:fillViewport="true"
android:padding="10dp"
android:layout_below="#id/caseItemScrollSpacer"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<RelativeLayout android:id="#+id/caseItemListPastLayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</RelativeLayout>
</ScrollView>
</RelativeLayout>
That should be a fully "working" example, with the addition of
<color name="background">#3E3E3E</color>
in the strings.xml
onCreateDialog() is only called once, while onPrepareDialog() is called each time the Dialog opens.
Override onPrepareDialog() and call ((AlertDialog) dialog).setMessage() here.

Change title size of alertdialog in android?

about text in the message there is not problem. I can't change (personalize) color, font, style of Title of alertdialog?
What's the way?
thanks!
About font size you can use this:
SpannableStringBuilder ssBuilser = new SpannableStringBuilder("Sample");
StyleSpan span = new StyleSpan(Typeface.ITALIC);
ScaleXSpan span1 = new ScaleXSpan(1);
ssBuilser.setSpan(span, 0, 5, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
ssBuilser.setSpan(span1, 0, 5, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(ssBuilser);
builder.show();
or
AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Hello world").show();
TextView textView = (TextView) dialog.findViewById(android.R.id.message);
textView.setTextSize(40);
Check out this link: Creating dialogs
This should help.
Or this Alert Dialog Builder
There is another way to set a new custom view to a dialog title. We can define every custom view such as TextView and add it some custom properties and set it to the dialog title:
AlertDialog.Builder builder = new AlertDialog.Builder(OrderItemsActivity.this);
TextView title_of_dialog = new TextView(getApplicationContext());
title_of_dialog.setHeight(50);
title_of_dialog.setBackgroundColor(Color.RED);
title_of_dialog.setText("Custom title");
title_of_dialog.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
title_of_dialog.setTextColor(Color.WHITE);
title_of_dialog.setGravity(Gravity.CENTER);
builder.setCustomTitle(title_of_dialog);
builder.create().show();
Here, I define a dynamic TextView and set some properties to it. Finally, I set it to dialog title using setCustomTitle().
Here is an full example how i would do it. See the last line of code.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(cntx);
alertDialogBuilder
.setMessage(Html.fromHtml(cntx.getString(R.string.UltimateDemoText)))
.setCancelable(false)
.setPositiveButton(cntx.getString(R.string.Ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
((TextView)alertDialog.findViewById(android.R.id.title)).setTextSize(TypedValue.COMPLEX_UNIT_SP, 9);
Let's take example of this customised Dialog. Below are the files used to make it :
res/layout/dialog.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"> <EditText
android:id="#+id/et_pc_forDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tv_pc_dialogTitle"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:ems="10"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:hint="Enter Something to search .."
android:singleLine="true"
android:textColor="#FFFFFF" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/tv_pc_dialogTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="5dp"
android:text="SEARCH"
android:textColor="#FFFFFF"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/bt_pc_goButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/et_pc_forDialog"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="GO"
android:textColor="#FFFFFF"
android:textSize="25sp" /> </RelativeLayout>
res/layout/activity_main.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/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="177dp"
android:text="Open Dialog" />
</RelativeLayout>
MainActivity.java :
package com.bhavit.stackoverflow;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.app.Dialog;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.app.Dialog;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button openDialog = (Button) findViewById(R.id.button1);
openDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Dialog dialog = new Dialog(MainActivity.this); // here write the name of your activity in place of "YourActivity"
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog);
TextView tv = (TextView)dialog.findViewById(R.id.tv_pc_dialogTitle);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/GOTHIC.TTF"); // here GOTHIC.TTF is the font-file I have pasted in the asset/fonts folder in my project
tv.setTypeface(tf); // Set the typeface like this
Button bt = (Button) dialog.findViewById(R.id.bt_pc_goButton);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Write here, what do you want to do on button click
}
});
dialog.show();
}
});
}
}
This is how you can make a customised dialog, and customise the components of that dialog. Here I have set a custom font to the title of the Dialog.

Back button in dialog

Am trying to get the back button in my dialog to go back to the original screen. I don't know if I have all the imports that I need. Can someone tell me where I am going wrong?
Java code:
package my.dlog;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
public class DlogActivity extends Activity {
/** Called when the activity is first created. */
Dialog dialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dialog = new Dialog(this);
dialog.setContentView(R.layout.main2);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onBackPressed() {
Intent intent = new Intent(DlogActivity.this, DlogActivity.class);
startActivity(intent);
finish();
}
public void onClick(View v) {
dialog.show();
}
});
}
}
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:minHeight="400dp"
android:minWidth="300dp" android:background="#drawable/mint1">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<ImageView
android:layout_width="236dp"
android:layout_height="220dp"
android:layout_marginRight="100dp" android:background="#drawable/carsee"/>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</RelativeLayout>
</LinearLayout>
b.setOnClickListener(new OnClickListener() {
public void onBackPressed() {
dialog.cancel();
// Simply Dismiss the dialog to make it close and return to back..
/*What you are using is not a valid construct */
}
Also make sure that button1 in in main layout as you have used findViewById(R.id.button1) directly for set content view
Well normally the back button works just without any help from us. If you take the
public void onBackPressed() {
Intent intent = new Intent(DlogActivity.this, DlogActivity.class);
startActivity(intent);
finish();
}
out, what happens when you press 'back'? If this is not what you want, then what do you wnat to happen? If there are no errors, I would think you have the required imports.
Cliff

dialog code wrong somewere?

Can someone help with this code. what am looking for is for the button on my dialog to go back a pagewhen the users press the button.so i dont have to press the phone back button thank you.
java file
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
public class DlogActivity extends Activity {
/** Called when the activity is first created. */
Dialog dialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dialog = new Dialog(this);
dialog.setContentView(R.layout.main2);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
Button b=(Button)findViewById(R.id.button1);
#SuppressWarnings("unused")Button button01 =(Button)findViewById(R.id.btn2);
Intent intent = new Intent(DlogActivity.this, DlogActivity.class);
startActivity(intent);
finish();
b.setOnClickListener(new OnClickListener() {
public void onBackPressed() {
dialog.cancel();
// Simply Dismiss the dialog to make it close and return to back..
/*What you are using is not a valid construct */
}
public void onClick(View v) {
dialog.show();
}
});
}
}
xml.code
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Button" />
<ImageView
android:layout_width="236dp"
android:layout_height="220dp"
android:layout_marginRight="100dp" android:background="#drawable/carsee"/>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</RelativeLayout>
</LinearLayout>
seems like the problem is at below code,
dialog.setContentView(R.layout.main2);
you cannot give layout like this, instead you need to inflate layout to your dialog.

Categories

Resources