Question

I´ve got a TableLayout with four TableRows and four buttons in each of the four rows. I had big problems with different screen resolutions so I built my layout file new.

First, I give you my code:

public class MainActivity extends Activity {

    Button b,b2,b3,b4, b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20;
    //b21,b22,b23,b24;


    ArrayList<Button> arr = new ArrayList<Button>();

    MediaPlayer mp;
    Button aktuell;
    TextView tv;
    String playtone ="";
    String shouldvibra ="";
    TextView tvPunktestand;
    int count = 0;
    CountDownTimer countdown;
    CountDownTimer eins;
    CountDownTimer zwei;
    CountDownTimer drei;
    AlertDialog.Builder alertDialog;
    boolean einsstarted = false;
    boolean zweistarted = false;
    boolean dreistarted = false;
    boolean countdownstarted = false;
    SharedPreferences app_preferences;
     Vibrator vibe;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         vibe = (Vibrator) this.getSystemService(this.VIBRATOR_SERVICE) ;

        b = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        b3 = (Button) findViewById(R.id.button3);
        b4 = (Button) findViewById(R.id.button4);
        b5 = (Button) findViewById(R.id.button5);
        b6 = (Button) findViewById(R.id.button6);
        b7 = (Button) findViewById(R.id.button7);
        b8 = (Button) findViewById(R.id.button8);
        b9 = (Button) findViewById(R.id.button9);
             b10 = (Button) findViewById(R.id.button10);
             b11 = (Button) findViewById(R.id.button11);
             b12 = (Button) findViewById(R.id.button12);
            b13 = (Button) findViewById(R.id.button13);
            b14 = (Button) findViewById(R.id.button14);
            b15 = (Button) findViewById(R.id.button15);
            b16 = (Button) findViewById(R.id.button16);
           b17 = (Button) findViewById(R.id.button17);
           b18 = (Button) findViewById(R.id.button18);
           b19 = (Button) findViewById(R.id.button19);
           b20 = (Button) findViewById(R.id.button20);
          // b21 = (Button) findViewById(R.id.button21);
          // b22 = (Button) findViewById(R.id.button22);
           //b23 = (Button) findViewById(R.id.button23);
         //  b24 = (Button) findViewById(R.id.button24);
           //tvPunktestand = (TextView) findViewById(R.id.tvPunktewaehrend);

//        b2.setVisibility(View.GONE);
//        b3.setVisibility(View.GONE);
//        b4.setVisibility(View.GONE);
//        b5.setVisibility(View.GONE);
//        b6.setVisibility(View.GONE);
//        b7.setVisibility(View.GONE);
//        b8.setVisibility(View.GONE);
//        b9.setVisibility(View.GONE);
//        b10.setVisibility(View.GONE);
//        b11.setVisibility(View.GONE);
//        b12.setVisibility(View.GONE);
//        b13.setVisibility(View.GONE);
//        b14.setVisibility(View.GONE);
//        b15.setVisibility(View.GONE);
//       b16.setVisibility(View.GONE);
//        b17.setVisibility(View.GONE);
//        b18.setVisibility(View.GONE);
//        b19.setVisibility(View.GONE);
//        b20.setVisibility(View.GONE);
//        b21.setVisibility(View.GONE);
//        b22.setVisibility(View.GONE);
//        b23.setVisibility(View.GONE);
//        b24.setVisibility(View.GONE);

        arr.add(b);
        arr.add(b2);
        arr.add(b3);
        arr.add(b4);
        arr.add(b5);
        arr.add(b6);
        arr.add(b7);
        arr.add(b8);
        arr.add(b9);
        arr.add(b10);
        arr.add(b11);
        arr.add(b12);
        arr.add(b13);
        arr.add(b14);
        arr.add(b15);
        arr.add(b16);
        arr.add(b17);arr.add(b18);
        arr.add(b19);
        arr.add(b20);
        //arr.add(b21);


        //arr.add(b22);arr.add(b23);
        //arr.add(b24);

         int boeserzufall = (int) (Math.random()*23);
         Button boese = arr.get(boeserzufall);
         arr.get(boeserzufall).setBackgroundResource(R.drawable.williboese);

         arr.get(boeserzufall).setTag(R.drawable.williboese);

         System.out.println("AAAAAAAAAAAAAAAAAAAAAA: " + boese.getTag());






         alertDialog = new AlertDialog.Builder(this);  
         alertDialog.setTitle("Countdown - gleich geht´s los");  
         alertDialog.setMessage("");
         alertDialog.setCancelable(false);

         final AlertDialog ag = alertDialog.show();

         final TextView myMsg = (TextView)ag.findViewById(android.R.id.message);
         myMsg.setGravity(Gravity.CENTER);
         ag.show();

         countdown =  new CountDownTimer(4000, 1000) {
             @Override
             public void onTick(long millisUntilFinished) {
                 countdownstarted = true;
                 shared_check();
                 shouldvibra = app_preferences.getString("vibra", "Fehler");
                myMsg.setText(" " + (millisUntilFinished) / 1000);
                if(shouldvibra.equals("j") || shouldvibra == null){
                    vibe.vibrate(100);
                }
             }

             @Override
             public void onFinish() {
                 ag.dismiss();

                 playtone = app_preferences.getString("ton", "Fehler");

                 if(playtone.equals("j") || playtone == null){
                     playmusic();
                 }




         eins = new CountDownTimer(10000, 790) {

            @Override
            public void onTick(long millisUntilFinished) {

                for(int i = 0; i< arr.size(); i++){
                    Button aga = arr.get(i);
                    if(aga.getVisibility() == View.VISIBLE){
                        aga.setVisibility(View.GONE);
                    }
                }
                einsstarted = true;

                int zufall = (int) (Math.random()*19);
                setNextButton(arr.get(zufall));
                //tvPunktestand.setText("" + count);
                System.out.println("HHH");
            }

            @Override
            public void onFinish() {

                //Intent highscore = new Intent (MainActivity.this, Highscore_eintragen.class);
               // highscore.putExtra("count", count);
               // startActivity(highscore);
                 System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");


                  zwei = new CountDownTimer(10000, 550) {

                    @Override
                    public void onTick(long millisUntilFinished) {
                        for(int i = 0; i< arr.size(); i++){
                            Button aga = arr.get(i);
                            if(aga.getVisibility() == View.VISIBLE){
                                aga.setVisibility(View.GONE);
                            }
                        }
                        zweistarted = true;
                        int zufall = (int) (Math.random()*19);
                        setNextButton(arr.get(zufall));
                        //tvPunktestand.setText("" + count);
                        System.out.println("HHH");
                    }

                    @Override
                    public void onFinish() {

                       // Intent highscore = new Intent (MainActivity.this, Highscore_eintragen.class);
                        //highscore.putExtra("count", count);
                        //startActivity(highscore);
                         System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

                        drei = new CountDownTimer(10000, 350) {

                            @Override
                            public void onTick(long millisUntilFinished) {
                                for(int i = 0; i< arr.size(); i++){
                                    Button aga = arr.get(i);
                                    if(aga.getVisibility() == View.VISIBLE){
                                        aga.setVisibility(View.GONE);
                                    }
                                }
                                dreistarted = true;
                                int zufall = (int) (Math.random()*19);
                                setNextButton(arr.get(zufall));
                                //tvPunktestand.setText("" + count);
                                System.out.println("HHH");
                            }

                            @Override
                            public void onFinish() {
                               System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                                Intent highscore = new Intent (MainActivity.this, NachdemSpiel.class);
                                highscore.putExtra("count", count);
                                startActivity(highscore);
                                beenden();


                            }
                        }.start();
                    }
                }.start();
            }
        }.start();
             }
         }.start();




        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }

        });


        b2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b2.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
                }
        });



        b3.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b3.setBackgroundColor(R.color.redwue);
                count++;

                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }

            }
        });


        b4.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b4.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }

            }
        });

        b5.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b5.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }

            }
        });

        b6.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b6.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });

        b7.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b7.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });

        b8.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b8.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });

        b9.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b9.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });

        b10.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b10.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });

        b11.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b11.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });
        b12.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b12.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });
        b13.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b13.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });
        b14.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b14.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });
        b15.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b15.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });
        b16.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b16.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });
        b17.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b17.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });
        b18.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b18.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });
       b19.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b19.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });
        b20.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //b20.setBackgroundColor(R.color.redwue);
                count++;
                if(shouldvibra.equals("j")){
                    vibrieren();
                }
                if(arg0.getTag() != null) {
                    count = 0;
                    showmessage();
                }
            }
        });
//        b21.setOnClickListener(new View.OnClickListener() {
//          
//          @Override
//          public void onClick(View arg0) {
//              //b20.setBackgroundColor(R.color.redwue);
//              count++;
//              if(shouldvibra.equals("j")){
//                  vibrieren();
//              }
//              if(arg0.getTag() != null) {
//                  count = 0;
//                  showmessage();
//              }
//          }
//      });
//        b22.setOnClickListener(new View.OnClickListener() {
//          
//          @Override
//          public void onClick(View arg0) {
//              //b20.setBackgroundColor(R.color.redwue);
//              count++;
//              if(shouldvibra.equals("j")){
//                  vibrieren();
//              }
//              if(arg0.getTag() != null) {
//                  count = 0;
//                  showmessage();
//              }
//          }
//      });
//        b23.setOnClickListener(new View.OnClickListener() {
//          
//          @Override
//          public void onClick(View arg0) {
//              //b20.setBackgroundColor(R.color.redwue);
//              
//              count++;
//              if(shouldvibra.equals("j")){
//                  vibrieren();
//              }
//              if(arg0.getTag() != null) {
//                  count = 0;
//                  showmessage();
//              }
//          }
//      });
//        b24.setOnClickListener(new View.OnClickListener() {
//          
//          @Override
//          public void onClick(View arg0) {
//              //b20.setBackgroundColor(R.color.redwue);
//              
//              count++;
//              if(shouldvibra.equals("j")){
//                  vibrieren();
//              }
//              if(arg0.getTag() != null) {
//                  count = 0;
//                  showmessage();
//              }
//          }
//      });


    }  

    @Override
    public void onBackPressed() { 
        System.out.println("asdhaskjdhaskjdhakjsdhkjasdhkjahsdkjashdkjh");
         Intent i = new Intent(MainActivity.this, MainMenu.class);
         startActivity(i);

         if( countdownstarted == true){
             countdown.cancel();
         }
         if(einsstarted == true){
             eins.cancel(); 
             if(playtone.equals("j")){
                 stopmusic();
             }
         }
         if(zweistarted == true){
             zwei.cancel(); 
             if(playtone.equals("j")){
                 stopmusic();
             }
         }
         if(dreistarted == true){
             drei.cancel();
             if(playtone.equals("j")){
                 stopmusic();
             }
         }
         this.finish();

    }


    public void showmessage(){
        Toast.makeText(MainActivity.this, "Oh nein... Du hast den Kaktus erwischt! Alle deine Punkte sind wieder weg.", Toast.LENGTH_SHORT).show();
    }
    public void setNextButton(Button str){

        System.out.println("&&&&&&&&&&& SETNEXTBUTTON");
        //str.setVisibility(ImageButton.GONE);

        int buttonid = str.getId(); 
        int buttonname = str.getId();
        //String buttonneu = "button" + zufall; 
        System.out.println("&&&&&&&&&&&" +  getResources().getResourceEntryName(buttonid));
        int[] buttonIds = {R.id.button1, R.id.button2, R.id.button3, R.id.button4, R.id.button5, R.id.button6, R.id.button7, R.id.button8, 
                R.id.button9, R.id.button10, R.id.button11, R.id.button12, R.id.button13, R.id.button14, R.id.button15, R.id.button16 , 
                R.id.button17, R.id.button18, R.id.button19, R.id.button20};
                //,R.id.button21, R.id.button22, R.id.button23, R.id.button24 

        //Button bnew = (Button) findViewById(buttonIds[zufall]);
        //Toast.makeText(this, "$ " + getResources().getResourceEntryName(bnew.getId()), Toast.LENGTH_LONG).show();
        //if(bnew.getId() == str.getId()){
        //   bnew = (Button) findViewById(buttonIds[zufall]);

        //}


        str.setVisibility(View.VISIBLE);  


        //b.setEnabled(enabled)
    }
    public void beenden(){
        this.finish();
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void playmusic(){
         mp = MediaPlayer.create(getApplicationContext(), R.raw.music);
        mp.start();
    }
    public void stopmusic(){
      mp.stop();    
    }


    public boolean shared_check(){
        boolean gibtswelche = false;
        String name;
        app_preferences = getSharedPreferences("wuestenfest", 0);
        SharedPreferences.Editor editor = app_preferences.edit(); 

        String tonvalue = app_preferences.getString("ton",null);
        String vibravalue = app_preferences.getString("vibra", null);
        if (tonvalue == null && vibravalue == null) {
            System.out.println(" Keine Shared Preferences gefunden");
        } else {
            app_preferences.getString("ton", "testasd");
            System.out.println("Shared Preferences gefunden");
            gibtswelche = true;
        }
        return gibtswelche;
    }
    public void vibrieren(){
        vibe.vibrate(50);
    }

}

Like you may see, my target is to set one button visible and every other invisible. Every few ms this should change.

Here is my layout file:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:background="@drawable/bg"
    android:stretchColumns="0,1,2,3,4,5"


    tools:context=".MainActivity" >

    <TableRow 
        android:id="@+id/tableRow1"

        android:paddingTop="22dp"
        android:layout_weight="1">

        <Button
            android:id="@+id/button1"
            android:text="1" 
            android:background="@drawable/willi"> 
        </Button>
         <Button
            android:id="@+id/button2"
            android:text="2"
            android:layout_marginLeft="10dp" 
            android:background="@drawable/willi"> 
         </Button>
         <Button
            android:id="@+id/button3"
            android:text="3" 
            android:layout_marginLeft="10dp"
            android:background="@drawable/willi"> 
          </Button>
          <Button
            android:id="@+id/button4"
            android:text="4" 
            android:layout_marginLeft="10dp"
            android:background="@drawable/willi"> 
           </Button>


    </TableRow>
        <TableRow 
            android:id="@+id/tableRow2"

       android:layout_weight="1"
       >

        <Button
            android:id="@+id/button5"
            android:text="1" 
             android:background="@drawable/willi"> 
            </Button>
                    <Button
                        android:id="@+id/button6"
            android:text="2"
            android:layout_marginLeft="10dp" 
             android:background="@drawable/willi"> 
            </Button>
                    <Button
                        android:id="@+id/button7"
            android:text="3" 
            android:layout_marginLeft="10dp"
             android:background="@drawable/willi"> 
            </Button>
                    <Button
                        android:id="@+id/button8"
            android:text="4" 
            android:layout_marginLeft="10dp"
             android:background="@drawable/willi"> 
            </Button>


    </TableRow>
  and so on....

</TableLayout>

The problem is, if one button is active, it looks like this:

enter image description here

The buttons are lying on top of each other and they are curious stretched.

So, I tested to set all buttons visible on start, then it looks like this (How it should look - all buttons beside the other): enter image description here

How is that possible that my code changes my layout file?

Was it helpful?

Solution

How is that possible that my code changes my layout file?

Its not. resource files can't be changed at runtime.

What it looks like is happening is that you are setting them to GONE so the space that it would occupy is available to the other Buttons. Instead of setting them to GONE, try making them INVISIBLE. This way the space will still be occupied but they won't show.

Just a suggestion but you can set the visibility in xml which would clean up your Java file. That's just a preference but that's probably what I would do. Also,

/*
    some comments
    some other comments
*/

is how to do block comments.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top