Android : Create View Without Using Xml in Android

on Monday, September 1, 2014


I have a abstract class:



public abstract class Component {
public abstract View addComponent();
public abstract void setAttribute(String attributeName, Object attributeValue);

}


And I have a sub class is EditText:



public class TextBox extends Component {

private EditText txt;

@Override
public EditText addComponent() {

//Error message: The constructor EditText(TextBox) is undefined.
txt = new EditText(this);

return txt;
}

@Override
public void setAttribute(String attributeName, Object attributeValue) {

switch (attributeName) {
case "Width":
txt.setWidth((int) attributeValue);
break;
case "Height":
txt.setHeight((int) attributeValue);
break;
case "Top":
txt.setY((float) attributeValue);
break;
case "Left":
txt.setX((float) attributeValue);
break;
case "Name":
txt.setText((String) attributeValue);
default:
break;
}

}

}


Finally, in the MainACtivity:



public class MainActivity extends ActionBarActivity {

LinearLayout LinLay;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

ScrollView sv = new ScrollView(this);

LinLay = new LinearLayout(this);
sv.addView(LinLay);

TextBox t = new TextBox();
EditText v = t.addComponent();

LinLay.addView(v);


}
}


I want to create a EditText without xml in MainActivity class. But in the EditText class have a error. The program do not run.


0 comments:

Post a Comment