How to use Dialog OnClick Listner in Android? - android

I am new to Android development. Please excuse me If my question is very simple.
I have tried to create a button on my Android Layout view using XML. Now within the Activity class I am trying to get the button and add a on click listner to it. This is working fine without any issues.
On similar lines on the button click i explained previously I have a dialog being popped up. In this dialog I have a ImageButton. On click of this Image button I am trying to set a on click listner using the below code.
The Activity on create is as below
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Button button = (Button) findViewById(R.id.btnAdd);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
final Button btnAdd = (Button) findViewById(R.id.btnAdd);
if(v==btnAdd) {
dialog = new Dialog(this);
dialog.setContentView(R.layout.add_dialog);
dialog.setTitle("Test Title.");
dialog.setCancelable(true);
dialog.show();
final ImageButton button = (ImageButton) findViewById(R.id.imageButton1);
try {
Log.i("Log","1");
button.setOnClickListener(this);
Log.i("Log","2");
}
catch(Exception e)
{
Log.i("Log","3");
dialog.dismiss();
//Dialog d = new Dialog(this);
//d.setTitle("test.");
Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG).show();
Toast.makeText(this,e.getLocalizedMessage(),Toast.LENGTH_LONG).show();
Toast.makeText(this,e.toString(),Toast.LENGTH_LONG).show();
Log.i("Log","4");
//d.show();
Log.i("Log","5");
}
}
}
In the above I get the Log in this sequence. 1,3,4,5. I dont get the 2. In the toast i get message of blank, blank followed by java.lang.Nullexception.
But this when executed gives me a force close pop up. Please advice how to do this. Or is there any workaround for the same? I need to have a dialog box to come on a button click, and then within the dialog I need to have more than one option of buttons. For each of buttons in the dialog I need to perform different activities. Any kind of help or advice is appreciable. Thank you in advance for your time and help.

Most probably you are trying to retrieve the button from the Activity class. It returns null because this method will only retrieve resources attached to the Activity (by using the method setContentView).
You have two options:
You can inflate the dialog layout using a LayoutInflater
If you are extending the Dialog class, add the listener inside that class instead.
Edit after the update:
As I said above, the problem is:
final ImageButton button = (ImageButton) findViewById(R.id.imageButton1);
because imageButton1 is not part of the layout in the activity. Just replace it by:
final ImageButton button = (ImageButton) dialog.findViewById(R.id.imageButton1);

Related

Button to hide a TextView

I've been searching for a solution for this for a while but cannot seem to get one working. There are one or two on here about this subject but I can't seem to get them going. I'm also a novice in Android and while I've been on and off playing with it for a few years, I still understand next to nothing about what I'm writing.
Basically I've got a TextView and a button. Ideally I'd like to put some text in the TextView, press a button it's gone, press the button again and it's back.
I've narrowed it down to needing to understand what findViewById(R.id.button2) does but honestly I'm a bit lost.
I've added my button code but apologies that this is such a noob question
public void onClick(Button v){
TextView t1 = (TextView)findViewById(R.id.editText);
v.setVisibility(View.GONE);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView t1 = (TextView)findViewById(R.id.TextView);
v.setVisibility(View.GONE);
}
});
}
Your code has a couple of issues. I'm not going to give you the code because that won't really help you learn. Instead I'll explain things and let you try to figure it out or come back with more explicit questions.
You know that xml file you set using setContentView? Some of the tags in it had a property android:id="xxxx". That xxxx is the id of that view, its used so you can find that view in your code. The findViewById function walks through all the views on screen and finds a view with that id and returns it. That gives you a reference to the view so you can change it. For example, you can set its visibility, set its background color, or set an OnClickListener.
So to have a button toggle the visibility of another view, you need to be able to do the following things:
1)Find the view who's visibility you want to change
2)Figure out what its visibility currently is
3)Figure out what you want it to be (the opposite of what it currently is
4)Set that visibility
You need to write a function that does all that. Then you need to do this
1)Find the button you want to use to change the visibility
2)Tell it to call your function when its pressed.
Figure out how to do each of those steps individually, and you should be able to put it together. Good luck.
findViewById(R.id.button2) finds the view with the id button2.
You can check inside onClick whether t1 is visible or not (t1.setVisibility(View.GONE); not v.setVisibility(View.GONE);), and toggle between View.GONE and View.VISIBLE.
Remember that your findViewById() should have a real id. They are normally set on the activity_name.xml.
You are using a onClick inside a onClick. Personally I recommend setting the listener manually with setOnClickListener.
There's a lot of work for you, start with these tutorials. Keep trying and try to understand what you are doing.
Look like you need a toogle button feature, here is a piece of code.
Important: you must pay heed to #GabeSechan and #SkyDriver2500 answers.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
//your other code
Button button = (Button) findViewById(R.id.button2);
final TextView t1 = (TextView) findViewById(R.id.editText);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
t1.setVisibility(t1.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
}
});
}
I'm not sure if the code will help you now. But just in case, here it is
final boolean[] isTvVisible = {false};
final TextView t1 = (TextView)findViewById(R.id.editText);
t1.setVisibility(View.GONE);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (isTvVisible[0]) {
t1.setVisibility(View.GONE);
isTvVisible[0] = false;
} else {
t1.setVisibility(View.VISIBLE);
isTvVisible[0] = true;
}
}
});

Android - setOnkeyListen method how does it work with EditText?

I create a dialog window in an activity, then I put the buttons in this dialog window and listened to them. However, I want to listen to android device found in the back button. and dialog.setOnkeyListen method I used for it. I have put the program by executing dialog window EDITTEXT not enter data into the field. So when I add code to the setOnkeyListen I can not enter data into the EditText field. I hope you know what I mean
If you're asking how to set the OnClickListener for a textview here is an example hope this helps:
TextView textView = (TextView) findViewById(R.id.textView);
textView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// Code you want to execute on the click goes here
}
});

Why one button can add multiple ClickListeners

I usually code
Button button1 = (Button)findViewById(R.id.start1);
button1.setOnClickListener(mStart1Listener);
Button button2 = (Button)findViewById(R.id.start2);
button2.setOnClickListener(mStart2Listener);
But in android sample, I found these in ServiceStartArgumentsController.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.service_start_arguments_controller);
// Watch for button clicks.
Button button = (Button)findViewById(R.id.start1);
button.setOnClickListener(mStart1Listener);
button = (Button)findViewById(R.id.start2);
button.setOnClickListener(mStart2Listener);
button = (Button)findViewById(R.id.start3);
button.setOnClickListener(mStart3Listener);
button = (Button)findViewById(R.id.startfail);
button.setOnClickListener(mStartFailListener);
button = (Button)findViewById(R.id.kill);
button.setOnClickListener(mKillListener);
}
What's the difference between them, and why one button can add multiple ClickListener
What's the difference between them, and why one button can add multiple ClickListener
A View can only have one OnClickListener. The second approach is just re-using the variable button, notice the third line:
Button button = (Button)findViewById(R.id.start1);
button.setOnClickListener(mStart1Listener);
button = (Button)findViewById(R.id.start2); // This one
It overrides the previous value of button with a new Button and the new Button will be assigned the next OnClickListener.
That's not one button. It changes before each setOnClickListener; The code just uses one variable for different buttons on that activity.

Android activity crashing immediately after start

I've been working on developing an app for a while, and everything was going well. However, when I added an onCLickListener for a button in one of my .xml files(not main.xml), it started crashing as soon as I tried to open it in the emulator. All the code for the onClickListener is the same as for the other two buttons I have. here's the code for my problematic listener:
Button button3 = (Button)findViewById(R.id.button3);
button3.setOnClickListener(bt3listener);
private OnClickListener bt3listener = new OnClickListener(){
public void onClick(View v){
setContentView(R.layout.main);
}
};
The only way that any of this code differs from my other onClickListeners is that it references a button in a different file than main.xml. It crashes before displays any of the ui except a black screen with my project name on the top, and gives me a prompt that says "un
for a button in one of my .xml files(not main.xml),
It have to be int the layout you set with setContentView(), in order to retrieve it with findViewById() using this or use an inflater in order to get the layout in which the Button is declared, and use the returned view with findViewById().
viewReturnedByInflater.findViewById()
You need to initialise the OnClickListener before you assign it to the button.
Try this
private OnClickListener bt3listener = new OnClickListener(){
public void onClick(View v){
setContentView(R.layout.main);
}
};
Button button3 = (Button)findViewById(R.id.button3);
button3.setOnClickListener(bt3listener);
It gives you the error because when Button button3 = (Button)findViewById(R.id.button3);
it tries to find the button, but since the button is in another xml layout, it crashes.
You should infalte the other layout or add the button on the main layout

In Android: How do you show layout twice using intent?

Let's pretend this was my Java Class...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button ScreentwoGameButton = (Button) findViewById(R.id.screentwo);
ScreentwoGameButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent ScreentwoGameIntent = new Intent(Main.this, Screentwo.class);
startActivity(StartGameIntent);
}
});
How do i use this code below but the right way like.
So let's put an example if I click screentwo button the screentwo.xml will show and it will allow me to click inside if any buttons are available. Instead just stare what's in the layout.
I don't want to use the Activity to activity cause the whole point is i'm trying to avoid the flashing looking feel going to another java class.
If you look at the moron test game on Android it says example: press the blue button then red and then green, so if u press the blue button the screen will remain and not flash at all but the image of the blue button will disappear and I'm allowed to click the red and then green.
Hope that helped.
Thanks
Wahid
Button ScreentwoButton = (Button) findViewById(R.id.screentwo);
ScreentwoButton.setOnClickListener(new OnClickListener() {
private Uri Uri;
#Override
public void onClick(View v) {
setContentView(R.layout.Screentwo);
Uri uri=Uri;
Intent i=new Intent(Intent.ACTION_VIEW, uri);
mSoundManager.playSound(1);
}
});
try to use:
setContentView(R.layout.next layout); in your button click.
You could use the viewflipper class and add the different layouts as childs to the viewflipper
and set the active child. Using setcontentView will be trouble some when you use findViewById for a old layout. As findViewById will look in the layout that is specified by setContentView

Categories

Resources