Button is now at the TOP after setting setEmptyView - android

After setting the setEmptyView, the button which was formerly at the bottom now goes up to the top of layout. Please help me figure out why. Here's my code:
activity_list.xml :
<?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:gravity="top|center"
android:orientation="vertical" >
<ListView
android:id="#+id/listview_snyc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
<Button
android:id="#+id/button_refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:gravity="center"
android:text="Sync" />
</LinearLayout>
activity_list_empty.xml :
<?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" >
<ImageView
android:id="#+id/imageview_empty"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/empty" />
</RelativeLayout>
Java Code :
list = (ListView) findViewById(R.id.listview_snyc);
list.setAdapter(new MasterFileMenuAdapter(this,data));
View emptyView = getLayoutInflater().inflate(R.layout.activity_list_empty, null);
addContentView(emptyView, list.getLayoutParams());
list.setEmptyView(emptyView);
Whenever the ListView is empty I got this result:
The Refresh Button should be in the bottom not on the top.
Any idea?

Use RelativeLayout in the activity_list.xml as well and put
alignParentBottom="true"
in the Button tag

Move your button to your empty view and align it to bottom:
activity_list.xml :
<?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:gravity="top|center"
android:orientation="vertical" >
<ListView
android:id="#+id/listview_snyc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
</LinearLayout>
activity_list_empty.xml :
<?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" >
<ImageView
android:id="#+id/imageview_empty"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/empty" />
<Button
android:id="#+id/button_refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:gravity="center"
android:layout_alignParentBottom="true"
android:text="Sync" />
</RelativeLayout>

Related

Android Button in LinearLayout Fit Left

I want to put button like above picture. But I have below image. I can't success this. Search a lot of but can't find it.
LinearLayout in LinearLayout has transparent background and there is button left of this.
Activity.xml :
<?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:background="#drawable/genel_arkaplan"
android:orientation="vertical" >
<LinearLayout
android:orientation="vertical"
android:layout_width="750dp"
android:layout_height="wrap_content"
android:paddingLeft="40dp"
android:layout_gravity="center_vertical|center_horizontal"
android:background="#drawable/lay_transparan_aramasecenek"
android:layout_marginTop="100dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/lay_transparan_aramasecenek_beyaz">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:background="#drawable/kod_ile_arama"
android:id="#+id/imageButton" />
</LinearLayout>
</LinearLayout>
remove android:paddingLeft="40dp from LinearLayout Tag
Just remove below code from your "LinearLayout"
android:paddingLeft="40dp"
This is adding extra padding to your parent layout.
Below will be your final code.
<?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:background="#drawable/genel_arkaplan"
android:orientation="vertical" >
<LinearLayout
android:orientation="vertical"
android:layout_width="750dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:background="#drawable/lay_transparan_aramasecenek"
android:layout_marginTop="100dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/lay_transparan_aramasecenek_beyaz">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:background="#drawable/kod_ile_arama"
android:id="#+id/imageButton" />
</LinearLayout>
</LinearLayout>
Try this code for batter result for your Button Icon. I Hope it is helpful for You.
<?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:background="#color/color_green"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/dimen_50"
android:layout_gravity="center"
android:layout_marginLeft="#dimen/dimen_10"
android:layout_marginRight="#dimen/dimen_10"
android:layout_marginTop="100dp"
android:background="#android:color/holo_blue_light">
<Button
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#mipmap/ic_share"
android:scaleType="fitCenter" />
</RelativeLayout>
</LinearLayout>

How to apply background Image on ScrollView android

I want to apply background image on Relativelayout Under ScrollView.
The Image Appear on XML View but Not Appear on Actual device.
I have added ImageView on Ralativelayout
>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/parentView"
>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/cloudbg"
/>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:id="#+id/vo"
android:layout_below="#id/relativeLayout"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="XYZ"
/>
<TextView
android:layout_width="wrap_content"
android:layout_marginLeft="25dp"
android:layout_height="wrap_content"
android:id="#+id/textvo"
android:text="Voter Number"
/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
Thanks
can try this way:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/cloudbg">
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:id="#+id/vo"
android:layout_below="#id/relativeLayout"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="XYZ"
/>
<TextView
android:layout_width="wrap_content"
android:layout_marginLeft="25dp"
android:layout_height="wrap_content"
android:id="#+id/textvo"
android:text="Voter Number"
/>
</LinearLayout>
</ScrollView>
Use android:background="#drawable/background_image" attribute in Relative or Linear Layout tag.
Set background for scrollview
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/ic_launcher" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>

Relative layout placing to other relative layout

I am developing a chat app.I want to place edittext to bottom then messages above it.But I have a problem:
I don't want to place messages to top of edittext and button.How can I prevent this ? This is my xml:
<?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="#ECECFB"
android:orientation="vertical" >
<ListView
android:id="#+id/message_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<EditText
android:id="#+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/get_from_user"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="Gönder" />
</RelativeLayout>
</RelativeLayout>
And I have one more question.How can I do message list system like other chat apps ? I mean when I send a message this message place to bottom of the list not to top.Now in my xml it's placing to top.Like this:
Set your ListView above the EditText layout.
Your layout will be as below :
<?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="#ECECFB">
<RelativeLayout
android:id="#+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<EditText
android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/get_from_user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="Gönder" />
</RelativeLayout>
<ListView
android:id="#+id/message_list"
android:layout_above="#id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
And for your second problem, set
android:stackFromBottom="true"
android:transcriptMode="normal"
to the ListView
For example, add
android:layout_below="#id/message_list"
to your inner RelativeLayoutto constrain the ListView and bottom RelativeLayout not to lay out on top of each other.
Try this:
Add this to your Relative layout : android:layout_below="#+id/message_list"
and change your listview to wrap_content
Basically do 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"
android:background="#ECECFB"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/comment_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
<EditText
android:id="#+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<Button
android:id="#+id/get_from_user"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="Gönder" />
</RelativeLayout>
<ListView
android:id="#+id/message_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/comment_layout"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
Try changing this:
<ListView
android:id="#+id/message_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
To this:
<ListView
android:id="#+id/message_list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
This should do the trick
First Problem:
You need to set on RelativeLayout
android:bellow="#+id/message_list"
2º Problem:
You need to set on ListView:
android:stackFromBottom="true"
android:transcriptMode="normal"
Final Xml:
<?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="#ECECFB"
android:orientation="vertical" >
<ListView
android:id="#+id/message_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stackFromBottom="true"
android:transcriptMode="normal"/>
<RelativeLayout
android:bellow="#+id/message_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<EditText
android:id="#+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/get_from_user"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="Gönder" />
</RelativeLayout>
</RelativeLayout>
Add android:layout_above="#+id/id_of_relative_layout".
Add an ID to the RelativeLayout which contains the EditText.

Layout: TextView at the top and ListView at the center

Currently my ListView is exactly below my TextView. How can I make the ListView at the center of the screen while my TextView maintain its position on top?
Below is my 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/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/hp_color"
tools:context=".MainActivity" >
<TextView
android:id="#+id/cityText"
style="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<TextView
android:id="#+id/condDescr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/cityText" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/condDescr"
android:orientation="vertical" >
<ListView
android:id="#+id/mainListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
</LinearLayout>
</RelativeLayout>
You can use Gravity to place the listview in center of the screen.
<?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/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/hp_color"
tools:context=".MainActivity" >
<TextView
android:id="#+id/cityText"
style="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<TextView
android:id="#+id/condDescr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/cityText" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/condDescr"
**android:gravity="center_vertical|center_horizontal"**
android:orientation="vertical" >
<ListView
android:id="#+id/mainListView"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
</LinearLayout>
</RelativeLayout>
Try setting
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
And although I'm no expert here, it's my impression that there's little point to having a ListView in a LinearLayout. Both are scrollable (which isn't advised) and there's not much point if LinearLayout only has one child.

Top and bottom layout does not work as expected

I have two view in a vertical orientated LinearLayout like this:
<?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">
<RelativeLayout
android:id="#+id/top"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text="dddd"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/below"
android:layout_below="#id/top"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:background="#33000000"
android:textColor="#80000000"
android:id="#+id/copy_right"
android:text="Copyright #2013"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
</LinearLayout>
While I want the bottom view with a wrap_content height, then the top take all the left space.
But it does not work.
What's the problem? Anyone can help me to fix it?
BTW, this is an alternative method:
<?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">
<RelativeLayout
android:id="#+id/top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:text="dddd"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/below"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_weight="0">
<TextView
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:background="#33000000"
android:textColor="#80000000"
android:id="#+id/copy_right"
android:text="Copyright #2013"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
</LinearLayout>
It meet my requirement, but I do not want make the bottom view with a fixed height.
Try this simplified layout:
<?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"
>
<TextView
android:id="#+id/copy_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:background="#33000000"
android:textColor="#80000000"
android:text="Copyright #2013"
/>
<RelativeLayout
android:id="#+id/top"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/copy_right"
android:layout_alignParentTop="true"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dddd"
/>
</RelativeLayout>
</RelativeLayout>
Hi you can set weight on top layout
<?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:background="#fff"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/top"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|center_horizontal"
android:text="dddd"
android:textColor="#000" />
</RelativeLayout>
<LinearLayout
android:id="#+id/below"
android:layout_width="match_parent"
android:gravity="right"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/copy_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#33000000"
android:text="Copyright #2013"
android:textColor="#80000000" />
</LinearLayout>
</LinearLayout>

Categories

Resources