View Breite/Höhe bestimmen

Java xyrse123

Bekanntes Mitglied
Hallo,
wie kann ich von einer View die Breite/Höhe bestimmen? getWidth() gibt immer Null zurück.
Java:
public class ZeichnenView extends View implements View.OnTouchListener {

public ZeichnenView(Context context) {
        super(context);
        setOnTouchListener(this);
        
        Toast toast = Toast.makeText(this.getContext(), ""+getWidth(),Toast.LENGTH_LONG);
        toast.show(); // ist immer Null
     
    }  
}
Schonmal Danke im Vorraus.
 

Robat

Top Contributor
Das Problem ist dass zu dem Zeitpunkt wo du getWidth() aufrufst die UI noch nicht fertig gezeichnet ist. Daher kommt dort 0 zurück.

Was ist denn dein Ziel? Es gibt zwar Möglichkeiten darauf zu warten bis fertig gelayouted wurde aber eventuell gibt es ja eine alternative - je nach dem was du vor hast.
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
R Android Zugriff auf view von MainActivity Android & Cross-Platform Mobile Apps 7
W Zur Laufzeit erstelltes MenuItem an eine View binden Android & Cross-Platform Mobile Apps 1
W Bild aus dem Internet in View bzw. ImageView laden (Fragment) Android & Cross-Platform Mobile Apps 2
B Android In einem View der ersten Activity zweite anzeigen Android & Cross-Platform Mobile Apps 2
S Dynamische EditText View eingaben in Datenbank speichern Android & Cross-Platform Mobile Apps 0
V PopUp in gleicher View anzeigen Android & Cross-Platform Mobile Apps 1
M Android Suche Activity/View Namen Android & Cross-Platform Mobile Apps 1
R Android Warum (View view)? Android & Cross-Platform Mobile Apps 4
J Android neue View mit OnTouchListener Android & Cross-Platform Mobile Apps 0
B Eigene View xml-Layout einbinden Android & Cross-Platform Mobile Apps 1
R Problem mit View in ScrollView Android & Cross-Platform Mobile Apps 6
R Android Android.view Serializable ? Android & Cross-Platform Mobile Apps 3
M Android View zu View hinzufügen Android & Cross-Platform Mobile Apps 4
M Activity wechseln aus List View mit Android & Cross-Platform Mobile Apps 2
M Suche Name von View Komponente Android & Cross-Platform Mobile Apps 10
G canvas in view anzeigen Android & Cross-Platform Mobile Apps 10
G Fehlermeldung: "No XML content. Please add a root view or layout to your documet." Android & Cross-Platform Mobile Apps 7
E Android View zur Laufzeit hinzufügen Android & Cross-Platform Mobile Apps 4
N neuen view öffnen Android & Cross-Platform Mobile Apps 13
J id's von view komponenten werden nicht gefunden Android & Cross-Platform Mobile Apps 2
C Panel/View für Android Android & Cross-Platform Mobile Apps 3
tfa Android Layout-Probleme: View programmatisch erweitern (addContentView) Android & Cross-Platform Mobile Apps 7
G Dynamische View Inhalt -> Lagesensor Android & Cross-Platform Mobile Apps 3
K Grafik Tablerow, Button erstreckt sich in der gesamten Breite trotz Beschrenkung durch (max)width Android & Cross-Platform Mobile Apps 2
D Android Schriftgröße der Display-Breite anpassen Android & Cross-Platform Mobile Apps 2

Ähnliche Java Themen

Neue Themen


Oben