How can I make a component on my MainUI invisible by pressing a button? I want to dismiss a warning box on my MainActivity with a button that says "Close" or with a cross image.
view.setVisibility(View.GONE);
where view is the component you want to hide
Button crossButton = (Button) findViewById(R.id.crossImage)
Button button1 = (Button) findViewById(R.id.buttonOne)
//and so on
crossButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
button1.setVisibility(GONE);
//and so on
}
});
Related
I am developing an android application that consists of a 2 dimensional array of buttons. when i touch the buttons, at a time only one button is pressed. i want to press multiple buttons at the same time. please give me a sample code.
You can click a button programmatically by using the button.performClick() method inside your other onclicklistener button
To press another button(button2), just call button2.performClick() from button1 listeners onClick() method.
Here is an example:
Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "button 1 clicked", Toast.LENGTH_SHORT).show();
button2.performClick();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "button 2 clicked", Toast.LENGTH_SHORT).show();
// Do something
}
});
I am trying to make a Button change its background color and launch a new activity on click. Below is the code I wrote, but I am getting error message saying to declare 'Button btn1' as 'final Button btn1'. If I do that, the button change its color on click and launch another activity as I wished, but if returning to Main activity from the new activity, the once changed background color stays permanently. How can I improve my code? Thank you.
public class Home extends ActionBarActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_home);
Button btn1 = (Button) findViewById(R.id.button);
btn1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
btn1.setBackgroundColor(Color.GRAY);
Intent intent = new Intent(v.getContext(), activity_street.class);
startActivityForResult(intent, 0);
}
});
No need to make btn1 final just use v parameter of onClick method to change color of clicked Button background :
v.setBackgroundColor(Color.GRAY);
I'm a beginner to Java and I want to validate an EditText. What I have in mind: my editText has to match "helloworld". When you press a button this has to be validated. If this is true--> go to a new class in which I have a setContentView to display a new layout.
If the text which I have just typed does not match "helloworld", it should do nothing. It seems very easy but since I'm a beginner you would help me BIGTIME!
Here's most of the logic handled. You will need to fill in your actual layout id's and make your launch intent. Put this code in your onCreate method in the activity with the layout that contains the edit text box
EditText editText = (EditText)findViewById(R.id.editTextBox);
Button btn = (Button)findViewById(R.id.checkBtn);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
if(editText.getText().toString().equalsIgnoreCase("helloworld")){
//Launch activity with new view
}
}
});
In an activity (or android class) you have to get the instance of your EditText. Your edit text has an id, and you can get it using R. R is the resources for your app.
EditText t = (EditText)findViewById(R.id.<Name of your textfield>);
Then you can get the value of that textfield and compare it
t.getText().toString().equals("helloworld");
will return true or false. If you dont care about the case of the letters use
t.getText().toString().toLowerCase().equals("helloworld");
you will need an onClickListener for your button, check out the android api
http://developer.android.com/reference/android/view/View.OnClickListener.html
in your onCreate, when declaring your submit button, add a listener
Button submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(submitListener);
make a new onClick listener and fire an Intent to start a new activity
View.OnClickListener submitListener = new View.OnClickListener() {
public void onClick(View v) {
//if string matches helloworld fire new activity
Intent newActivity = new Intent();
startActivity(newActivity);
}
};
// create a reference to the EditText in your layout
EditText editText = (EditText)findViewById(R.id.editTextIdInLayout);
// create a reference to the check Button in your layout
Button btn = (Button)findViewById(R.id.buttonIdInLayout);
// set up an onClick listener for the button reference
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String userInput = editText.getText().toString(); // get the user input
if (userInput.equals("helloworld") // see if the input is "helloworld"
{
setContentView(R.layout.newLayout); // change the content view
}
}
});
Right now I have an EditText with id "getUserName" and a button next to it (both in a linear view) with id "setName"
I want someone to be able to click setName, and have the EditText field disappear, the button disappear, and a TextView take it's place. Here's what I have thus far:
public void setName(View view){
EditText editText = (EditText) findViewById(R.id.getUserName);
Button button = (Button) findViewById(R.id.setName);
TextView textView = (TextView) findViewById(R.id.displayName);
String playerName = editText.getText().toString();
((ViewManager)editText.getParent()).removeView(editText);
((ViewManager)button.getParent()).removeView(button);
Log.d("ScoreKeeper", playerName);
}
So I am successfully removing the desired elements from the screen, but I don't know how to add the textView to take their place.
How can I do that? I'm brand new to Android, so forgive me if this seems ignorant. I've tried looking it up!
Thanks
OPSRCFTW
You can simply hide the EditText, Button and TextView using turn visibility on.
You can add textview in your xml file and keep it invisible..
On button click, just change its visibility...
So the code is on buton click like below:
textview.setVisibility(View.VISIBLE);
edittext.setVisibility(View.GONE);
button.setVisibility(View.GONE);
First -> make ur textview Gone,
textview..setVisibility(View.GONE)
when u click the button..
Second -> Make
`Make the EditText and Button GONE with` `edittext.setVisibility(View.GONE);` and make textview visible textview..setVisibility(View.VISIBLE)
What about starting with
textView.setVisibility(View.GONE);
and then set an OnClickListener to your button:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setVisibility(View.VISIBLE);
}
});
Write code onCreate method of your class
EditText editText = (EditText) findViewById(R.id.getUserName);
Button button = (Button) findViewById(R.id.setName);
TextView textView = (TextView) findViewById(R.id.displayName);
textView.setVisibility(View.GONE);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
editText.setVisibility(View.GONE);
button.setVisibility(View.GONE);
textView.setVisibility(View.VISIBLE);
}
});
Hope it will help you.
You can also dynamically create the text view , like textview view= new textview(context); set the height and width thru layout params; and then add this view to parent view or pare layout like parent view.addview(textview). Change the visibility of the button and edittext rather than totally removing them.
on startup
textView.setVisibility(View.GONE);
on button click
textview.setVisibility(View.VISIBLE);
edittext.setVisibility(View.GONE);
button.setVisibility(View.GONE);
How to change button's color to default after next button is clicked? I have this code that sets color to the button in onclicklistener:
Button button = (Button) v;
button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x003333));
I have one click listener for all my buttons. How to clear this botton's background color when the next one is clicked?
Either, store the reference to the previous button in the scope of your activity and use it in the onclick to reset it.
Or set background of all buttons, except the one which is clicked.
Button previousButton = null;
#Override
public void onClick(View v) {
//reset old button
if (previousButton != null) {
previousButton.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x000000));
}
//prettify new button
Button button = (Button) v;
button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x003333));
previousButton = button;
}