<ToggleButton> onClickListener - Creating and deleting buttons? - java

Is it possible to create and delete buttons upon toggling an onClickListener?
Currently my code looks like this:
Button minuskegle, minuskugle, pluskugle, pluskegle, plusmidkegle, minusmidkegle;
ToggleButton toggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toggle = (ToggleButton) findViewById(R.id.bRedGreen);
toggle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
pluskugle = (Button) findViewById(R.id.bBallhole);
minuskugle = (Button) findViewById(R.id.bBallhole);
pluskegle = (Button) findViewById(R.id.bKegle);
minuskegle = (Button) findViewById(R.id.bKegle);
plusmidkegle = (Button) findViewById(R.id.bKeglemid);
minusmidkegle = (Button) findViewById(R.id.bKeglemid);
if(toggle.isChecked())
{
minuskugle.setBackgroundResource(R.drawable.redballinhole);
minuskegle.setBackgroundResource(R.drawable.redkegle);
minusmidkegle.setBackgroundResource(R.drawable.midkegleminus);
}
else
{
pluskugle.setBackgroundResource(R.drawable.whiteballinhole);
pluskegle.setBackgroundResource(R.drawable.kegleb);
plusmidkegle.setBackgroundResource(R.drawable.midkegleplus);
}
}
});
}
I need it to create buttons upon if(toggle.isChecked())
else
delete them

Easy enough :)
Find the parent view in which you want to insert your new button, create a button, insert button into view :
RelativeLayout parentView = (RelativeLayout) findViewById(R.id.parentView);
Button buttonTest = new Button(MyActivity.this);
parentView.addView(buttonTest);
MyActivity.this is necessary because you are inside the click function and this does not refer to the activity.

Related

going back to previous layout makes the button stop working

I am testing my knowledge of android studio and seem to be running into a problem I am trying to make an app that takes the value of a edittext and puts that value into a textview when you click a button. After that it brings you to a page with a back button on it and when I click that it brings me to the first page. But when I try to click the first button it doesn't work anymore.
Code:
public class TestApp extends AppCompatActivity {
private EditText ET;
private Button Add;
private TextView TV;
public String Array[] = new String[9999999];
private Button GoBack;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_app);
ET = (EditText) findViewById(R.id.ET);
Add = (Button) findViewById(R.id.Add);
Add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String getC = ET.getText().toString();
setContentView(R.layout.value);
TV = (TextView) findViewById(R.id.TV);
GoBack = (Button) findViewById(R.id.GoBack);
if (getC.length() > 0){
for (int i = 0; i < Array.length; i++){
if (Array[i] == null){
Array[i] = getC;
TV.setText(Reminders[i]);
}
}
}
GoBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.activity_calender_for_glasses);
}
});
}
});
}
}
thank you for your help.
If not necessary, don't modify the current content view. If you want multiple screens, you have to create multiple activities or fragments...
Moreover :
at the beginning, you inflate R.layout.activity_test_app
when you click on goBack button (1), you inflate R.layout.activity_calender_for_glasses and it's not the same as the first screen
(1) be careful to coding rules: https://source.android.com/setup/contribute/code-style

What is scope of the object inside the DoAnimation method?

Actually i am getting confused as the scope of the variables inside the method is limited so how come it can check for whether the animation is running or not wont it create a new instance every time a button is clicked as method is called when button is clicked. Can somebody explain it to me as this is working perfectly alright
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment);
myView= (TextView) findViewById(R.id.label1);
myButton= (Button) findViewById(R.id.myButton);
newPlayer = MediaPlayer.create(this, R.raw.soundmusic);
final EnlightenApp instance1 = new EnlightenApp();
myButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
final String answer=instance1.EnlightenMe();
myView.setText(answer);
DoAnimation();
}
});
}
public void DoAnimation(){
ImageView myImage= (ImageView) findViewById(R.id.imageView5);
myImage.setImageResource(R.drawable.ball_animation);
AnimationDrawable ballAnimation=(AnimationDrawable) myImage.getDrawable();
if(ballAnimation.isRunning()){
ballAnimation.stop();
}
ballAnimation.start();
}

How to make a text view that changes it content depending on what button is clicked? (android,eclipse)

I have a page with buttons and I want to place a description bar for each button so when someone presses a button the text changes depending on what button was clicked.
So I tired setting the textviews and giving each one of them an id in a separate xml file except for the first one I wrote it in the main xml and wrote this code:
TextView dis;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.weapons);
dis = (TextView) findViewById(R.id.k_k);
......
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.kk_btn:
dis.setText(R.id.k_k);
break;
case R.id.ss_btn:
sp.play(soundId2, 1, 1, 0, 0, 1);
dis.setText(R.id.s_s);
break;
case R.id.hd_btn:
sp.play(soundId2, 1, 1, 0, 0, 1);
dis.setText(R.id.h_d);
break;
but it doesn't work it writes false in the place where the text is supposed to be.
example:
it's something like a table with 2 columns, the first contains buttons the second contains the description of each button but the only description that is view is the description of the button clicked. so on the 2nd column the text changes depending on what button is clicked and it gets the text from an textview and each button has it's own.
in your oncreate you creating a textview with a resource id of R.id.k_k
dis = (TextView) findViewById(R.id.k_k);
and in your onClick method you are using setText with a resource id
dis.setText(R.id.k_k);
you can use of array for your description
String[] descriptionArray={"this is button 1","this is button 2"};
//set onclick listener to your button
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dis.setText(descriptionArray[0]);
}
});
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dis.setText(descriptionArray[1]);
}
});
if you want to use the ids of your textview just make a TextView array
TextView[] textViewArray={(TextView)findViewById(R.id.k_k),(TextView)findViewById(R.id.h_d) };
to set the text to dis TextView
dis.setText(textViewArray[0].getText().toString());
If the problem is just to get description on the basis of button click then you can try this code.In this case I have get the button text so in you case you can get the description from the strings.
result=(TextView) findViewById(R.id.textView1);
btn1=(Button) findViewById(R.id.button1);
btn2=(Button) findViewById(R.id.button2);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
result.setText(btn1.getText().toString());
}
});
btn2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
result.setText(btn2.getText().toString());
}
});

How to reference button for onClickListener?

I am trying to create an Android app, and i want to create a on click listener, here is what I have so far.
public void amazonListener() {
amazonButton = (Button) findViewById(R.id.amazonButton);
}
As you see, i am in the very early stages, but where I first referenced amazonButton (before the = sign) button, it turns into red text and it says Cannot resolve symbol 'amazonButton'. Also, I have referenced this method in the onCreate method
This is how you'd go about creating a button and setting a click listener to it.
public class MainActivity extends YouTubeFailureRecoveryActivity {
Button amazonButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
amazonButton = (Button) findViewById(R.id.amazonButton);
amazonButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Define what should happen when the button is clicked.
}
});
}
}
You can also put the initializations in a single method and call that method, like you've tried :
public class MainActivity extends YouTubeFailureRecoveryActivity {
Button amazonButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeViews();
}
private void initializeViews() {
//Make sure you've declared the buttons before you initialize them.
amazonButton = (Button) findViewById(R.id.amazonButton);
amazonButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Define what should happen when the button is clicked.
}
});
// Add more Views initialization here ...
....
}
}
You need to give the type of the variable when you declare it.
Button amazonButton = (Button) findViewById(R.id.amazonButton);
The alternative is to declare it (but not initialize it) outside of any method and then initialize it later.
Button amazonButton;
/* ... */
private void amazonListener() {
amazonButton = (Button) findViewById(R.id.amazonButton);
}

how to give id for Button By Dynamic Creations in Android?

I am trying to create a button by Dynamically and also i created .Now i want to write the function for click Event So i need the id for Button.I Dont know How to Create id for button Dynamically.Guide me
Thanks in Advance
Here My coding
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
sv.addView(ll);
Button main=new Button(this);
CharSequence value="Main +";
main.setText(value);
ll.addView(main);
}
this.setContentView(sv);
}
You can set id of any controls by
btn.setId(integer value) at runtime.
If you don't want to set id then there is no issue
Also when you create new view then you have to set its layout parameters(Height, Width)
for example
btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
So whole process is like
Button btn = new Button(Context);
btn.setId(1);
btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btn.setText("Dynamic button");
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(v.getContext(),"Dynamic button is clicked", 3000).show();
}
});
Try this code.
Button text = new Button(this);
text.setId(1);
text.setText("text here");
ll.addView(text);
text.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int id = arg0.getId(); // you get ID of your dynamic button
Toast.makeText(getApplicationContext(), "Dynamic textview!", Toast.LENGTH_SHORT).show();
}
});
In that "ll" is a Layout and that add button.
after that you use anytime for click execute that clickListener code.
you can simply use this
Button main= new Button(this);
main.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
and there is main.setId(pass here int value); to set id but i think you will not need it

Categories