how work with edit text and unicode? - java

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.

Related

why my textinputs doesn't take any data and appear empty in android studio?

When I try to use the input data with workmanager, it throws an exception: java.lang.NumberFormatException:Input"", it means that the variables used are empty but they are filled in the gui. Also tried by converting the variable to int but is the same, the problem is that it doesn't have the input
mainactivity.java
public class MainAvtivity extends AppCompatActivity {
Button btNn;
EditText value1, value2;
public static final String VALUE1 = "value1";
public static final String VALUE2 = "value2";
#SuppressLint("WrongViewCast")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
value1 = (EditText)findViewById(R.id.Firstvalue);
value2 = (EditText) findViewById(R.id.Secondvalue);
btNn = (Button)findViewById(R.id.calculate);
String Value1 = value1.getText().toString().trim();
String Value2 = value2.getText().toString().trim();
Data DATa = new Data.Builder()
.putString(VALUE1, Value1)
.putString(VALUE2, Value2)
.build();
final OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(smabullsetwork.class)
.setInputData(DATa)
.build();
btNn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
WorkManager.getInstance().enqueue(workRequest);
}
});
}
workmanager file
public class calculator extends Worker {
public claculator(#NonNull Context context, #NonNull WorkerParameters workerParams) {
super(context, workerParams);
}
#NonNull
#Override
public Result doWork() {
Data data = getInputData();
String valuee1 = data.getString(MainActivity.VALUE1);
String valuee2 = data.getString(MainActivity.VALUE2);
int Value1 = Integer.parseInt(valuee1);
int value2 = Integer.parseInt(valuee2);
int result = value1*value2;
DisplayNotiFication("Result is...", result);
return Result.success();
}
You should call getText and build your variable DATa inside the setOnClickListener.
Your code will be :
public class MainAvtivity extends AppCompatActivity {
Button btNn;
EditText value1, value2;
public static final String VALUE1 = "value1";
public static final String VALUE2 = "value2";
#SuppressLint("WrongViewCast")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
value1 = (EditText) findViewById(R.id.Firstvalue);
value2 = (EditText) findViewById(R.id.Secondvalue);
btNn = (Button) findViewById(R.id.calculate);
btNn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String Value1 = value1.getText().toString().trim();
String Value2 = value2.getText().toString().trim();
Data DATa = new Data.Builder()
.putString(VALUE1, Value1)
.putString(VALUE2, Value2)
.build();
final OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(smabullsetwork.class)
.setInputData(DATa)
.build();
WorkManager.getInstance().enqueue(workRequest);
}
});
}

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.

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

how work with trim in java and android?

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

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