7 Mar 2012

Custom Controls in Android

Create Method For Custom Controls:

public LinearLayout Generate_Button(int id, final String catname)
       {
              Button cat_btn = new Button(this);
              cat_btn.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

              cat_btn.setTag(""+ id);
              cat_btn.setText(catname);
              cat_btn.setTextSize(18);
              cat_btn.setBackgroundResource(R.color.White);
              cat_btn.setGravity(Gravity.LEFT);

              //This Text View For Seprator....
              TextView txt_seprator=new TextView(MainCategory.this);
              txt_seprator.setBackgroundResource(R.drawable.line);

              cat_btn.setOnClickListener(new OnClickListener()
              {
                     @Override
                     public void onClick(View v)
                     {
                           Button btn = (Button) v;
                           int id = Integer.parseInt(btn.getTag().toString());

                     }
              });

              //Add Button And Text View in Layout..
              LinearLayout lin = new LinearLayout(this);
              lin.setOrientation(LinearLayout.VERTICAL);

              lin.addView(cat_btn);
              lin.addView(txt_seprator, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,2)); 
              return lin;
       }

       Call Method:

              LinearLayout lin_lay = Generate_Button(1,HB);
       LinearLayout lin_dym_cat_main.addView(lin_lay);


I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.
Thank you :)

No comments:

Post a Comment