Programmatically close dialog opened by theme.dialog - java

I have two xml files, and two java files. In the first xml I have few buttons, and one of them is EXIT. In the java file i write in the onCreate:
Button exitButton = (Button) this.findViewById(R.id.button_exit);
exitButton.setOnClickListener(this);
Then further down the code I write:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_exit:
Intent switchtoExit = new Intent(StartActivity.this, ExitActivity.class);
startActivityForResult(switchtoExit, MESSAGE_REQUEST);
break;
}
}
the second java files is called ExitActivity.java. In the manifest file I wrote:
<activity android:name=".ExitActivity"
android:label="#string/exit_title"
android:theme="#android:style/Theme.Dialog"/>
In order to make the second xml file popup like a dialog.
My second java file is(the one that popup like a dialog):
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ExitActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_exit);
Button noExitButton = (Button) this.findViewById(R.id.exit_no_button);
noExitButton.setOnClickListener(this);
Button yesExitButton = (Button) this.findViewById(R.id.exit_yes_button);
yesExitButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.exit_no_button:
Toast.makeText(this, "Good Choice :-D", Toast.LENGTH_SHORT).show();
break;
case R.id.exit_yes_button:
Toast.makeText(this, "So sad... \nnice playing with you...", Toast.LENGTH_SHORT).show();
break;
//break;
}
}
}
and my second java file has:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/red">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/exitTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/exit_body"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/exit_no_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/exit_no_button"
android:layout_weight="1.0"
/>
<Button
android:id="#+id/exit_yes_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/exit_yes_button"
android:layout_weight="1.0"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
What I wanted to do is to have one button close the dialog, and the other button to stop the application. I tried finish(), and it worked just fine. but then when I added the onDestroy() method one button would restart the application, and the other just close it. Also, when I pressed the Back button, it would close the application.
Can anyone explain me how to get the following to happen:
When the dialog pops up, and I press the back button, it just closes the dialog?
When I press on exit button, it closes the application.
When I press on the Stay button it closes the dialog.
Thanks

You're doing this the wrong way. Using a second activity is overkill. Use an AlertDialog, which you can easily make with AlertDialogBuilder. Set it to have a positive and a negative button. Set an OnClickListener for the positive button that calls finish on the only activity. There you go- 1 activity, and the dialog class itself will take care of popping up and closing itself.

Related

Android buttons in xml with onClickListeners, have to click a second time for the button to Click

Have tried various solutions including those in here:
Have to click a button twice for it to work in Android Studio and here:
I have to click the button twice for it to work
And have tried " android:focusableInTouchMode="false" " on the buttons in the xml file which did not work either.
My current code is as follows:
SplashActivity.java
//THIS BIT IS IN THE ONCREATE METHOD
Button enterButton = (Button) findViewById(R.id.enter_button);
enterButton.setOnClickListener(OnClick);
Button bookButton = (Button) findViewById(R.id.book_button);
bookButton.setOnClickListener(OnClick);
}
//THIS BIT IS OUTSIDE OF THE ONCREATE METHOD
private OnClickListener OnClick = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
switch (v.getId()) {
case R.id.enter_button:
handler.removeCallbacksAndMessages(null);
startActivity(intent);
break;
case R.id.book_button:
handler.removeCallbacksAndMessages(null);
intent.putExtra("source", "onClick");
startActivity(intent);
break;
default:
break;
}
}
};
}
activity_splash.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="#+id/enter_button"
android:theme="#style/AppTheme.DevButton"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Enter"
android:stateListAnimator="#null"
/>
<Button
android:id="#+id/book_button"
android:theme="#style/AppTheme.DevButton"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Book"
android:stateListAnimator="#null"
/>
</LinearLayout>
Really can't work out how to work around this!
UPDATE to - activity_splash.xml
So I've narrowed this down to the Java file, it doesn't seem to be the xml code causing the issue as I've trimmed down the xml file to the following and the symptoms are the same:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="#+id/enter_button"
android:layout_width="100dp"
android:layout_height="50dp"
/>
<Button
android:id="#+id/book_button"
android:layout_width="100dp"
android:layout_height="50dp"
/>
</LinearLayout>
I would normally extend SplashActivity to include View.OnClickListener and implement
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
#Override
public void onClick(View view_) {
...
}
Not sure if this would make a difference or not?
I think the focus is the parent view so add the android:descendantFocusability= attribute to your parent view
So after all of the testing I've managed to find out what the problem was.
It was this line of code here which helps with display the image as full screen:
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
The SYSTEM_UI_FLAG_HIDE_NAVIGATION flag does not register touch events so you need to change it to SYSTEM_UI_FLAG_IMMERSIVE so it reads as follows:
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_IMMERSIVE);
Note however this flag was only released after Android 4.4 API Level 19 so won't work in Android versions before this.
Thanks guys for looking into this as well for me, most appreciated.

How do i create a button to another page for my app

i was just wondering how i would make a button on my page so that it would go to another page for my app. I am a beginner so if you could explain how it all works and where it goes it would help a lot.
PS i am using android studio if that makes a difference and this is the code i have so far in my fragment_main.xml. I have not entered any code into the .java
<TextView android:text="#string/hello_world"
android:id="#id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/firstbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/homebutton"
android:layout_below="#+id/text"/>
You can declare views and objects dynamically in Java, and then pass the button from fragment to fragment (or Activity to Activity, depending on your app).
To declare a Relative Layout with a Button, for example:
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.RelativeLayout;
public class JavaLayoutActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button myButton = new Button(this);
RelativeLayout myLayout = new RelativeLayout(this);
myLayout.addView(myButton);
setContentView(myLayout);
}
This doesn't set the properties or anything, I am just using this as proof of concept.
XML makes things easy with regards to user interface design because you don't have to manage it in code, but this is one case where it is an exception. If you want dynamic interface objects, you need to use Java.
Instead of creating view dynamically you should obtain your view in activity
ImageButton button = (ImageButton) findViewById(R.id.firstButton)
and assign an onClick listener to id
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// start new Activity here
}
});
You can also do it in xml:
<ImageButton
android:id="#+id/firstbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/homebutton"
android:onClick="sendMessage"
android:layout_below="#+id/text"/>
with such configuration, you should add method in activity:
public void sendMessage(View view) {
// start another activity here
}
There are two methods available in android using which you can go from one Activity to another.
1. Use button.setOnClickListener()
Create a button in xml file.
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
Now set event listener for the button in your .class file
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//set the event you want to perform when button is clicked
//you can go to another activity in your app by creating Intent
Intent intent = new Intent(getApplicationContext, Activity2.class);
startActivity(intent);
}
});
2. Use <android:onClick="goNext">
Put the onClick as the attribute of the button you have created in xml file.
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="goNext" />
Now in your .class file define an event for that button as,
goNext() {
Intent intent = new Intent(getApplicationContext, Activity2.class);
startActivity(intent);
}

how to use timepicker to make alarm clock

Right now I am making a simple alarm clock. It has to activities. One is main and the other is "test". On the main I have created one button to set an alarm. The button is called "new" and it lead to the second activity test. In test I have a time picker and two buttons. One called "cancel" which leads back to main activity. The other is "done" which should save the time that I have picked on the time picker. I have the done button working but I don't know how to do the rest. I am very new to programming. Right now I want to know how to safe the time that I have picked on the time picker so I can access it later on to compare it with the real time. I hope to save the time when I click the "done" button. Please be as specific as possible. I am really new to this. Thank you!!!!!!!!
below are the codes I have so far
activity_test.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=".Test" >
<TimePicker
android:id="#+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp" />
<Button
android:id="#+id/done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/timePicker1"
android:layout_centerHorizontal="true"
android:layout_marginTop="51dp"
android:onClick="doneButton"
android:text="Done" />
<Button
android:id="#+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/done"
android:layout_below="#+id/done"
android:layout_marginTop="52dp"
android:onClick="cancelButton"
android:text="Cancel" />
</RelativeLayout>
Test.java
package com.example.alarmclock;
import java.util.Calendar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TimePicker;
public class Test extends Activity implements View.OnClickListener{
Button cancelButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
cancelButton = (Button)findViewById(R.id.cancel);
cancelButton.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test, menu);
return true;
}
private void cancelButton(View v){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
public void onClick (View v){
switch (v.getId()){
case R.id.done:
cancelButton(v);
break;
}
}
}

Using onClick in button to a view

Just started learning Android this morning, need help on this. I have few buttons in my App, I want when a user clicks a button a image will be shown and back button to load main.xml .
Code:
In main.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"
android:orientation="vertical" >
<Button
android:layout_marginTop="40dp"
android:layout_width="100dp"
android:layout_height="80dp"
android:id="#+id/b1"
android:text="xyz"
android:background="#ff3375"
android:layout_marginLeft="20dp"
/>
<Button
android:layout_marginTop="40dp"
android:layout_width="100dp"
android:layout_height="80dp"
android:id="#+id/b2"
android:layout_toRightOf="#id/b1"
android:text="abc"
android:background="#ff3375"
android:layout_marginLeft="80dp"
/></RelativeLayout>
And in Activity.java
package com.sam;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class A2Activity extends Activity {
/** Called when the activity is first created. */
Button a,b;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
a= (Button) findViewById(R.id.b1);
b= (Button) findViewById(R.id.b2);
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});}}
Now, what do I need to add inside the onCLick method to open a image or a XML file and also a back button to return to main.xml
please explain your question a little further . what do you mean open an image or xml file? do you mean to create a bitmap ? do you mean to show it? if so , where? also , what do you mean that the back button will return to main.xml? main.xml is a layout file , not an activity .
in any case, maybe you meant that you wish to open an image in full screen upon pressing on the button ,and when clicking on the back button of the device go back to the activity you've created?
if so , you can create a new class that extends Activity , update the manifest so that it would be reachable , and start it (using startActivity) . in the new activity class , set the content view to be of an ImageView that shows an image , or of a layout file that has an imageView that shows an image .
for starting a new activity , you need to call :
startActivity(new Intent(CurrentActivity.this, NewActivity.class);
where "CurrentActivity" is the current activity that you are at (called "A2Activity" in your sample) and "NewActivity" is the one that shows the image.
as written before , do not forget to update the manifest.

My app is unable to start activity?

When i try to run the app on my phone,it force closes when i try to do activity 3.
Logcat says:
01-13 17:53:25.368: E/AndroidRuntime(3235): Caused by: java.lang.NullPointerException
01-13 17:53:25.368: E/AndroidRuntime(3235): at android.app.activity3.onCreate(activity3.java:18)
where line 18 is
Button wg = (Button) findViewById(R.id.Back);
heres my full code for activity3.java:
package android.app;
import android.app.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class activity3 extends Activity{
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
Button wg = (Button) findViewById(R.id.Back);
wg.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}
Thanks in advance
You need to modify your 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"
>
<!-- your textview -->
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/text1"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<!-- your back button -->
<Button
android:id="#+id/back"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/Back"
/>
</RelativeLayout>
What I did:
Removed text and orientation from your root RelativeLayout, these do nothing.
Modified your height and width, feel free to change these as you please.
Added a Button with id of R.id.back and text of #string/Back.
You will now be able to reference your button using findViewById(R.id.back) and set your click listener.
I don't see a button in the xml. And once you have that, it needs to have:
android:id="#+id/Back"
This is happening because you don't have a button declared in your view. Only a Textview. You must create the button in the view. You're referencing nothing hence the null.
You can find a nice example of how to make a button here.
You don't have button with id "Back" in xml posted, that is why you are getting null there. Add button entry in your xml.

Categories