I want to make a search page with 3 text fields. Well, those text fields must expand when i select them and go back to the initial size after I am done writing. So, when I select one of the EditText, I must change the width and after I deselect it to go back to initial size.
Can someone help me with that?
Here is what I tried:
final EditText searchByName = (EditText) getActivity().findViewById(R.id.search_by_name);
searchByName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
searchByName.setWidth(250);
}
});
The problem with this solution is that the text field won't come back to initial size after I deselect it.
Try with onFocusChangeListener without onClick listener.. this allows you to handle the event of selection and deselection both
You can use the onFocusChange event to verify if the user is in the EditText or not:
final EditText searchByName = (EditText) getActivity().findViewById(R.id.search_by_name);
searchByName.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
/* When focus is lost check that the text field
* has valid values.
*/
if (hasFocus) {
searchByName.setWidth(250);
} else {
searchByName.setWidth(normalSize); //Set back to normal.
}
}
});
Related
I am building an app, and I want the app to trigger an event when the user has entered text into the editText and clicks away from it or closes the keyboard? How can I do this?
I am not very skilled in Java, so I would be grateful if you provided some description or code.
You can use onFocusChange listener on your edit text to overcome this problem
yourEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b==true){
//entered in the edit text
}
else {
//left edit text
}
}
});
Simply set an onFocusChangedListener on your EditText and configure what happens when the EditText loses focus. Here's an illustration:
mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean bool) {
if(bool){
// here, the EditText is in focus
}
else {
//here, the EditText is no longer in focus.
// do what you want to do
}
}
});
I hope this helps.
Summed up, can a ToggleButton change what other buttons in the activity do when toggled? If so, a more specific explanation of what I want to do is below:
Basically there are three buttons and a togglebutton. When the togglebutton is toggled, pressing any of the three buttons will take a picture and 'save it' for that button. When untoggled, pressing any of the three buttons simply displays their images. I think I can figure out the camera capture part, but I need some direction when it comes to the togglebutton.
Any help is appreciated and I can explain further if necessary.
What I would do is keep a couple flags for each state at the class level, like this:
public class MyClass {
private static final int STATE_SAVE = 0;
private static final int STATE_DISPLAY = 1;
private int currentState = STATE_DISPLAY;
// I made this default for the example,
// you should use what makes sense to your project.
}
Then, inside your toggle button, you can set the flag. Paraphrasing this code since I don't have an editor open:
toggleButton.setOnToggleListener(new OnToggleListener() {
#Override
public void onToggled(boolean toggled) {
if(toggled) {
currentState = STATE_SAVE;
} else {
currentState = STATE_DISPLAY;
}
});
Now, when the buttons are clicked, you can switch based on the state to do an action:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(currentState == STATE_SAVE) {
// Save the image.
} else if (currentState == STATE_DISPLAY) {
// Display the image.
}
});
Create a boolean such as isToggleOn that is true or false depending on the ToggleButton. Then for each of your buttons, you can simply do:
Button button1 = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isToggleOn){
//do one thing
} else {
//do other thing
}
}
});
Yes, you definitely can. Very rarely is anything impossible with code!
All you have to do is to change the listener of the three buttons when the togglebutton is pressed. You keep alternating between the listeners each time you toggle.
For your purpose, I'd suggest defining two sets of listeners - two for each of the three buttons and then keep changing between them.
I have morethan 100 button in my MainLayout.xml and i want to handle click on each. I want to group all the button and then listen to click of the group button, get the ID and put it into String and then use it as my base id to query from my Sqlite Databse Table. How can i do this? so far i have this on my MainClass.java.
MainClass.java
public class MainClass extends AppCompatActivity implements View.OnClickListener {
Button B0,B1,B2,B3....;
String baseId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
buttonInitializer();
}
public void buttonInitializer(){
B0 = (Button)findViewById(R.id.B0); B0.setOnClickListener(this);
B1 = (Button)findViewById(R.id.B1); B1.setOnClickListener(this);
...
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.B0:
baseId = "B0";
break;
case R.id.B1:
baseId = "B1";
break;
..........
default:
break;
}
QueryFire(); // Fire a query
}
Public void QueryFire(){
//Fire the query with id of the clicked button
}
}
As you can see if i continue doing this, i will have Long Code, i want to shorten my code, instead of the above code, i'd like to group the button and then listen of each click just like RadioButton, but NOTE. i dont want to use RadioButton. I'm trying to implement this seudo code.
// Put the button into RelativeLayout or something and use it to group the button
// OnClick of Each Button inside the GroupButton:
// Get the ID of the Clicked Button:
// Put the ID of Button into String:
// and FIRE my query with ID of Button
Any help woul be appreciated! Thank You Very Much!!!
You should be able to specify a String for the "android:tag" attribute for each button in your xml layout, as well as an "android:onclick" attribute. This should eliminate the need for all of the findViewById() and setOnClickListener() calls.
Then, in your onClick method, you can just do something like baseId = (String) view.getTag()
Might be a bit of a stretch but why not use a ListView with a custom adapter? each adapter will hold a button (or whatever you want) and with every click on button you will get the id (the position of clicked element). Its far more easy on the memory as well :)
I would like to change the contents of an EditText box after the user has finished. So say if the user has entered a line of numbers such as 50126057, after he/she has finished I need some characters added to it before or after the user clicks the send button on my application. It needs to look like this: scn|50126057{) so that my application on the PC will understand it. Is there anyway this can be done?
Thanks and any help is much appreciated
P.S Sorry for spelling/grammar
While clicking on Button event,
btn.setOnClickListener(new onClickListener(){
public void onClick(View v){
String data="scn|"+edt.getText().toString()+"{)";
}
});
EditText word = (EditText) findViewById(R.id.username);
//inside button click event
//assign the word entered in the edit text to a string variable(in this case 'wordtoformat')
String wordtoformat=word.getText().toString();
//Now format the word as you want
wordtoformat="scn|"+wordtoformat+"{)";
//now assign the formated string back to the edit text
word.setText(text);
Hope it worked
You can use the OnFocus listener, something like this:
EditText et = (EditText)findViewById(*editText id*);
et.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
String text = et.getText().ToString();
String newText = "scn|"+text+"{)";
et.setText(newText);
}
});
Sorry if I am not 100% accurate, but I've answered from work I've got no Android IDE here :)
Hope it helps you!
PS: The user is going to see the modified text only if he loses the focus of the EditText and then clicking the button or if by clicking the button the user won't leave the activity. But for the purpose of modifying the text, should work fine.
I've got a few questions about edittext..
I'm trying to create a login screen on Android. I'm taking in a username and a password and then sending the results to another method. I've just got a few questions about it...
1) If I have two edittext's and the user enters values in both and then hits a "login" button, I want the button to take the values that were entered in the 2 edittext boxes, convert and save them to a string and then pass them to a function. I'm unsure how I'd construct this button. The problem I'm having is making it a string I'm currently doing this..
String user = edittext.getText();
But that doesn't seem to work. Also I'm unsure what way Android saves Edittext inputs.. Will it just automatically save it to the string once the user is no longer working out of the box? e.g will...
public boolean onKey(View v, int keyCode, KeyEvent event) {
String user = edittext.getText();
}
work? If not how would I define it?
Thanks.
You have to subscribe for the onClick event of the button,
then simple get the text from the EditText
You don't need to run code on the onKey event of EditText
(Button) findViewById(R.id.btnName)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//called when you press the button
String user =((EditText) findViewById(R.id.txtUser)).getText().toString();
String password =((EditText) findViewById(R.id.txtPassword)).getText().toString();
// do the rest of the job
}
});
Essentially this is what you want to do
String userName = "";
String password = "";
EditText userEdit = (EditText)findViewById(R.id.useredit);
EditText passEdit = (EditText)findViewById(R.id.passedit);
Button okButton = (Button)findViewById(R.id.okButton);
okButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
userName = userEdit.getText().toString();
password = passEdit.getText().toString();
}
});
Instead of using onKeyDown(...) you should register an on-click listener for your button and define its behavior there. Strangely enough you actually have to put .toString() after .getText() in order to store the text.
I would also suggest making all of your views (fields, buttons, images, ect) member variables so your references are persistent.