This question already has answers here:
How to make Views with an Invisible attribute 'Visible' after clicking a button
(4 answers)
Closed 7 years ago.
I have four EditText that I set to invisible in the XML and when the button is clicked, I want them to be visible in pairs. For example, when the button is clicked, I want et1 and et2 to be visible, then when the button is clicked again eet1 and eet2 to be visible. And when all of them are visible, i want the TextView to be visible .
public class app extends ActionBarActivity {
EditText et1;
EditText et2;
EditText eet1;
EditText eet2;
TextView sum;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app);
et1 = (EditText)findViewById(R.id.et1);
et2 = (EditText)findViewById(R.id.et2);
eet1 = (EditText)findViewById(R.id.eet1);
eet2 = (EditText)findViewById(R.id.eet2);
sum = (TextView)findViewById(R.id.sum);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClick();
}
});
}
public void onClick() {
et1.setVisibility(View.VISIBLE);
eT1.setVisibility(View.VISIBLE);
eet1.setVisibility(View.VISIBLE);
eet2.setVisibility(View.VISIBLE);
}
}
Set up a variable where you store how many times you clicked the button:
public class app extends ActionBarActivity {
int counter;
...
Then in onClick you increment counter and distinguish cases:
public void onClick() {
switch(counter):
case 0:
et1.setVisibility(View.VISIBLE);
...
break;
case 1:
...
break;
case ...
}
counter++;
}
public void onClick() {
if (if (et1.getVisibility() == View.INVISIBLE &&
et2.getVisibility() == View.INVISIBLE)) {
et1.setVisibility(View.VISIBLE);
eT1.setVisibility(View.VISIBLE);
} else if() ///... you Get the idea
}
Related
It is code due to him I could make my button to start music, now I want my button to start my music + new image. (Initially image shoulndnt be visible, after clicking buttton it should start display new image and my music)
P.s with music are not problems
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (Button)findViewById(R.id.clickme);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.sample);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mp.start();
}
});
}
enter image description here
use this code :
private Button btn;
private ImageView imgDisplay;
private MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
btn=(Button)findViewById(R.id.clickme);
imgDisplay=(ImageView) findViewById(R.id.img_display);
imgDisplay.setVisibility(View.GONE); //or use INVISIBLE
mp = MediaPlayer.create(this, R.raw.sample);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handleMusicState(mp);
}
});
}
private void handleMusicState(MediaPlayer mediaPlayer){
if (mediaPlayer.isPlaying()) mediaPlayer.pause();
else mediaPlayer.start();
ImageDisplay();
}
private void ImageDisplay(){
if (mp.isPlaying()){
imgDisplay.setVisibility(View.VISIBLE);
}else{
imgDisplay.setVisibility(View.GONE);
}
}
In your XML add ImageView and set its property to invisible or Gone..
(Invisible will keep the image space in layout & Gone will
remove the image from layout & space will be filled by the next element.)
and in the Java create reference of ImageView & on button tap set its property to visible.
Example (Without following proper language syntax):
XML:-
<ImageView
.....imageId
.....width
.....height
/>
Java:-
ImageView image = findViewById(R.id.imageId);
image.setVisibility = View.INVISIBLE;
// on button click:
onButtonClick(){
image.setVisibility = View.VISIBLE;
}
Please use the accurate syntax that language have, here is only the logic is provided.
I am working on FAQ page I don't want to use Expandable list view and stuff.
So I set 2 TextViews(1 for Question and 1 for Answer) and made one clickable.
The above image shows when the first textview mfaq is clicked it sets second one mAns to visible.
The below code works well to Set the mAns textview visible:
public class faq extends AppCompatActivity {
TextView mfaq,mAns;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_faq);
mfaq=findViewById(R.id.faq);
mAns=findViewById(R.id.ans);
mfaq.setOnClickListener(new View.OnClickListener() {
int counter=0; //setting counter to count onclick
#Override
public void onClick(View view) {
++counter; //incrementing counter first click
if(counter==1){
mAns.setVisibility(View.VISIBLE);
}
//this sets mAns visible , but when i click on it again i want it to hide the text view
counter=0; //resetting the counter
}
});
}
}
So I want to set the visibilty to gone when the textview is clicked again(Should function like Click-visible,ClickAgain-Invisible,Repeat).
Note-I am a beginner please try to explain me what the code is doing so I learn more :)
Thanks.
If I understand well you wanna hide/show your textview each time you click on the other text?
mfaq.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mAns.getVisibility() == View.VISIBLE){
mAns.setVisibility(View.GONE);
}
else
mAns.setVisibility(View.VISIBLE);
}
});
I'm extremely new to coding, so apologies if this question is trivial or the answer is easily found somewhere. I have searched, but I cannot find anything that helps.
Basically, I'm trying to code a simple, 2 button app in Android Studio.
Button1 is meant to simply display a series of commands to the user via text box.
Button2 merely resets.
My problem is, I would like Button1 to change what's displayed in the text view each time it is pressed, but I cannot figure out how to do so. I don't want to make 6 or 7 buttons.
Basically I would like it to run as follows;
Text = "Pick a number"
user presses Button1
Text = "Add 15" (This is as far as I've gotten)
user presses Button1
Text = "Multiply times 5"
user presses Button1 etc. etc. etc.
If anybody could please explain or usher me in the right direction, I would be greatly appreciative.
You can use button.setOnClickListener
public class MyActivity extends Activity {
EditText et;
Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout_id);
et = (EditText)findViewById(R.id.edittext);
button = (Button)findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Perform your Logic here.
et.setText("New text");
}
});
}
}
you can use a globle and a swithchcase
public class MyActivity extends Activity {
EditText et;
int CLICKS=0;
Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout_id);
et = (EditText)findViewById(R.id.edittext);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CLICKS++;
switch(clicks)
{
case 1:
et.setText("Pick a number");
break;
case 2:
et.setText("Add 15");
break;
case 3:
et.setText("Multiply times 5");
break;
}
}
});
}
}
I have 7 ToggleButtons created and one button. Each named Monday, Tuesday, ... Sunday.
When I select ("Switch ON") for example Monday and Thursday, I want to touch a button and display "you have selected Monday and Thursday".
Any ideas how do it on the MainActivity.java?
Thank you in advance.
You can solve this by setting onCheckedChangeListener for all the seven toggle buttons and appending it to a string. On click this string could be displayed.
public class MainActivity extends AppCompatActivity {
private ToggleButton toggleButton_Monday;
private ToggleButton toggleButton_Thursday,toggleButton_wednesday;
private Button buttonSubmit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton_Monday=(ToggleButton)findViewById(R.id.toggleButton1);
toggleButton_Thursday=(ToggleButton)findViewById(R.id.toggleButton2);
toggleButton_wednesday=(ToggleButton)findViewById(R.id.toggleButton3);
buttonSubmit=(Button)findViewById(R.id.button1);
buttonSubmit.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
StringBuilder result = new StringBuilder();
if(toggleButton_Monday.isChecked()) {
result.append("Monday ");
}
if(toggleButton_Thursday.isChecked()) {
result.append("Thursday ");
}
if(toggleButton_wednesday.isChecked()) {
result.append("Wednesday ");
}
//Displaying the message in toast
Toast.makeText(getApplicationContext(),"You have selected "+result.toString(),Toast.LENGTH_LONG).show();
}
});
}
}
You can do like this.
Always in my apps I added buttons in void onCreate, but now I'm trying to do app with more buttons (about 10). I would like to all buttons active on start app.
In my opinion it is too much buttons to add in this onCreate and app will be starting to long.
I tried to put this:
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
myMethod();
}
})
out of onCreate
but AndroidStudio underlines setOnClickListener and view
I don't have ideas, how and where can i add button out of onCreate.
If you don't want to overcrowd your oncreate method, then create a clicklistener outside onCreate anywhere in activity and in onCreate just set it.
onCreate :
edit_a_member = (Button) findViewById(R.id.edit_member);
delete_a_member = (Button) findViewById(R.id.delete_member);
edit_a_member.setOnClickListener(handleClick);
delete_a_member.setOnClickListener(handleClick);
clickListener:
private View.OnClickListener handleClick = new View.OnClickListener() {
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.edit_member:
member_selected = EDIT_MEMBER_SELECTED;
callDialog();
break;
case R.id.delete_member:
callDeleteAlert();
break;
}
}
};
You can simply add a separate method for your buttons in the same class, e.g.:
public void onCreate(...){
//Standard setup of views or whatever you want to do here
this.addButtons();
}
private void addButtons(){
Button b1 = new Button("Hi");
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
myMethod();
}
});
Button b2 = new Button("Hi to you too");
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
myMethod();
}
});
}
This is an example. You can do this in soooo many ways. I feel like you should thoroughly learn Java's fundamental Object Oriented programming, because that's really what your question suggests you don't understand. Go follow a youtube tutorial. I always like "The New Boston"'s Java tutorial series on youtube.
PS: You can make code like this beautiful under the 'Words of wisdom': Don't repeat yourself
If you have to do a lot of work in your onCreate but you are worried that the UI will take too long to load you can always post a delayed runnable to a handler so in the onCreate method put :
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//add your code here
}
},10);
what this will do is your UI will load then the code in your Runnable will be executed 10 milliseconds after your UI loads thus your app will not take too long to load the UI, even though in your case I doubt it would be necessary.
If you are declaring the buttons in xml file :
Add these properties in each button Declaration in your Xml :
android:clickable="true"
android:onClick="onClick"
And now in Activity Class create a method like this :
public void onClick(View v){
switch(v.getId){
case R.id.{buttons_id_in_xml}
(Your Code)
break;
(Like for others)
}
}
If you want to add buttons dynamically :
Create a method to add the button like this:
void addButton(String buttonName, int button id){
Button button = new Button(this);
button.setText("Push Me");
(add it to parent Layout of xml)
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(id){
case id1:
(handle )
break;
(like for others)
}
}
});
}
The best way to do this is:
add implements View.OnClickListener to
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
// declare variables
private Button mBtn1;
private Button mBtn2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
// make an instance to the btns
mBtn1 = findViewById(R.id.btn1);
mBtn2 = findViewById(R.id.btn2);
// set onClickListener
mBtn1.setOnClickListener(this); // with "this" you are passing the view
mBtn2.setOnClickListener(this);
}
// implement onClick
#Override
public void onClick(View view) {
// check which btn was clicked by id
switch (view.getId()) {
case R.id.btn1:
btn1Clicked();
break;
case R.id.btn2:
btn2Clicked();
break;
}
}
private void btn1Clicked() {
// your code btn1 clicked
}
private void btn2Clicked() {
// your code btn2 clicked
}
Hope this helped. Cheers!