user interface - how to find view by its view class in android -


i inspired following code:

@test public void ontype() throws exception {     ...     onview(isassignablefrom(edittext.class)).perform(typetext("a"));     ... } 

how view view class in activity, think it's useful control 3rd custom view

if have object reference class example class derived view.class

view someview <-  findviewbyid(int),  getview(), getrootview() etc // method return view  

you can check if can assign other class cast - in way moving right left ( examples bellow ):

edittext <- textview <- view  framelayout <- viewgroup  <- view numberpicker <- linearlayou <- viewgroup <- view 

example application:

// view class reference call find view id  view view = findviewbyid(r.id.someview); if(view!=null) {      class viewclass = view.getclass()      //  check if can assign edit text class  class ???        if(edittext.class.isassingablefrom(viewclass)) {            // u can cast edit text , stuff edit text  class did casting  while in case allowed:            edittext edittext = (edittext) view;             edittext.settext(string);       } } 

this way not rise exception when example sort of cast without check:

 ((edittext) view).settext(string); 

if view class type other edittext or not derived edit text class exception thrown.

from class implementation:

public final class class<t> implements serializable, annotatedelement, genericdeclaration, type {  ...   /**  * can {@code c}  assigned class? example, string can assigned object  * (by upcast), however, object cannot assigned string potentially exception  * throwing downcast necessary. interfaces, class implements (or  * interface extends) can assigned parent, not vice-versa.  * classes may assign themselves. classes primitive types may not assign each other.  *  * @param c class check.  * @return {@code true} if {@code c} can assigned class  *         represented {@code class}; {@code false} otherwise.  * @throws nullpointerexception if {@code c} {@code null}.  */ public boolean isassignablefrom(class<?> c) { 

see also:

the type comparison operator instanceof


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -