Button at bottom of programmatically created layout - java

I made a linear layout where I have a view and a button. And inside that linear layout I am also adding some more elements but programmatically. Here is the code:
<LinearLayout
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<View android:layout_width="match_parent"
android:layout_height="20dp"
android:background="#color/transparentBlack1"/>
<Button
android:layout_width="70dp"
android:layout_height="40dp"
android:shadowColor="#color/noColor"
android:background="#color/transparentBlack1"
android:text="#string/NewButton"
android:textSize="30sp"
android:gravity="center"
android:textColor="#color/background2"
android:onClick="addLine"/>
</LinearLayout>
With the addLine function I just add another element in the layout.
Is there any way to keep the button at the bottom of every element inside the layout, so other elements that I create with code will be above the button?

I guess you use linearLayout.addView(view); to add your views, correct? You can specify where the view should be added like the following:
linearLayout.addView(view, linearLayout.getChildCount() - 1);
This should add it between the view and the button.
See documentation for method explanations:
addView(View, int)
getChildCount()

Try make that button outside that layout, something like this:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<View android:layout_width="match_parent"
android:layout_height="20dp"
android:background="#color/transparentBlack1"/>
</LinearLayout>
<Button
android:layout_width="70dp"
android:layout_height="40dp"
android:shadowColor="#color/noColor"
android:background="#color/transparentBlack1"
android:text="#string/NewButton"
android:textSize="30sp"
android:gravity="center"
android:textColor="#color/background2"
android:onClick="addLine"/>
</LinearLayout>
Hope this helps

Related

Scrollview's last bottom contents hidden by the bottom navigation view

Hi coders I have a constraint layout, in it is a bottom navigation view
a tool bar on top and a scrollview which has lots of buttons going down vertically.
The problem is that the scrollview's last buttons are hiding behind the bottom navigation view after I scroll all the way to bottom how can this be solved
On the actual code there are plenty of buttons in scrollview through here I add only a few here.
<android.support.constraint.ConstraintLayout 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:id="#+id/container"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
android:id="#+id/tutorialsinclude"
layout="#layout/app_bar_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/tutorialsinclude"
android:id="#+id/webtut">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="web "
android:id="#+id/tutorialsButton1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tutorials 1 "
android:id="#+id/tutorialsButton1"/>
</LinearLayout>
</ScrollView>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigationbb"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="#ffffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/navigation" />
</android.support.constraint.ConstraintLayout>
You can just put a footer in the end to fix this issue. Just make a View below the last button. Something like this:
<View android:layout_width="match_parent"
android:layout_height="32dp" />
You just have to adjust the height to make it work for your layout. Also, you really shouldn't manually put all these buttons in a scrollView. should use a RecyclerView instead.
Set constraint to bottom of scrollview to top of bottom navigation view and set height as 0dp which will make height to match the constraint
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="#id/tutorialsinclude"
app:layout_constraintBottom_toTopOf="#id/navigationbb"
android:id="#+id/webtut">

how to do expert animated swiping layout

I dont know what is this called , but i want to make my layout work like this, i got a linear layout inside a layout like this
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:orientation="vertical">
.....
</LinearLayout>
</LinearLayout>
and below of this i got recycler view which height size is not more than the layout
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/form_dosen_list"
android:layout_width="0dp"
android:layout_height="0dp"
android:paddingBottom="10dp"
app:layout_constraintTop_toBottomOf="#+id/mhs3_TV1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintWidth_percent="1"
app:layout_constraintWidth_default="percent"
app:layout_constraintHeight_percent=".9"
app:layout_constraintHeight_default="percent"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:background="#color/gray"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
and i want to make this linear layout gone ( animated went to above and gone) by swipe up
and then focusing this recycler and make this recycler view height become match with layout
i know i can jst simply use simple scrollview but its not like what i wanted

How to make included component always on top

How to set the background to white (view layout) below my search component? Here is my code
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<include
layout="#layout/component_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"/>
<View
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#color/white"
android:elevation="1dp"/>
</RelativeLayout>
If by below you mean in the y axe, such as in a row after another, what you already have is probably fine. Having the items one after the other inside your relative layout. You can even force this position by using android:layout_toBottomOf="#+id/your_view_above"
Now if by below you mean on the z axe, the one coming down the screen in your direction, (behind) you may want to do as #amit suggested and use elevation instead.
Just Change the order of include tag and View tag like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#color/white"/>
<include
layout="#layout/component_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"/>
</RelativeLayout>
Update: android:orientation="horizontal" was there by mistake as I edited LinearLayout to make it RelativeLayout. You don't need to add that in your RelativeLayout.
Give an id to the include view like search,
set its layout_alignParentTop attribute to true and
set the View's layout_below attribute to "#id/search":
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/search"
layout="#layout/component_search"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"/>
<View
android:layout_below="#id/search"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#color/white"
android:elevation="1dp"/>
</RelativeLayout>
You don't need this attribute android:orientation="horizontal", since it does not apply to a RelativeLayout.

how to put listview inside a horizontalscrollview so that list can scroll hoizontally?

i am creating an application where requirement is like i have list which scroll vertically and inside that list each item is having one list which must scroll horizontally. So i have to set that list inside horizontal scroll view. i have created a custom adpater for that data is showing on the list everything is working fine but only one problem the list is scrolling vertically because i was unable to set that list horizontal. Please help i have searched lots of answers but it is not working.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<android.support.v7.widget.CardView
android:id="#+id/card_view_applicant"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#fff"
app:cardCornerRadius="8dp"
app:cardElevation="1dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="#+id/tv_table_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="T1"
android:textAlignment="center"
android:textColor=" #00008B"
android:textSize="25sp"
android:textStyle="bold" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#000000" />
</LinearLayout>
<HorizontalScrollView
android:id="#+id/hsv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:measureAllChildren="false"
android:scrollbars="none">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_marginLeft="50dp"
android:layout_height="match_parent">
<ListView
android:id="#+id/lv_timing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</HorizontalScrollView>
</android.support.v7.widget.CardView>
</RelativeLayout>
</LinearLayout>
i have tried to do something like this this is but it is not working,and i don't want to use any recyclerview my requirement is just simple i just want to show some that that's why.
You have to use RecyclerView for Horizontal scrollong like:
LinearLayoutManager layoutManager= new
LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(layoutManager);
Use android:layout_weight="1" inside ListView. Make LinearLayout the parent layout of ListView instead of RelativeLayout and check it's work or not. I think it should be work.
<LinearLayout
..........>
<ListView
........
android:layout_weight="1"/>
</LinearLayout>

Splitting Android Layout Vertically Adding Gravity To Both Sides To Center Objects

I have an android layout I'd like to split down the middle vertically and then add gravity so I can center items within the left and the right halves of the layout.
Can someone explain or post an example of how this might be accomplished?
Thus far I've created a layout and a horizontal layout. My problem is the horizontal layout needs balance so I'd like to figure out a way to change my current layout (show below) to include a way of centering the objects within the left and right halves of the screen.
SOURCE:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:orientation="vertical" >
<ImageView
android:id="#+id/emblem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="24dp"
android:gravity="center"
android:scaleType="fitStart"
android:src="#drawable/apn_app_logo" />
<Button
android:id="#+id/go_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/emblem"
android:layout_alignParentRight="true"
android:layout_marginBottom="23dp"
android:layout_marginRight="22dp"
android:background="#drawable/apn_app_go_button" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/go_button"
android:gravity="center"
android:text="#string/start_text2"
android:textColor="#000000"
android:textSize="18sp" />
</RelativeLayout>
First of all android:orientation attribute will not work in Relative Layout.If you want to split the screen half into two equal layout your parent layout will be a linear layout with android:orientation= horizontal. Then inside that have 2 LinearLayout with each android:orientation= vertical. Inside 1st Linear layout you can have the ImageView , Button and TextView with each of their layout_gravity=center.
Hope this helps. If you want to do something in the 2nd half of teh screen , do all teh stuffs in the 2nd LinearLayout.
Happy Coding.
Here's some code which should be self-explanotory:
<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:orientation="horizontal"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#00dd00"
android:gravity="center"
>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#dd0000"
android:gravity="center_horizontal"
>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
and here's what you'll achieve:

Categories