How can EditText occupy all the remaining space in a layout? - java

I have a linear layout (linear2 in code) with horizontal orientation.
I've placed a button, editText and a second button into this layout.
I'd like to have the button's width as "wrap_content".
How do I give all the remaining space to editText without using weight?
Is it possible to place this layout to the bottom without using weight
of vertical views?
Code is here:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/backNormal"
android:orientation="vertical">
<ScrollView
android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:orientation="vertical"
android:layout_weight="12">
<LinearLayout
android:id="#+id/linear1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/linear2"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="2"
android:orientation="horizontal">
<ImageView
android:id="#+id/buttonClip"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity= "center_vertical|center_horizontal"/>
<EditText
android:id="#+id/editChatMessage"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:gravity="top|left"
android:maxLines="5"
android:layout_gravity= "center_vertical|center_horizontal"
android:layout_weight="5"/>
<Button
android:id="#+id/buttonProcess"
android:text='Process'
android:gravity="center"
android:layout_gravity= "center_vertical|center_horizontal"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
Thanks!

Try this way,hope this will help you to solve your problem.
Here i try to improve your LinearLayout weight allocation
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/backNormal"
android:orientation="vertical">
<ScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.85">
<LinearLayout
android:id="#+id/linear1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/linear2"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.15"
android:gravity="center">
<ImageView
android:id="#+id/buttonClip"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/ic_launcher"
android:scaleType="fitXY"/>
<EditText
android:id="#+id/editChatMessage"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="top|left"
android:maxLines="5"
android:layout_weight="1"/>
<Button
android:id="#+id/buttonProcess"
android:text='Process'
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>

Keep your Edittext hight wrap_content.....that's it.
<EditText
android:id="#+id/editChatMessage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="top|left"
android:maxLines="5"
android:layout_gravity= "center_vertical|center_horizontal"
android:layout_weight="5"
/>

Related

Bottom Navigation View goes up with windowSoftInputMode=adjustResize

I need windowSoftInputMode= adjustRezise for my layout so that my buttons go up with the keyboard. BUT I don't want my BottomNagivationView to be affected by this.
How can I solve such?
My BottomNavigationView comes from MainActivity.xml which is:
<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"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottom_navigation"></FrameLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
app:menu="#menu/bottom_menu"/>
</RelativeLayout>
My manifest has android:windowSoftInputMode="adjustPan" but I adjust it by code when I open the fragment with:
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
Here are some pictures so you could understand what I mean: https://imgur.com/a/vjePxI6
So what I would like to know is how I could set the BottomNavigationView to do nothing on keyboard opening while I'd like my buttons to be affected by :
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
In my case problem solved by just adding following line in manifest file.
android:windowSoftInputMode="adjustResize|adjustPan"
add android:windowSoftInputMode="adjustPan" inside your activity tag into your manifest file.
then go to your activity and change the parent layout to RelativeLayout, then create your FrameLayout which holds your fragments inside your relative layout which is your parent layout. Then add your second layout which is LinearLayout that holds the bottomNavigationView and make its Height and width match_parent but below the FrameLayout.
Below is the example of the main activity layout
<?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:orientation="vertical"
tools:context=".MainActivity">
//FrameLayout which holds all your fragments
<FrameLayout
android:id="#+id/fragments_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
//BottomNavigationView Container
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="bottom"
android:layout_below="#id/fragments_container">
//BottomMenus
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="#+id/bottom_navigation"
android:background="#drawable/rectangle_4_dash"
android:layout_alignParentBottom="true">
//Container of Menus
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="5">
//BottomMenu One
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:src="#drawable/home_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Home" />
</LinearLayout>
//BottomMenu Two
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:src="#drawable/contact_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Contact" />
</LinearLayout>
//BottomMenu Three
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:src="#drawable/about_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About"
android:layout_gravity="center"/>
</LinearLayout>
//BottomMenu Four
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical">
<ImageView
android:layout_width="20dp"
android:layout_height="17dp"
android:layout_gravity="center"
android:src="#drawable/services_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Services"
android:layout_gravity="center"/>
</LinearLayout>
</LinearLayout>
</com.google.android.material.bottomnavigation.BottomNavigationView>
</LinearLayout>
Then go to your fragment which you want to scroll and design it like below one.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Title Of Your Form"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/f_name"
android:inputType="textPersonName"
android:padding="10dp"
android:textSize="14sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="#string/m_name"
android:inputType="textPersonName"
android:padding="10dp"
android:textSize="14sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="#string/l_name"
android:inputType="textPersonName"
android:padding="10dp"
android:textSize="14sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="Message"
android:inputType="text"
android:padding="10dp"
android:textSize="14sp" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Send Messagge"
android:textAllCaps="false"
android:textColor="#color/white"
android:layout_marginBottom="40dp"/>
</LinearLayout>
</ScrollView>
That is how i solved it,
if you have any doubt please let me know but if its helps or looks like a correct answer please mark it as a correct answer thanks.

Change size layout ImageView when WebView scrolls

I have a project with Android Studio and in Activity I have content html using WebView. The Image should be shown as an ImageView to make it look good. I want to change the height of the ImageView when I scroll the WebView to the bottom and then back to normal size when I scroll back to the top. How can enable it?
Here is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:background="#ffffff"
android:orientation="vertical"
tools:context="com.demowordpress.app.DetailActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="220dp">
<ImageView
android:id="#+id/imageView"
android:scaleType="center"
android:layout_width="match_parent"
android:layout_height="220dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom">
<TextView
android:padding="10dp"
android:id="#+id/textView"
style="#style/TextAppearance.AppCompat.Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:ellipsize="end"
android:maxEms="10"
android:maxLines="3"
android:background="#android:drawable/screen_background_dark_transparent"
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
</LinearLayout>
</RelativeLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/wv"
android:scrollbars="none"
android:scrollbarAlwaysDrawVerticalTrack="false"
android:visibility="gone">
</WebView>
<ProgressBar
android:id="#+id/pBar"
style="?android:attr/progressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<Button
android:id="#+id/Button"
android:layout_width="80dp"
android:layout_height="80dp"
android:focusable="true"
android:clickable="true"
android:visibility="gone"
android:drawableTop="#drawable/ic_retry"
android:text="try Again"
android:textColor="#color/colorPrimary"
android:background="#android:color/transparent"
android:layout_gravity="center"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/white"
android:clickable="true"
ads:srcCompat="#drawable/ic_share"
app:backgroundTint="#color/white"
android:layout_gravity="end|bottom"
android:layout_marginBottom="50dp"
android:layout_marginRight="40dp"
/>
</FrameLayout>
</LinearLayout>

How do you set a scrollView to fill the remainder of the screen with a static navbar at the bottom?

My goal is to have 2 static buttons at the bottom of the screen at all times "Back" and "Add Event." I have a scrollView that holds a list of items, currently that view just shows one row instead of all that rows that can fit on the screen. The reason I used a scrollView was because I wanted the user to be able to scroll through more options if it exceeds the screen length. Is there a way to expand the Scrollview/Listview without using DP(since that doesn't always correlate well with different screen sizes).
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<ListView
android:id="#+id/user_event_table"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left|bottom"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/back_button"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_gravity="left|center"
android:text="back" />
<Button
android:id="#+id/add_button"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:text="add Event" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Do not nest ListView inside ScrollView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/button_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<Button
android:id="#+id/back_button"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:text="back"/>
<Button
android:id="#+id/add_button"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:text="add Event"/>
</LinearLayout>
<ListView
android:id="#+id/user_event_table"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/button_layout"
android:layout_alignParentTop="true"/>
</RelativeLayout>
Here is the solution
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/user_event_table"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left|bottom"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/back_button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="left|center"
android:text="back" />
<Button
android:id="#+id/add_button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:text="add Event" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

My items in my listView are not aligning properly?

rather than explain what's happening, here's a screen-grab:
As you can see my item list is aaaalll over the shop. Going to paste both activity_main.xml and another one that formats the list, list_row.xml
ACTIVITY_MAIN.XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#121212">
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/logo" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/main_title"
android:textColor="#33b5e5"
android:background="#1f1f1f"
android:textStyle="bold"
android:textSize="14sp"/>
<ListView
android:id="#+id/android:list"
android:background="#121212"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="2dip"/>
</LinearLayout>
LIST_ROW.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip"
android:background="#121212">
<TableLayout
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="fill_parent"
android:stretchColumns="*"
android:background="#121212">
<TableRow>
<ImageView
android:id="#+id/icon"
android:padding="2dip"
android:background="#121212"/>
<TextView
android:id="#+id/description"
android:padding="2dip"
android:textSize="18sp"
android:textColor="#ffffff"
android:singleLine="true"
android:background="#121212"/>
</TableRow>
</TableLayout>
</LinearLayout>
Try giving weights to ImageView and TextView like android:layout_weight as the ratio you want.
And dont forget to make android:layout_width = "match_parent" otherwise weights will have no effect..
remove the Table layout
and use this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="64dp"
android:background="#121212"
android:orientation="horizontal"
android:padding="6dip" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#121212"
android:padding="2dip"
android:src="#drawable/instructions" />
<TextView
android:id="#+id/description"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#121212"
android:padding="2dip"
android:singleLine="true"
android:text="tomer"
android:textColor="#ffffff"
android:textSize="18sp" />
</LinearLayout>

Trouble with ImageView gravity on Java, Android

I have this code:
<LinearLayout
android:layout_weight="0.5"
android:layout_width="0px"
android:id="#+id/linearLayout2"
android:orientation="vertical"
android:layout_height="fill_parent">
<LinearLayout
android:layout_weight="0.45"
android:layout_width="fill_parent"
android:id="#+id/linearLayoutMainLogo"
android:orientation="horizontal"
android:gravity="center"
android:layout_height="0px">
<ImageView
android:src="#drawable/logo"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:id="#+id/imageViewLogo"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
I need to set ImageView in center of LinearLayout by horizontal and by vertical simultaneously, but my code doesn't work. Where did I do mistake? Thank you
This is wrong i think:
android:layout_width="0px"
android:layout_height="0px"
Just correct with this:
<LinearLayout
android:layout_width="fill_parent"
android:id="#+id/linearLayout2"
android:orientation="vertical"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_weight="0.45"
android:layout_width="fill_parent"
android:id="#+id/linearLayoutMainLogo"
android:orientation="horizontal"
android:gravity="center"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/icon"
android:gravity="center"
android:layout_width="wrap_content"
android:id="#+id/imageViewLogo"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>

Categories

Resources