Layout im Javacode ändern

  • Antworten:2
krzy
  • Forum-Beiträge: 3

24.01.2019, 13:26:33 via Website

Hallo,
ich bin aktuell dabei zu lernen wie man Android-Apps programmiert, hänge aber leider bei einem Problem fest, bei dem ich einfach nicht weiterkomme.
Das Layout der App wird ja in der XML-Datei gespeichert. Aber wie kann ich jetzt z.B. im Javacode dieses Layout anpassen. Beispielsweise ich will einen neuen Button hinzufügen mit new Button und dafür muss irgendwas andere verschoben werden.

Mal Folgendes als Beisspiel: `
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HigherLowerPreparationActivity">

android:id="@+id/edittext_higherlower_preparation_player2"
android:layout_width="307dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textPersonName"
android:text="@string/edittext_higherlower_preparation_player"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.153"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edittext_higherlower_preparation_player1"
app:layout_constraintVertical_bias="0.0" />
`

Wie könnte ich - bspw. über einen Button - jetzt den Wert von "app:layout_constraintTop_toBottomOf="@+id/edittext_higherlower_preparation_player1"" ändern. Also quasi ich habe einen Button und wenn ich da drauf drücke, soll statt dem was aktuell im Layout steht, dort stehen "app:layout_constraintTop_toBottomOf="@+id/edittext_higherlower_preparation_player2"

Ich bedanke mich schon mal für jede Hilfe und Antwort.

Kommentieren
swa00
  • Forum-Beiträge: 3.704

24.01.2019, 19:18:47 via Website

Hallo und willkommen im Forum,

Zur Runtime kann man keine Layout-xml verändern.
Sie ist fester Bestandteil des Projektes vor dem compilieren.

Eine Vorgehensweise ist , dir z.b. einen RelativeLayout Container in der xml zu definieren und dann mit inflate und/oder addView einen Button zur Runtime hinzufügen.

Dies setzt aber schon sehr fundierte Kenntnisse voraus, die du aufgrund deiner Frage noch nicht hast.

Diese Technik solltest du also nach hinten schieben , bist du 100% fit in der Layout-Thematik bist
und bis dahin manuell dein XML Layout definieren.

— geändert am 24.01.2019, 19:35:22

Liebe Grüße - Stefan
[ App - Entwicklung ]

Hilfreich?
krzy
Kommentieren
Jokel
  • Forum-Beiträge: 1.527

24.01.2019, 20:22:37 via Website

Hallo eine Möglichkeit wäre mit der Sichtbarkeit der Views zu arbeiten .
Dazu gibt es im XML
android:visibility="invisible“
android:visibility="visible"

Im Java code kannst du die auch setzen indem du die View über die ID gesucht hast .
setVisibility(View.INVISIBLE);
setVisibility(View.VISIBLE);

public class MainActivity extends AppCompatActivity {

Button bt;
TextView et;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    et = (TextView) findViewById(R.id.textView);
    bt = (Button) findViewById(R.id.button);
    bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            bt.setVisibility(View.INVISIBLE);
            et.setVisibility(View.VISIBLE);
        }
    });
}

}

<?xml version="1.0" encoding="utf-8"?>

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
        android:text="Button"
        android:layout_width="88dp"
        android:layout_height="wrap_content" android:id="@+id/button" android:layout_marginTop="8dp"
        app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp" app:layout_constraintStart_toStartOf="parent"
        android:layout_marginStart="8dp" android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="parent" android:visibility="visible"/>
<TextView
        android:text="Hallo "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/textView" app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginEnd="8dp" app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp" android:layout_marginTop="8dp"
        app:layout_constraintTop_toTopOf="parent" android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="parent"
        android:textSize="40sp"
        app:layout_constraintHorizontal_bias="0.498" app:layout_constraintVertical_bias="0.496" android:visibility="invisible"/>

Hilfreich?
krzyswa00
Kommentieren