Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

I'm trying to increase the number when I click the button and at the same time I want to show it with bar graph.

Hello

I'm trying to increase the number when I click the button(increase) and subtract when I click other button(decrease).

and I want to display current number on textview, and make it graph

I was able to make change at textview but graph isn't changing at all

Also I'm trying to save the data daily and update automatically(daily) on graph

I'm using GraphView, which is one of graph library

can anyone help me solving this problem?

here is my code

import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView;

import com.jjoe64.graphview.GraphView; import com.jjoe64.graphview.ValueDependentColor; import com.jjoe64.graphview.helper.DateAsXAxisLabelFormatter; import com.jjoe64.graphview.series.BarGraphSeries; import com.jjoe64.graphview.series.DataPoint; import com.jjoe64.graphview.series.LineGraphSeries;

import java.util.Calendar; import java.util.Date;

public class MainActivity2 extends AppCompatActivity { int numtest;

Button add, sub;
TextView display;

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

/////////////////////////////////////////////////////////////////////////////////////////////////// add, subtract code numtest = 0;

    add = (Button) findViewById(R.id.button2);
    sub = (Button) findViewById(R.id.button3);
    //display = (TextView) findViewById(R.id.counter);

    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            numtest = numtest + 1;
            display.setText("" + numtest);
        }
    });

    sub.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (numtest < 0){
                numtest =  0;
                display.setText(numtest+ "");
            }
            if (numtest > 0) {
                numtest = numtest - 1;
                display.setText(numtest+ "");
            }
        }
    });

///////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////// convert text to integer (numtest) //String value= counter.getText().toString(); //int i = Integer.parseInt(value);

    display = (TextView)findViewById(R.id.counter);
    int i = Integer.valueOf(display.getText().toString());

///////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////graph code

    Calendar calendar = Calendar.getInstance();
    Date d1 = calendar.getTime();
    calendar.add(Calendar.DATE, 1);
    Date d2 = calendar.getTime();
    calendar.add(Calendar.DATE, 1);
    Date d3 = calendar.getTime();
    calendar.add(Calendar.DATE, 1);
    Date d4 = calendar.getTime();
    calendar.add(Calendar.DATE, 1);
    Date d5 = calendar.getTime();
    calendar.add(Calendar.DATE, 1);

    GraphView graph = (GraphView) findViewById(R.id.graph);
    BarGraphSeries<DataPoint> series = new BarGraphSeries<DataPoint>(new DataPoint[] {
            new DataPoint(d1, i),
            new DataPoint(d2, 5),
            new DataPoint(d3, 3),
            new DataPoint(d4, 2),
            new DataPoint(d5, 6)
    });
    graph.addSeries(series);

// styling series.setValueDependentColor(new ValueDependentColor<DataPoint>() { @Override public int get(DataPoint data) { return Color.rgb((int) data.getX() * 255 / 4, (int) Math.abs(data.getY() * 255 / 6), 100); } });

    series.setSpacing(70);

// draw values on top series.setDrawValuesOnTop(true); series.setValuesOnTopColor(Color.BLACK); //series.setValuesOnTopSize(50);

// set date label formatter graph.getGridLabelRenderer().setLabelFormatter(new DateAsXAxisLabelFormatter(MainActivity2.this)); graph.getGridLabelRenderer().setNumHorizontalLabels(4); // only 4 because of the space

// set manual x bounds to have nice steps graph.getViewport().setMinX(d1.getTime()); graph.getViewport().setMaxX(d4.getTime()); graph.getViewport().setXAxisBoundsManual(true);

///////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////// Button to activity3 Button activitymain3;

    activitymain3=(Button)findViewById(R.id.button5);
    activitymain3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent1 = new Intent(MainActivity2.this, MainActivity3.class);
            startActivity(intent1);
        }
    });

///////////////////////////////////////////////////////////////////////////////////////////////////

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}