Thursday, 2 May 2013

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, newLinearLayout.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);