0
        ViewHolder holder = null;

        if (convertView == null) {

            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.row_ten_words,null);

            holder = new ViewHolder();
            holder.txtSrc = (EditText) convertView.findViewById(R.id.txt_src_word);
            holder.btnTranslate = (Button) convertView.findViewById(R.id.btn_translate);
            holder.txtDes = (EditText) convertView.findViewById(R.id.txt_des_word);}`

Here , I want to use my RelativeLayout that I have created programmatically in place of R.layout.row_ten_words

Code that I want to use is:

          ` EditText txtSrc=new EditText(this);

    EditText txtDes=new EditText(this);

    Button btn_translate=new Button(this);

    translate.setText("translate");

     rl=new RelativeLayout(this);

    rl.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT));

    rl.addView(txtSrc);

    rl.addView(btn_translate);

    rl.addView(txtDes);`

I am totally new to android but know JAVA well. Please suggest me something if I am wrong in my approach.

Thanks in advance.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
Amol Bharmoria
  • 239
  • 4
  • 23

1 Answers1

0

You are essentially replacing the inflation of a View with your dynamic View creation. To create a View programmatically, you need a Context reference. You will need to add a class member and set it in the Adapter's constructor (this may not be exactly like yours, but you get the idea):

Context context;

public SomeAdapter(Context context, int resource)
{
    super(context, resource);

    this.context = context;
    ...
    ...

And then:

if (convertView == null)
{
    EditText txtSrc = new EditText(context);
    EditText txtDes = new EditText(context);
    Button btnTranslate = new Button(context);
    btnTranslate.setText("translate");

    rl = new RelativeLayout(context);
    rl.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    rl.addView(txtSrc);
    rl.addView(btn_translate);
    rl.addView(txtDes);

    holder = new ViewHolder();
    holder.txtSrc = txtSrc;
    holder.btnTranslate = btnTranslate;
    holder.txtDes = txtDes;

    convertView = rl;
}
Mike M.
  • 38,532
  • 8
  • 99
  • 95
  • Thanks for the answer but method addView is not defined for View @Mike – Amol Bharmoria Mar 13 '14 at 09:13
  • ClassCastException in convertView=rl; – Amol Bharmoria Mar 13 '14 at 09:49
  • hmm ok..m new to android....any suggestion will be appreciated..thanks for the help – Amol Bharmoria Mar 13 '14 at 10:06
  • 03-13 06:18:11.417: E/AndroidRuntime(2351): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams 03-13 06:18:11.417: E/AndroidRuntime(2351): at android.widget.ListView.measureScrapChild(ListView.java:1167) 03-13 06:18:11.417: E/AndroidRuntime(2351): at 03-13 06:18:11.417: E/AndroidRuntime(2351): at android.view.View.measure(View.java:15848) 03-13 06:18:11.417: E/AndroidRuntime(2351): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008) 03-13 06:18:11.41 – Amol Bharmoria Mar 13 '14 at 10:30
  • 03-13 07:23:28.997: E/AndroidRuntime(2939): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams 03-13 07:23:28.997: E/AndroidRuntime(2939): at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1723) – Amol Bharmoria Mar 13 '14 at 11:24
  • I am Using the following code...but it is giving above error...RelativeLayout convert = new RelativeLayout(context); convert.setLayoutParams(new android.widget.RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT)); convert.addView(txtSrc); convert.addView(btnTranslate); convert.addView(txtDes); holder = new ViewHolder(); holder.txtSrc = txtSrc; holder.btnTranslate = btnTranslate; holder.txtDes = txtDes; convertView=convert; – Amol Bharmoria Mar 13 '14 at 11:32
  • No, no. You need to use `AbsListView.LayoutParams`, __not__ `RelativeLayout.LayoutParams`. – Mike M. Mar 13 '14 at 11:36
  • Even after using AbsListView it is giving samme exception as above. M doing this coz somemone have asked me to do it without xml. – Amol Bharmoria Mar 13 '14 at 11:49
  • To set the position of widgets according to different device screen resolution on percentage basis. – Amol Bharmoria Mar 13 '14 at 11:56
  • In XML we can give only fixed size.....moreover I am eager to know if we can do it this way or not...in xml it is working perfectly fine – Amol Bharmoria Mar 13 '14 at 11:57
  • I am missing somethin somewhere.....lack of experience in android...even after changing to RelativeLayout.LayoutParams and AbsListView.LayoutParams .....the cast exception shows that ViewGroup LayoutParams can not be casted into RelativeLayout params. Which means changes arn't having any effect. the layout params for convert remain ViewGroup.LayoutParams – Amol Bharmoria Mar 13 '14 at 12:11
  • By the way...thanx a lot for helping me...M new to this site too...can we add users here for future reference?? – Amol Bharmoria Mar 13 '14 at 12:17
  • your email id??? By adding users I mean if I want to cantact you in future to clearify any doubts...Is it possible? – Amol Bharmoria Mar 13 '14 at 12:24