public class ListItem extends AppCompatActivity {
ListView listView;
ListAdapter adapter;
ProgressDialog loading;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
listView = (ListView) findViewById(R.id.lv_items);
getItems();
}
private void getItems() {
loading = ProgressDialog.show(this,"Loading","please wait",false,true);
StringRequest stringRequest = new StringRequest(Request.Method.GET, "GooglSheet URL",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
parseItems(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
);
int socketTimeOut = 50000;
RetryPolicy policy = new DefaultRetryPolicy(socketTimeOut, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(policy);
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(stringRequest);
}
private void parseItems(String jsonResposnce) {
ArrayList<HashMap<String, String>> list = new ArrayList<>();
try {
JSONObject jobj = new JSONObject(jsonResposnce);
JSONArray jarray = jobj.getJSONArray("items");
for (int i = 0; i < jarray.length(); i++) {
JSONObject jo = jarray.getJSONObject(i);
String scriptName = jo.getString("scriptName");
String breakoutTimeUnformated = jo.getString("breakoutTime");
String breakoutTime=breakoutTimeUnformated.replace("+0530", "").replace("T", " ");
String TypeOfCall = jo.getString("TypeOfCall");
double TriggerPrice = jo.getDouble("TriggerPrice");
double StopLoss = jo.getDouble("StopLoss");
double TargetOne = jo.getDouble("TargetOne");
double TargetTwo = jo.getDouble("TargetTwo");
String rangeTarget="("+TargetOne+">-<"+TargetTwo+")";
double ReverseTrade = jo.getDouble("ReverseTrade");
HashMap<String, String> item = new HashMap<>();
item.put("breakoutTime",breakoutTime);
item.put("scriptName", scriptName);
item.put("TriggerPrice",TypeOfCall+" @\n"+TriggerPrice);
item.put("StopLoss","SL @\n"+StopLoss);
item.put("rangeTarget","Target\n"+rangeTarget);
item.put("ReverseTrade","Reverse\n"+ReverseTrade);
list.add(item);
Collections.reverse(list);
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new SimpleAdapter(this,list,R.layout.list_item_row,
new String[]{"scriptName","breakoutTime","TriggerPrice","StopLoss",
"rangeTarget","ReverseTrade"},
new int[]{R.id.tv_scriptName,R.id.tv_breakoutTime,R.id.tv_TriggerPrice,R.id.tv_StopLoss,R.id.tv_target,R.id.tv_reverse});
listView.setAdapter(adapter);
loading.dismiss();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@color/colorPrimary"
android:id="@+id/resv">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
android:id="@+id/l">
<TextView
android:id="@+id/tv_breakoutTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="ForDate"
android:gravity="center"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="1"
android:textSize="15sp"
android:letterSpacing="0.2"
android:padding="2sp"
android:textColor="@android:color/white"
/>
<TextView
android:id="@+id/tv_scriptName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Script Name"
android:gravity="center"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="1"
android:textSize="15sp"
android:padding="2sp"
android:letterSpacing="0.2"
android:textColor="@android:color/white"
android:layout_below="@+id/tv_breakoutTime"
/>
<TextView
android:id="@+id/tv_TypeOfCall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Call Type"
android:gravity="center"
android:layout_weight="1"
android:ellipsize="end"
android:letterSpacing="0.2"
android:maxLines="3"
android:padding="2sp"
android:textSize="15sp"
android:textColor="@android:color/white"
android:layout_below="@+id/tv_scriptName"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_below="@+id/l"
android:id="@+id/l1">
<TextView
android:gravity="center"
android:id="@+id/tv_TriggerPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="TriggerPrice"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="5"
android:padding="2sp"
android:textSize="15sp"
android:textColor="@android:color/white"
android:letterSpacing="0.2"
/>
<TextView
android:gravity="center"
android:id="@+id/tv_StopLoss"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="SL"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="5"
android:padding="2sp"
android:textSize="15sp"
android:textColor="@android:color/white"
android:letterSpacing="0.2"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_below="@+id/l1"
android:id="@+id/l2">
<TextView
android:gravity="center"
android:id="@+id/tv_target"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Target"
android:layout_weight="1"
android:padding="2sp"
android:ellipsize="end"
android:maxLines="5"
android:textSize="15sp"
android:textColor="@android:color/white"
android:letterSpacing="0.2"
/>
<TextView
android:gravity="center"
android:id="@+id/tv_reverse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Reverse"
android:layout_weight="1"
android:ellipsize="end"
android:padding="2sp"
android:maxLines="5"
android:textSize="15sp"
android:textColor="@android:color/white"
android:letterSpacing="0.2"
/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/colorSell"
android:layout_below="@+id/l2"/>
/>
</RelativeLayout>
How do I change the color of linearLayout based on the text I get from TypeOfCall textView (If contains BUY then another color if SELL then Other?
I tried various code structure but did not get a working solution.
Its an AppCompatActivity activity using ListView, and simple adapter to pulled in data which I am getting from google sheet.