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());
Related
<?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("");
}
}
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()));
I'm new for android java coding. I'm try to do menu list where just have tick box, and once tick the items, n press next, it should go to view layout and show items and the total of the item selected, then press next it should open details page where user must put their details n press send button to send via email. I don't know how to call the items from menu to Cart and to confirmation class.
This is menu.java .
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MenuActivity extends Activity {
Button btnorder;
Button btnback;
Button btnlinkcart;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
btnlinkcart = (Button) findViewById(R.id.button1);
btnback = (Button) findViewById(R.id.button2);
// back button click event
btnback.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MenuActivity.this, MainActivity.class);
startActivity(intent);
}
});
// Link to Cart Screen
btnlinkcart.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
ViewActivity.class);
startActivity(i);
finish();
}
});
}
}
This is Cart.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ViewActivity extends Activity {
Button btnconfirm;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.cart);
btnconfirm = (Button) findViewById(R.id.button1);
// Link to Cart Screen
btnconfirm.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
ConfirmActivity.class);
startActivity(i);
finish();
}
});
}
}
This is cart.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/wallpaper" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Confirm" />
</RelativeLayout>
This is menu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/wallpaper"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Next" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Back" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="28dp"
android:text="Pizza (Large) RM30.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox1"
android:layout_marginTop="20dp"
android:text="Pizza (Mediume) RM20.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox2"
android:layout_marginTop="20dp"
android:text="Pizza (Personal) RM10.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox3"
android:layout_marginTop="20dp"
android:text="Chicken Wings RM12.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox4"
android:layout_marginTop="19dp"
android:text="Garlic Bread RM6.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox5"
android:layout_marginTop="20dp"
android:text="Soft Drink (Large) RM5.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox6"
android:layout_marginTop="19dp"
android:text="Soft Drink (Medium) RM4.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Make the variable you want to get in the menu class static, then in the cart class you can get them like this:
menu.variableFromMenuClass
public static ArrayList<YourObject> selectItemList= new ArrayList<YourObject>();
on selectItem(){
//each Item you add here
selectedList.add(selectedObject);
}
on disselectItem(){
//remove if exist in list
selectedList.remove(disselectedOject);
}
sendemail(){
send your public static selecteditem list.
}
This is the algorithm you have to write simple.
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
I am creating an android app, I won't to navigate from "test1" to "test2" but when I press "btnNext" nothing happens. I am using the same code that I used for other navigations within my app so I don't understand why it won't work. Can someone help please?
"test1" xml code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/txtSubTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtTitle"
android:layout_centerHorizontal="true"
android:text="Please Answer the 9 Following Questions"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:text="Depression Test"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/txtQ1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtSubTitle"
android:layout_centerHorizontal="true"
android:layout_marginTop="59dp"
android:text="Q.1. Have you found little pleasure or interest in doing things?"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RadioButton
android:id="#+id/RadioButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtQ1"
android:layout_below="#+id/radioButton1"
android:text="On some days" />
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtQ1"
android:layout_below="#+id/txtQ1"
android:text="No, not at all" />
<RadioButton
android:id="#+id/RadioButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/RadioButton01"
android:layout_below="#+id/RadioButton01"
android:text="On more than half the days" />
<RadioButton
android:id="#+id/RadioButton03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/RadioButton02"
android:layout_below="#+id/RadioButton02"
android:text="Nearly every day" />
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="26dp"
android:layout_toRightOf="#+id/txtQ1"
android:text="Next" />
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/RadioButton03"
android:layout_marginTop="14dp"
android:layout_toRightOf="#+id/txtTitle"
android:text="Next" />
</RelativeLayout>
"Test1.java" code:
package com.lifematters;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class Test1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test1);
//define Navigation Image Buttons
final Button nextBtn = (Button) findViewById(R.id.btnNext);
//Set up listener for Test
nextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//listener call this function
openTest2();
}
});
}
//Open test page
public void openTest2() {
//create new textview
Intent i = new Intent(getApplicationContext(), Test2.class);
startActivity(i);
}
}
"Test2.java" code:
package com.lifematters;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class Test2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test2);
//define Navigation Image Buttons
final Button nextBtn = (Button) findViewById(R.id.btnNext);
//Set up listener for Test
nextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//listener call this function
openTest3();
}
});
}
//Open test page
public void openTest3() {
//create new textview
Intent i = new Intent(getApplicationContext(), Test3.class);
startActivity(i);
}
}
I have declared the activities in the Android Manifest and I am getting zero errors in my log cat.
You have two buttons named "btnNext". That's why it doesn't work.
Double check this:
"#+id/btnNext"
You have the same id set twice...
Change buttons code in xml to this
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="26dp"
android:layout_toRightOf="#+id/txtQ1"
android:text="Next" />
<Button
android:id="#+id/btnNext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/RadioButton03"
android:layout_marginTop="14dp"
android:layout_toRightOf="#+id/txtTitle"
android:text="Next" />
you are having problem because you are calling button with wrong id. check your button id's in test2.xm as well. it will run then