I want to include two simple buttons in my activity, for example, "Start" and "End".
Now i want "Start" button to be selected by default (with any background color) when i run my activity. How to do that through java in Android studio?
Note:- i am not talking about radio button's small circle. I am talking about simple button.
use setBackgroundColor method.
btn1.setBackgroundColor(Color.GREEN);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// your code here
}
});
Related
I wanted to know if its possible to have two onclick methods for one buttton..Im trying to have a button that can open a new activity and send a id token to the server for firebase purposes, if possible how do i go about it on android studio
I think you are getting the underlying concept wrong.
Buttons react to clicks.
The "ActionListener" that gets triggered on that click ... can do whatever it wants. There is nothing (conceptually) that prevents you in your code to just trigger various things. Of course, you have to understand what you are doing (things like: not blocking the UI thread for too long; or how to kick of things in background threads, and so on).
No. There is only one onClick method for a Button. But you can still perform two different purposes by one button.
I am using a button to hide and show a linear layout. The code is given below :
final int[] count = {2};
//here startTopics is the button....
startTopics.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(count[0] %2==0)
{
topicLin.setVisibility(View.VISIBLE);
count[0]++;
}
else
{
topicLin.setVisibility(View.GONE);
//here topicLin is the linear layout
count[0]++;
}
}
});
It is one button and so you should apply only one onClick listener which performs the buttons job.
In your onClick-method you can just call another (private) method if you want to do multiple things without sacrificing code management.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
sendTokenToServer();
// Include your code to open the activity here or outsource it again into another private method
}
});
And your method to send the token to the server:
private void sendTokenToServer() {
// Your code here.
}
I've found numerous tutorials on how to accomplish this when you can grab the textview from the xml but nothing on how to implement such a function when you have multiple edit texts created programmatically that all need the same functionality.
After you create the EditText, you would add a click listener the same way you would to any other view. You may also need to disable the focusable attribute to prevent clicks from activating the keyboard:
editText.setFocusable(false);
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Open dialog here
}
});
If you have all EditText's, simply create one OnClickListener, call setOnClickListener on every EditText and pass the OnClickListener Object.
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
//open dialog here;
}
};
editText1.setOnClickListener(listener);
editText2.setOnClickListener(listener);
You should maybe store the EditText's in an array, so that you can just loop through all of them.
I'm trying to make a method that is activated when another method gives it an int, and at the same time, the method can also be activated by a view.
Here is the top line of the method and where in Java the method is called:
checkNum(theNumber, null);
public void checkNum (int num, View view){
I tried using "onClick" in the xml for a button, but checkNum did not appear as a suggestion and the app crashed when I ran it. How can I fix this?
Thanks so much!
When using the onClick attribute in XML, the correct signature to use is
public void checkNum (View view)
If you want to pass in a other parameters, I suggest that you set it in Java code.
Add a click listener to your button this way:
Button button = (Button) findViewById(R.id.your_button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something when the button is clicked
}
});
I am very new to mobile development, this isn't homework I am working ahead of my class, I developed a simple app that has a button and when it's pressed it shows a message "Hello Android". I would like to build on this and change the color of the background when the onClickListener is called, I will post my code below, I am asking for the best approach to achieve my goal (change background). I want to iterate that this code below works, and that I am not asking for anything to do with the code I have presented, I want to add to it to change the background color (it's currently white, I'm assuming by default). Oh and I have never worked with Java before (very difficult course teaching android/iOS/WinMobile in 1 class). Thank you.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupMessageButton();
}
private void setupMessageButton() {
// 1. Get a reference to the button
final Button messageButton = (Button) findViewById(R.id.helloDroidButton);
//Set the click listener to run my code.
//Code will run when user clicks button.
messageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Anonymous class? --> not sure what he means
Log.i("DemoButtonApp", "Hello Android!");
Toast.makeText(
MainActivity.this,
"Hello Android!",
Toast.LENGTH_LONG
).show();
}
});
}
Android support feature called Selector , that help you to change the background of any view in each state of it like pressed , forces and so one , take look on this useful tutorial and feed me back in any not obvious point
http://www.mkyong.com/android/android-imagebutton-selector-example/
hope it help you
i know the brainy people wont like my petty questions but im trying to learn
I,m trying to make a pairs game i have been using int so far on my apps but this game needs a different approach ive created the pairs game with ints but confusing code and a floor that pushing same button twice will delete the pair as below ive been trying with tags the code all looks clean as in no errors
public class MainActivity extends Activity {
//added Tag here for the if (pic2.getTag()==(beck));
Tag beck;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageButton pic1 = (ImageButton ) findViewById(R.id.imageButton1);
final ImageButton pic2 = (ImageButton ) findViewById(R.id.imageButton2);
pic1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pic1.setBackgroundResource(R.drawable.becks);
pic1.setTag(R.drawable.becks);
if (pic2.getTag() == pic1.getTag()){
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE);}
}});
pic2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pic2.setBackgroundResource(R.drawable.becks);
pic2.setTag(R.drawable.becks);
if (pic1.getTag() == pic2.getTag()){
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE);
}
}});
}}
ive tried since my original post to work out how to do ive shown code for 2 buttons all i want to do is compare and make invisible after the second button is clicked
if (pic1.getTag().equals(pic2.getTag())){
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE); }
the .equals crashes the app
pic1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pic1.setBackgroundResource(R.drawable.becks);
pic1.setTag(beck);
if (pic2.getTag()==(beck));{
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE);}
}});
this works with or without semi but both buttons dissapear when either button clicked
if (pic1.getTag()==(pic2.getTag())){
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE); }
this changes the image but the buttons don,t disappear when second image clicked trying to not use ints if possible
this line works with comma to
if (pic1.getTag()==(pic2.getTag()))
if (pic1.getTag()==(pic2.getTag()));
with effect of both button disappear on 1 click of either button dread moving to the else if lol
Also can a Tag be removed if the pair of images compared if false eg
if no match remove the button tag and reset all remaining images to Default image as when i put all 24 buttons on i need a reset method
i,m finding the semi colon at end of if statement has different effects to not having can anyone point correct way when and when not to use semi colons
Use tags for saving your image-id:
pic1.setTag(R.drawable.becks);
pic2.setTag(R.drawable.becks);
You can then check and compare those by calling getTag() on the buttons that have been clicked:
public boolean isMatch(View x, View y) {
return x.getTag() == y.getTag();
}