Reputation: 133
I have put two linear layouts, under the first linear layout I have put the "textview" and In the second one I have put "imageview". But the problem is that image view is not moving, I want that dice icon in the centre. Whenever I try to drag that icon it gets stucked at the top left corner of the layout.
I can't figure out what's going wrong as I am new to android.
Have a look at the screenshot https://i.sstatic.net/SJmEY.png
activity_main.xml
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/Logo"
android:textAlignment="center"
android:textColor="#ffffff"
android:textSize="36sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/five" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.snakes;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Upvotes: 1
Views: 61
Reputation: 149
You cannot drag elements in a LinearLayout. They are placed in the order they are declared in the layout file, depending on the orientation set (either horizontally or vertically).
There is also no reason to have two linear layouts like this in your case. You can place both of your elements inside of the first one, and use the gravity property to center the image view (ex. gravity = "center_vertical").
EDIT: I ended up trying it myself, and resolved it using a RelativeLayout inside a LinearLayout. Make sure the RelativeLayout matches the parents width and height, then center the ImageView inside of the RelativeLayout, like so:
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/someImage"
android:orientation="vertical"
android:layout_centerInParent="true"/>
</RelativeLayout>
</LinearLayout>
Upvotes: 1
Reputation: 63
You need to add to the second linear layout - constraints statements. I can't recall the exact name of them but I think It's something like "layout_constraintTop_toBottomOf". this should make them constraint. then if you want you can determine the gap and adjust the view.
Upvotes: 0