Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am working for the first time with Android Studio, but I have a problem with the code. I want to make a button who chooses a random string. but the button doesn't do anything. Can someone help me? if that is not the case, do you have another code which I can use
private final String[] jokes = {"text1","text2","text3","text4"};
TextView tv;
Button button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_javatest);
tv = (TextView) findViewById(R.id.tv);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
Random random=new Random();
int num = random.nextInt(jokes.length);
tv.setText(jokes[num]); }
});
}
}
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="150dp"
android:layout_marginLeft="150dp"
android:layout_marginTop="294dp"
android:text="#string/hallo"
android:textSize="50sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="79dp"
android:text="#string/generate"
tools:layout_editor_absoluteX="154dp"
tools:layout_editor_absoluteY="542dp" />
You have to point corrent id of button in this line of code
button2 = (Button) findViewById(R.id.button2);
So replacing R.id.button2 to R.id.bt will fix your issue
You have typed a wrong ID for button
Instead of this
button2 = (Button) findViewById(R.id.button2);
Use this
button2 = (Button) findViewById(R.id.bt);
Good practice : always use descriptive names for button id
Related
So i'm starting out with Android dev and i have a problem I can't seem to get my head around.
Im building a calculator obviously utilising a range of buttons. I can get the other buttons to post their corresponding numbers in the EditText just fine, but the dot/period simply wont appear even though it is almost identical in almost every way and linked up in the same manner in the Java code. My code is below: I have shown the XML for button number 9 and then the decimal button to show their similarity
<Button
android:id="#+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:minWidth="48dp"
android:text="9"
app:layout_constraintBaseline_toBaselineOf="#+id/button8"
app:layout_constraintStart_toEndOf="#+id/button8" />
<Button
android:id="#+id/buttonDot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:minWidth="48dp"
android:text="."
app:layout_constraintBaseline_toBaselineOf="#+id/button0"
app:layout_constraintStart_toEndOf="#+id/button0" />
And the Java to initialise and use them is as follows
private EditText newNumber;
private EditText baseNumber;
private TextView operation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
baseNumber = (EditText) findViewById(R.id.baseNumber);
newNumber = (EditText) findViewById(R.id.newNumber);
operation = (TextView) findViewById(R.id.operation);
Button button9 = (Button) findViewById(R.id.button9);
Button buttonDot = (Button) findViewById(R.id.buttonDot);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Button b = (Button) v;
newNumber.append(b.getText().toString());
}
};
button9.setOnClickListener(listener);
buttonDot.setOnClickListener(listener);
I've taken out alot of the other buttons so to not have an extensive list. Simply showing a button that seems to work - button9, and one that doesn't - buttonDot, despite being created and initialised almost identically.
Many thanks
Add this to your EditText in xml:
android:inputType="numberSigned|number|numberDecimal"
If it doesn't have numberDecimal then it will not allow you to enter decimal numbers.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a EditText, TextView and a Button. The user type a number into the EditText. I then want it to be divided by 100 once the user click on the button and display the answer back in the TextView.
Here is my layout (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<EditText
android:id="#+id/tv_numb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="77dp"
android:hint="Provide a number"/>
<Button
android:id="#+id/btn_doCalc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"
android:layout_below="#+id/tv_numb"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/tv_answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btn_doCalc"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:textSize="30sp"
android:text="Answer" />
MainActivity.java
public class MainActivity extends ActionBarActivity {
private EditText editTextNumb;
private Button buttonCalc;
private TextView tvAnswer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextNumb = (EditText) findViewById(R.id.tv_numb);
buttonCalc = (Button) findViewById(R.id.btn_doCalc);
tvAnswer = (TextView) findViewById(R.id.tv_answer);
//I think this is where my problem is-------
final int result = (editTextNumb.getText().toString())/100;
//
buttonCalc.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tvAnswer.setText(result);
}});
}
}
Any help would be appreciated.
Button mButton;
EditText mEdit;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button)findViewById(R.id.button);
mEdit = (EditText)findViewById(R.id.edittext);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
try
{
int result = Integer.parseInt(mEdit.getText().toString())/100;
// show it to them
Log.v("EditText", "result is "+result);
Toast.makeText(getApplicationContext(),"result is "+result,
Toast.LENGTH_LONG).show();
}
catch (final NumberFormatException e)
{
// tell them they didnt enter a valid number
Toast.makeText(getApplicationContext(),"Please Enter a valid number",
Toast.LENGTH_LONG).show();
}
}
});
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am new to Android and Java programming and I am trying to make an app where it starts out with "Goodbye World" and a button underneath. i am trying to make it so that when you press the button "Goodbye World" changes to "Hello World". Does anyone know how you can do this?
Basic idea to do this would be like this
TextView text = (TextView) findViewById(R.id.textview); // change with your TextView id
text.setText("Goodbye World");
Button btn = (Button) findViewById(R.id.button); // change with your button id
btn.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText("Hello World");
}
});
if you want to change the text of Button
public void on Create(Bundle saved Instance State) {
super.onCreate(savedInstanceState);
set Content View(R.layout.main);
btn=(Button) findViewById(R.id.button);
btn.setOnClickListener(new OnclickListener(){
#override
public void onClick(view view){
btn.setText("Hello World");
}
});
Your Java file will look like this:
`package com.example.button;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView = (TextView)findViewById(R.id.textView);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText("Hello World");
}
});
}
}
Your XML file will look like this:
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:text="#string/a" />
`
The answer for your question is here.
activity_main.xml:
<LinearLayout 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:orientation="vertical" >
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Goodbye World" />
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"
android:visibility="gone" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="click" />
</LinearLayout>
on click of button:
public void click(View v){
text1.setVisibility(TextView.GONE);
text2.setVisibility(TextView.VISIBLE);
}
I've got two buttons: Share, Continue. Both of them are created in a new XML file because I wanted to keep my application with a nice flat Windows 8/10 looking GUI.
I am able to display the dialog message, but the problem I am facing is, how can I check which button was clicked by the user: Share or Continue? I can't set up the onClickListener for them because this alert dialog has been created in a new file, thus, it crashes the app if I try to do so.
Here's the XML code for the buttons:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/share_button"
android:layout_marginRight="5dp"
android:text="SHARE"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:background="#drawable/button_blue" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/continue_button"
android:layout_marginLeft="5dp"
android:text="CONTINUE"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:background="#drawable/button_green" />
And the java code where I display this as an alert dialog:
Dialog d = new Dialog(MainActivity.this);
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.dialog);
d.show();
You can call it by your dialog d i.e :
Dialog d = new Dialog(MainActivity.this);
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.dialog);
Button button = (Button) d.findViewById(R.id.share_button);
Button button2 = (Button) d.findViewById(R.id.continue_button)
d.show();
And then you can make a normal onClickListener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//STUFF
}
});
Hope it helps ;)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm learning android programing and i just dont know how turn my xml into a working calculator.
here is my xml, i removed the styling for better reading
<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"
tools:context="com.afm.calculator.MainActivity" >
<TextView
android:id="#+id/tvresult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="40dp" />
<Button
android:id="#+id/times"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x"
android:textSize="40sp" />
<Button
android:id="#+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:textSize="40dp" />
<Button
android:id="#+id/divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="รท"
android:textSize="40sp" />
<Button
android:id="#+id/mines"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:textSize="40sp" />
<Button
android:id="#+id/equal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:textSize="40sp" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal" >
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal" />
</RelativeLayout>
here is my java:
package com.afm.cal;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements android.view.View.OnClickListener {
Button mines, plus, divide, times, equal;
EditText eto, ett;
TextView tv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mines = (Button) findViewById(R.id.mines);
plus = (Button) findViewById(R.id.plus);
divide = (Button) findViewById(R.id.divide);
times = (Button) findViewById(R.id.times);
equal = (Button) findViewById(R.id.equal);
eto = (EditText) findViewById(R.id.editText1);
ett = (EditText) findViewById(R.id.editText2);
tv = (TextView) findViewById(R.id.tvresult);
}
public void onClick(View arg0) {
}
}
please help me finish it and thx in advance
First add Listener to all your buttons using setOnClickListener method.
Eg: plus.setOnClickListener(this);
Make modifications in your onClick method something like below.
public void onClick(View view) {
if (view == mines) {
// do the calculation here
//update the TextView using tv.setText()
} else if (view == plus) {
// do the calculation here
//update the TextView
}
//.. So on for all buttons
}
Assign each button a method using android:onClick attribute of Button in xml.
For example android:onClick="doSomething" . This button will call the doSomething() method when it is clicked.
Your methods should get values from TextFields and do the operation inside these methods.Then display the result in TextView