When pressed Letter, Write the letter a Edittext - java

I have a problem
As it can be seen in the picture there are 3 Buttons and a EditText
Need to write in the box which button is pressed and write the corresponding character in the EditText. Just like a keyboard.
Sorry for my bad English.
Like this:
Thanks

You should define a StringBuilder, then every time you press a button add that characted to the StringBuilder and update the content of EditText.
Just a quick snippet:
StringBuilder s = new StringBuilder();
EditText et = (EditText) findViewById(EDITTEXT_ID_PATH);
Button button_q = (Button) findViewById(BUTTON_Q_ID_PATH);
button_q.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s.append("q");
et.setText(s);
}
});
Button button_e = (Button) findViewById(BUTTON_E_ID_PATH);
button_e.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s.append("e");
et.setText(s);
}
});
Button button_w = (Button) findViewById(BUTTON_W_ID_PATH);
button_q.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s.append("w");
et.setText(s);
}
});

You can also use switch case, here is the completed code
public class MainActivity extends Activity implements OnClickListener{
EditText et1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button)findViewById(R.id.button1);
Button b2 = (Button)findViewById(R.id.button2);
Button b3 = (Button)findViewById(R.id.button3);
et1 = (EditText)findViewById(R.id.editText1);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
et1.append("Q");
break;
case R.id.button2:
et1.append("E");
break;
case R.id.button3:
et1.append("W");
break;
default:
break;
}
}
}

Related

How to pass edit Text value to Toast?

I've just started with android and I'm trying to make simple program to take string from user and display the result as Toast
EditText e = (EditText) findViewById(R.id.editText);
final String result = e.getText().toString();
Button btx = (Button) findViewById(R.id.button2);
btx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
Now If I type anything in editText it's supposd to print that value but instead it's printing the default text value and even if I edit that it prints the same value
As you can see in the picture on pressing the button it shows "Name" on toast
and even upon changing it shows the same thing that is "Name". I want it too show the value that I typed later.
What should I do?
You store the text when you setup the views. Thats why you get the default text.
move
final String result=e.getText().toString();
into the onClick
#Override
public void onClick(View v) {
final String result = e.getText().toString();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
and it should get the text when the button is pressed.
Reason is your variable String result is not taking latest value when button is clicked.
Try this:
final EditText e = (EditText) findViewById(R.id.editText);
Button btx = (Button) findViewById(R.id.button2);
btx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String result = e.getText().toString();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
your requirement is show EditText Text in Toast
first make EditText object as globle
class ActivityClassFileName extend AppCompactActivity
{
EditText ToastText;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activitylayout);
//initialize EditText object
ToastText = (EditText) findViewById(R.id.editText);
Button btx = (Button) findViewById(R.id.button2);
btx.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//showing Toast
Toast.makeText(getApplicationContext(), ToastText.getText().toString(), Toast.LENGTH_LONG).show();
}
}
}
}
Sometimes you have to display toast outside of onclick,by creating new method.
public class MainActivity extends AppCompatActivity {
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button button = findViewById(R.id.button);
editText=findViewById(R.id.editTextView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String testString=editText.getText().toString();
showToast(testString);
}
});
}
private void showToast(String str) {
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
}

Button click is not working: button does nothing

I'm using the following class and ive been trying for the last 24 hours i cant figure out why its not working. when i press the button it does nothing.
I'm creating this so i can verify the use login information
My class
public class login extends AppCompatActivity {
Button button11;
EditText usernameField, passwordField;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
usernameField = (EditText) findViewById(R.id.user);
passwordField = (EditText) findViewById(R.id.password);
Button clickButton = (Button) findViewById(R.id.loginButton);
clickButton.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"it
clicked",Toast.LENGTH_LONG);
TextView mt=(TextView) findViewById(R.id.messy);
mt.setText("Please Wait");
}
});
}}
I Added my XML file on https://codeshare.io/5XWxOE
You are creating Toast but do not call it to show its content try code blew :
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"it
clicked",Toast.LENGTH_LONG).show();
TextView mt=(TextView) findViewById(R.id.messy);
mt.setText("Please Wait");
}
you forgot to use show()
Please try this solution
Toast.makeText(login.this,"it
clicked",Toast.LENGTH_LONG).show;
Instead of the getApplicationContext() you have.
Try this ...
TextView mt;
Button clickButton;
mt=(TextView) findViewById(R.id.messy);
clickButton = (Button) findViewById(R.id.loginButton);
clickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(this, "Button click toast", Toast.LENGTH_SHORT).show();
mt.setText("Your text here");
}
});
When something is going wierd.. you can check basic log. try this one~
public class login extends AppCompatActivity {
Button button11;
EditText usernameField, passwordField;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
usernameField = (EditText) findViewById(R.id.user);
passwordField = (EditText) findViewById(R.id.password);
Button clickButton = (Button) findViewById(R.id.loginButton);
clickButton.setText("Button Found");
clickButton.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
clickButton.setText("Button Clicked");
}
});
}}
There is another way to write code for button clicking
public class login extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button firstButton = (Button) findViewById(R.id.loginButton);
firstButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.loginButton: {
Toast.makeText(login.this, "Button click toast", Toast.LENGTH_SHORT).show();
TextView textView = (TextView) findViewById(R.id.messy);
textView.setText("Your text here");
break;
}
default: {
Toast.makeText(login.this, "Something happens", Toast.LENGTH_SHORT).show();
break;
}
}
}
}
I hope this will resolve your issue.

Opening next Activity

I have have a problem here with my code. I want to open the next Activity using submit button but I'm having issues. Can anybody help me on the mistake I am making so that I can implement it? Thanks
public class Chairperson extends Activity implements View.OnClickListener{
TextView textView;
Button submit_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chairperson);
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(this);
textView = (TextView) findViewById(R.id.welcome_txt);
String message = getIntent().getStringExtra("message");
textView.setText(message);
Button submit_btn = (Button) findViewById(R.id.submit_btn);
final TextView submitTextView = (TextView) findViewById(R.id.submitTextView);
final RadioGroup rg1 = (RadioGroup) findViewById(R.id.rg1);
submit_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Get the checked Radio Button ID from Radio Grou[
int selectedRadioButtonID = rg1.getCheckedRadioButtonId();
// If nothing is selected from Radio Group, then it return -1
if
(selectedRadioButtonID != -1) {
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedRadioButtonID);
String selectedRadioButtonText = selectedRadioButton.getText().toString();
submitTextView.setText(selectedRadioButtonText + " selected.");
} else {
submitTextView.setText("Nothing selected .");
}
}
});
}
#Override
public void onClick(View v) {
startActivity(new Intent(this, ViceChairperson.class));
}
}
I have written a code for your button, delete all previous code for submit_btn in your code and replace with this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addListenerOnButton();
public void addListenerOnButton() {
final Context context = this;
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (radioGroup.getCheckedRadioButtonId() == -1)
{
Toast.makeText(context, "Select an option.", Toast.LENGTH_LONG).show();
}
else{
Intent intent = new Intent(context, ViceChairperson.class);
startActivity(intent);
finish();
}
}
});
}
}
If you have any issues please let me know.
Just move the line
startActivity(new Intent(getApplicationContext(), ViceChairperson.class));
after the if (selectedRadioButtonID != -1) check. If that check succeeds you start the new activity, if not, nothing is launched.
There's no need for the second onClick method, which is not bound to anything and will never be invoked.

How to change a text on the click of a button in android?

I need to set text of a button by taking value from edit text ,when button is clicked it has to change its text to the text specified in Edittext.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn=(Button)findViewById(R.id.button);
assert btn != null;
btn.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v)
{
TextView txt=(TextView)findViewById(R.id.editText);
btn.setText((CharSequence) txt);
}
}
);
}
you just need define EditText variable instead of TextView and call getText method of editText and then toString method like this:
final Button btn=(Button)findViewById(R.id.button);
assert btn != null;
btn.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v)
{
EditText txt = (EditText)findViewById(R.id.editText);
btn.setText(txt.getText().toString());
}
}
);
Declare your views globally
Button btn;
EditText txt;
set your id in onCreate method
btn=(Button)findViewById(R.id.button);
txt=(EditText)findViewById(R.id.editText);
keep your onclicklistener as follow
btn.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v)
{
final String newText=txt.getText().toString().trim();
if(newText.length()>0){
btn.setText(newText);
}
}
}
);
final Button btn = (Button)findViewById(R.id.button);
if (btn != null) {
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText et = (EditText)findViewById(R.id.editText);
if (et != null){
Button btn = (Button)view;
btn.setText(et.getText());
}
}
});
}

How to set 4 button on one Listener?

This is simple method but i don't know the code so i mean is like this
button1 = (Button)findViewById(R.id.b1);
button2 = (Button)findViewById(R.id.b2);
button3 = (Button)findViewById(R.id.b3);
button4 = (Button)findViewById(R.id.b4);
//button1-button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View Button1-4){
{
//Do same method for button1-4
}
}
});
button5 = (Button)findViewById(R.id.b5);
button6 = (Button)findViewById(R.id.b6);
button7 = (Button)findViewById(R.id.b7);
button8 = (Button)findViewById(R.id.b8);
//button5-button8.setOnClickListener(new View.OnClickListener() {
public void onClick(View Button5-8){
{
//Do same method for button5-8
}
}
});
button9 = (Button)findViewById(R.id.b9);
button10 = (Button)findViewById(R.id.b10);
button11 = (Button)findViewById(R.id.b11);
button12 = (Button)findViewById(R.id.b12);
//button5-button8.setOnClickListener(new View.OnClickListener() {
public void onClick(View Button9-12){
{
//Do same method for button9-12
}
}
});
so i have 12 button on my project that i put in 3 group(layout), each group have 4 button inside which i mean the 4 button in each group do same thing.
Can anyone help me to give some sample code? Thank's
This is one template. Hope this help.
public class MyActivity extends Activity implements View.OnClickListener {
Button button1, button2, button3;
#Override
public void onCreate(Bundle bundle) {
super.onCreate();
...
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) {
switch(v.getId()) {
case R.id.button1:
// do stuff; You can called your method from here also.
break;
case R.id.button2:
// do stuff;
break;
...
}
}
You can also set it in your layout xml using the android:onclick attribute.
android:onClick="onClick"
Then in your activity class add the onClick method.
public void onClick(View v) {
//Write your code here
if(v.getId==R.id.button1 || v.getId==R.id.button2 || v.getId==R.id.button3 ||v.getId==R.id.button4){
// Do stuff
}
}
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
button8.setOnClickListener(this);
button9.setOnClickListener(this);
button10.setOnClickListener(this);
button11.setOnClickListener(this);
button12.setOnClickListener(this);
#Override
public void onClick(View v) {
if(v.getID==R.id.button1 || v.getID==R.id.button2 || v.getID==R.id.button3|| v.getID==R.id.button4){
// fill
}else if(v.getID==R.id.button5 || v.getID==R.id.button6 || v.getID==R.id.button7|| v.getID==R.id.button8){
// fill
}else if(v.getID==R.id.button9 || v.getID==R.id.button10 || v.getID==R.id.button11|| v.getID==R.id.button12){
// sadsda;
}
}
try this
try this I hope your problem is solve..
public class MyActivity extends Activity implements View.OnClickListener {
Button button1, button2, button3,button4;
#Override
public void onCreate(Bundle bundle) {
super.onCreate();
...
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.getId==R.id.button1 || v.getId==R.id.button2 || v.getId==R.id.button3 ||v.getId==R.id.button4){
// Do somthin what you want
}
}
}
public class MyActivity extends Activity implements View.OnClickListener {
Button button1, button2, button3;
#Override
public void onCreate(Bundle bundle) {
super.onCreate();
...
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
// do stuff; You can called your method from here also.
break;
case R.id.button2:
// do stuff;
break;
...
}
}
this itself should do the trick

Categories

Resources