I have a custom ListView. This ListView contains Bluetooth Devices found during a Scan. I want to have the possibility to click on one single item (i.e. Bluetooth Device) of the list and at the end of the list I want a Button, that allows me to connect to the selected Device.
The problem is that I can't get a single button at the end of the list, but I have a button for each item of the list.
This is my XML:
<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=".DeviceScanActivity"
tools:ignore="ExtraText">
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
<TextView
android:id="#+id/device_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20sp" >
</TextView>
<TextView
android:id="#+id/device_address"
android:textSize="12sp"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/connectButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Connect"
android:onClick="ConnectButton"
android:layout_gravity="center">
</Button>
</LinearLayout>
</RelativeLayout>
Why do you only have weight on your ListView and not your other Layout objects?
Try this:
1)Change your parent layout from relative to Linear (Vertical).
2)Nest everything outside of the ListView (Your texts and Button) in another Linear (or any other) layout. Then set both your ListView and other view's height to "0dp". Also add weights to them, based on your preference; I'd say probably something like 6:1 or 7:1 (big numbers for ListView and small number for your other layout containing buttons)
This should show them at the bottom of your ListView
Related
This is my current design in my main activity.
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/txtSubestacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Subestacion"
android:textSize="18sp" />
<Spinner
android:id="#+id/spSubestacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown" />
<TextView
android:id="#+id/txtClasificacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Clasificacion"
android:textSize="18sp" />
<Spinner
android:id="#+id/spClasificacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown" />
<ScrollView
android:id="#+id/scrollEventos"
android:layout_width="match_parent"
android:layout_height="350dp"
android:paddingTop="30dp"
android:paddingBottom="10dp">
<LinearLayout
android:id="#+id/linearEventos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<Button
android:id="#+id/btnRegistros"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Actualizar Registros" />
</LinearLayout>
</ScrollView>
I have a second scrollview inside the first one because this one is filled with multiple labels created from code. These labels can take up a lot of space, so a scrollview is suitable for proper navigation.
The first scrollview that contains everything is so that when changing the screen in landscape mode, you can continue browsing and access all the controls that are displayed.
The problem is that when changing the screen in landscape mode, only the first scrollview works, which allows you to see all the controls correctly. However, the second scrollview showing the records cannot be moved, and therefore the records are cut.
Is there a way to freeze the first scrollview when the user wants to slide the records and allow the second scrollview to be moved?
Or is it that I should create my design differently?
Note: Everything works correctly with the screen in portrait mode. All the controls are accessible at first instance and the second scrollview can be slid correctly allowing to see the records.
I've tried working with the layout_weight property but it didn't work for me or I just didn't know how to use it.
Use this:
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<!-- Other Layout or scrollView-->
</android.support.v4.widget.NestedScrollView>
Android site: NestedScroll
I have a RecyclerView with expandable items, each item having a list of its own. I want a particular button to be sticky on the item while scrolling, like the Apply for selected shifts button in the image given below.
<RelativeLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="16dp"
tools:context=".fragments.TestFragment">
<LinearLayout
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/Id_btn"
android:layout_alignParentTop="true"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/Id_recyc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
>
</android.support.v7.widget.RecyclerView>
</LinearLayout>
<android.support.v7.widget.AppCompatButton
android:id="#+id/Id_btn"
android:layout_width="match_parent"
android:layout_height="#dimen/btn_height"
android:layout_marginTop="10dp"
android:text="test"
android:layout_alignParentBottom="true"
android:textAllCaps="false"/>
</RelativeLayout>
This will place the button at the bottom of the screen irrespective of the length of the recyclerview.The button will stick to its place as you desire hope it helps
So I'm creating a list view that grows and shrinks based off of the user input and I need four buttons but I don't know how to have buttons that are aligned with the bottom of the list view and are in a 2x2 grid fashion. I've already tried relative layout and it didn't seem to work. Thank you
I've created the XML according to what I believed you wanted to see.
Please check the following screen shot:
The following is the XML file:
<?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"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="BUTTON3"/>
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="BUTTON4"/>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_above="#id/linearLayout1">
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="BUTTON1"/>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="BUTTON2" />
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/linearLayout2">
</ListView>
</RelativeLayout>
As you can see that I am using the RelativeLayout as the base wrapper and within the RelativeLayout, I have 2 linearlayouts for the buttons (2 each) and finally the list view. The purpose for the LinearLayout is simply to add weight to the buttons so they would share the equal amount of horizontal space. If that does not fit your criteria, feel free to remove them. The main thing to learn is that relative layout allows you to use attributes such as alignParenBottom, layout_above, layout_below and so on. These attributes allow you to place your elements anywhere on the screen and adjust them according to these attributes.
Let me know if you have any questions.
Friends I'm new to Android and I'm facing all those beginner's problems such as overload of information in the first steps of the learning proccess.
I want to develop an activity where its content resembles a picture gallery, with 2 pics per row. With HTML + CSS I would create divs with float:left and half of the parent element's width. Or maybe creating a table with two cells per row. Wouldnt do that in HTML for a photo gallery but I know it works.
I want to do this programmatically and Ive seen solutions with TableLayout and RelativeLayout. But i dont understand how to create a container such as DIV in HTML for the data (will be a title and an image) i want to place in the layout. And i dont understand how to make every two of them side by side.
How would you do that in terms of layout and programming?
I will update this thread with an image to a better understanding of my problem later.
Use below XML code as template to inflate in your activity and then add to ListView
In Main Activity inflate view by LayoutInflater:
LayoutInflater inflater = getActivity() .getLayoutInflater();
View view = inflater.inflate(R.layout.my_stats_layout, null);
Use Above view as item in ListView - Use this tutorial for custom ListView
http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92
XML Layout:
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/img1" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/img2" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Img1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Img2"
android:textColor="#000" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
How can I make the question textview to be on top of the list, as if it was the first item of the list, letting the question stretch as down as it needs then starting the list underneath it, while both of them being scrollable by the same scroller?
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroller"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_above="#+id/linearLayout2"
android:layout_alignParentTop="true"
android:layout_weight="4.34"
android:fillViewport="true"
android:scrollbars="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/question_content"
android:layout_width="match_parent"
android:layout_height="56dp"
android:padding="8dip"
android:textSize="20sp" />
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="84dp"
android:layout_weight="0.10" >
</ListView>
</LinearLayout>
</ScrollView>
You should not put ListView inside ScrollView. Use ListView.addHeaderView() instead.
You don't need, and probably not want, a ListView in ScrollBar. The ListView is already scrollable. You can replace the ScrollView with a RelativeView. Then in your 'ListView` use
android:layout_below="#+id/question_content"
this should give you what you want. Then you shouldn't even need the LinearLayout