Salve, sto da poco scrivendo la mia prima applicazione per android. All'inizio avevo finito di scrivere il codice java ed il layout per la mia applicazione con tutti i toast funzionanti, ma quando mettevo lo schermo in landscape le icone erano tutte in posizioni sbagliate, così ho capito che dovevo fare un layout anche per il landscape. Fatto anche il nuovo layout imposto il codice java affinchè facesse partire l'applicazione a seconda se il telefono fosse in portrait o landscape, funziona tutto ma l'unico problema ora sono i toast; appena avvio l'app, sia in landscape che in portrait, i toast appaiono ma quando ruoto lo schermo, anche se clicco e riclicco, i toast non appaiono più, nemmeno se ritorno alla visualizzazione iniziale. Il codice del layout per landscape e portrait è lo stesso, cambiano solo delle distanze tra imagebutton.
1
2<ScrollView android:id="@+id/ScrollView01"
3android:layout_width="fill_parent"
4android:layout_height="fill_parent"
5xmlns:android="http://schemas.android.com/apk/res/android"
6android:visibility="visible"
7android:saveEnabled="true"
8android:scrollbars="none">
9<LinearLayout android:scrollbarAlwaysDrawVerticalTrack="true"
10android:fadeScrollbars="true"
11android:scrollbars="vertical"
12android:saveEnabled="true"
13android:layout_width="fill_parent"
14android:layout_height="fill_parent"
15android:orientation="vertical"
16android:id="@+id/instructions_view" android:background="@drawable/sfondo" android:fitsSystemWindows="true">
17<LinearLayout android:layout_height="83dip"
18android:layout_marginBottom="30dip"
19android:layout_width="fill_parent"
20android:id="@+id/linearLayout1" android:layout_marginTop="95dip">
21<ImageButton android:layout_height="fill_parent"
22android:id="@+id/imageButton1"
23android:layout_width="wrap_content"
24android:background="@drawable/longdrinks"
25android:clickable="true" android:layout_marginLeft="19dip"></ImageButton>
26<ImageButton android:layout_height="fill_parent"
27android:id="@+id/imageButton2"
28android:layout_width="wrap_content"
29android:background="@drawable/nalcoholicdrinks"
30android:clickable="true" android:layout_marginLeft="30dip"></ImageButton>
31<ImageButton android:layout_height="fill_parent"
32android:id="@+id/imageButton3"
33android:layout_width="wrap_content"
34android:background="@drawable/frozendrinks"
35android:clickable="true" android:layout_marginLeft="30dip"></ImageButton>
36</LinearLayout>
37<LinearLayout android:layout_height="83dip"
38android:layout_marginBottom="30dip"
39android:layout_width="fill_parent"
40android:id="@+id/linearLayout2">
41<ImageButton android:layout_height="fill_parent"
42android:layout_marginBottom="2dip"
43android:id="@+id/imageButton4"
44android:layout_width="wrap_content"
45android:background="@drawable/nfrozendrinks"
46android:clickable="true" android:layout_marginLeft="49dip"></ImageButton>
47<ImageButton android:layout_height="fill_parent"
48android:id="@+id/imageButton5"
49android:layout_width="wrap_content"
50android:background="@drawable/energydrinks"
51android:clickable="true" android:layout_marginLeft="50dip"></ImageButton>
52<ImageButton android:layout_height="fill_parent"
53android:id="@+id/imageButton6"
54android:layout_width="wrap_content"
55android:background="@drawable/sparkling"
56android:clickable="true" android:layout_marginLeft="40dip"></ImageButton>
57</LinearLayout>
58<LinearLayout android:layout_height="83dip"
59android:layout_width="fill_parent"
60android:id="@+id/linearLayout3" android:layout_marginBottom="10dip">
61<ImageButton android:layout_height="fill_parent"
62android:id="@+id/imageButton7"
63android:layout_width="wrap_content"
64android:background="@drawable/hotdrinks"
65android:clickable="true" android:layout_marginLeft="33dip"></ImageButton>
66<ImageButton android:layout_height="100dip"
67android:id="@+id/imageButton8"
68android:layout_width="wrap_content"
69android:background="@drawable/punches"
70android:clickable="true" android:layout_marginLeft="50dip"></ImageButton>
71<ImageButton android:layout_height="97dip"
72android:id="@+id/imageButton9"
73android:layout_width="wrap_content"
74android:background="@drawable/vegetabledrinks"
75android:clickable="true" android:layout_marginLeft="28dip"></ImageButton>
76</LinearLayout>
77</LinearLayout>
78</ScrollView>
79
1001package it.caronte.bar;
2
3
4
5
6
7import android.app.Activity;
8import android.content.res.*;
9import android.os.Bundle;
10import android.view.*;
11import android.widget.*;
12
13
14
15
16public class BartendersActivity extends Activity {
17
18
19
20/** Called when the activity is first created. */
21
22
23public void onCreate(Bundle savedInstanceState) {
24super.onCreate(savedInstanceState);
25Configuration c = getResources().getConfiguration();
26if(c.orientation == Configuration.ORIENTATION_PORTRAIT ) {
27setContentView(R.layout.main);
28}
29else if(c.orientation == Configuration.ORIENTATION_LANDSCAPE ){
30setContentView(R.layout.main_landscape);}
31}
32
33
34public void onConfigurationChanged(Configuration newConfig){
35super.onConfigurationChanged(newConfig);
36Configuration c = getResources().getConfiguration();
37if(c.orientation == Configuration.ORIENTATION_PORTRAIT ) {
38setContentView(R.layout.main);
39}
40else if(c.orientation == Configuration.ORIENTATION_LANDSCAPE ){
41setContentView(R.layout.main_landscape);
42}
43
44}
45
46
47private ImageButton imageButton1;
48private ImageButton imageButton2;
49private ImageButton imageButton3;
50private ImageButton imageButton4;
51private ImageButton imageButton5;
52private ImageButton imageButton6;
53private ImageButton imageButton7;
54private ImageButton imageButton8;
55private ImageButton imageButton9;
56
57
58
59
60
61
62
63protected void onStart() {
64super.onStart();
65
66imageButton1 = (ImageButton)findViewById(R.id.imageButton1);
67imageButton2 = (ImageButton)findViewById(R.id.imageButton2);
68imageButton3 = (ImageButton)findViewById(R.id.imageButton3);
69imageButton4 = (ImageButton)findViewById(R.id.imageButton4);
70imageButton5 = (ImageButton)findViewById(R.id.imageButton5);
71imageButton6 = (ImageButton)findViewById(R.id.imageButton6);
72imageButton7 = (ImageButton)findViewById(R.id.imageButton7);
73imageButton8 = (ImageButton)findViewById(R.id.imageButton8);
74imageButton9 = (ImageButton)findViewById(R.id.imageButton9);
75
76//Creo i Toast
77imageButton1.setOnClickListener(new View.OnClickListener() {
78public void onClick (View view) {
79Toast.makeText(getApplicationContext(),
80"COCKTAILS & LONG DRINKS",
81Toast.LENGTH_SHORT).show();
82}
83});
84// Stesso codice per gli altri imagebutton
85
86}
87});
88}
89//Creo il menu con un submenu
90
91public boolean onCreateOptionsMenu(Menu menu) {
92super.onCreateOptionsMenu(menu);
93SubMenu sub = menu.addSubMenu(0,1,0, "Cerca"). setIcon(R.drawable.cerca);
94sub.add(0,11,0,"Per ingredienti");
95sub.add(0,12,0,"Per cocktail");
96return true;
97}
98}
99
100
Grazie a chiunque sarà di aiuto.