I have 2 buttons to switch between 2 layouts in the same activity: clicking button1 on layout1, it goes to layout 2 (using setContentView). On layout2, clicking button2, it goes back to layout1. Then button1 is no longer responding OnClickListener. I looked into "Input Events" but still couldn't figure it out. What happened and how to fix it?
Thanks in advance!
Button submitBtn;
Button backBtn;
submitBtn = (Button)findViewById(R.id.button1); //on layout1
backButn = (Button)findViewById(R.id.button2); //on layout2
submitBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.layout2);
}
});
backBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.layout1);
}
});
You should re-assign listeners when you switch layout, cause when you're calling setContentView old view is destroyed, and new components are created.
You MUST set the contentview when it is time to change the layout, otherwise the views will be null.
Related
Writing a simple program that has 2 windows that switch from one to the other at a button press. At first I managed to do this with two layout files in the same XML and just hide one and show the other, but it was near impossible to make a UI this way since the preview window would just bug out and not show what i added to the bottom layer ( pic related: ) http://i.imgur.com/7RsP9t7.png
So I thought instead, I would make 2 XMLS and have 1 layout in each and now it was easy to make a UI but the code on the other hand does not work.
java.lang.NullPointerException # btn2.setOnClickListener(new View.OnClickListener() which doesnt seem to unlogical, guess u cant listen to a button that isnt active or?
public class MyActivity extends Activity {
Button btn1, btn2;
LinearLayout layHome, layAddNumer;
RelativeLayout layAddNumber ;
EditText ph0ne;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
btn1= (Button) findViewById(R.id.buttonGoTonumber);
btn2= (Button) findViewById(R.id.buttonAddNumber);
layHome = (LinearLayout) findViewById(R.id.layHomeddddddd);
layAddNumber = (RelativeLayout) findViewById(R.id.layyPhone);
ph0ne = (EditText) findViewById(R.id.phoneNumberText);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.phonelayout);
//layHome.setVisibility(View.GONE);
//layAddNumber.setVisibility(View.VISIBLE);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.activity_my);
}
});
xml: This site does not let me add XML files. But btn1, LinearLayout layHome is in activity:my while Button btn2, RelativeLayout layAddNumber and ph0ne is in my second XML file
Sorry for the weird question, but is it possible to click on two buttons at the same time in android(having two logs, "clicked on b1" and "clicked on b2"), if one totally covers the other one?
This is not ordinarily possible; the top button will absorb the button click event and not pass it on to the one behind it. It is not clear whether or not you want to obtain this behaviour or avoid it, nonetheless, you can force it by propagating the event manually across the click listeners.
Here is one way (there are a few); assume buttonOne is on top of buttonTwo:
final Button buttonOne = (Button) findViewById(...);
final Button buttonTwo = (Button) findViewById(...);
buttonOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ButtonTest", "ButtonOne clicked");
buttonTwo.performClick();
}
});
buttonTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ButtonTest", "ButtonTwo clicked");
}
});
The click event enters the listener on button one, which then causes a click event on button two.
Here is another way which would work (and could be changed to support long click events easily):
final Button buttonOne = (Button) findViewById(...);
final Button buttonTwo = (Button) findViewById(...);
final OnClickListener listenerTwo = new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ButtonTest", "ButtonTwo clicked");
}
};
final OnClickListener listenerOne = new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ButtonTest", "ButtonOne clicked");
listenerTwo.onClick(buttonTwo);
}
};
buttonOne.setOnClickListener(listenerOne);
buttonTwo.setOnClickListener(listenerTwo);
Yes, it is possible. You will need to pass the click event that occurs on the foreground view to the background view. You can do this by checking where the click occurs and if it occurs within the view's bounds.
Here is the scenario : I have a gui which contains two buttons.Now is there any way by which second button is clickable only after first button is clicked ?
say you have activity with two buttons defined in the xml layout : button1 and button2
in activity onCreate method write:
button2.setEnabled(false);
In the in click listener of first button write
button2.setEnabled(true);
so finally
in onCreate method of the activity we have
button2.setEnabled(false);
private OnClickListener l = new OnClickListener() {
public void onClick(View v) {
button2.setEnabled(true);
}
};
button1.setOnClickListener(l);
I want to remove a Button in his own onClick method after it's clicked. I tried it with the normal way:
layout.removeView(save);
But the button will not be removed and I get no error. If I want to add the Button I get an error because the button already excists.
I think it isn't working because I trie to remove the button during his OnClickHandler is active. So my Question is how can I remove the button after he is clicked?
Here's the complete, fully tested solution:
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
view.setVisibility(View.GONE);
}
});
You can also completely remove the view from the layout like this (also tested):
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
ViewGroup parentView = (ViewGroup) view.getParent();
parentView.removeView(view);
}
});
Try to set its state with button.setVisibility(Visibility.GONE)
How about just hide it? e.g. in your button onclick handler you can do something like:
button.setVisibility(View.GONE);
i set my activity theme to translucent so as to see through to the underneath activity window.
I want to know if it's possible to enable click event when user tap on empty area on this translucent activity?
Thanks,
dara kok
It is possible to add click event to your activity. You need to do as below:
You could have done setContentView(R.layout.main); in onCreate() of your activity.
In main.xml, give some id to the root layout. For eg.,
Lets consider you have root as LinearLayout with id set as below,
Then in onCreate() of your activity, you will have to do the following:
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Overriding this would work:
http://developer.android.com/reference/android/app/Activity.html#onTouchEvent(android.view.MotionEvent)
However i think it's your translucent activity that will get the taps, not the one visible under it.
You can add OnClickListener on parent view of your layout.
For example, add android:id="#+id/some_id" to your parent LinearLayout in main.xml.
Then add this code after setContentView in onCreate method:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.some_id);
FrameLayout frameLayout = (FrameLayout) linearLayout.getParent(); // Get parent FrameLayout
frameLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed(); // Close activity, for example
}
});
linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// empty block for prevent frameLayout click event, if you need
}
});