Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align different elements vertically in android studio? [duplicate]

I am novice in android development. I am trying to align my page elements vertically, but unable to do that. I don't know, how I can use front-end layout control in Java.

My main.xml file is as follows:

<LinearLayout 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" 
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <!--<View-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="30dip"/>-->
    <TextView
        android:text="@string/getVerse"
        android:layout_width="wrap_content"
        android:layout_height="100dip"
        android:textSize="@dimen/header"
        android:layout_gravity="center_horizontal"
     />

    <!--<View-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="20dip"-->
     <!--/>-->
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="@dimen/questionSize"
        android:hint="@string/questionHint"
        />
    <!--<View-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="25dip"-->
    <!--/>-->

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="@dimen/buttonTextSize"
        android:text="@string/buttonText">
    </Button>

</LinearLayout>

The page looks like:

screenshot of avd Please help me!!!

like image 316
user3641971 Avatar asked Dec 26 '22 00:12

user3641971


2 Answers

Add following code to your parent LinearLayout

 android:orientation="vertical"

To show Gif in Activity:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new GifView(this));
    }

    static class GifView extends View {
        Movie movie;
        GifView(Context context) {
            super(context);
            File myFile = new File("/sdcard/test.gif");
            InputStream fis = null;
            try {
                fis = new BufferedInputStream(new FileInputStream(myFile),
                        16 * 1024);
                fis.mark(16 * 1024);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            movie = Movie.decodeStream(fis);
            Log.e("SS", "movie.duration()" + movie.duration());

        }

        @Override
        protected void onDraw(Canvas canvas) {
            try {
                if (movie != null) {
                    movie.setTime((int) SystemClock.uptimeMillis()
                            % movie.duration());
                    movie.draw(canvas, 0, 0);
                    invalidate();
                }
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }

        }
    }
like image 37
Anjali Avatar answered Dec 30 '22 16:12

Anjali


Just use android:orientation="vertical" attribute in your LinearLayout. You are getting the elements aligned Horizontally as this is the default provided by android.

The code after doing so is shown below :

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <!--<View-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="30dip"/>-->
    <TextView
        android:text="@string/getVerse"
        android:layout_width="wrap_content"
        android:layout_height="100dip"
        android:textSize="@dimen/header"
        android:layout_gravity="center_horizontal"
     />

    <!--<View-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="20dip"-->
     <!--/>-->
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="@dimen/questionSize"
        android:hint="@string/questionHint"
        />
    <!--<View-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="25dip"-->
    <!--/>-->

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="@dimen/buttonTextSize"
        android:text="@string/buttonText">
    </Button>

    </LinearLayout>

Hope this Helps!

like image 173
gsanskar Avatar answered Dec 30 '22 17:12

gsanskar