Search code examples
androidandroid-layoutandroid-relativelayoutviewflipper

Android set a view on viewflipper if a match is found


I know that it might be a simple solution for this but I am not able to figure it out. I want to set the first view on the viewflipper to be the one which was found true in if statement. Heres the code, which is inside a while loop:

String itrstr = itr.next();
RelativeLayout rl = new RelativeLayout(this);
TextView titletv= new TextView(this);
TextView bodytv = new TextView(this);
            
if(itrstr.equalsIgnoreCase(titlestr)){
    titletv.setText(Html.fromHtml(itrstr));
    titletv.setPadding(0, 0, 0, 0);
    bodytv.setText(Html.fromHtml(news.get(itrstr)));
    titletv.setId(count);
    bodytv.setPadding(0, 50,0, 0);
    rl.addView(titletv);
    rl.addView(bodytv);
    rl.setId(1);
 }else{    
        titletv.setText(Html.fromHtml(itrstr));
        titletv.setPadding(0, 0, 0, 0);
        bodytv.setText(Html.fromHtml(desc.get(count)));
        titletv.setId(count);
        bodytv.setPadding(0, 50,0, 0);
        rl.addView(titletv);
        rl.addView(bodytv);
        rl.setId(2+count);
  }
 vf.addView(rl);
 count++;

Thanks

Edit-1

I solved this by adding the matched title and body before looping through them, but I am getting an empty screen in between the matched view and the rest of the views.

titletv.setText(Html.fromHtml(titlestr));
        titletv.setPadding(0, 0, 0, 0);
       bodytv.setText(Html.fromHtml(news.get(titlestr)));
      
        titletv.setId(count);
        bodytv.setPadding(0, 50,0, 0);
        
        bodytv.setHorizontalScrollBarEnabled(true);
        bodytv.setVerticalScrollBarEnabled(true);
        bodytv.setMovementMethod(new ScrollingMovementMethod());
        rl.addView(titletv);
        rl.addView(bodytv);
        rl.setId(1);
        vf.addView(rl);
        
      //add the view 
      while(itr.hasNext()){
          //Log.i("ArrayList: "+Integer.toString(count),itr.next()+" Desc: "+desc.get(count));
          String itrstr = itr.next();
          rl = new RelativeLayout(this);
            titletv= new TextView(this);
            bodytv = new TextView(this);
            
            if(!itrstr.equalsIgnoreCase(titlestr)){
                
                 
                    
                    titletv.setText(Html.fromHtml(itrstr));
                    titletv.setPadding(0, 0, 0, 0);
                   bodytv.setText(Html.fromHtml(desc.get(count)));
                    titletv.setId(count);
                    bodytv.setPadding(0, 50,0, 0);
                    bodytv.setMovementMethod(new ScrollingMovementMethod());
                    rl.addView(titletv);
                    rl.addView(bodytv);
                    
                    rl.setId(2+count);
            }
            
            vf.addView(rl);
          count++;
      }

Solution

  • You need to create two relative layouts (or what ever). The position in a ViewFlipper is not set through the id. It is by the time of adding them. First added -> first shown