EditText Android| input text by pressing a button - android

How to implement the method, when the button is clicked, EditText can be edited. Before this, EditText must be closed for text entry.
code located below does not help
Entertext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Total.setFocusable(true);
}
});

Try this use android:focusable="false" in xml to disable your EditText
<EditText
android:id="#+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false" />
now enabled your Edittext inside button click listener using android:focusable like this
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
edittext.setFocusable(true);
edittext.setFocusableInTouchMode(true);
edittext.setEnabled(true);
edittext.requestFocus();
}
});

make edittext in xml as focusable false like below ..
android:focusable="false"
then after click event used below code..
Entertext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Total.setFocusable(true);
Total.setFocusableInTouchMode(true);
Total.requestFocus();
}
});
you can also used enable and disable concept in edittext.

Related

How to hide the keyboard, and add text to EditText

I have a EditText and a Button .
When you press EditText , I want to not show the keyboard , And when you press the Button, I want to type a number 1 on the EditText .
observation
I want to cursor does not disappear .
![
When you press the 1 1 writes
When you press the Del licked
Can be controlled in the text
Without the appearance of the keyboard ]1
Set this in your EditText xml to disable the keyboard
android:focusableInTouchMode="false"
Set a "1" on your edit text.
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mYourEditText.setText("1");
}
});
Next configuration to your EditText:
android:longClickable="false"
android:clickable="false"
android:focusableInTouchMode="false"
Add a onClickListener to your button:
myButton.setOnClickListener(new View.OnCLickListener(){
#Override
public void onClick(View v) {
myEditText.setText( myEditText.getText().toString() + "1");
}
});

Android : Focus on TextView?

I have a TextView in ScrollView I need that when set text on TextView , going foucus on the TextView .I need that doing this work in OnClickListener a Button .
You need to set focusable as true.
txtview.setFocusableInTouchMode(true);
and then in onClick of button, request focus as:
txtview.requestFocus();
button.setOnClickListener(new View.onClickListener(){
public void onClick(View v) {
textbox.requestFocus();
}
});
i really hope i understood the question :)
Why don't you just setText and then requestFocus like:
final TextView textView = findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText("Some text");
textView.requestFocus();
}
});
Note:
TextView will have to be focusable. (setFocusable(true), setFocusableInTouchMode(true)).
Or in the xml:
android:focusable="true"
android:focusableInTouchMode="true"

Button Value to EditText Logic - Android

What is the logic of adding text to an edittext when button is pressed. Like in simple calculator, when u press a button the numbers will be shown to the edittext. What do you call that logic? I need a developers explanation.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
edittext.setText("text");
}
});
The logic is get the text from button and set it to the edittext,in aappending mode
Just use append() of the EditText. The Argument will be appended at the end of the Editable.
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editText1.append("your string to add at the end.");
}
});

Facing problem in implementing OnClickListener on android

I want to implement a click listener for a button on my main view. My code is something like below
protected void onCreate(Bundle savedValues) {
...
// Capture our button from layout
Button button = (Button)findViewById(R.id.btnFinish);
// Register the onClick listener with the implementation above
button.setOnClickListener(mFinishListener);
...
}
private OnClickListener mFinishListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
But shows me error as follows
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (DialogInterface.OnClickListener) MobileTrackerActivity.java /MobileTracker/src/com/example/mobiletracker line 37 Java Problem
I have no idea what to do. Please help.
You are not using the correct interface to instantiate the mFinishLinstener variable...
It is possible you have an import specifying DialogInterface and that is confusing the view.
Try specifying View.OnClickListener explicitly.
private View.OnClickListener mFinishListener = new View.OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
As per my opinion Best way to implement On click event for the Button.
Instead of applying an OnClickListener to the button in your activity, you can assign a method to your button in the XML layout, using the android:onClick attribute. For example:
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/self_destruct"
android:onClick="selfDestruct" />
Now, when a user clicks the button, the Android system calls the activity's selfDestruct(View) method. In order for this to work, the method must be public and accept a View as its only parameter. For example:
public void selfDestruct(View view) {
// Kabloey
}
Note: The above code is given in Android SDK - Button.
try this code :::
final Button button = (Button) findViewById(R.id.btnFinish);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
Simply try this one as:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// do something when the button is clicked
}
};
you can also use like below code..
Button button = (Button)findViewById(R.id.btnFinish);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v)
{
//Write Your code here
}
});
You can also declare the onclick in the xml.
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onclick="buttonClick" />
And in your code you would define the function as:
public void buttonClick(View view)
{
// handle click
}

Change EditText by Button

I am having EditText and using the numbers inside the EditText for my variables.
I am also having two buttons, one to increase the number in EditText by one and another to decrease it by one.
Can somebody tell me the code to make this possible?
Use the following line in the xml of EditText for setting the input type as number :
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:text="200"
android:id="#+id/editText1"
android:layout_alignParentLeft="true">
</EditText>
And in source file use the following code to decrease the number by 1:
final EditText ed=(EditText)findViewById(R.id.editText1);
Button b1=(Button)findViewById(R.id.button01);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
int a=Integer.parseInt(ed.getText().toString());
int b=a-1;
ed.setText(new Integer(b).toString());
}
});
Similarly add one more button in the xml to increase the number by one.
You need to create a on click listener for each button, doing something like:
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText editText = (EditText)findViewById(R.id.edit_text);
int newVal = ... //retrieve the previous val and increment it (or decrement it)
editText.setText(newVal, TextView.BufferType.EDITABLE);
}
});
Simply you have to use the click event of both the buttons, on increase button get the text in edittextbox and increment it and set it in the edittextbox, same way do for decrement button also and decrement the value.
button.setOnClickListener(new OnCLickListener(){
#Override
public void onClick(View arg0) {
if(arg0.equals(button1))
{
String s = editText.getText().toString();
Integer i = Integer.parseInt(s);
i=++i;
s = s.valueOf(i);
editText.setText(s);
}
if(arg0.equals(button2))
{
//decrement
}
}
})

Categories

Resources