Pip997
Pip997

Reputation: 13

Can't get CalendarView to setDate to current date

I searched around on here and google trying to find something that can help me focus the current date of the system. I tried using the .setDate(long,boolean,boolean) But it doesn't seem to work.

public class MainActivity extends ActionBarActivity {

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

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

public void Currentdate(View view) {
    CalendarView cal = new CalendarView(this);
    long time = System.currentTimeMillis();
    cal.setDate(time,false,true);


}

}

my xml

    <CalendarView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/calendarView"
    android:layout_gravity="center_horizontal"
    android:firstDayOfWeek="2"
    android:weekNumberColor="#ff8b8b88"
    android:clickable="true"/>

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Current date"
    android:id="@+id/button"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/calendarView"
    android:layout_alignEnd="@+id/calendarView"
    android:onClick="Currentdate"/>

Upvotes: 1

Views: 2404

Answers (2)

Refnhaldy
Refnhaldy

Reputation: 211

I use this code

private void Currentdate() {
    CalendarView cal = (CalendarView)findViewById(R.id.calendarView);
    long time = System.currentTimeMillis();
    cal.setDate(time,false,true);
}

Upvotes: 0

Alvaro De la Cruz
Alvaro De la Cruz

Reputation: 51

You don't even cast your CalendarView into your code, try this:

CalendarView cal = (CalendarView)findViewById(R.id.calendarView);

instead of:

CalendarView cal = new CalendarView(this);

Upvotes: 1

Related Questions