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.
Related
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
I want to know how to link a button to edittext so that anytime a certain id is put on the edittext and a button is press, it send you to another page and give detail information about that I'd. I am a beginner so help me pls.
In the XML file, you need to have EditText and Button like the following,
<EditText
android:id="#+id/txt"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="#+id/btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Submit" />
And your code should be like this.
public void onCreate(Bundle savedInstanceState) {
// Initialize edittext and button
EditText txt = (EditText) findViewById(R.id.txt);
Button btn = (Button) findViewById(R.id.btn);
// Set listener to the button
btn.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
String text = txt.getText().toString();
yourFunction(text);
}
});
}
private void yourFunction(String text) {
// Your search code goes here
}
I'm just starting to learn Android programming with Android Studio and unfortunately, but I have a big problem with probably a very simple thing, namely in the layout file of the main activity, I assigned the startActivity() method to the android:onClick property of the both buttons (android:onClick="startActivity()").
And now I should in the MainActivity class, implement startActivity() method but.... I do not know how to do it.
I saw that I should have in MainActivity: public void startActivity(View v). I tried and I was looking for solutions for a few hours and I already lost hope.
Especially that I can implement e.g. View.OnClickListener but methods, startActivity() can not do anymore. How could I implement this method?
I assigned the startActivity() method to the android:onClick property of the both buttons in activity_main.xml:
<Button
android:id="#+id/button"
android:onClick="startActivity()"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="24dp"
android:text="#string/button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="OnClick" />
<Button
android:id="#+id/button2"
android:onClick="startActivity()"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
android:text="#string/button2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button"
tools:ignore="OnClick" />
Next i should in the MainActivity class, implement startActivity() method but.... I do not know how to do it :
public class MainActivity extends AppCompatActivity implements startActivity() {
Button b1, b2;
EditText et1, et2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = findViewById(R.id.button);
b2 = findViewById(R.id.button2);
et1 = findViewById(R.id.editText);
et2 = findViewById(R.id.editText2);
public void startActivity(View v) {
if (v.getId() == R.id.button) {
String name = et1.getText().toString();
int age = Integer.parseInt(et2.getText().toString());
Intent i = new Intent(this, ResultActivity.class);
i.putExtra("name", name);
i.putExtra("age", age);
startActivity(i);
} else {
Intent i = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.google.pl/"));
startActivity(i); }
}
}
You're incorrectly using onClick attribute. This is wrong:
android:onClick="startActivity()"
it should be:
android:onClick="startActivity"
Read more at https://developer.android.com/guide/topics/ui/controls/button#HandlingEvents
Suggestion
You should avoid using android:onClick in your xml. Use the onClickListener instead. It's important to separate your logic and UI layout so you don't need to think too much whenever your xml layout is changed. Use something like this:
Button button = (Button) findViewById(R.id.your_button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something here when button is clicked.
}
});
As mentioned by ישו אוהב אותך9A
https://developer.android.com/guide/topics/ui/controls/button#HandlingEvents
Responding to Click Events
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
//onClick function/method name don't use round brackets
android:onClick="sendMessage" />
and in your activity
//JAVA
/** Called when the user touches the button */
public void sendMessage(View view) {
// Do something in response to button click
}
//Kotlin
/** Called when the user touches the button */
fun sendMessage(view: View) {
// Do something in response to button click
}
and remove the tools:ignore="OnClick"
i hope it helps
Firstly, change this
android:onClick="startActivity()"
to this:
android:onClick="startActivity"
Then move your startActivity method below OnCreate method. It is currently inside OnCreate
I am new to android development and I am troubled with something.
I want to create a user interface first and I have a question regarding adding dynamic fields. I am already using XML to setup my interface but I don't know how to proceed.
For example, the user can select 1 2 3 or 4 and based on the selection I would like the dialog box to show that number of EditText. Also the same thing would apply later. A table would show that number of textviews at the header.
Is there a way to do this by using some XML and some java? Because I believe by using only java it will be a pain to style different things.
Please let me know if you need further info.
Thanks in advance
You should check out the visibility attribute of a View.
If you have a fixed set of ui elements (say, 5 buttons) you can just include them in the layout and show them later with setVisibility(View.Visible).
However, if you have a dynamic amount of elements (the user can chose from 1 to n buttons) then you will have to implement it in Java. You can still use XML layouts for parts of the work, but most stuff will have to be done by hand.
I have written a sample code , See if it helps you
public class ActivityMain extends Activity {
LinearLayout main;
private int id = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
main = (LinearLayout) findViewById(R.id.parent);
main.setOrientation(LinearLayout.VERTICAL);
final EditText editText = (EditText) findViewById(R.id.et_count);
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
final int count = Integer.parseInt(editText.getText()
.toString());
addEditText(count);
}
});
}
private void addEditText(int count) {
for (int i = 0; i < count; i++) {
LinearLayout editTextLayout = new LinearLayout(this);
editTextLayout.setOrientation(LinearLayout.VERTICAL);
main.addView(editTextLayout);
EditText editText1 = new EditText(this);
editText1.setId(id++);
editTextLayout.addView(editText1);
}
}
}
and the layout test.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/parent"
android:padding="10dp" >
<EditText
android:id="#+id/et_count"
android:layout_width="100dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
To add style
editTextLayout.setTextAppearance(getApplicationContext(), R.style.boldText);
I'm trying to make an android app, and as I am a total beginner, I am wondering if anyone could help me out with the code.
There will be three boxes to enter different numbers, and I want the app to divide the second value by the first, and then multiply it by the third. And then display the answer on screen.
And the app does have a purpose aha.
So like (b/a)*c
For taking inputs take 3 EditText and take one Button for get Result by Clicking on it.
Follow this
public class result extends Activity
{
private EditText edit1;
private EditText edit2;
private EditText edit3;
public void onCreate(Bundle savedInstanceState)
{
try
{
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
edit1 = (EditText)findViewById(R.id.edit1);
edit2 = (EditText)findViewById(R.id.edit2);
edit3 = (EditText)findViewById(R.id.edit3);
Button click = (Button)findViewById(R.id.btn);
click.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int a = Integer.parseInt(edit1.getText().toString());
int b = Integer.parseInt(edit2.getText().toString());
int c = Integer.parseInt(edit3.getText().toString());
double result = ((double) a/b)*c;
Toast.makeText(result.this, Double.toString(result),Toast.LENGTH_LONG).show();
}
});
}catch (Exception e) {
e.printStackTrace();
}
}
}
Result.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
>
<EditText
android:id="#+id/edit1"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:inputType="text"/>
<EditText
android:id="#+id/edit2"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:inputType="text"/>
<EditText
android:id="#+id/edit3"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:inputType="text"/>
<Button
android:id="#+id/btn"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="Click"/>
</LinearLayout>
Declare 3 EditTexts in your applications layout, as well as a button, and a TextView. Give them unique id's.
The following code will do what you want. It's very simple, so make sure you don't just copy and paste,and that you understand it. I always find it easier to learn from examples, which is why I'm giving one. I hope it helps.
public class MainActivity extends Activity {
//Declare textviews as fields, so they can be accessed throughout the activity.
EditText mEditText1;
EditText mEditText2;
EditText mEditText3;
TextView mTextView;
Button mButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Bind the EditText views
mEditText1 = (EditText)findViewById(R.id.editText1);
mEditText2 = (EditText)findViewById(R.id.editText2);
mEditText3 = (EditText)findViewById(R.id.editText3);
mTextView = (TextView)findViewById(R.id.textView1);
mButton = (Button)findViewById(R.id.calculateButton);
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//When the button is clicked, call the calucate method.
calculate();
}
});
}
public void calculate(){
//get entered texts from the edittexts,and convert to integers.
Double value1 = Double.parseDouble(mEditText1.getText().toString());
Double value2 = Double.parseDouble(mEditText2.getText().toString());
Double value3 = Double.parseDouble(mEditText3.getText().toString());
//do the calculation
Double calculatedValue = (value2/value1)*value3;
//set the value to the textview, to display on screen.
mTextView.setText(calculatedValue.toString());
}
}
int answer=(Integer.parse(editTextB.getText()) /Integer.parse(editTextA.getText())*Integer.parse(editTextC.getText()