user interface - Android: View embedded in layout. onDraw() method triggered but the screen not update immediately -


i new on android. working on simple sudoku solver , have problem view. solverview contain on custom view embedded layout contains 2 buttons. when click on cell of sudoku grid, keypad dialog appear allow me choose number cell. however, after click on number on keypad, sudoku grid not display although ondraw() method called. instead, update on next time touch screen. following code

public class solverview extends view{  private string tag = "solver view"; private int size; private solver solver;  private static int selu, selv;  public solverview(context context) {     super(context);     this.solver = (solver) context;     setfocusable(true);     setfocusableintouchmode(true);     setlayertype(view.layer_type_software, null); }  public solverview(context context, attributeset attrs) {     super(context, attrs);     this.solver = (solver) context;     setfocusable(true);     setfocusableintouchmode(true);     setlayertype(view.layer_type_software, null); } public solverview(context context, attributeset attrs, int defstyle) {     super(context, attrs, defstyle);     this.solver = (solver) context;     setfocusable(true);     setfocusableintouchmode(true);     setlayertype(view.layer_type_software, null); }     public int getboardsize() {     int height = getheight();     int width = getwidth();     return math.min(height, width); }  @override protected void ondraw(canvas canvas) {     super.ondraw(canvas);     size = getboardsize();     int cellsize = size / 9;      paint light = new paint();     light.setcolor(getresources().getcolor(r.color.puzzle_light));      paint dark = new paint();     dark.setcolor(getresources().getcolor(r.color.puzzle_dark));     dark.settypeface(typeface.default_bold);      (int = 0; <= 9; i++) {         canvas.drawline(i * cellsize, 0, * cellsize, size, light);         if (i % 3 == 0)              canvas.drawline(i * cellsize, 0, * cellsize, size, dark);     }       (int = 0; <= 9; i++) {         canvas.drawline(0, * cellsize, size, * cellsize, light);         if (i % 3 == 0)             canvas.drawline(0, * cellsize, size, * cellsize, dark);     }     paint foreground = new paint(paint.anti_alias_flag);     foreground.setcolor(getresources().getcolor(r.color.puzzle_dark));     foreground.setstyle(style.fill);     foreground.settextsize(cellsize * 0.75f);     foreground.settextalign(paint.align.center);     fontmetrics fm = foreground.getfontmetrics();     float x = cellsize / 2 - (fm.ascent + fm.descent) / 2;     float y = cellsize / 2 ;     (int = 0; < 9; i++) {         (int j = 0; j < 9; j++) {             canvas.drawtext(this.solver.getcellstring(i, j), j * cellsize + y, * cellsize + x, foreground);         }     }  }  @override public boolean ontouchevent(motionevent event) {     if (event.getaction() != motionevent.action_down)         return super.ontouchevent(event);     int cellsize = size / 9;     int = (int)event.gety() / cellsize;     int j = (int)event.getx() / cellsize;     if (i < 0 || > 8 || j < 0 || j > 8)          return false;     this.selu = i;     this.selv = j;     log.d(tag, "ontouchevent: " + selu + " , " + selv);     this.solver.showkeypad(i, j);     invalidate();     return true; }  public void setcell(int t) {     log.d(tag, "in setcell: t = " + t);     log.d(tag, "in setcell: selu = " + selu);     log.d(tag, "in setcell: selv = " + selv);     if (solver.setcellifvalid(selu, selv, t)) {         log.d(tag, "setcell: going invalidate()");         log.d(tag, "setcell: value change: " + solver.a[selu][selv]);         invalidate();     }  } 

}

and structure of layout:

<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:paddingbottom="@dimen/zero" android:paddingleft="@dimen/zero" android:paddingright="@dimen/zero" android:paddingtop="@dimen/zero" tools:context="com.trungkienioicamp.helloworld.solver" >  <com.trungkienioicamp.helloworld.solverview     android:id="@+id/board"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:layout_alignparentleft="true"     android:layout_alignparentright="true"     android:layout_alignparenttop="true" />  <button     android:id="@+id/clear"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignparentbottom="true"     android:layout_alignparentleft="true"     android:layout_marginbottom="74dp"     android:layout_marginleft="29dp"     android:text="clear" />  <button     android:id="@+id/solve"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignbaseline="@+id/clear"     android:layout_alignbottom="@+id/clear"     android:layout_alignparentright="true"     android:layout_marginright="61dp"     android:text="solve" /> 

can suggest me solution one? thank reading

updated: problem gone when set content view of main activity solverview instance

setcontentview(solverview); 

but not work when set content view layout

setcontentview(r.layout.activity_solver); 

instead, update on next time touch screen

try call invalidate instance of solverview after click on number on keypad.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

qt - How to embed QML toolbar and menubar into QMainWindow -