amitsharma amitsharma - 3 months ago 12
Android Question

How to design text view in android without layout use only java class code

I am designing an android application where I have not need to create any layout xml file, but create over the Java code itself. I try with this way:-

public class MainActivity extends Activity{
TextView tb;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
getWindow().getDecorView().setBackgroundResource(R.drawable.ch);

}
}

Answer

Use this code, this will work for you

public class Amit extends Activity{
TextView tb;    

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    getWindow().getDecorView().setBackgroundResource(R.drawable.ch);
       tb = new TextView(Amit.this);

    tb.setText("hallo hallo");
    tb.setId(5);
    tb.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));
setContentView(tb);


}