Elements not going where they should in fragment XML - java

fragment_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/movies_fragment_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MoviesFragment">
<ListView
android:id="#+id/lvMovies"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#efefef"
android:layout_weight="100"/>
<Button
android:id="#+id/bLoadData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load api data"
android:layout_weight="1"/>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="vertical"
tools:context=".MainActivity">
<include layout="#layout/fragment_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
onCreate from MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.movies_fragment_ll, new MoviesFragment())
.commit();
}
}
As you can see from the screenshot, the code is meant to display what's on the left, but what it's actually displaying is different. I've tried to play around with the XML and java but it keeps doing that. In some instances, depending on how I arrange the ListView and Button on the screen, I sometimes get two Buttons instead of one.
Why?
To clear the confusion.
Data is being added to the ListView.
Data is added on button click.
But before the button is clicked, and data is added to the ListView.
How do I make the ListView height finish just where the button starts at the bottom. To make the layout look like the one in the XML preview.

android preview shows a list with child.But at runtime you are not adding any element in the list view , so list view is empty . in that case your button moves to up.
if you want your button to stick in bottom use Relative layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/movies_fragment_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MoviesFragment">
<Button
android:id="#+id/bLoadData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load api data"
/>
<ListView
android:id="#+id/lvMovies"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#efefef"
android:layout_above="#id/bLoadData"
android:layout_weight="100"/>
</RelativeLayout>

the list wont be visible if you dont have any data in that list. So that why the button will be shown on the top because listview's height will be 0.
Set some data to the list, and use relative layout to manage the location of the button at bottom

Well change it like This,and Please add some data to adapter
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/movies_fragment_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MoviesFragment">
<ListView
android:id="#+id/lvMovies"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#efefef"
android:layout_weight="100"/>
<Button
android:id="#+id/bLoadData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load api data"
android:layout_weight="1"/>
</LinearLayout>

Related

Scroll whole fragment with fragments inside

I have a fragment called SearchFragment which contains 3 FrameLayouts inside. Fragments are loading at runtime to these FrameLayouts. It looks like this:
<?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"
xmlns:tools="http://schemas.android.com/tools">
<FrameLayout
android:id="#+id/fragment_search__song_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".ui.fragments.search.SearchFragment"/>
<FrameLayout
android:id="#+id/fragment_search__artist_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/fragment_search__song_container"
tools:context=".ui.fragments.search.SearchFragment" />
<FrameLayout
android:id="#+id/fragment_search__album_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/fragment_search__artist_container"
tools:context=".ui.fragments.search.SearchFragment" />
</RelativeLayout>
So, I need to scroll this whole SearchFragment down (to see the third "more" button).
I tried to add ScrollView and NestedScrollView and place layout with these FrameLayouts inside of it, but it only results in scrolling each fragment separately, which is not what I need

When passing a message from one activity to the next, that message always appears under anything else I write. How do I make it appear on top?

Temperature is the text that needs to be displayed at the bottom and activity_display_message is the 1337 that needs to be displayed at the top.
Add a TextView and use textView.setText(your_string); on it. I'm assuming you are trying to print the text on the LinearLayout currently.
<?xml version="1.0" encoding="utf-8"?>
<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="vertical"
tools:context="com.example.myapplication.DisplayMessageActivity">
<TextView
android:id="#+id/activity_display_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/temperature"/>
</LinearLayout>
Code,
TextView textView = (TextView) findViewById(R.id.activity_display_message);
textView.setText(your_string);
you can try this
use two text view to show your value and "Temperature" text. you can use RelativeLayout to easily manage show temperature text at bottom.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_display_message"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tempValue"
android:textSize="25sp"
android:gravity="center"
android:text="Value"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Temperature"
android:textSize="25sp"
android:gravity="bottom|center"
android:layout_below="#+id/tempValue"/>
</RelativeLayout>
and inside java write
TextView txtTempValue = (TextView)findViewById(R.id.tempValue);
txtTempValue.setText(your_temp_value);

Use RelativeLayout outside CoordinatorView

I have a CoordinatorLayout, in where a RecyclerView displays some CardViews, defined in an other layout. Under this scrollable view, i am trying to add a RelativeLayout, which inherits fields for text input, buttons for sending it, etc.
The problem: CoordinatorLayout seems to block the whole screen, although i told to wrap contents height. Nevertheless, RelativeLayout is added (tested it by setting Coordinator-height = 50dp), but off-screen. What is wrong here?
PS: I know how to solve it with android:layout_weight=xx. But thats not what i want to achieve, because it blocks my EditText to expand, if bigger texts are entered.
<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.support.design.widget.CoordinatorLayout
android:id="#+id/data_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</android.support.design.widget.CoordinatorLayout>
<RelativeLayout
android:id="#+id/delete_data_layout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="#id/data_coordinator_layout">
<EditText/>
<Button/>
<Button/>
<Button/>
</RelativeLayout>
</RelativeLayout>
It's actually pretty simple. You need to align your Relative Layout (delete_data_layout) to bottom of your parent. Then you make the Coordinator layout above the Relative Layout. This causes, that Coordinator layout will always stay above the Relative Layout - even when the Edittext expands.
Here's the 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.support.design.widget.CoordinatorLayout
android:id="#+id/data_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#+id/delete_data_layout"> <---- Change here
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.CoordinatorLayout>
<RelativeLayout
android:id="#+id/delete_data_layout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"> <---- Change here
<EditText/>
<Button/>
<Button/>
<Button/>
</RelativeLayout>
</RelativeLayout>

android image and button inside a scrollview

I am using following configuration to properly fit image inside a scrollview.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical" >
<ImageView
android:id="#+id/textNimbo"
android:layout_width="match_parent"
android:layout_height="2492dp"
android:layout_alignParentRight="true"
android:src="#drawable/inicio"
android:adjustViewBounds="true" />
</ScrollView>
However I need to set also a button inside scrollView and below the ImageView. I tried relative and linear layouts inside scrollView but no succes, button is not visible or image doesn't fit to scrollView. Any recommended configuration? Thank you.
A ScrollView can only have one direct child element. So to add a Button and an ImageView, you'll need something like this:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical" >
<LinearLayout
android:id="#+id/layout"
android:layout_width="fill_parent"
android"layout_height="fill_parent">
<ImageView
android:id="#+id/textNimbo"
android:layout_width="match_parent"
android:layout_height="2492dp"
android:layout_alignParentRight="true"
android:src="#drawable/inicio"
android:adjustViewBounds="true" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
You could also use ImageButton I
instead of a ImageView with a transparent Button on it

alignParentBottom In a Layout Inflate

I have a ListActivity that I'm trying to inflate a Button from this layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/add_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/add_prop"
android:layout_alignParentBottom="true"
/>
</LinearLayout>
Using this code:
View footer = getLayoutInflater().inflate(R.layout.footer, null);
ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.addFooterView(footer, null, false);
The problem is that since the list isn't so big that takes the entire screen (at least I think this is why) the button stays at the end of the list instead of sticking to the bottom because of the android:layout_alignParentBottom="true" property. What should I do to make it stick at the bottom?
You have to create a new layout file with this code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/add_button" />
<Button
android:id="#+id/add_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/add_prop"
android:layout_alignParentBottom="true" />
</RelativeLayout >
Then set it as content view of your Activity using setContentView()

Categories