Search code examples
androideclipseandroid-layouttextviewsettext

How to change 7 textview values at a time by clicking one imageButton 12 times in Android?


Sorry for this Silly question, actually i am new in Android... Actually I have 7 Text Views and 1 image button on my screen, i want when i click on image Button then all text values changed(as i want text), remember that all text values are different from each other and then i again click on image Button then text value again change (as i want text) and it's run up-to 12 times and change values of text views by click image Button on 12 times. if you know it can done by and Array then plz comment.

  My XML:
<RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent" >

  <ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@null"
    android:src="@drawable/pictitle
    android:onClick="nextText"  />

 <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="" />
  <TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="" />
  <TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="" />
 <TextView 
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="" />
  <TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="" />
  <TextView
    android:id="@+id/textView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="" />
 <TextView
    android:id="@+id/textView7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="" />
</RelativeLayout>

My Java:

     public class MyProject extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_pics);
}

public void indexPics(View v) {
    Intent i = new Intent(this, MainActivity.class);
    startActivity(i);
}

public void nextText(View view) {

   }
}

Solution

  • The easy way is to instantiate all your TextView's and then use the setText() method to change their text.

    textView1.setText("One");
    

    The other way is to get all the TextViews as child of your parent layout (like LinearLayout or the one you are using and using getChildAt method)

    LinearLayout ll = //Your Layout this can be any Linear or Relative layout 
                     //in which you added your spinners at runtime ;
    
    int count = ll.getChildCount();
    for(int i =0;i<count;i++)
    {
        View v = ll.getChildAt(i);
        if(v instanceof TextView)
        {
            // you got the spinner
            EditText s = (EditText) v;
            if(s.getID() == R.id.textView1){
                textView1.setText("One");
            }
        }
    }
    

    The first one is easier and simpler. I prefer this. Hope I helped. :)

    EDIT - Supoose on first click of the button you want the TextViews to have name of days and ont the next it displays numbers, the code will be:

     //You can also use List here
     String [] days = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
     String [] nums = {"1","2","3","4","5","6","7"};
    
     //Your method to update the TextViews
     public void updateTextViews(String [] texts){
             textView1.setText(texts[0]);
             textView2.setText(texts[1]);
             textView3.setText(texts[2]);
             textView4.setText(texts[3]);
             textView5.setText(texts[4]);
             textView6.setText(texts[5]);
             textView7.setText(texts[6]);
     }
    
     //Count the number of clicks, (or the condition on which you want to change the text displayed)
     int counter = 1;
    
     //your ImageBUtton's onClick() method
     @override
     public void onClick(View v){
    
        //Check the click count (or your condition)
        switch(counter){
            case 1: //first click
               updateTextViews(days);
               break;
            case 2: //second click
               updateTextViews(nums);
               break;
            default:
               //This is executed when no case matches. 
               break;
        } 
        **counter = counter + 1;**      
     }