Pages

Saturday, September 8, 2012

Example of Linear Layout in Java code without Xml


package example.withoutxml;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;

public class examplenoxml extends Activity {
 



    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
      //  setContentView(R.layout.main);
       
   
        LinearLayout ll=new LinearLayout(this);
        ll.setBackgroundColor(R.drawable.day);
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setBackgroundResource(R.drawable.bunch);
        ll.setGravity(LinearLayout.VERTICAL);
       // ll.setLayoutParams(LinearLayout.)
        ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                                    LayoutParams.MATCH_PARENT));
     
        EditText et=new EditText(this);
        EditText et1=new EditText(this);
        ProgressBar pb=new ProgressBar(this);
        pb.setPressed(false);
        pb.setLayoutParams(new LayoutParams
                    (LayoutParams.WRAP_CONTENT,
                     LayoutParams.WRAP_CONTENT));
     
        et1.setWidth(100);
        et1.setText("text");
        et1.setTextSize(50);
        et1.setSelection(et1.getText().length());
       // et.setGravity(Gravity.LEFT);
        ll.addView(et);
        ll.addView(et1);
        ll.addView(pb);
       // ll.addView(list);
        Button b1=new Button(this);
        ll.addView(b1);
        b1.setWidth(50);
        b1.setHeight(50);
        b1.setBackgroundResource(R.drawable.pinkrose);
        b1.setText("click here");
        b1.setGravity(Gravity.BOTTOM);
       
        b1.setOnClickListener(this);
       setContentView(ll);
    }
}

No comments:

Post a Comment