how work with trim in java and android? - java

I have an activity in which I have this snippet....
et = (EditText) findViewById(R.id.et);
tv = (TextView) findViewById(R.id.textView8);
String name =et.getText().toString();
String a ="a";
String b ="b";
String c ="c";
String d ="d";
String e ="e";
String f ="f";
String g ="g";
String h ="h";
String a1 ="\u24B6";
String b1 ="\u24B7";
String c1 ="\u24B7";
String d1 ="\u24B9";
String e1 ="\u24BB";
String f1 ="\u24BB";
String g1 ="\u24BD";
String h1 ="\u24BD";
name =name.replaceAll(a,a1);
name =name.replaceAll(b,b1);
name =name.replaceAll(c,c1);
name =name.replaceAll(d,d1);
name =name.replaceAll(e,e1);
name =name.replaceAll(f,f1);
name =name.replaceAll(g,g1);
name =name.replaceAll(h,h1);
tv.setText(name.trim());
when write a, b or c in edit text show space in text view
what is solution?

You're reading value from EditText only once - when you're starting an activity. There's fully working example: text is replaced when typed into EditText.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText et = (EditText) findViewById(R.id.et);
final TextView tv = (TextView) findViewById(R.id.textView8);
final String a = "a";
final String b = "b";
final String c = "c";
final String d = "d";
final String e = "e";
final String f = "f";
final String g = "g";
final String h = "h";
final String a1 = "\u24B6";
final String b1 = "\u24B7";
final String c1 = "\u24B7";
final String d1 = "\u24B9";
final String e1 = "\u24BB";
final String f1 = "\u24BB";
final String g1 = "\u24BD";
final String h1 = "\u24BD";
et.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
}
#Override
public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
String name = et.getText().toString();
name = name.replaceAll(a, a1);
name = name.replaceAll(b, b1);
name = name.replaceAll(c, c1);
name = name.replaceAll(d, d1);
name = name.replaceAll(e, e1);
name = name.replaceAll(f, f1);
name = name.replaceAll(g, g1);
name = name.replaceAll(h, h1);
tv.setText(name.trim());
}
#Override
public void afterTextChanged(final Editable s) {
}
});
}

Related

Parsing string from protected void onCreate method to public class main activity

I have declared strings string1, string2, string3, string4.. string7 in public class. and I am getting values from MySQL database using JSON in onCreate method and storing in String variables st1 , st2, st3...., st7.
Now I need to pass these st1 ,st2,st3,...,st7 values to the string1, string2, string3... string7 respectively.
public class MainActivity extends AppCompatActivity {
private Context mContext;
private Activity mActivity;
private CoordinatorLayout mCLayout;
private Button mButtonDo;
private TextView mTextView;
private String mJSONURLString = "http://paolo.....";
String string1, string2, string3, string4, string4, string5, string6, string7;
String seats = string1 + "" + string2 + "" + string3 + "" + string4 + "" + string5 + "" + string6 + "" + string7;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = getApplicationContext();
mActivity = MainActivity.this;
mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
mTextView = (TextView) findViewById(R.id.tv);
mTextView.setText("");
RequestQueue requestQueue = Volley.newRequestQueue(mContext);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, mJSONURLString, null,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray seat) {
try {
// Loop through the array elements
for (int i = 0; i < seat.length(); i++) {
// Get current json object
JSONObject student = seat.getJSONObject(i);
String st1 = student.getString("st1");
String st2 = student.getString("st2");
String st3 = student.getString("st3");
String st4 = student.getString("st4");
String st5 = student.getString("st5");
String st6 = student.getString("st6");
String st7 = student.getString("st7");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(jsonArrayRequest);
}
}
If you are getting the elements in the for loop as you are doing :
String st1 = student.getString("st1");
String st2 = student.getString("st2");
String st3 = student.getString("st3");
String st4 = student.getString("st4");
String st5 = student.getString("st5");
String st6 = student.getString("st6");
String st7 = student.getString("st7");
You should change it to :
string1 = student.getString("st1");
string2 = student.getString("st2");
string3 = student.getString("st3");
string4 = student.getString("st4");
string5 = student.getString("st5");
string6 = student.getString("st6");
string7 = student.getString("st7");
And if you want to update UI or something just add the method inside the onResponse() I mean if you want to display that text you can create a
private void showText(){
your_text_view1.setText(string1);
(....)
}
And then in the end of onResponse() just put this method.

how work with edit text and unicode?

This is my activity when run activity is stopped.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText et = (EditText) findViewById(R.id.et);
final TextView tv = (TextView) findViewById(R.id.textView8);
final String a = "a";
final String b = "b";
final String c = "c";
final String d = "d";
final String e = "e";
final String f = "f";
final String g = "g";
final String h = "h";
final String a1 = "\u24B6";
final String b1 = "\u24B7";
final String c1 = "\u24B7";
final String d1 = "\u24B9";
final String e1 = "\u24BB";
final String f1 = "\u24BB";
final String g1 = "\u24BD";
final String h1 = "\u24BD";
et.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
}
#Override
public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
String name = et.getText().toString();
name = name.replaceAll(a,a1);
name = name.replaceAll(b, b1);
name = name.replaceAll(c, c1);
name = name.replaceAll(d, d1);
name = name.replaceAll(e, e1);
name = name.replaceAll(f, f1);
name = name.replaceAll(g, g1);
name = name.replaceAll(h,h1);
tv.setText(name.trim());
#Override
public void afterTextChanged(final Editable s) {
}
});
}
}
what is the solution?
Please write correct code because all code in stack overflow is mistake.
Please help me with this problem as it is very hard.
You can use the URLEncoder
String encodedURL = URLEncoder.encode(yourInputString, "UTF-8");
Then decode with URLDecoder as needed on wherever you will use it.

to add unicode in android and phone?

This is my activity
String name =et.getText().toString();
String a = "a";
String a1 = "\u24B6";
String b = "b";
String b1 = "\u24B7";
String c = "c";
String c1 = "\u24B7";
String d = "d";
String d1 = "\u24B9";
String e = "d";
String e1 = "\u24BB";
name = name.replaceAll(a,a1);
name = name.replaceAll(b,b1);
name = name.replaceAll(c,c1);
name = name.replaceAll(d,d1);
name = name.replaceAll(e,e1);
tv.setText(name);
when write a or b or c in edittext show space in textview
what is solution?
Use a TextWatcher on your EditText.
et.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
et.removeTextChangedListener(this);
// make changes to s here
et.addTextChangedListener(this);
}
});

always null pointer exception

sorry for asking an odd question, I'm new in java and I will make an android app which can make a counting from c = a + b
but it always return nullpointerexception
here is my code
public class Kreasi extends Activity {
// Inisialisasi
private EditText e1;
private EditText e2;
private Spinner s1;
private Button b1;
private Button b2;
private TextView t1;
private TextView t2;
private TextView t3;
private TextView t4;
private TextView t5;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.kreasi);
// INISIALISASI VARIABEL
e1 = (EditText)findViewById(R.id.isiharga);
e1 = (EditText)findViewById(R.id.isilaba);
s1 = (Spinner)findViewById(R.id.spin);
b1 = (Button)findViewById(R.id.process);
b2 = (Button)findViewById(R.id.reset);
t1 = (TextView)findViewById(R.id.hMax);
t2 = (TextView)findViewById(R.id.angPokok);
t3 = (TextView)findViewById(R.id.angSewa);
t4 = (TextView)findViewById(R.id.angTotal);
b1.setOnClickListener(new OnClickListener(){
int a,b;
#Override
public void onClick(View v) {
hideSoftKeyboard(b1);
String aa,bb;
aa = e1.getText().toString();
bb = e2.getText().toString();
a = Integer.valueOf(aa);
b = Integer.valueOf(bb);
int c = (int) (a + b);
t1.setText(Integer.toString(c));
}
});
}
// Menyembunyikan Keyboard
public void hideSoftKeyboard(Button btn) {
btn.setInputType(0);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(btn.getWindowToken(), 0);
}
/*
// Fungsi saat tombol process ditekan
public void tombolprocess(){
b1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
hideSoftKeyboard(b1);
aa = e1.getText().toString();
bb = e2.getText().toString();
Toast.makeText(getBaseContext(),"a="+aa +" b=" +bb,Toast.LENGTH_SHORT).show();
}
});
}
*/
}
thanks for any advice
It's because you're not passing value for e2.
Simply change:
e1 = (EditText)findViewById(R.id.isiharga);
e1 = (EditText)findViewById(R.id.isilaba);
to
e1 = (EditText)findViewById(R.id.isiharga);
e2 = (EditText)findViewById(R.id.isilaba);
You've set e1 twice, you need to set e2 also, like so
e1 = (EditText)findViewById(R.id.isiharga);
e2 = (EditText)findViewById(R.id.isilaba);

How to change cell number in Android?

In my application I have three edit text and I am using shared preference to store those number. I want to fetch the numbers from shared preference and display in edit text and i want to edit the text also. When i run the below code i am getting NullPointerException. Can somebody help me?
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.change_cell_number);
init();
/*SharedPreferences sharedPref=this.getSharedPreferences("MY_PREF_2",MODE_WORLD_READABLE);
SharedPreferences.Editor shredPref_Editor=sharedPref.edit();
shredPref_Editor.putString(MY_ACTIVITY,"changeSMSnumber");
shredPref_Editor.commit();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(ChangeSmsNumber_Activity.this);
String servername = settings.getString("sharedPreferencesKey", "defaultValue");
//server.setText(servername);
*/
btnSubmit.setOnClickListener(new OnClickListener()
{
#SuppressWarnings("unused")
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
String numberISD=edtxt_ISD_Code1.getText().toString();
if(edtxt_ISD_Code1.getText().toString().isEmpty())
{
Toast.makeText(getApplicationContext(),"Enter ISD 1st Number * ", Toast.LENGTH_SHORT).show();
}
else if(edtxt_Cell_Number1.getText().toString().isEmpty())
{
Toast.makeText(getApplicationContext(),"Enter 1st Number *", Toast.LENGTH_SHORT).show();
}
else
{
String strSign1 = edtxt_PlusSign1.getText().toString();
String strSign2 = edtxt_PlusSign2.getText().toString();
String strSign3 = edtxt_PlusSign3.getText().toString();
String strSign4 = edtxt_PlusSign4.getText().toString();
String strSign5 = edtxt_PlusSign5.getText().toString();
String strIsd1 = edtxt_ISD_Code1.getText().toString();
String strIsd2 = edtxt_ISD_Code2.getText().toString();
String strIsd3 = edtxt_ISD_Code3.getText().toString();
String strIsd4 = edtxt_ISD_Code4.getText().toString();
String strIsd5 = edtxt_ISD_Code5.getText().toString();
String strNum1 = edtxt_Cell_Number1.getText().toString();
String strNum2 = edtxt_Cell_Number2.getText().toString();
String strNum3 = edtxt_Cell_Number3.getText().toString();
String strNum4 = edtxt_Cell_Number4.getText().toString();
String strNum5 = edtxt_Cell_Number5.getText().toString();
String final_Num_1 = strSign1 + strIsd1 + strNum1;
String final_Num_2 = strSign2 + strIsd2 + strNum2;
String final_Num_3 = strSign3 + strIsd3 + strNum3;
String final_Num_4 = strSign4 + strIsd4 + strNum4;
String final_Num_5 = strSign5 + strIsd5 + strNum5;
SharedPreferences settings = ChangeSmsNumber_Activity.this.getSharedPreferences("MyPref_CellNumber",0);
strGetCellNum = settings.getString("num1", "n/a");
final_Num_1=strGetCellNum;
}
}
});
}
Please guide me.
sharedPref=this.getSharedPreferences("MY_PREF_2",MODE_PRIVATE);
String number= sharedPref.getString("KeyValue","defaultValue")//you can use defaultValue=""
For setting in Edittext
editText.setText(number);

Categories

Resources