Automatic input before next click input [duplicate] - java

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.

Related

Android: Changing the contents of a Text View using a radio button

So, I am trying to write code that will show that Radio Button clicks register. Ideally, when a radio button is chosen, the contents of a TextView will change. To start, I have a radio button for 'North'. When North is checked, the contents of the TextView will become 'North'. I know there are action listeners involved, but I am not familiar with Java. This will surely pop my Java cherry. That being said, the code I have written is not working. Can anyone tell me if I am on the right track, or offer some suggestions? Note, this is not for a class assignment. This is for a very open ended class project that I am working on with another person.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onRadioButtonClicked(View v){
TextView text = (TextView)findViewById(R.id.text);
RadioButton N = (RadioButton) findViewById(R.id.north);
//evaluates 'checked' value of radio button
boolean checked = ((RadioButton) v).isChecked();
if(N.isChecked () ){
text.setText("N");
}
}
}
To use RadioButton properly you'd better group a bunch of RadioButtons into a set, named RadioGroup.
<RadioGroup
android:id="#+id/rg1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="#+id/rg1_rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="North" />
<RadioButton
android:id="#+id/rg1_rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="South" />
<RadioButton
android:id="#+id/rg1_rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Whatever" />
</RadioGroup>
The critical part is that you have to set unique android:id for each RadioButtons, or they won't work!
Next, find RadioButtons from your XML.
RadioButton rb1, rb2, rb3;
rb1 = (RadioButton) findViewById(R.id.rg1_rb1);
rb2 = (RadioButton) findViewById(R.id.rg1_rb2);
rb3 = (RadioButton) findViewById(R.id.rg1_rb3);
Finally, prepare a RadioButton.OnClickListener class instance and attach it to RadioButtons.
View.OnClickListener optionOnClickListener
= new View.OnClickListener() {
public void onClick(View v) {
TextView tv = (TextView) findViewById(R.id.textview);
String str = null;
// you can simply copy the string of clicked button.
str = ((RadioButton)v).getText().toString();
tv.setText(str);
// to go further with check state you can manually check each radiobutton and find which one is checked.
if(rb1.isChecked()) {
// do something
}
if(rb2.isChecked()) {
// do something
}
if(rb3.isChecked()) {
// do something
}
}
};
rb1.setOnClickListener(optionOnClickListener);
rb2.setOnClickListener(optionOnClickListener);
rb3.setOnClickListener(optionOnClickListener);
// check rb1 by default, if you want.
rb1.setChecked(true);
ADDED:
I'm sorry but I couldn't understand the edited version of my answer, since calling setOnClickListener() inside the View.OnClickLister.OnClick() was somewhat weird to me.
So I rolled back to my original answer.
Try
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.your_radio_group_id);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.north ;
// set text North for your textview here
break;
case R.id.another_radio_button_id:
// do something
break;
}
}
});
Check your xml file. radio button must be inside the radio group for work perfectly.
<RadioGroup
android:id="#+id/Type"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/n"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="I am a Blood Donor" />
<RadioButton
android:id="#+id/s"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RadioGroup>
then change in your java file
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (i == R.id.s) {
//Your action
} else if (i == R.id.n) {
//Your action
}
}
});
it will work.. :) :)
Okay, so making 'onCheckedChange' the onClick event seemed to do the trick. Thanks for all the help people.

2 (or more) button views for the same onClick method

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);
}

Android Button Works Only on the second Click

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"

Numbers increasing when button clicked

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));
}

Android app and java activities, issues with onCreate() method and XML

I am trying use the onClick() function in an activity for an android app. So far I have:
public class Activity2 extends Activity implements OnClickListener {
private ImageButton closeButton;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
Button myButton = (Button) findViewById(R.id.wowButton);
myButton.setOnClickListener(this);
this.closeButton = (ImageButton)this.findViewById(R.id.close);
this.closeButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
finish();
}
});
}
public void onClick(View v) {
// TODO Auto-generated method stub
TextView lowerText = (TextView) findViewById(R.id.textView2);
EditText boxText = (EditText) findViewById(R.id.editText1);
lowerText.setText(boxText.getText());
}
}
This is just a stripped down version of my project. When I enter my app as usual in the emualtor, everything works fine. When I click the button to open up this particular activity, everything crashes. I assume the issue lies within the onClick() method. This code was sent to me by my project partner without the XML files so I think thats where the problem lies.
here is my basic XML file that I thought should work:
<?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">
<Button android:id="#+id/wowButton" android:layout_width="fill_parent"
android:layout_height="200dp" android:text="WoW" android:typeface="sans"
android:background="#drawable/btn_default_normal_red" />
<EditText
android:id="#+id/editText1"
/>
<TextView
android:id="#+id/textView2"
/>
</LinearLayout>
While very basic, I though it should work. Thanks for your help.
you instantiate an ImageButton through this.closeButton = (ImageButton)this.findViewById(R.id.close);
But you don't declare it in the xml file. That's why it's null and throws a NullPointer Exception.

Categories

Resources