This is how i added buttons in java file
btnAdd=(Button)findViewById(R.id.btnAdd);
btnDelete=(Button)findViewById(R.id.btnDelete);
btnModify=(Button)findViewById(R.id.btnModify);
btnView=(Button)findViewById(R.id.btnView);
btnViewAll=(Button)findViewById(R.id.btnViewAll);
btnShowInfo=(Button)findViewById(R.id.btnShowInfo);
Once you grab a reference to the button, you need to set an onClickListener to the button. For example:
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something after the add button is clicked
}
});
Additionally, make sure the android:clickable="true" flag is included in your XML file.
You're code seems to be fine, If you mean why your buttons aren't responding to touch, you need to set an onClickListener to each button, or add an onClick attribute your xml layout file, check this for more details
http://developer.android.com/training/basics/firstapp/starting-activity.html#RespondToButton
Related
I was having some problem when trying to check and uncheck radio button upon click.
What I am trying to do is, when the activity on load, the radio button is set to checked by default. When user pressed on the checked radio button, I set the radio button to uncheck and empty the textview. Then, when the user pressed on the radio button again, I set it to checked and set some text in textview. Here is my code:
#Click(R.id.radioButtonEmail)
void radioButtonEmailClicked(View v) {
if(radioButtonEmail.isChecked() == true){
radioButtonEmail.setChecked(false);
editTextEmail.setText("");
}else {
radioButtonEmail.setChecked(true);
editTextEmail.setText("TEST");
}
}
However, this only worked for the first time when I tried to uncheck the radio button. After I set it to false, when I try to check it again, it does not work. Any ideas? Thanks!
I don't have an exact explanation for the behavior you are seeing, but your logic looks off to me. I think you intend to do this:
#Click(R.id.radioButtonEmail)
void radioButtonEmailClicked(View v) {
if (radioButtonEmail.isChecked()) {
editTextEmail.setText("");
}
else {
editTextEmail.setText("TEST");
}
}
In other words, you don't manage whether the radio button is clicked from your event handler. Rather, you check for that state inside the handler, and then respond appropriately.
I am stucked in a problem developing an android App.
When the user clicks in a Button, I need to change the background colour of this button. But I need to do in a way that doesn't affects the style, mainly the shapes.
Okay as you have said without changing its style, i think here's what you want,
1.you might be interested in color filters like this
Button btn = (Button) findViewById(R.id.button1);
btn.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));
You use different values according to your required colour.If you want to know the constant values of colours, you can refer the documents.
2.you can programmatically set the shade of the entire button using the PorterDuff multiply mode. This will change the button colour rather than just the tint.
For example for a red shaded button
btn.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);
For a green shaded button
btn.getBackground().setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY);
And so on.What it actually does is, it works by multiplying the current button colour value by your colour value.
3.You can also use an online tool like this Android Button Maker to customize your button and use android:background="#drawable/custom_btn" in your layout(inside the <Button> tag) to define the customized button.
Now there are many more ways too to achieve what you want but i think these are some easy and quick fixes you can use.Hope this helps.
Try:
Button myButton = (Button) findViewById(R.id.myButton1);
myButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
myButton.setBackgroundColor(color.Green);
}
});
Try this:
Button btn = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
btn.setBackgroundResource(color.lightblue);
}
});
The easiest way of changing a buttons background is using a state list drawable. This is a xml file where different drawables for the background of the button for different states are defined. An Example:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true"
<item android:drawable="#drawabel/normal"/> // Drawable in normal state
<item android:drawabel="#drawable/pressed"/ android:state_pressed="true"> //Drawabel when pressed
</selector>
Just use android:background="#drawable/background.xml" in the buttons xml or setBackground(R.drawable.background). The framework will do the work for you.
Basic question but i have checked a number of places and can only find information on pop up messages like toast etc.
How would I be able to link buttonA (stored in fragment xml file) so that it will view activitypage.xml?
Thanks!
One thing you can do make a new Activity for each "Screen". Let's make an example, MyNewActivity. The rest is pretty simple. In your button XML add this line:
android:onClick = "nextActivity"
Then in the Fragment that has contains buttonA, do something like this:
public void nextActivity (View v)
{
Intent intent = new Intent (getActivity(), MyNewActivity.class); //using getActivity since this is from a fragment
getActivity().startActivity (intent);
}
make sure that in MyNewActivity you put this line in onCreate
setContentView(R.layout.activitypage);
In your Activity which inflates your button implement a method to open the desired second Activity when the button is clicked. Either button.setOnClickListener() of a method declared in the xml for the button under the android:onClick="buttonClick" attribute.
I have set a content view in Android with:
setContentView(R.layout.activity_main);
Now after one of the buttons is clicked, the following code is executed to enable another button:
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
buttonPause.setEnabled(true);
...
This enables the button. BUT only after a minute or so.
Do I need to refresh the button or layout? Or is that bad practice? I am wondering what causes this delay. I have read about notifyDataSetChanged(), but I do not think that is the right method.
notifyDataSetChanged() has nothing to do with Buttons, but with Adapters.
Did you try to add a buttonPause.invalidate() right after enabling it ?
My application has an intro page made up of some text and image elements in a relative layout. I would like to be able to click any part of the screen and have it go to the next activity. Is it possible to use a entire relative layout as a button? If so how would you do this?
You can add android:clickable="true" to the XML for your RelativeLayout and use a standard OnClickListener as you would for a button.
Depending on what you're trying to do (perhaps touching anywhere to dismiss a screen?), you could also look into extending onTouchEvent(MotionEvent event) in your Activity, which would pick up any touches in the entire activity that were not responded to by views.
You can grab the root view as follows and add a click listener to it:
findViewById(android.R.id.content).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//make your call to startActivity();
}
});
This should require less maintenance than retrieving a specific layout.
Add android:onClick="myFunction" in the RelativeLayout of the XML file and make the following function in the corresponding Activity file:
public void myFunction(View view)
{
...
}
I think you will have to add android:onClick="myFunction" for all the nested XML tags too which are nested inside the main RelativeLayout.