I am trying to do a simple thing in my app.
There are three button that take the user to three different screen. Once the user clicks the button and comes back to the activity I want the focus to stay on the button that the user had left before.
I tried saved instance state but its not working.
I've done the same in one of my apps.
btn1,btn2,btn3 are 3 buttons in main activity. On click of them, I'm navigating to deifferent activities.
btnenable(the red one) and btndisable(the grey one) are two images used for displaying highlighted button.
This is the snapshot:
btn1.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
btn1.setBackgroundResource(R.drawable.btnenable);
btn2.setBackgroundResource(R.drawable.btndisable);
btn3.setBackgroundResource(R.drawable.btndisable);
}
});
btn2.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
btn2.setBackgroundResource(R.drawable.btnenable);
btn3.setBackgroundResource(R.drawable.btndisable);
btn1.setBackgroundResource(R.drawable.btndisable);
}
});
btn3.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
btn3.setBackgroundResource(R.drawable.btnenable);
btn2.setBackgroundResource(R.drawable.btndisable);
btn1.setBackgroundResource(R.drawable.btndisable);
}
});
Hope it hepls !!!Feel free to ask your query !!
Related
I am trying to remove a button when the button itself is tapped, I am trying the following:
View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
tagsView.removeView(button);
}
};
}
This code is working, but when I add the following line of code:
editText.setText(button.getText());
The code stops working and the button does not get removed. I add it like so:
View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
editText.setText(button.getText());
tagsView.removeView(button);
}
};
}
What is the problem here?
use this in your OnClick method
button.setVisibility(view.GONE);
Your code will look like this
View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
editText.setText(button.getText());
button.setVisibility(view.GONE);
}
};
}
Or Try this
Button mybtn = (Button)findViewById(R.id.mybtn_id);
mybtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mybtn.setVisibility(view.GONE); // or (view.INVISIBLE)
}
});
Depending on what you're trying to achieve, something like deejay proposed would work just fine. If want the button to hide, call button.setVisibility(View.INVISIBLE). However, if you are trying to dismiss it completely from the view hierarchy, call button.setVisibility(View.GONE).
just set button visibility to false
Obviously button.setVisibility(View.GONE) comes to mind but if it doesn't work you should look one level above for the source of the bug. Maybe you don't set OnClickListener you created to the button and hence nothing happens?
Now my question is this, is there any way to apply any default animation when each of the layout is set to visible? Instead of appearing all of a sudden, any sliding animation would look sleek.
//to show home page
homeButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
liMenu.setVisibility(View.VISIBLE);
}
});
//to show .net page
dotnetButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
liDotnet.setVisibility(View.VISIBLE);
}
});
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.
This is my first post on the site, and I'll try to be just as specific as the tips requested. I'm using eclipse Helios, with the ADT 10.0.1
I've been working on an Android application which is supposed to be a general guide of Starcraft II. Pretty basic stuff, it's for a programming class. It's supposed to have an intro background with a continue button, linking to the main menu.
The main menu consists a few buttons that should all link to different layouts created. When I start my application in an emulator (I tried level 12, 9 etc.) the first button links to the menu, but the buttons on the menu fail to link. I have no syntax errors in my code, however it does show the whole yellow underline for all the buttons except the first one. I've fiddled with the basic syntax a bit on and off to get it to not display any red or yellow underlines, and it didn't get me anywhere either.
When I removed the last button from the code, which is a back button from the layouts, linking back to the front menu, the buttons on the menu instead started linking to the last button's link, by which I mean the last one in the code. So I thought it might be skipping all of the other listeners and just using the one for the last button in the code, or something like that. Bear in mind I'm not very good at programming yet.
Here's the basic look of the code.
package lol.lol;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ofk extends Activity {
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle button1) {
super.onCreate(button1);
setContentView(R.layout.intro);
Button button = (Button) findViewById(R.id.continuebutton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.frontmenu);
}
});
final Button button2 = (Button) findViewById(R.id.aboutapp);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.aboutsc2g);
}
});
final Button button3 = (Button) findViewById(R.id.aboutsc);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.aboutsc2);
// Perform action on click
}
});
final Button button4 = (Button) findViewById(R.id.micro);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.micro);
// Perform action on click
}
});
final Button button5 = (Button) findViewById(R.id.macro);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.macro);
// Perform action on click
}
});
final Button button6 = (Button) findViewById(R.id.mechanics);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.mechanics);
// Perform action on click
}
});
final Button button7 = (Button) findViewById(R.id.zergbasics);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.zergbasics);
// Perform action on click
}
});
final Button button8 = (Button) findViewById(R.id.terranbasics);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.terranbasics);
// Perform action on click
}
});
final Button button9 = (Button) findViewById(R.id.protossbasics);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.protossbasics);
// Perform action on click
}
});
final Button button10 = (Button) findViewById(R.id.backbutton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.frontmenu);
// Perform action on click
}
});
So my question being, how do I get the buttons on the layout frontmenu to link where they're supposed to? Are there any syntax errors or things I need to add to this code snippet to make it function properly?
Thanks in advance!
well, let me first say: this is not the way it was meant by the android sdk. you gotta have an activity for every change of a screenful of content, which is realized by switching of contentview here. maybe look here, if you haven´t done so: http://developer.android.com/guide/topics/fundamentals.html
the right way (with activities), it´s possible for the users to go back with the system-backbutton, so there´s basically no need to have a backbutton, and users will expect the system-backbutton to work like that. now, pressing the system-backbutton causes the app to 'close' and to show what was opened before.
however, it is possible to do it like you tried it to. here´s a short snippet:
public class Start extends Activity {
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
LayoutInflater mLayoutInflater = getLayoutInflater();
final LinearLayout mLinearLayoutIntro = (LinearLayout) mLayoutInflater.inflate(R.layout.intro, null);
setContentView(mLinearLayoutIntro);
final LinearLayout mLinearLayoutFrontMenu = (LinearLayout) mLayoutInflater.inflate(R.layout.frontmenu, null);
Button continueButton = (Button) findViewById(R.id.continuebutton);
continueButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(mLinearLayoutFrontMenu);
}
});
final LinearLayout mLinearLayouAboutSc2g = (LinearLayout) mLayoutInflater.inflate(R.layout.aboutsc2g, null);
final Button aboutAppButton = (Button) mLinearLayoutFrontMenu.findViewById(R.id.aboutapp);
aboutAppButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(mLinearLayouAboutSc2g);
}
});
final Button backButton = (Button) mLinearLayoutFrontMenu.findViewById(R.id.backbutton);
backButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(mLinearLayoutIntro);
// Perform action on click
}
});
super.onCreate(savedInstanceState);
}
}
you see, there´s quite a bit difference to your code. first error in your code was to bind the click listeners only to the first button (possibly copy/pasted and then forgot).
second, if you want to solve it like this, you have to manually inflate your layouts. otherwise you will get null pointers everywhere, because these layouts and its childs (buttons etc) are instantiated lazily (only when they are needed e.g. called in setcontentview).
and third: well...really, do your app with one activity for every screenful of content :)
I'm having a button in a sliding drawer in a Android Application. The problem is it does not seem to react to any clicks as normal buttons do.
I'm guessing the problem is that it's a different view than buttons on the normal view.
If I implement a button the normal way like this
myAgenda = (Button)findViewById(R.id.BtnMyAgenda);
myAgenda.setOnClickListener(this);
public void onClick(View v) {
switch(v.getId()){
case R.id.BtnMyAgenda:
test.setAnimation(leftLeft);
test.startAnimation(leftLeft);
break;
}
I'm guessing there is something wrong with the above code since the button is in a SlidingDrawer and not in the "normal" view.
Any ideas how to fix the problem?
Here is the code
Register with event listner like below code
button.setOnClickListener(clickButtonListener);
and create this listner for button
private OnClickListener clickButtonListener= new OnClickListener()
{
#Override
public void onClick(View v)
{
if(v == button)
{
}
}
}
I actually found the solution to the problem, I simply created a new view.onclicklistener specific to that button.
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});