l have 3 buttons and 1 edittext
<Button
android:id="#+id/button1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="1" />
<Button
android:id="#+id/button2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="2" />
<Button
android:id="#+id/button3" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="3" />
<EditText
android:id="#+id/display" android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="#string/result" />
when i click on android emulator button 1 the text value of 1 should display in edittext how can do this
you have a method button.getText() from which you can get the text of button 1 and then can update the edittext using edittext.setText() method
Just take the reference of that button from the xml layout file in your java file,the setOnClickListener() on it and override the method onClick() of the onClickListener().In that onClick method just set The text of the edittext to the one you wish to add into it.See this Android: How to set the contents of a Edit text from a Button click?
final Button b1 = (Button) findViewById(R.id.button1);
final EditText e1 = (EditText) findViewById(R.id.display);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
e1.setText(b1.getText().toString());
}
});
just copy paste this code it will surely work for you
Its working.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.edittext1);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Button button = (Button)v;
editText.setText(button.getText().toString());
}
Related
How is it possible to hyperlink a button on a android application. this is my code can you please edit it and then reply as it is very needed my app is in a short time limit
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginLeft="13dp"
android:layout_marginTop="48dp"
android:text="GTA 3" />
You want to bind an onClickListener to your button. Put this in your activity:
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// do your stuff here
}
});
I am developing a simple game and in an activity I have 2 image buttons:
<ImageButton
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/desc"
android:src="#drawable/img1"
android:onClick="btn1_click" />
<ImageButton
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/desc"
android:src="#drawable/img2"
android:onClick="btn2_click" />
And I am showing some animations when buttons are clicked:
public void btn1_click(View v) {
v.startAnimation(animLeftTranslate);
}
public void btn2_click(View v) {
v.startAnimation(animRightTranslate);
}
Of course, only the clicked button is being animated but what I want to do is to show animation for both two buttons when one of them are clicked. How can I do this?
Instead of using android:onClick, you can do it in java code:
ImageButton btn1 = (ImageButton) findViewById(R.id.bt1);
ImageButton btn2 = (ImageButton) findViewById(R.id.bt2);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
btn1.startAnimation(animRightTranslate);
btn2.startAnimation(animRightTranslate);
}
};
btn1.setOnClickListener(listener);
btn2.setOnClickListener(listener);`
`
You can accomplish this by your own approach i.e. using android:onClick attribute:
Assign one function to both ImageButton for example btns_clicked(). So add android:onClick='btns_clicked' to both your buttons. And in that function write:
public void btns_clicked(View v) {
View btn1 = findViewById(R.id.btn1);
View btn2 = findViewById(R.id.btn2);
btn1.startAnimation(animLeftTranslate);
btn2.startAnimation(animRightTranslate);
}
you can do this as describe below.
<ImageButton
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/desc"
android:src="#drawable/img1"
android:onClick="btn_click" />
<ImageButton
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/desc"
android:src="#drawable/img2"
android:onClick="btn_click" />
and in activity, you have to use
public void btn_click(View v) {
if(v.getId() == R.id.btn1)
v.startAnimation(animLeftTranslate);
else if(v.getId() == R.id.btn2)
v.startAnimation(animRightTranslate);
}
I have MainActivity where I've got a button which opens a dialog. Once I click this button, I have my dialog frame opened, and now I have two text fields in this dialog and two buttons.
How do I change the text in that field by clicking one of those buttons?
I was trying some tricks, but I've always got NullPointerException.
Thanks in advance!
I would use a custom dialog.
You need to use dialog object to findViewById and initialize views.
dialog.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" >
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_alignParentRight="true"
android:layout_marginRight="58dp"
android:text="Cancel" />
<EditText
android:id="#+id/ed1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="56dp"
android:layout_toLeftOf="#+id/button2"
/>
<EditText
android:id="#+id/ed2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView2"
android:layout_below="#+id/textView2"
android:layout_marginRight="27dp"
android:layout_marginTop="39dp"
android:text="ChangeText" />
</RelativeLayout>
Then in your activity say on button click
dialogPopup();
Then
public void dialogPopup()
{
final Dialog d = new Dialog(MainActivity.this); // initialize dialog
d.setContentView( R.layout.dialog);
d.setTitle("my title");
final EdiText ed1= (TextView) d.findViewById(R.id.ed1);
// initialize edittext using the dialog object.
final EdiText ed2= (TextView) d.findViewById(R.id.ed2);
Button b = (Button) d.findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() // button 1 click
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// on click of button 1 change the text in textview's
ed1.setText("Changing text 1");
ed2.setText("Changing text 2");
}
});
Button b1 = (Button) d.findViewById(R.id.button2); // button2 click
b1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
d.dismiss(); // dismiss dialog
}
});
d.show(); // show the dialog
}
When you create the dialog with AlertDialog.Builder, set the Button's onClickListener to change the EditText's value:
AlertDialog.Builder dialog = new AlertDialog.Builer(this);
...
final EditText edit = new EditText(this);
dialog.setView(edit);
...
dialog.setPositiveButton("Change the freakin text",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id){
edit.setText("bla bla bla");
}
});
...
Hi please lead me some examples which demonstrate how to create editText with "delete" button , when click on the delete option it will remove the created editText . Thanks in advance ...
code for creating textEdit dynamically..
LinearLayout rAlign = (LinearLayout)findViewById(R.id.lId);
EditText newPass = new EditText(getApplicationContext());
allEds.add(newPass);
newPass.setHint("Name of Label");
newPass.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
newPass.setWidth(318);
newPass.setTextColor(Color.parseColor("#333333"));
newPass.setId(MY_BUTTON);
//newPass.
//newPass.setOnClickListener(this);
rAlign.addView(newPass);
MY_BUTTON ++;
addSpinner(); //Function for adding spinner
The layout could be something like this:
<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" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="50dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText1"
android:layout_alignBottom="#+id/editText1"
android:layout_toRightOf="#+id/editText1"
android:text="Button" />
And the Java could be like this:
public class MainActivity extends Activity implements OnClickListener {
TextView tv;
Button btn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.editText1);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
tv.setVisibility(View.GONE);
}
}
I am making an app where when we click on add button it adds 1 to the number and when we click subtract it subtracts the number.
I am doing everything right but dont know why its giving error while processing in Emulator.
Below is my source code :
XML Code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/total"
android:id="#+id/tvDisplay"
android:textSize="20dp"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add"
android:layout_gravity="center"
android:textSize="20dp"
android:id="#+id/bAdd"
android:layout_marginTop="25dp"
android:layout_marginBottom="2dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sub"
android:layout_gravity="center"
android:textSize="20dp"
android:id="#+id/bSub"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp"
/>
</LinearLayout>
Java Code
package com.example.plus.minus;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class PlusMinus extends Activity {
int counter;
Button add , sub;
TextView display;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
counter = 0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
display = (Button) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Adds 1 to the counter
counter = counter + 1;
display.setText(" Your total is :" + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Subtract 1 from counter
counter = counter - 1;
display.setText(" Your total is :" + counter);
}
});
}
}
Also please let me know how can I find runtime errors myself.
And also how to debug errors myself.
Thanks In Advance
because you are casting the wrong view.
change
display = (Button) findViewById(R.id.tvDisplay);
to
display = (TextView) findViewById(R.id.tvDisplay);
display = (TextView) findViewById(R.id.tvDisplay);
not Button
It should be TextView instead of Button
ClassCastException
This is because of you're identify your TextView as Button So, just try to identify your TextView as like below
display = (TextView) findViewById(R.id.tvDisplay);
instead of
display = (Button) findViewById(R.id.tvDisplay);