I am using java to code a small program. this program will take data from the keyboard and check the parity of that sequence and this is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edso = findViewById(R.id.so);
btnchan = findViewById(R.id.btnchan);
btnle = findViewById(R.id.btnle);
tvkq=findViewById(R.id.tvkq);
String n=edso.getText().toString();
List<Integer> arr = new ArrayList<Integer>();
arr.add(Integer.parseInt(n));
Integer[] array = arr.toArray(new Integer[5]);
btnchan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for (int i=0;i< array.length;i++){
if (array[i]%2==0){
tvkq.setText("chăn"+array[i]);
}
}
}
});
btnle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for (int i=0;i< array.length;i++){
if (array[i]%2!=0){
tvkq.setText("lẽ"+array[i]);
}
}
}
});
}
help me
i want to use data from edittext to check and display it in textview
int num = Integer.parseInt(mEditText.getText().toString());
arr.add(num);
or
mEditText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int num = Integer.parseInt(s.toString());
arr.add(num);
}
#Override
public void afterTextChanged(Editable s) {
}
});
Related
I am making a application which use Search bar to filter item in list view. when user search and choose an item they want, application will display activity of that item.
I try to find solution but each time user select item they want to go. the application always shows the activity of the first item. can anyone please help me how to solves this issue. below is my java code:
BottomNavigationView bottomNavigationView;
ListView listView;
FloatingActionButton share;
MaterialSearchBar timkiembar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reading);
listView = findViewById(R.id.Lv);
listView.setSelected(true);
share = findViewById(R.id.share);
timkiembar = findViewById(R.id.timkiembar);
String[] storyname = getResources().getStringArray(R.array.stories_names);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,R.layout.row,R.id.row_txt,storyname);
listView.setAdapter(adapter);
timkiembar.addTextChangeListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s.toString());
adapter.notifyDataSetChanged();
}
#Override
public void afterTextChanged(Editable s) {
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Reading.this, BilingualEssayDetails.class);
intent.putExtra("story_key",position);
startActivity(intent);
}
});
and below is code of BilingualEssayDetails.class:
translate = findViewById(R.id.translate);
dich = findViewById(R.id.dich);
nihon = findViewById(R.id.nihon);
paint = findViewById(R.id.paint);
layout = findViewById(R.id.layout);
txt_essay = findViewById(R.id.txt_essay);
colors= getSharedPreferences("MyColor",MODE_PRIVATE);
int colorcode= colors.getInt("color", 0);
if (colorcode!=0){
layout.setBackgroundColor(colorcode);
}
int position = getIntent().getIntExtra("story_key",0);
if (position ==0){
txt_essay.setText(""+getString(R.string.essay1));
translate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt_essay.setText(""+getString(R.string.essay1_eng));
}
});
dich.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt_essay.setText(""+getString(R.string.essay1_vn));
}
});
nihon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt_essay.setText(""+getString(R.string.essay1));
}
});
}
if (position ==1){
txt_essay.setText(""+getString(R.string.essay2));
translate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt_essay.setText(""+getString(R.string.essay1_eng));
}
});
dich.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt_essay.setText(""+getString(R.string.essay2_vn));
}
});
nihon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt_essay.setText(""+getString(R.string.essay2));
}
});
}
I have the strange problem when using SwiftKey Keyboard if I passed a string from EditText to SpannableStringBuilder and set it back to EditText; any Space character at the end will be trimmed automatically, using default keyboard this not happen here the code:
public class MainActivity extends AppCompatActivity implements TextWatcher {
EditText txtEntry;
boolean IsChanged = false;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtEntry = (EditText) findViewById(R.id.txtEntry);
txtEntry.addTextChangedListener(this);
}
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
if (IsChanged)
return;
IsChanged = true;
SpannableStringBuilder sb = new SpannableStringBuilder();
sb.append(txtEntry.getText());
txtEntry.setText(sb);
txtEntry.setSelection(txtEntry.getText().length());
IsChanged = false;
}
}
I m doing this as I,m adding dynamic emotions to EditText, but can't add spaces so I made a very simple code to discover the issue.
Ok i fixed the problem, first of all must add inputType="textNoSuggestions" to EditText; this will prevent firing OnTextChanged Event twice, second the processing of string should be throw Editable; not throw EditText itself
Here is the code :
public class MainActivity extends AppCompatActivity implements TextWatcher{
EditText txtEntry;
boolean IsChanged = false;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtEntry = (EditText) findViewById(R.id.txtEntry);
txtEntry.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
txtEntry.addTextChangedListener(this);
}
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String ss = "";
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
if (IsChanged)
return;
IsChanged = true;
String sInput =editable.toString().replace(" ","#space#");
SpannableStringBuilder ObjBuilder = new SpannableStringBuilder();
ObjBuilder.append(sInput);
while (true)
{
int iStartIndex = sInput.indexOf("#space#");
int iEndIndex = iStartIndex + "#space#".length();
if (iStartIndex == -1)
break;
sInput = sInput.replaceFirst("#space#","[space]");
Drawable ObjDraw = getResources().getDrawable(R.drawable.space);
ObjDraw.setBounds(0,0,10,60);
ImageSpan Image = new ImageSpan(ObjDraw);
ObjBuilder.setSpan(Image,iStartIndex,iEndIndex,0);
}
editable.clear();
editable.append( ObjBuilder);
txtEntry.setSelection(txtEntry.getText().length());
IsChanged = false;
}
}
In one activity of my app I have 5 EditText fields. However, I want etPlayer5 only to be enabled when the other 4 EditTexts are not empty. I wrote this little code to achieve this:
etPlayer1 = (EditText) findViewById(R.id.etPlayer1);
etPlayer2 = (EditText) findViewById(R.id.etPlayer2);
etPlayer3 = (EditText) findViewById(R.id.etPlayer3);
etPlayer4 = (EditText) findViewById(R.id.etPlayer4);
etPlayer5 = (EditText) findViewById(R.id.etPlayer5);
if (namePlayer1.matches("")||namePlayer2.matches("")||namePlayer3.matches("")||namePlayer4.matches("")) {
etPlayer5.setEnabled(false);
etPlayer5.setFocusable(false);
}
else {
etPlayer5.setEnabled(true);
etPlayer5.setFocusable(true);
}
Of course this code doesn't work in real time. What do I need to do that when the last of the EditTexts 1 to 4 is filled in, etPlayer5 is automatically set enabled without using a button or something?
Thank you!
You can have textChangeListener with all 4 Edittexts and keep boolean against all edittext. Update booleans to true when you get call of onTextChange and see if the string length is one in this edittext and further check remaining booleans are also true.This is the trick. Now this is the time to make the edittext enable.Hope that helps.Below is the code for above explanation
public class MainActivity extends AppCompatActivity {
EditText et1, et2, et3, et4, et5;
boolean firsEditText, secondEditText, thirdEditText, fourthEditText, fifthEditText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.et1);
et2 = (EditText) findViewById(R.id.et2);
et3 = (EditText) findViewById(R.id.et3);
et4 = (EditText) findViewById(R.id.et4);
et5 = (EditText) findViewById(R.id.et5);
et1.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().length() > 0) {
firsEditText = true;
if (checkAllTrue())
et5.setEnabled(true);
} else {
firsEditText = false;
et5.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
et2.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().length() > 0) {
secondEditText = true;
if (checkAllTrue())
et5.setEnabled(true);
} else {
secondEditText = false;
et5.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
et3.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().length() > 0) {
thirdEditText = true;
if (checkAllTrue())
et5.setEnabled(true);
} else {
thirdEditText = false;
et5.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
et4.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().length() > 0) {
fourthEditText = true;
if (checkAllTrue())
et5.setEnabled(true);
} else {
fourthEditText = false;
et5.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
private boolean checkAllTrue() {
if (firsEditText && secondEditText && thirdEditText && fourthEditText)
return true;
return false;
}
}
You could do something like this
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean player1Changed = false, player2Changed = false;
setContentView(R.layout.main);
etPlayer1 = (EditText) findViewById(R.id.etPlayer1);
etPlayer2 = (EditText) findViewById(R.id.etPlayer2);
etPlayer5 = (EditText) findViewById(R.id.etPlayer5);
etPlayer1.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// you can call or do what you want with your EditText here
player1Changed = true;
if(player1Changed && player2Changed){
etPlayer5.setEnabled(true);
etPlayer5.setFocusable(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
}
etPlayer2.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// you can call or do what you want with your EditText here
player2Changed = true;
if(player1Changed && player2Changed){
etPlayer5.setEnabled(true);
etPlayer5.setFocusable(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
}
}
Could anyone tell me why my button isn't disabled when the text in the edittext is empty? I've tried to do this so many ways but it never works! This is the simplified code I have at the minute.
Code:
public class MapStartActivity extends FragmentActivity {
EditText mapName;
Button NextPageStart;
private TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
checkFieldsForEmptyValues();
}
#Override
public void afterTextChanged(Editable s) {
}
};
private void checkFieldsForEmptyValues(){
String s1 = mapName.getText().toString();
if (s1.trim().isEmpty()) {
NextPageStart.setEnabled(true);
} else {
NextPageStart.setEnabled(false);
NextPageStart.setAlpha(.5f);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_start);
NextPageStart = (Button) findViewById(R.id.NextStatLocBut);
mapName = (EditText) findViewById(R.id.MapNameText);
//Click Listener for button
NextPageStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mapName.addTextChangedListener(textWatcher);
}
});
}
}
Add text watcher outside click listener like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_start);
NextPageStart = (Button) findViewById(R.id.NextStatLocBut);
mapName = (EditText) findViewById(R.id.MapNameText);
// To disable the button intially
NextPageStart.setEnabled(false);
mapName.addTextChangedListener(textWatcher);
//Click Listener for button
NextPageStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// You can do some click action here
}
});
}
In your code change these below lines only
#Override
public void afterTextChanged(Editable s) {
String textFromEditText = mapName.getText();
if(TextUtils.isEmpty(textFromEditText)){
NextPageStart.setEnabled(false);
} else{
NextPageStart.setEnabled(true);
}
}
I don't know why it's not working not for you, but I guess it should work fine. but you should learn about name convention first.
My issue is when first EditText length greater than 10 then second EditText will be enabled and second EditText length greater than 8 then after Button will be enabled what is the problem in my code? Please any one can solve this problem.
public void loginPage(View v) {
editText = (EditText) findViewById(R.id.username);
editText1 = (EditText) findViewById(R.id.pasword);
username = editText.getText().toString();
password = editText1.getText().toString();
btn = (Button) findViewById(R.id.login1);
editText1.setEnabled(false);
btn.setEnabled(false);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.length() >= 10) {
editText1.setEnabled(true);
btn.setEnabled(false);
}
}
});
editText1.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.length() >= 8) {
btn.setEnabled(true);
}
}
});
}
Use this for the equal or greater than 10 word
paste this method in OnCreate() method
editText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if (s.length() >= 10)
editText1.setEnabled(true);
else
editText1.setEnabled(false);
}
});
and for the button for 8 words
paste this method in OnCreate() method
editText1.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if (s.length() >= 8)
btn.setEnabled(true);
else
btn.setEnabled(false);
}
});
enjoy coding .........
You need to use Text Watcher with Edit Text.
Try this code.
public class Login extends AppCompatActivity {
EditText editText, editText1;
Button btn;
String username, password;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
loginPage();
}
public void loginPage()
{
editText = (EditText) findViewById(R.id.username);
editText1 = (EditText) findViewById(R.id.password);
username = editText.getText().toString();
password = editText1.getText().toString();
btn = (Button) findViewById(R.id.login1);
editText1.setEnabled(false);
btn.setEnabled(false);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.length() >= 10) {
editText1.setEnabled(true);
btn.setEnabled(false);
}
}
});
editText1.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.length() >= 8) {
btn.setEnabled(true);
}
}
});
}
}
EDIT :
Create new function to Change from Login page on Button Click.
public void changeLoginPage(){
Intent intentNew = new Intent(First.this,Destinatin.this);
startActivity(intentNew);
}
and add this on onCreate method.
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
changeLoginPage();
}
});