1

I am developing the Android version of an app and I am trying to replicate the design shown in this picture. http://s1298.photobucket.com/user/Mitch_Thornton/media/Screenshot2013-06-03at50428PM_zps3d9b6acc.png.html?sort=3&o=0

The data I have stored is fed in from a website. So far, I have successfully made it so the autocomplete searches through the data correctly. My problem is the formatting. I want to be able to control and stylize certain parts of the text. Specifically the percentage. If the percentage is above 75%, it should be green, if below, it should be either red or yellow.

The problem is, I am handing one long string, so I can't individually style certain pieces.

I was able to get a poor man's version of styling by adding spaces and a line, but nothing too special.... Here's what I have so far http://s1298.photobucket.com/user/Mitch_Thornton/media/Screenshot2013-06-03at51100PM_zps6859e634.png.html?sort=3&o=1

I am new to Android, so really what I have gotten so far is taken from tutorials and some self-teaching. I am really appreciative for your help

Search_Page:

public class Search_Page extends Activity implements TextWatcher {

AutoCompleteTextView myAutoComplete;

// PROBLEM: This initializes and defines the string array...It also says how
// big the string array can be
// So this limits how many items I can grab from Parse and fill in the
// array....
// I can only fill/replace as many items/elements that are defined right
// here
String item[] = { "Jack Daniels", "Captain Morgan", "Corona", "Dubra",
        "KeyStone Light", "Burnettes", "Budweiser", "Bud Light",
        "Smirnoff", "Grey Goose", "Smirnoff", "Mike's Hard Lemonade",
        "Poland Springs", "Yukon Jack", "Magic Hat",
        "Captain Morgan Black", "Absolut", "Absolut Raspberry",
        "Absolut Mandarin", "Absolut Peppar", "Absolut Citron",
        "Absolut Vanilla", "Wild Turkey" };
public String selection = "";
ParseObject dealsObject;
int n = 0;
int p = 0;
String mydate;
String objectId;

private String[] obj = { "Jack Daniels", "Captain Morgan", "Corona",
        "Dubra", "KeyStone Light", "Burnettes", "Budweiser", "Bud Light",
        "Smirnoff", "Grey Goose", "Smirnoff", "Mike's Hard Lemonade",
        "Poland Springs", "Yukon Jack", "Magic Hat",
        "Captain Morgan Black", "Absolut", "Absolut Raspberry",
        "Absolut Mandarin", "Absolut Peppar", "Absolut Citron",
        "Absolut Vanilla", "Wild Turkey" };

private int[] percent = { 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4,
        5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2,
        3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_page);

    Parse.initialize(this, "vUz23Z6zdIL1jbxbVWeLpsSdu1ClTu3YiG30zTWY",
            "4BTyoq1QQKows8qVJV7lvU3ZokSRrLFyOCPzffwJ");

    myAutoComplete = (AutoCompleteTextView) findViewById(R.id.myautocomplete1);

    // Using this Parsequery...I can grab/locate the objects from the Parse
    // list...Here I grabbed the objects that have Burnettes and the price
    // of it
    // Eventually I may need to set a limit on how many results I will get
    // from the for loop....So far I think it is limited to 100 by default
    ParseQuery query = new ParseQuery("Deals");

    // query.whereEqualTo("Brand", "Burnettes");
    query.findInBackground(new FindCallback() {

        @Override
        public void done(List<ParseObject> objects, ParseException e) {
            if (e == null) {
                Log.d("Brand", "Retrieved " + objects.size() + " Brands");
                for (ParseObject dealsObject : objects) {
                    // use dealsObject.get('columnName') to access the
                    // properties of the Deals object

                    SimpleDateFormat format = new SimpleDateFormat(
                            "MMM dd, yyyy");
                    String date = format.format((dealsObject.getCreatedAt()));
                    objectId = dealsObject.getObjectId();
                    // Grabs the position of the object in Parse and spits
                    // out the Brand, price.
                    // This is then put into the autocomplete dropdown
                    percent[n] = dealsObject.getInt("Percentage");

                    item[n] = dealsObject.getString("Brand") + " "
                            + dealsObject.getString("Size") + " $"
                            + dealsObject.getString("Price")
                            + "                "
                            + dealsObject.getInt("Percentage") + "%" + "\n"
                            + date;
                    obj[n] = objectId;

                    n++;
                }

            } else {
                Log.d("Brand", "Error: " + e.getMessage());
            }
        }

    });
    myAutoComplete.addTextChangedListener(this);
    myAutoComplete.setAdapter(new ArrayAdapter<String>(this,
            R.layout.my_custom_dropdown, item));

    // When the user selects an element from the dropdown, it will
    // automatically
    // go to the DealPage
    myAutoComplete.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

            while (true) {
                if (arg0.getItemAtPosition(arg2).equals(item[p])) {
                    break;
                }
                p++;
            }

            selection = (String) arg0.getItemAtPosition(arg2);
            Log.d("Before", selection);

            Intent myIntent = new Intent("com.alpha.dealtap.DEALPAGE");
            myIntent.putExtra("Stuff", selection);
            myIntent.putExtra("ObjectId", obj[p]);
            myIntent.putExtra("Percent", percent[p]);

            startActivity(myIntent);
        }
    });
    Log.d("After", selection);

    Button deal = (Button) findViewById(R.id.b2);
    Button map = (Button) findViewById(R.id.map);

    deal.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            startActivity(new Intent("com.alpha.dealtap.DEALPAGE"));

        }

    });

    Intent myIntent = new Intent(Search_Page.this, DealPage.class);

    map.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            startActivity(new Intent("com.alpha.dealtap.MAP"));
        }
    });

}

@Override
protected void onPause() {

    super.onPause();
}

@Override
public void afterTextChanged(Editable s) {
    // 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 onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub

}

}

Search_Page.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#FFFFFF" >

<Button
    android:id="@+id/map"
    style="@style/ButtonText"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/b2"
    android:background="@drawable/yellow_button"
    android:text="Map"
    android:textSize="20sp" />

<Button
    android:id="@+id/b2"
    style="@style/ButtonText"
    android:layout_width="160dp"
    android:layout_height="50dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/yellow_button"
    android:text="Deal"
    android:textSize="20sp" />

<AutoCompleteTextView
    android:id="@+id/myautocomplete1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/map"
    android:completionThreshold="1"
    android:ems="10"
    android:hint="Search for deals" >

    <requestFocus />
</AutoCompleteTextView>

</RelativeLayout>

my_custom_dropdown.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:textColor="@color/black"
android:textSize="17sp" />

1 Answers1

1

well the answer is to customize your list view and not set a long single string , instead use a model with different properties, look at a nice example here how to customize listview row android

look at the adpapter where a template is inflated which represents each row

public View getView(int position, View convertView, ViewGroup parent){
        String myText = getItem(position);           

        if(convertView == null){ // If the View is not cached
            // Inflates the Common View from XML file
            convertView = this.inflater.inflate(R.id.my_row_layout, null);
        }

        // Select your color and apply it to your textview
        int myColor;
        if(myText.substring(0, 1) == "a"){
            myColor = Color.BLACK;
        }else{
        ....
        }
Community
  • 1
  • 1
varun
  • 4,522
  • 33
  • 28