radiobutton in android doesn't work - java

in this program I use radiobuttons. Currently I've some problems with the button number 2 (button2), in fact when it is pressed nothing happens!
When I press button2 it should show the name of each radio button in "TextView"
(while button1 is used to clear choosing radio buttons).
Can anyone please help me or give me any kind of tips? Thanks.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class RadiobuttonActivity extends Activity {
Button button1;
Button button2;
TextView textView1;
RadioButton r1;
RadioButton r2;
RadioButton r3;
RadioGroup radioGroup1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
textView1 = (TextView) findViewById(R.id.textView1);
radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
r1 = (RadioButton) findViewById(R.id.r1);
r2 = (RadioButton) findViewById(R.id.r2);
r3 = (RadioButton) findViewById(R.id.r3);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
radioGroup1.clearCheck();
textView1.setText("AllUnChecked");
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
int selectedid = radioGroup1.getCheckedRadioButtonId();
switch (selectedid) {
case 0:
textView1.setText("Red");
break;
case 1:
textView1.setText("Green");
break;
case 2:
textView1.setText("Black");
break;
}
}
});
}
}
this is the main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/r1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Red" />
<RadioButton
android:id="#+id/r2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Green" />
<RadioButton
android:id="#+id/r3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Black" />
</RadioGroup>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AllUnchecked" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Witch radio button is ckecked?" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text View"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
and this is main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/r1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Red" />
<RadioButton
android:id="#+id/r2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Green" />
<RadioButton
android:id="#+id/r3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Black" />
</RadioGroup>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AllUnchecked" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Witch radio button is ckecked?" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text View"
android:textAppearance="?android:attr/textAppearanceLarge" />

As explained in this SO post
Instead of use:
int selectedid = radioGroup1.getCheckedRadioButtonId();
Use this:
int selectedid = radioGroup1.indexOfChild(findViewById(radioGroup1.getCheckedRadioButtonId()));

Related

Android getting value from selected radiobutton using DataBinding

So i am making a quiz with two activities and a class with questions, answers and right answers in collections, one main activity with the question to answer, 4 options (RadioGroup with RadioButtons) and a button submit, when this last is clicked, i want to get the Radio Button value to another activity which shows a sentence whether if you are right or wrong and a button to continue the next question (in MainActivity). I know how to get the RadioButton text in the old way, but not with binding initialized.
This is the activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"\>`
<data />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/campofutbol" />
<LinearLayout
android:id="#+id/linear1"
android:layout_width="match_parent"
android:layout_height="320dp"
android:orientation="vertical">
<TextView
android:id="#+id/txtQuestionNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="185dp"
android:layout_marginTop="130dp"
android:fontFamily="sans-serif-medium"
android:text="#string/contador_pregunta_1"
android:textColor="#color/black"
android:textSize="20sp" />
<TextView
android:id="#+id/txtQuestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:paddingStart="60dp"
android:paddingEnd="50dp"
android:text="#string/Question_1"
android:textAlignment="inherit"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="420dp"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="290dp">
<RadioButton
android:id="#+id/rButtonA"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginStart="60dp"
android:layout_marginTop="70dp"
android:layout_marginEnd="30dp"
android:paddingEnd="40dp"
android:text="#string/Opcion_1_a" />
<RadioButton
android:id="#+id/rButtonB"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginStart="60dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="30dp"
android:paddingEnd="40dp"
android:text="#string/Opcion_1_b" />
<RadioButton
android:id="#+id/rButtonC"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginStart="60dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="30dp"
android:paddingEnd="40dp"
android:text="#string/Opcion_1_c" />
<RadioButton
android:id="#+id/rButtonD"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginStart="60dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="30dp"
android:paddingEnd="40dp"
android:text="#string/Opcion_1_d" />
</RadioGroup>
<Button
android:id="#+id/btnSubmit"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginStart="150dp"
android:layout_marginTop="40dp"
android:text="#string/submit"
android:theme="#color/blue_clue" />
</LinearLayout>
</RelativeLayout>
and this is the MainActivity.Java i have right now.
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
import dam.pmdm.a05_quizzes.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
TextView txtQuestionNumber;
TextView txtQuestion;
RadioGroup rg;
RadioButton optA;
RadioButton optB;
RadioButton optC;
RadioButton optD;
Button btnSubmit;
int score = 0;
int totalQuestion = QuestionsAnswers.questions.length;
int currentQuestion = 0;
ActivityMainBinding binding;
#SuppressLint("SetTextI18n")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
txtQuestionNumber = binding.txtQuestionNumber;
txtQuestion = binding.txtQuestion;
rg = (RadioGroup) binding.radioGroup; ...
}
This is what i want to change to binding "way".
public class MainActivity extends AppCompatActivity implements View.OnClickListener {...
rg = (RadioGroup) binding.radioGroup;
final String value = ((RadioButton) findViewById(rg.getCheckedRadioButtonId()))
`.getText().toString();`
...}
The rest of the code from MainActivity.java if someone wants to optimize it. I was testing if i could display the radio button value in a Toast and now can't even see the screen (as you can see i'm learning).
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
Toast.makeText(getBaseContext(), value, Toast.LENGTH_SHORT).show();
}
});
optA = binding.rButtonA;
optB = binding.rButtonB;
optC = binding.rButtonC;
optD = binding.rButtonD;
btnSubmit = binding.btnSubmit;
optA.setOnClickListener(this);
optB.setOnClickListener(this);
optC.setOnClickListener(this);
optD.setOnClickListener(this);
btnSubmit.setOnClickListener(this);
txtQuestionNumber.setText(currentQuestion + 1 + " / " + totalQuestion);
}

how do I add a reset button for my two TextViews in Android Studio

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="#+id/away"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="#string/away"
android:gravity="center"
android:textSize="20sp"/>
<TextView
android:id="#+id/score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"
android:gravity="center"
android:text="#string/_0"
android:textSize="20sp" />
<Button
android:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="50dp"
android:onClick="score1"
android:text="#string/score" />
<TextView
android:id="#+id/home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"
android:text="#string/home"
android:textSize="20sp"
android:gravity="center"/>
<TextView
android:id="#+id/score2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginBottom="50dp"
android:text="#string/_0"
android:textSize="20sp" />
<Button
android:id="#+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="score2"
android:text="#string/score" />
<Button
android:id="#+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/reset"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:textColor="#ff1100"/>
</LinearLayout>
I searched it on multiple websites but I still can't the answer. I also tried using the__setText("")it
didn't work as well as the the if() method. I have also tried the intent method also doesn't seem to
work, and many other codes , please help me because I am stuck at this bit , I know that it is simple but
I just can't find it. Thank you
The following codes will set the TextView score to blank when Button one is clicked
First approach
To use the android:onClick="score1" in your XML
Add this to your activity
TextView mTvScore1 = (TextView) findViewById(R.id.score)
public void score1(View v) {
mTvScore1.setText("");
}
Second approach
Button mBtn = (Button) findViewById(R.id.one)
TextView mTvScore1 = (TextView) findViewById(R.id.score)
mBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTvScore1.setText("");
}
});
Activity code
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView mTvScore1 = (TextView) findViewById(R.id.score);
Button mBtn = (Button) findViewById(R.id.one);
mBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
score1(v);
}
});
}
public void score1(View v) {
mTvScore1.setText("");
}
}

Making android EditText vertical scrollable

I am new to Android development and I have an issue with making Android EditText vertically scroll able. The vertical scrolling of the EditText does not work for me. I followed several posts and resources but none of them worked for me.
I am using the following code sample to enable the vertical scroll ability.
this.detailsEditText.setScroller(new Scroller(this));
this.detailsEditText.setMaxLines(1);
this.detailsEditText.setVerticalScrollBarEnabled(true);
this.detailsEditText.setMovementMethod(new ScrollingMovementMethod());
The following are the activity class and layout resource file, respectively I am using in my app.
CreateAppointmentActivity.java
package lk.iit.appointmentmanagerapp.activities;
import java.sql.Date;
import java.sql.Time;
import lk.iit.appointmentmanagerapp.data.Appointment;
import lk.iit.appointmentmanagerapp.data.DatabaseHandler;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Scroller;
public class CreateAppointmentActivity extends Activity {
private EditText titleEditText;
private EditText timeEditText;
private EditText detailsEditText;
private EditText dateEditText;
private Button saveButton;
private Button resetButton;
private final DatabaseHandler handler = new DatabaseHandler(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_appointment);
this.titleEditText = (EditText)(this.findViewById(R.id.edit_title));
this.timeEditText = (EditText)(this.findViewById(R.id.edit_time));
this.detailsEditText = (EditText)(this.findViewById(R.id.edit_details));
this.dateEditText = (EditText)(this.findViewById(R.id.edit_date));
this.detailsEditText.setScroller(new Scroller(this));
this.detailsEditText.setMaxLines(1);
this.detailsEditText.setVerticalScrollBarEnabled(true);
this.detailsEditText.setMovementMethod(new ScrollingMovementMethod());
SharedPreferences preferences = getSharedPreferences("date_variables", MODE_PRIVATE);
this.dateEditText.setText(preferences.getInt("Year", 0) + "-" + preferences.getInt("Month", 0) + "-" + preferences.getInt("Day", 0));
this.dateEditText.setEnabled(false);
this.saveButton = (Button)(this.findViewById(R.id.save_new_appointment_button));
this.saveButton.setOnClickListener(new View.OnClickListener() {
#SuppressWarnings("deprecation")
#Override
public void onClick(View v) {
String[] dateComponents = dateEditText.getText().toString().split("-");
String[] timeComponents = timeEditText.getText().toString().split(":");
Appointment appointment = new Appointment();
appointment.setAppointment_title(titleEditText.getText().toString());
appointment.setAppointment_date(new Date(Integer.parseInt(dateComponents[0]), Integer.parseInt(dateComponents[1]), Integer.parseInt(dateComponents[2])));
appointment.setAppointment_time(new Time(Integer.parseInt(timeComponents[0]), Integer.parseInt(timeComponents[1]), Integer.parseInt(timeComponents[2])));
appointment.setAppointment_details(detailsEditText.getText().toString());
boolean inserted = handler.addAppointment(appointment);
//
//
}
});
this.resetButton = (Button)(this.findViewById(R.id.reset_button));
this.resetButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
titleEditText.setText("");
timeEditText.setText("");
detailsEditText.setText("");
dateEditText.setText("");
}
});
}
}
activity_create_appointment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#color/background"
tools:context="lk.iit.appointmentmanagerapp.activities.CreateAppointmentActivity" >
<TextView
android:id="#+id/create_activity_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/welcome_create"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dip"
android:layout_marginBottom="25dip"
android:textSize="18.5sp"
android:textColor="#ffffff" />
<TextView
android:id="#+id/title_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/create_activity_title"
android:layout_marginTop="15dip"
android:text="#string/title_textview_text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/edit_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/create_activity_title"
android:layout_marginTop="15dip"
android:layout_toRightOf="#+id/title_textview"
android:layout_marginLeft="30dip"
android:background="#ffffff"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/time_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title_textview"
android:layout_marginTop="25dip"
android:text="#string/time_textview_text"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<EditText
android:id="#+id/edit_time"
android:background="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/time_textview"
android:layout_below="#+id/edit_title"
android:layout_marginLeft="25dip"
android:layout_marginTop="25dip"
android:ems="10"
android:inputType="time" />
<TextView
android:id="#+id/details_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/time_textview"
android:text="#string/details_textview_text"
android:layout_marginTop="25dip"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/edit_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/details_textview"
android:layout_alignBottom="#+id/details_textview"
android:layout_alignLeft="#+id/edit_title"
android:background="#ffffff"
android:ems="10"
android:inputType="text" >
</EditText>
<TextView
android:id="#+id/date_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/details_textview"
android:text="Date"
android:layout_marginTop="25dip"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/edit_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/date_textview"
android:layout_below="#+id/edit_details"
android:background="#ffffff"
android:layout_marginLeft="25dip"
android:layout_marginTop="25dip"
android:ems="10"
android:inputType="date" >
</EditText>
<Button
android:id="#+id/save_new_appointment_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/date_textview"
android:layout_marginTop="15dip"
android:layout_marginBottom="10dip"
android:layout_centerHorizontal="true"
android:text="#string/save_button_text"
android:textAllCaps="false" />
<Button
android:id="#+id/reset_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/save_new_appointment_button"
android:layout_marginTop="2dip"
android:layout_centerHorizontal="true"
android:text="#string/reset_button_text"
android:textAllCaps="false" />
</RelativeLayout>
Please bear with me if I have done any mistakes as I am relatively new to this area of development.
I would be grateful if someone help me out with this issue.
Try this in java code:
EditText dwEdit = (EditText) findViewById(R.id.DwEdit);
dwEdit.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
// TODO Auto-generated method stub
if (view.getId() ==R.id.DwEdit) {
view.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction()&MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_UP:
view.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
});
And in your xml:
<EditText
android:id="#+id/DwEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minLines="10"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
android:overScrollMode="always"
android:inputType="textCapSentences">
</EditText>
If you wanna scroll editText only in detailEditText then you specify height of editText 50dp or your need and remove inputType="text" from your editText ....it fulfill your need..
and you don't need any code in java relative to that editText..

Unable to run code for Simple Counter App for Android

I am trying to make an application on Android Studio with the help of a tutorial. I managed to get the User Interface right and I think that I have assigned the correct buttons too. I am unable to get where I am going wrong with my code. I am new to Java and so I am unable to pinpoint the error I have committed. I am posting below the code from the files I was asked to edit in the tutorial.
package com.example.to_dolistapplication.app;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.Color;
public class MainActivity extends Activity implements OnClickListener {
Button btn1;
Button btn2;
Button btn3;
TextView textTitle;
EditText scoreText;
int counter = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.button);
btn2 = (Button)findViewById(R.id.button2);
btn3 = (Button)findViewById(R.id.button3);
scoreText = (EditText)findViewById(R.id.textView);
textTitle = (TextView)findViewById(R.id.editText);
//---set on click listeners on the buttons-----
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
// change font size of the text
textTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
}
#Override
public void onClick(View v) {
if (v == btn1){
counter++;
scoreText.setText(Integer.toString(counter));
scoreText.setBackgroundColor(Color.CYAN);
}
if (v == btn2){
counter--;
scoreText.setText(Integer.toString(counter));
scoreText.setBackgroundColor(Color.GREEN);
}
if (v == btn3){
counter = 0;
scoreText.setText(Integer.toString(counter));
scoreText.setBackgroundColor(Color.RED);
}
}
}
Above is the File from MainActivity.java
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.to_dolistapplication.app.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+1"
android:id="#+id/button"
android:onClick="#string/intro"
android:layout_below="#+id/editText"
android:layout_toRightOf="#+id/textView"
android:layout_marginTop="79dp"
android:layout_alignRight="#+id/editText"
android:layout_alignEnd="#+id/editText" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-1"
android:id="#+id/button2"
android:layout_centerVertical="true"
android:layout_alignLeft="#+id/button"
android:layout_alignStart="#+id/button"
android:layout_alignRight="#+id/editText"
android:layout_alignEnd="#+id/editText" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:id="#+id/button3"
android:layout_below="#+id/button2"
android:layout_marginTop="64dp"
android:layout_alignRight="#+id/button2"
android:layout_alignEnd="#+id/button2"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2" />
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textClock"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="#+id/textView2"
android:layout_alignBottom="#+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editText"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="29dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Score"
android:id="#+id/textView"
android:layout_alignBottom="#+id/editText"
android:layout_toLeftOf="#+id/editText" />
This is from the file activity_main.xml.
The app, when run on emulator, displays Unfortunately Counter App has stopped.
what might be the reason for the app not working? Please help.
**EditText** scoreText = (EditText)findViewById(R.id.textView);
**TextView** textTitle = (TextView)findViewById(R.id.editText);
you missmathed with types: android:id="#+id/editText" is EditText, but in Activity you wrote his id to TextView.
And you missmathed with types: android:id="#+id/textView" is TextView, but but in Activity you wrote his id as EditText.
You've mismatched id's in onCreate() method.
Use the debug mode or just look at your console output

Android: OnClick Button Doesn't Append/Delete Value to Text View

I can't make Android onClick events work at all. I've rendered a TextView and some buttons in activity_main.xml. In MainActivity.java I tryied to attach an onClick event to all buttons that will append their value to an input field. However, I'm having trouble finding some tutorials, books, etc on the subject of making dialers work. I have three books and none talk about using the call functions or making dialers. The tutorials I find online are all about people showing users how to root and theme their dialer, not how to program one. So my issues:
1) Where am I going wrong with appending a value to my Text view?
2) How will I remove the last value with the del onClick event?
3) How do I stop the Text view from pulling up the keyboard when a user clicks it? I.e. how to make it disabled/readonly (maybe) so only the buttons can update it?
Thanks for your help. In the below code I've cut the buttons down to one. All I've done is copy all the buttons and change the IDs, text, etc. So all the methods are the same but I can't get just one button working. Again, please don't laugh at me too hard for the code below.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/title_two"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:maxLength="15" >
<requestFocus />
</EditText>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<Button
android:id="#+id/one"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:id="#+id/two"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:id="#+id/three"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="3" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<Button
android:id="#+id/four"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:id="#+id/five"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:id="#+id/six"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="6" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<Button
android:id="#+id/seven"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="7" />
<Button
android:id="#+id/eight"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="8" />
<Button
android:id="#+id/nine"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="9" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<Button
android:id="#+id/star"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="*" />
<Button
android:id="#+id/zero"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="0" />
<Button
android:id="#+id/pound"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="#" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<Button
android:id="#+id/callButton"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="Call" />
<Button
android:id="#+id/contacts"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="Con" />
<Button
android:id="#+id/del"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:text="Del" />
</LinearLayout>
</LinearLayout>
And MainActivity.java:
package com.example.dialertest
import android.app.Activity;
import android.content.Intent;
import android.content.ActivityNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
Button oneBtn;
Button twoBtn;
Button threeBtn;
Button fourBtn;
Button fiveBtn;
Button sixBtn;
Button sevenBtn;
Button eightBtn;
Button nineBtn;
Button starBtn;
Button zeroBtn;
Button poundBtn;
Button callBtn;
Button conBtn;
Button delBtn;
EditText numTxt;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
oneBtn = (Button) findViewById(R.id.one);
twoBtn = (Button) findViewById(R.id.two);
threeBtn = (Button) findViewById(R.id.three);
fourBtn = (Button) findViewById(R.id.four);
fiveBtn = (Button) findViewById(R.id.five);
sixBtn = (Button) findViewById(R.id.six);
sevenBtn = (Button) findViewById(R.id.seven);
eightBtn = (Button) findViewById(R.id.eight);
nineBtn = (Button) findViewById(R.id.nine);
starBtn = (Button) findViewById(R.id.star);
zeroBtn = (Button) findViewById(R.id.zero);
poundBtn = (Button) findViewById(R.id.pound);
callBtn = (Button) findViewById(R.id.callButton);
conBtn = (Button) findViewById(R.id.contacts);
delBtn = (Button) findViewById(R.id.del);
numTxt = (EditText) findViewById(R.id.editText1);
oneBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
oneBtn.setText("1");
numTxt.setText(numTxt.getText() + " " + oneBtn.getText());
//numTxt.append("1");
}
});
/**private void performDial(String numberString) {
if (!numberString.equals("")) {
Uri number = Uri.parse("tel:" + numberString);
Intent dial = new Intent(Intent.ACTION_CALL, number);
startActivity(dial);
}
}*/
/*private void call() {
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel: 8880000000"));
startActivity(callIntent);
} catch(ActivityNotFoundException e) {
Log.e("Dialed Number","Call Failed", e);
}
}
}*/
}
}
change your onClicklistener to this :
oneBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// no need to use onBtn here cuz the param v is the clicked View
Button clickedButton = (Button) v;
clickedButton.setText("1");
numTxt.setText(numTxt.getText().toString() + " " + clickedButton.getText());
//numTxt.append("1");
}
});
Edittext's getText() method returns an Editable Object, see :
http://developer.android.com/reference/android/widget/EditText.html#getText()
http://developer.android.com/reference/android/text/Editable.html
for getting the text of edittext you must call toString() after getText()
numTxt.setText(numTxt.getText().toString() + " " + clickedButton.getText());

Categories