I am Developing my first android Calculator app. I'm stuck with a single defect. I have added few buttons and on clicking those buttons, it will put the respectives text on the EditText Field. The Main Problem is described below,
When on running the project, the buttons have to be clicked twice to put the respective text on the EditText field for the first time. For example, Button1 puts '1' on the EditText field on click. When on run, First click on that button does nothing. Only on the second click it puts '1' on the EditText field.
The Code follows,
XML Button and EditField,
<EditText
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:ellipsize="end"
android:ems="10"
android:gravity="right"
android:hint="#string/textView1" />
<Button
android:id="#+id/button1"
android:layout_width="60dp"
android:layout_height="60dp"
android:hint="#string/button1"
android:onClick="set1" />
MainActivity.java
The respective function for Button onClick,
public void set1(View v){
final Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
EditText tv = (EditText) findViewById(R.id.textView1);
String text=tv.getText().toString();
tv.setText(text+"1");
}
});
}
Change your set1() method as follows,
public void set1(View v)
{
EditText tv = (EditText) findViewById(R.id.textView1);
String text=tv.getText().toString();
tv.setText(text+"1");
}
If you are calling your set1() method onClick of button in xml then you don't need to find ID for button in that method again. So simply it looks like,
public void set1(View v)
{
EditText tv = (EditText) findViewById(R.id.textView1);
String text=tv.getText().toString();
tv.setText(text+"1");
}
Now in your xml for button will be
android:onClick="set1"
Related
I know these type of question asked many time .but still nobody gave perfect answer for that.
I have question :
I want to move from EditText1 ** to another **EditText2 .
I had already detect to editText1 but how to move cursor to editText2.?
In short I had to move my cursor position from one editText1 to another EditText2 directly.
I faced this type of issue and found the solution as below.
Here I have two editText, if I press "a", my cursor will move to next step. I used below code for doing it.
final EditText editText = (EditText) findViewById(R.id.editText1);
editText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v , int keyCode , KeyEvent event) {
EditText editText2 = (EditText) findViewById(R.id.editText2);
// TODO Auto-generated method stub
if (keyCode == event.KEYCODE_A) {
Selection.setSelection((Editable) editText2.getText(),editText.getSelectionStart());
editText2.requestFocus();
}
return true;
}
});
Let me know if you are facing any error regarding this.
For this, all you need to do is...add below two properties to your EditText tag in xml, except the last EditText(In case, you add it to the last EditText also, then the cursor control will again go to the first EditText when you press enter/next from the keypad)
<EditText
.
.
android:singleLine="true"
android:imeOptions="actionNext"
.
.
/>
Hope this helps
Here is a working example, hope this helps.
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some string of text"
/>
<EditText
android:id="#android:id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some string of text"
/>
<Button
android:id="#android:id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
/>
</LinearLayout>
Class:
public class Example extends Activity {
TextView text1;
TextView text2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text1 = (EditText) findViewById(android.R.id.text1);
text2 = (EditText) findViewById(android.R.id.text2);
Button button = (Button) findViewById(android.R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Selection.setSelection((Editable) text2.getText(), text1.getSelectionStart());
text2.requestFocus();
}
});
}
}
Set onKeyListener to detect the key pressed on every key pressed checked your condition and when your condition will be fulfilled set edittext property edittext2.requestFocus();
I have tested all the previous code segments, and find all are working fine. But I find just calling "requestFocus()" with the proper edittext object is also working. As per as ques asked, the ans can be:
edittext2.requestFocus();
which is working fine for me. Please correct me if I am wrong.
When I run this code on my Android Phone, it keeps on crashing.
My java code is as follows
public class MainActivity extends Activity {
EditText edit = (EditText) findViewById(R.id.text);
Button button = (Button) findViewById(R.id.button);
String string = edit.getText().toString();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (string){
case "c":
MediaPlayer mp1 = MediaPlayer.create(getApplicationContext(), R.raw.sample1);
mp1.start();
break;
My XML code:
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout 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="pembroke.com.example.algorhythmic.MainActivity">
<EditText
android:id="#+id/box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="308dp" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.781" />
The code is crashing, but I don't know why. I might not be using the string correctly.
No errors in the code but it is still crashing. Is it because the Android IDE can't compute my code?
There are 3 problems with the code :
For doing the findViewById, we do this after setContentView in onCreate method of activity. This is because, before setContentView, all views are null. Hence, I moved the findViewById code for both EditText and button after setContentView(R.layout.activity_main) in onCreate method of activity.
For the edittext, the ID which you used is R.id.text in java code, but in xml, it is R.id.box. So I have changed that to R.id.box in java code when doing the findViewById.
To find the value present in EditText, we have to get text present in EditText inside the onClickListener since the value inside EditText keeps on changing as user enters inside EditText.
Hence we assign the value to variable string inside the onClickListener's onClick method.
So final code would be :
public class MainActivity extends Activity {
EditText edit;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) findViewById(R.id.box);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String string = edit.getText().toString();
switch (string){
case "c":
MediaPlayer mp1 = MediaPlayer.create(getApplicationContext(), R.raw.sample1);
mp1.start();
break;
}
}
}
}
I need help in the MainActivity.java coding such that the value of the text field is stored into a variable on button click so that i could use it for search query later.
The activity_main.xml contains the following:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:text="Search"
android:ems="10"
android:id="#+id/SearchText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/button2" />
Could anyone suggest what the MainActivity.java file should contain as to recieve the text value as i am new to coding?
Add onClick on your button
onClick:"btnClicked"
Like
<Button
android:text="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/button2"
android:onClick="btnClicked" />
Add these code in Java file
public void btnClicked(View view){
EditText et = (EditText)findViewById(R.id.SearchText);
String value = et.getText().toString();// your edit text value
}
//Variable to store value.
String variable;
//References to the views.
Button btnClick = (Button)findViewById(R.id.button2);
EditText etText = (EditText)findViewById(R.id.SearchText);
//Setting click to the button
btnClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
// Do Your stuff.
variable = etText.getText().toString();
}
});
what the MainActivity.java file should contain as to receive the text value as i am new to coding?
Write This Code in Your MainActivity.java Whatever you will give in Text field and click Button , It will update and show in Toast Message.
public class MainActivity extends Activity {
EditText et;
Button bt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
et = (EditText)findViewById(R.id.SearchText);
bt = (Button)findViewById(R.id.button2);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String eText = et.getText().toString();
Toast.makeText(getApplicationContext(),"Your Text is here"+eText,Toast.LENGTH_SHORT).show();
}
});
}
String YOUR_VARIABLE = "";
Button btn= (Button)findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText edtText = (Edittext)findViewById(R.id.SearchText);
if(!edtText.isEmpty()){
YOUR_VARIABLE = edtText.getText().toString();
}
});
This will store your EditText text in your Variable
if you want to use That variable Globally you should make if public static
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String value = SearchText.getText().toString();
}
});
I'm a novice android developper and was wondering:
Could I have 2 buttons to be linked into the same, 1 onClick method (which i'll presumably override to accept 2 extra parameter, int btnId and View targetTextView for instance) in order to decide which button is calling the method and then which TextView text to update?
For Example:
btn1 will update the text on text_view_1
and btn2 will update text_view_2.
Except they we will be linked to the same method:
public void generalOnClick(View view, String btnId, String textViewId){...}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="One"
android:onClick="btnClick"/>
<TextView
android:id="#+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Two"
android:onClick="btnClick"/>
</LinearLayout>
Button Click Function in your Activity
public void btnClick(View view) {
TextView tv = (TextView)view;
int id = tv.getId();
if(id==R.id.one) {
tv.setText("One Clicked");
} else if(id==R.id.two){
tv.setText("Two Clicked");
}
}
set an tag to button and verify it with onClick method that which buttons click event has been triggered , if it doesn't work then follows following trick,
define an common method for the functionality which you are going to execute on button click, make it as common function.
define independent onclick event for both button and then while calling the common function which created above pass some unique param and verify it.
use following code:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View.OnClickListener clickListener = new View.OnClickListener() {
public void onClick(View v) {
if(v.getId().Equals(btn1.getId())){
//do your work here
}
else{
//work for 2nd button
}
};
btn1 = (Button)findViewById(R.id.btn_1);
btn2 = (Button)findViewById(R.id.btn_2);
btn1.setOnClickListener(clickListener);
btn2.setOnClickListener(clickListener);
}
I have this Button:
<Button
android:id="#+id/button1"
android:layout_width="130dp"
android:layout_height="35dp"
android:layout_alignRight="#+id/editText2"
android:layout_alignTop="#+id/imageView1"
android:layout_marginTop="23dp"
android:text="Increase" />
How can i make it so that whenever the button is clicked, the count in a textview goes up, for example if the number is first 0, and then you click the button it changes to 1?
Attach a listener to the button to handle the click event.
When you click on it, increment a counter variable and update the text of the textview.
Hint: Use String.valueOf(myCounter); to convert the int to a String
You must add a listener to the Button via the Button's setOnClickListener method, passing in an OnClickListener object.
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int i = Integer.parseInt(myTextView.getText().toString());
myTextView.setText(String.valueOf(i + 1));
}
});
where myTextView is the TextView with the counter, obtained with a call to findViewById. Be sure to pass a String to setText, otherwise the method overload with the integer parameter will be called, which has a different meaning (the integer represents a resource id).
it is very simple ! use this code :
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Increase"
android:onClick="inc"/>
<TextView
android:id="#+id/text"
..../>
and in your activity :
public void inc(View v){
TextView t = (TextView) findViewById(R.id.text);
t.setText(String.valueOf(Integer.parseInt(t.getText)+1));
}