Saturday, 8 December 2018

How to programmatically set the width of the LinearLayout or ImageView or RelativeLayout?


First of all get the device screen width:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
try {
    display.getRealSize(size);
} catch (NoSuchMethodError err) {
    display.getSize(size);
}
int width = size.x;
int height = size.y;
now just create a LayoutParams and set it to the Text:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int)(width/3),
                    LinearLayout.LayoutParams.WRAP_CONTENT);  

mMainLinearLayout.setLayoutParams(lp);
// OR
mProfileImage.setLayoutParams(lp);
//OR

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams((int)(width/3),
                    LinearLayout.LayoutParams.WRAP_CONTENT);  

mLoginRelativeLayout.setLayoutParams(lp);

as same for all other layouts
which is provide according to screen size of device .

Sunday, 1 April 2018

How to set editText or textview or button below of other editText or textview or button in relativeLayout in XML

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:app="http://schemas.android.com/apk/res-auto" 
 xmlns:tools="http://schemas.android.com/tools"     
 android:layout_width="match_parent"     
 android:layout_height="match_parent"     
 android:background="@drawable/button_border">

    <ImageView         
            android:id="@+id/imageView3" 
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_alignParentTop="true"         
            android:layout_centerHorizontal="true" 
            android:layout_marginTop="48dp" 
            app:srcCompat="@drawable/app_logo1" />

    <TextView 
        android:id="@+id/textView2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_below="@+id/imageView3"         
        android:layout_centerHorizontal="true" 
        android:text="RentLey" 
        android:textSize="30sp" />

    <EditText 
       android:id="@+id/register_email"         
       android:layout_width="300dp" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true"         
       android:layout_marginTop="231dp" 
       android:background="@drawable/edittext_border" 
       android:drawableLeft="@drawable/email" 
       android:ems="10" 
       android:padding="8dp" 
       android:hint="Email"         
       android:inputType="textEmailAddress" />

    <EditText 
         android:id="@+id/register_password" 
         android:layout_width="300dp" 
         android:layout_height="wrap_content" 
         android:layout_centerHorizontal="true" 
         android:background="@drawable/edittext_border"         
         android:ems="10" 
         android:padding="8dp" 
         android:layout_marginTop="8dp" 
         android:inputType="textPassword" 
         android:drawableLeft="@drawable/password"         
         android:hint="Password" 
         android:layout_below="@+id/register_email"/>

    <EditText         
         android:id="@+id/confirm_password"         
         android:layout_width="300dp"         
         android:layout_height="wrap_content"         
         android:layout_centerHorizontal="true" 
         android:background="@drawable/edittext_border"         
         android:ems="10" 
         android:padding="8dp" 
         android:layout_marginTop="8dp"         
         android:inputType="textPassword" 
         android:drawableLeft="@drawable/password" 
         android:hint="Confirm Password"         
         android:layout_below="@+id/register_password" />

    <Button 
        android:id="@+id/register_button" 
        android:layout_width="180dp" 
        android:layout_height="wrap_content" 
        android:layout_centerHorizontal="true" 
        android:background="@drawable/button" 
        android:text="@string/register" 
        android:textAllCaps="false" 
        android:layout_marginTop="8dp" 
        android:layout_below="@+id/confirm_password"/>

</RelativeLayout>
 
 
 
Thanks 
Help Code 
any suggestion or query comment me