obj.getString - Styling in android studio, possible? - java

Looking to style (bold, italic etc.) "quote" and "name", but have tried several potential solutions with no luck:
private void settextView(String json) throws JSONException {
JSONArray jsonArray = new JSONArray(json);
String[] tasks = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
tasks[i] = obj.getString("quote") + "\n\n" + obj.getString("name");
textView.setText(tasks[0]);
setBtnCopyOnClick(tasks[0]); //Here
String a = "quote";
SpannableString spanned = new SpannableString(a);
spanned.setSpan(new UnderlineSpan(), 1, a.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
They both generate in the same textView, so unfortunately I can only style all, or not at all.

If you are looking to style only specific parts of text you should be using spans. The Android Developers site has good documentation on this which you can read here.
For your particular use case pay most attention to the StyleSpan which allows you to set Typeface flags.
For example:
String a = "underlined text";
String b = " normal text";
SpannableString spanned = new SpannableString(a + b);
spanned.setSpan(new UnderlineSpan(), 0, a.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

Related

Change text styling - settextView getString

I have two strings that are pulled from json, need to change styling for both elements.
My code is:
private void settextView(String json) throws JSONException {
JSONArray jsonArray = new JSONArray(json);
String[] tasks = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
tasks[i] = obj.getString("quote") + "\n\n" + obj.getString("name");
TextView quote = null;
quote.setTextColor(Color.parseColor("#FFFFFF"));
textView.setText(tasks[0]);
setBtnCopyOnClick(tasks[0]); //Here
}
}
As you can see, I have tried to insert:
TextView quote = null;
quote.setTextColor(Color.parseColor("#FFFFFF"));
However this doesn't do anything. Anyone have any ideas? I could be doing this the completely wrong way.
I would have had both strings separate in my .xml file, however it is pulled through as one.
TextView quote=null;
quote=findViewById(R.id.textView1);
this is not working because you give only null value to TextView;
here textview1 is id of TextView in you xml file;
replace textView1 with your textview id

How to retrieve selected data from JSON Array?

The data on server I want to retrieve, data in JSON format but problem is that I want to retrieve only data having specific CHEF_NAME from JSON array.I am using Android Studio.
JSON Array
[{"status":"Success","recpie":[{"id":"162","image":"1580130013.png","cat_id":"17","name":"Tikka Boti Mix Recipe","chef_name":"Mehran","link":"https:\/\/www.youtube.com\/watch?v=sbPNGHIOpQY","uvlink":"","like":"2","view":"227"},{"id":"168","image":"1580211123.png","cat_id":"17","name":"Seekh Kabab Paratha Roll Recipe","chef_name":"Mehran","link":"https:\/\/www.youtube.com\/watch?v=q36Syxhjg3o","uvlink":"","like":"19","view":"158"},{"id":"176","image":"recipe_1583154151.jpg","cat_id":"17","name":"Windows","chef_name":"Imran Ahmed","link":"","uvlink":"","like":"2","view":"119"},{"id":"190","image":"recipe_1584214019.jpg","cat_id":"17","name":"Chicken Broast","chef_name":"Imran Developer","link":"","uvlink":"http:\/\/billing.synctechsol.com\/video\/VID-20200112-WA0020.mp4","like":"3","view":"108"},{"id":"161","image":"1580309239.png","cat_id":"14","name":"Sindhi Biryani Mix Recipe","chef_name":"Mehran","link":"https:\/\/www.youtube.com\/watch?v=OUG7gYTrnEk","uvlink":"","like":"4","view":"104"},{"id":"182","image":"recipe_1584126527.jpeg","cat_id":"17","name":"Broast","chef_name":"Imran Developer","link":"","uvlink":"","like":"1","view":"64"},{"id":"198","image":"recipe_1584737360.jpeg","cat_id":"17","name":"Distribution Board","chef_name":"Imran Ahmed","link":"","uvlink":"http:\/\/billing.synctechsol.com\/video\/VID-20200219-WA0035.mp4","like":"7","view":"60"},{"id":"191","image":"recipe_1584260119.png","cat_id":"17","name":"ggbvn","chef_name":"Imran Developer","link":"","uvlink":"http:\/\/billing.synctechsol.com\/video\/VID-20200315-WA0001.mp4","like":"1","view":"58"},{"id":"163","image":"1580134470.png","cat_id":"15","name":"Achar Gosht Mix Recipe","chef_name":"Mehran","link":"https:\/\/www.youtube.com\/watch?v=5EQWOSRpzHg","uvlink":"","like":"8","view":"54"},{"id":"101","image":"1580210522.png","cat_id":"12","name":"Fish Fry Mix Recipe","chef_name":"Mehran","link":"https:\/\/www.youtube.com\/watch?v=GeSloGLoPMk","uvlink":"","like":"9","view":"50"},{"id":"178","image":"recipe_1583407824.jpg","cat_id":"17","name":"uhnbj","chef_name":"hi igh","link":"","uvlink":"","like":"0","view":"47"},{"id":"177","image":"recipe_1583234674.jpeg","cat_id":"17","name":"Kabuli Pulao","chef_name":"Imran Ahmed","link":"","uvlink":"","like":"0","view":"46"}]}]
Java Code
JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObject = jsonArray.getJSONObject(0);
String status = jsonObject.getString("status");
if (status.equals("Success")) {
JSONArray recipe = jsonObject.getJSONArray("recpie");
Log.e("Lenght", String.valueOf(recipe.length()));
for (int i = 0; i < recipe.length(); i++) {
JSONObject jsonObject1 = recipe.getJSONObject(i);
SearchGetSet searchGetSet = new SearchGetSet();
String id = jsonObject1.getString("id");
String image = jsonObject1.getString("image");
String cat_id = jsonObject1.getString("cat_id");
String name = jsonObject1.getString("name");
String chef_name = jsonObject1.getString("chef_name");
String link = jsonObject1.getString("link");
String uvlink = jsonObject1.getString("uvlink");
String like = jsonObject1.getString("like");
String view = jsonObject1.getString("view");
searchGetSet.setId(id);
searchGetSet.setRec_name(name);
searchGetSet.setRec_image(image);
searchGetSet.setLikes(like);
searchGetSet.setCat_id(cat_id);
searchGetSet.setChef_name(chef_name);
searchGetSet.setLink(link);
searchGetSet.setUVlink(uvlink);
searchGetSet.setView(view);
searchList.add(searchGetSet);}}
Create and add your searchGetSet object only if the chef_name is matching the name you're searching.
My guess is (I don't know Java), you've to use a simple if-statement, while looping :
for (int i = 0; i < recipe.length(); i++) {
JSONObject jsonObject1 = recipe.getJSONObject(i);
SearchGetSet searchGetSet = null; // Do not create your searchGetSet() object yet.
String chef_name = jsonObject1.getString("chef_name");
// Check the chef's name is 'John'.
if (chef_name.equals('John')) {
String id = jsonObject1.getString("id");
String image = jsonObject1.getString("image");
String cat_id = jsonObject1.getString("cat_id");
String name = jsonObject1.getString("name");
// ...
searchGetSet = new SearchGetSet();
searchGetSet.setId(id);
searchGetSet.setRec_name(name);
searchGetSet.setRec_image(image);
// ...
// Let's add the result we have found.
searchList.add(searchGetSet);
}
}

Get View from Listview to String

I have a Listview and I need to get the String values of each row when I click the List view.
This is my code
public void ListDrwaer() {
List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("lectureraccount");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("lecturerName");
String code = jsonChildNode.optString("lecturerCode");
String outPut = name + " --- " + code;
employeeList.add(createEmployee("lecturer", outPut));
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error" + e.toString(),
Toast.LENGTH_SHORT).show();
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList,android.R.layout.simple_list_item_1,new String[] { "lecturer" }, new int[] { android.R.id.text1 });
listView.setAdapter(simpleAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
String str = parent.getAdapter().getItem(position).toString();
System.out.println(str);
}});
}
I Managed to get output like this.
{lecturer=MAGESWARY A/P MUNIANDI --- C-MAGESWARY}
My Question is how to get output like this
C-MAGESWARY
Thats all. I am new in programming and not know much. Do i need to change my code completly. If it is, How?
Thank you.
You can do this using split in your onItemClick method as below:
String str = parent.getAdapter().getItem(position).toString();
String[] parts = str.split("---");
String part1 = parts[0];
String part2 = parts[1]; // This is the part you want
String output = part2.replaceAll("\\s+","") //trim whitespace
String output2 = output.replace("}", ""); // remove the "}" character
System.out.println(output2);
Output:
C-MAGESWARY
This is a Java question. Look at link Class String Oracle. Besides the previous answer, look at methods substring, indexOf, and lastIndexof.
You can make it easy on yourself since you have control of the string format in your ListView. Simply add unique characters before your example "C-MAGESWARY", like "!#" . Then you can search the string for those characters. Remember this technique for future apps.

How to use removeSpan() on android textview?

Pardon me if I'm asking a dumb question but I would like to know how to remove a span from text in my textview. This is how my span method looks like.
public CharSequence setTextStyleItalic(CharSequence text) {
StyleSpan style = new StyleSpan(Typeface.ITALIC);
SpannableString str = new SpannableString(text);
str.setSpan(style, 0, text.length(), 0);
return str;
}
and this is how i call it.
tvTitle.setText(setTextStyleItalic(tvTitle.getText()));
I would really like to know how to remove this italic span in java using removeSpan() please.
Get the text of the TextView and cast it to SpannableString then you can use the getSpans(int queryStart, int queryEnd, Class<T> kind) method to iterate over those spans and remove them
SpannableString ss=(SpannableString)txtView.getText();
ForegroundColorSpan[] spans=ss.getSpans(0, txtView.getText().length(), ForegroundColorSpan.class);
for(int i=0; i<spans.length; i++){
ss.removeSpan(spans[i]);
}
First get text from your span using getSpans()
removeTextSpan = tvTitle.getSpans(0, tvTitle.getText().length(),ForegroundColorSpan.class);
for (int i = 0; i < removeTextSpan.length; i++)
tvTitle.removeSpan(removeTextSpan[i]);
i think this will help
Set and remove span for ITALIC
val spannable: Spannable = SpannableStringBuilder(editText.text)
val start = editText.selectionStart
val end = editText.selectionEnd
val allSpans: Array<StyleSpan> = spannable.getSpans(0,
spannable.length, StyleSpan::class.java)
if (doItalic) {
spannable.setSpan(
StyleSpan(ITALIC), start, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
} else {
for (span in allSpans) {
if (span.style == ITALIC)
spannable.removeSpan(span)
}
}
editText.setText(spannable)

How to apply onClick function to open its URL

I have the following code:
try {
jArray = new JSONArray(result);
JSONObject json_data = null;
String myString = null;
for (int i = 0; i < jArray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
json_data = jArray.getJSONObject(i);
map.put("id", String.valueOf(json_data.getString("news_id")));
map.put("title", json_data.getString("news_title"));
myString = myString + "| "
+ json_data.getString("news_title").toString();
}
String myString1 = myString.replace("null", "");
txt1 = (TextView) findViewById(R.id.twxmorq);
txt1.setSelected(true);
txt1.setText(myString1);
I am printing whole title in text view with marquee style in single line with ' ! ' partition. Now I want to apply onclick on each title and open it in new activity with detail news in web view.
Detail news will load with this data:
"http://www.taxmann.com/TaxmannWhatsnewService/Services.aspx?service=getnewsdetails&newsid=" + id
Where id will differ for different title. Can you please tell me the logic or post some code for Implementation?
I'm not able to do this.
Either in layout xml file, for TextView attribute add,
android:autoLink="web"
Or,
txt1.setMovementMethod(LinkMovementMethod.getInstance());

Categories