Android Buttons wont activate - android

I know this has been asked a million times but none have led me to solving my problem. The onclicklistener will not activate the code for any of the buttons. Here are the different sections that apply to the five buttons.
Button btnGuysMax;
Button btnGuysMedium;
Button btnEven;
Button btnGirlsMedium;
Button btnGirlsMax;
....
private void init()
{
datasource = new BarsDataSource(this);
datasource.open();
Intent intent = getIntent();
long id = intent.getLongExtra("bar_id",0);
bar = datasource.getBarById(id);
title = (TextView)findViewById(R.id.title);
btnGuysMax = (Button)findViewById(R.id.btnGuysMax);
btnGuysMedium = (Button)findViewById(R.id.btnGuysMedium);
btnEven = (Button)findViewById(R.id.btnEven);
btnGirlsMedium = (Button)findViewById(R.id.btnGirlsMedium);
btnGirlsMax = (Button)findViewById(R.id.btnGirlsMax);
......
btnGuysMax.setOnClickListener(this);
btnGuysMedium.setOnClickListener(this);
btnEven.setOnClickListener(this);
btnGirlsMedium.setOnClickListener(this);
btnGirlsMax.setOnClickListener(this);
.....
#Override
public void onClick(View view)
{
//resetButtons();
switch (view.getId()) {
case R.id.btnGuysMax:
//bar.setSexRatio(-2);
//btnGuysMax.setBackgroundColor(guysMaxColor);
Toast.makeText(this,"Max clicked!",Toast.LENGTH_LONG);
break;
case R.id.btnGuysMedium:
bar.setSexRatio(-1);
Toast.makeText(this,"Medium clicked!",Toast.LENGTH_LONG);
//btnGuysMedium.setBackgroundColor(guysMediumColor);
break;
case R.id.btnEven:
bar.setSexRatio(0);
//Toast.makeText(this,"Medium clicked!",Toast.LENGTH_LONG);
break;
case R.id.btnGirlsMedium:
bar.setSexRatio(1);
//btnGirlsMedium.setBackgroundColor(girlsMediumColor);
break;
case R.id.btnGirlsMax:
bar.setSexRatio(2);
break;
.....

To display a toast you need to call show method.
Try:
Toast.makeText(this,"message",Toast.LENGTH_LONG).show();

try doing:
#Override
public void onClickListener(View view)
{
instead of:
#Override
public void onClick(View view)
{

Related

How to change several texts on button click in android studio

HELP ME PLEASE! I want to create 2 buttons ("Next" and "Previous") that will change the text in TextView. I made a switch and "systemcounter" to change the cases, which then will set another text in TextView. When I test my program in this window buttons do not change the pages. I think this is because the system cannot see the "systemcounter"
private Button next_button;
private Button previous_button;
private TextView Text_set1;
int systemcounter = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_learningpage);
next_button = (Button) findViewById(R.id.next_button);
previous_button = (Button) findViewById(R.id.previous_button);
Text_set1 = (TextView) findViewById(R.id.Text_set);
next_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
systemcounter = systemcounter + 1;
}
});
previous_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
systemcounter = systemcounter - 1;
}
});
switch (systemcounter) {
case (0):
Text_set1.setText("Thermodynamics is the branch of physics that deals with the relationships between heat and other forms of energy.");
previous_button.setVisibility(View.INVISIBLE);
break;
case (1):
Text_set1.setText("Hi there");
previous_button.setVisibility(View.VISIBLE);
break;
case (2):
Text_set1.setText("How are you");
previous_button.setVisibility(View.VISIBLE);
break;
case (3):
Text_set1.setText("How old are you?");
break;
default:
Text_set1.setText("OPS");
break;
}```
you have switch statement in oncreate method and so it executes it only once, make a seperate method like
setEditText(int systemcount) and create your switch tehre and call this methods from button onclick methods
private void setText() {
switch (systemcounter) {
case (0):
Text_set1.setText("Thermodynamics is the branch of physics that deals with the relationships between heat and other forms of energy.");
previous_button.setVisibility(View.INVISIBLE);
break;
case (1):
Text_set1.setText("Hi there");
previous_button.setVisibility(View.VISIBLE);
break;
case (2):
Text_set1.setText("How are you");
previous_button.setVisibility(View.VISIBLE);
break;
case (3):
Text_set1.setText("How old are you?");
break;
default:
Text_set1.setText("OPS");
break;
}
}
and call this method in your listeners (like this)
next_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
systemcounter = systemcounter + 1;
setText();
}
});

How to add text to TextView without replacing the old text?

As I mentioned in the title ,I want to add text to a Textview without replacing the previous text .
In my application I have a TextView and 7 buttons .On every button click I set the text of button to the TextView.
If the button is clicked on first time ,Setting the text to TextView ,and if the same button is clicked 2nd time I am removing that button's text from TextView.
Here What I want to do is for 7 buttons I want to set positions(uniqueness for sun to sat) in TextView and when the respective button is clicked that text is set to the TextView and if the button is clicked 2nd time that specific position of the text should remove .
Here text shouldn't replace the previous text that is important to have and if some button's are selected and again that are deselected means TextView should show the default text as "Never"
I tried to get source from SO but I can't find a clear solution for this .
If anyone helps me to come out from this ,that's much helpful for me .
coding
public class CreateAlarm extends Activity implements View.OnClickListener {
private Button mbtn_Sun, mbtn_Mon, mbtn_Tue, mbtn_Wed, mbtn_Thu, mbtn_Fri, mbtn_Sat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_alarm);
mRepeat = (TextView) findViewById(R.id.mRepeat);
mbtn_Sun = (Button) findViewById(R.id.mbtn_Sun);
mbtn_Mon = (Button) findViewById(R.id.mbtn_Mon);
mbtn_Tue = (Button) findViewById(R.id.mbtn_Tue);
mbtn_Wed = (Button) findViewById(R.id.mbtn_Wed);
mbtn_Thu = (Button) findViewById(R.id.mbtn_Thu);
mbtn_Fri = (Button) findViewById(R.id.mbtn_Fri);
mbtn_Sat = (Button) findViewById(R.id.mbtn_Sat);
mbtn_Sun.setOnClickListener((View.OnClickListener) this);
mbtn_Mon.setOnClickListener((View.OnClickListener) this);
mbtn_Tue.setOnClickListener((View.OnClickListener) this);
mbtn_Wed.setOnClickListener((View.OnClickListener) this);
mbtn_Thu.setOnClickListener((View.OnClickListener) this);
mbtn_Fri.setOnClickListener((View.OnClickListener) this);
mbtn_Sat.setOnClickListener((View.OnClickListener) this);
int hours = mTimePicker.getCurrentHour();
mCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mbtn_Sun:
if (mRepeat.getText().toString().contains("Sun")) {
mRepeat.setText("");
} else
mRepeat.setText("Sun");
break;
case R.id.mbtn_Mon:
if (mRepeat.getText().toString().contains("Mon")) {
mRepeat.setText("");
} else
mRepeat.setText("Mon");
break;
case R.id.mbtn_Tue:
if (mRepeat.getText().toString().contains("Tue")) {
mRepeat.setText("");
} else
mRepeat.setText("Tue");
break;
case R.id.mbtn_Wed:
if (mRepeat.getText().toString().contains("Wed")) {
mRepeat.setText("");
} else
mRepeat.setText("Wed");
break;
case R.id.mbtn_Thu:
if (mRepeat.getText().toString().contains("Thu")) {
mRepeat.setText("");
} else
mRepeat.setText("Thu");
break;
case R.id.mbtn_Fri:
if (mRepeat.getText().toString().contains("Fri")) {
mRepeat.setText("");
} else
mRepeat.setText("Fri");
break;
case R.id.mbtn_Sat:
if (mRepeat.getText().toString().contains("Sat")) {
mRepeat.setText("");
} else
mRepeat.setText("Sat");
break;
default:
mRepeat.setText("Never");
}
}
}
Image :
By default the TextView text is "Never".
You can define a TreeMap as:
private TreeMap<Integer, String> mAlarmDays = new TreeMap<>();
as a field of your class and add/remove the days to/from the TreeMap when the corresponding button is clicked. So the implementation of onClick method will be:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mbtn_Sun:
if (mRepeat.getText().toString().contains("Sun")) {
mAlarmDays.remove(0);
} else
mAlarmDays.put(0, "Sun");
break;
case R.id.mbtn_Mon:
if (mRepeat.getText().toString().contains("Mon")) {
mAlarmDays.remove(1);
} else
mAlarmDays.put(1, "Mon");
break;
case R.id.mbtn_Tue:
if (mRepeat.getText().toString().contains("Tue")) {
mAlarmDays.remove(2);
} else
mAlarmDays.put(2, "Tue");
break;
case R.id.mbtn_Wed:
if (mRepeat.getText().toString().contains("Wed")) {
mAlarmDays.remove(3);
} else
mAlarmDays.put(3, "Wed");
break;
case R.id.mbtn_Thu:
if (mRepeat.getText().toString().contains("Thu")) {
mAlarmDays.remove(4);
} else
mAlarmDays.put(4, "Thu");
break;
case R.id.mbtn_Fri:
if (mRepeat.getText().toString().contains("Fri")) {
mAlarmDays.remove(5);
} else
mAlarmDays.put(5, "Fri");
break;
case R.id.mbtn_Sat:
if (mRepeat.getText().toString().contains("Sat")) {
mAlarmDays.remove(6);
} else
mAlarmDays.put(6, "Sat");
break;
}
StringBuilder repeatDays = new StringBuilder();
if (mAlarmDays.size() == 0) {
repeatDays = new StringBuilder("Never");
} else {
for (String day:mAlarmDays.values()) {
repeatDays.append(day).append(" ");
}
}
mRepeat.setText(repeatDays.toString());
}
You should set each button id first,add this to your xml for each specific button : android:id="sun" and ...
My suggestion is: use a single TextView can make your logic quite complex
Use a horizontal LinearLayout instead, you will have 7 TextView inside with predefine text and position. Just simply show/hide them according to which button is clicked and you don't have to deal with any complex string analize.

How can i link one AlertDialog to two actions?

Let's say i have a button which inserts a number to the front of a linked list and a button that inserts a number to the end of the linked list. I have this AlertDaialog and I'm trying to use it with both buttons. How can i differentiate the buttons from one another so that when i press "OK" on the dialog, the onClick method know where to insert the number(front or end).
I've tried somthing like this but it doesn't work, only the default case gets activated.
ok.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
String input = inputText.getText().toString().trim();
int numberInput = Integer.parseInt(input);
switch (v.getId()){
case R.id.btn_front: //insert to the beginning
break;
case R.id.btn_end: //insert to the end
break;
default: Log.e("DIALOG_ERROR", "Error!");
}
}
});
Use a class that extend dialog like:
DialogClass obj=new DialogClass(context,true);
obj.show();
class will be:
public DialogClass(final Context context,Boolean isAddToFront) {
super(context);
setContentView(R.layout.dialog_layout);
setTitle(Title);
TextView Ok = (TextView) findViewById(R.id.ok);
Ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(isAddToFront){
/// Add to front
}else{///add to back}
});
}

Getting the current active editTextField when two or more editTextFields are using same event listener?

I have two editTextField, the same listener is attached to both of them. How would I figure out in the listener which one is clicked?
inputStart = (EditText)findViewById(R.id.editText3);
inputStart.setOnClickListener(TimePickerButtonOnClickListener);
inputEnd = (EditText)findViewById(R.id.editText4);
inputEnd.setOnClickListener(TimePickerButtonOnClickListener);
private Button.OnClickListener TimePickerButtonOnClickListener = new Button.OnClickListener() {
public void onClick(View v) { }
};
I have checked, one possible solution is in this question,
but I am looking for any property like EditText gotEditField = (EditText)v.targetEditField;
Does any such property exist?
Use switch case, in onClick() with parameter View v using v.getId()
private Button.OnClickListener TimePickerButtonOnClickListener = new Button.OnClickListener()
{
public void onClick(View v) {
switch(v.getId){
case R.id.editText3:
break;
.
.
.
}
};
Also you can get the object of EditText using Type Casting the View v parameter..
Like EditText editText = (EditText)v;
View v from the parameters in your onCLick method is the view that was clicked.
EditText gotEditField = (EditText) v;
public void onClick(View v)
{
int id= v.getId();
switch (id)
{
case R.id.editText3:
break;
case R.id.editText4:
break;
}
}

Trying to get an alertdialog to change textview based in chosen option

I want the dialog to show the correct text based on the selected option (ie. If pressed TWO I want is to show the text "you pressed TWO")
on the first press it seemingly does nothing the text goes to the initialized
on the second press you find out the text was switched to default
I'm new to android and I don't think I understand what the activity is doing here.
Can someone please help me find a way that works?
public class AndMainT extends Activity {
private GameLogicT myGame = new GameLogicT();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b_com = (Button)findViewById(R.id.button);
b_com.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CharSequence[] pick_one = {"ONE", "TWO", "THREE"};
call_menu(pick_one);
updateAwesomeText();
}
});
}
public void updateAwesomeText(){
TextView newText = (TextView)findViewById(R.id.text);
newText.setText(myGame.getCurrent_opt().getDescription() + "\n");
}
public void call_menu(CharSequence[] items){
final CharSequence[] f_items = items;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Orders Captain?");
builder.setItems(f_items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
myGame.startOpt(item);
}
});
builder.show();
}
}
public class GameLogicT {
class GOpt{
private CharSequence description = "this is the intialized text sadface";
public CharSequence getDescription() {
return description;
}
public void setDescription(CharSequence description) {
this.description = description;
}
}
private GOpt current_opt = new GOpt();
public void startOpt(int item){
switch(item){
case 1:
current_opt.setDescription("you pressed ONE");
case 2:
current_opt.setDescription("you pressed TWO");
case 3:
current_opt.setDescription("you pressed THREE");
default:
current_opt.setDescription("I am a fart and think you have pressed nothing sadface");
}
}
public GOpt getCurrent_opt() {
return current_opt;
}
public void setCurrent_opt(GOpt current_opt) {
this.current_opt = current_opt;
}
}
I also tried
public void onClick(View v) {
CharSequence[] pick_one = {"ONE", "TWO", "THREE"};
call_menu(pick_one);
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
updateAwesomeText();
}
public void onClick(DialogInterface dialog, int item) {
myGame.startOpt(item);
notify();
}
This causes a force close when the button is pressed, it's not my question just saying I tried!
sorry but I can't test your code due a lot of work.
However,after taking a look I noticed the following:
1) I think that position of items starts from index 0.
2) Maybe you lost some break in the switch-casestatement
So, try to replace it with:
public void startOpt(int item){
switch(item){
case 0:
current_opt.setDescription("you pressed ONE");
break;
case 1:
current_opt.setDescription("you pressed TWO");
break;
case 2:
current_opt.setDescription("you pressed THREE");
break;
default:
current_opt.setDescription("I am a fart and think you have pressed nothing sadface");
}
}
Hope it helps you!

Categories

Resources