ListView and Button(android) - android

I want to make to the listView with an increase fell button:
My attempt at implementing this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#214075"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listbascet"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<Button
android:id="#+id/sendtoEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Заказать услуги" />
</LinearLayout>

You would better remove the button from the XML and add it to another one.
Then you can use the inflate to get the button and add it as footer view to the original list view.
This way it will be at the end of the list and scroll will it as well!
How to inflate:
LayoutInflater inflater = (LayoutInflater)
getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.mylayout, null);
Button item = (Button) view.findViewById(R.id.item); // how to reference your button
How to add footer:
listView.addFooterView(view); // view is the inflated layout.
More details about footer view contained in this answer as well.

Related

View not getting inserted

I am trying to add an empty view with height 30dp below my gridview but unfortunately, I am not able to see the expected result.
The only thing visible is gridview.
How to display the empty view after all contents of gridview are rendered?
Here is my code:
<?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="#color/colorPrimary">
<GridView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:background="#color/colorAccent"
/>
<View
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="#id/list"
android:background="#color/colorPrimaryDark"/>
</RelativeLayout>
This is my current output.
EDIT:
There are many workarounds you could use to achieve what you're looking for.
In my opinion, using a dedicated library could be the best options for lightness.
Have a look at this: GridViewWithHeaderAndFooter
From the docs:
GridViewWithHeaderAndFooter gridView = (GridViewWithHeaderAndFooter) v.findViewById(R.id.ly_image_list_grid);
LayoutInflater layoutInflater = LayoutInflater.from(this);
View headerView = layoutInflater.inflate(R.layout.test_header_view, null);
View footerView = layoutInflater.inflate(R.layout.test_footer_view, null);
gridView.addHeaderView(headerView);
gridView.addFooterView(footerView);

Dynamic content inside scrollview lost last view

I have ScrollView with LinearLayout and button inside:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:id="#+id/layout_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/layout_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#color/card_color"
android:orientation="vertical"
android:padding="10dp">
</LinearLayout>
<Button
android:id="#+id/button_leave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/leave" />
</LinearLayout>
</ScrollView>
Then in code I add view to layout_list:
View view = LayoutInflater.from(this)
.inflate(R.layout.item_member, mLayoutMemberList, true);
If I add not many items, then button_leave is visible. But if I add a lot of items (more then showing on the screen) then button is not visible on the screen even if I scroll down.
Update:
Looks like it is bug of android.support.design.widget.CoordinatorLayout. I change it to simple linear layout and all work good.
Try like this:
I think layout view is working fine and try below code,
LayoutInflater inflater = context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.layout_list, null, true);

Onclick on a ListView with a scrollable item layout does'nt work

i have an xml layout that i want to use in 2 cases:
1- for listView item in the adapter (without scroll in a row)
2- for another activity (with a scroll)
I've tried to make this layout Scrollable ( scrollView on the parent)
- when it your for listView item ( disacle the scroll effect)
- when it used for the other activity, enable the scrollView
But when i make this solution, i can't have the onClickListner on the listView item.
my xml file: list_messages_fragment_item.xml
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/light_gray_2"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:descendantFocusability="blocksDescendants"
isScrollContainer="false"
>
-- My One chlid LinearLayout containing my content
</ScrollView>
this Xml file is the adapter of my listView: (no need to scroll in this case)
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
vi = inflater.inflate(R.layout.list_messages_fragment_item, null);
....
}
this xml file is used also for an activity layout: (need scroll in this case)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().hide();
setContentView(R.layout.list_messages_fragment_item);
....
}
i found a solution for what i want.
i use a LinearLayout parent in my xml file.
i create an other layour for the activity: this layout contains a ScrollView, inside i include my xml file that i want to re-use.
the new layout to use in the activity:
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:orientation="vertical"
android:scrollbars="none">
<include layout="#layout/list_messages_fragment_item"/>
</ScrollView>
list_messages_fragment_item.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:orientation="vertical"
android:background="#color/light_gray_2"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:descendantFocusability="blocksDescendants"
>
...... my Layout content
</LinearLayout>

Elements inside RelativeLayout not showing up

I am trying to design a 3D page curl.
In my mainActivity, I have a viewPager which contains pages to be curled. For each page I have a separate layout file.
In the layout file if i just add a text view like below it is showing up fine:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/PageTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/green"
android:text="VIEW 1" /> -->
But if I wrap the textview inside relative layout, it just shows a white blank screen and textview is not shown.
<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"
tools:context=".DemoActivity" >
<TextView
style="#style/PageTitle"
android:id="#+id/sampletextview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="VIEW 1" />
</RelativeLayout>
Can someone please help me to know why i am not able to use relative layout in my view layout file:
I inflate the view like this:
LayoutInflater inflater = (LayoutInflater) myAppContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view1 = inflater.inflate(mViewIds[0], null);
Try changing the second parameter of the inflate() call from null to the ViewGroup that view1 is added to. Or post the code where you add it (and include the part where you define the LayoutParams).

List view with buttons at the bottom of dialog theme activity keep overlapping

I am working on android project where I have an activity which is using the theme holo dialog.
Within the dialog I have a list view and what I want to have is at the bottom always on show a linear layout which contains two buttons next to each other. The list view is populated by retrieving data from the call log and populating the adapter of the list.
The problem I am having is the list view always shows on top taking up the whole dialogue with the buttons at the bottom, with the content of the list overlapping the buttons. I want the list to be within the space of the top of the dialogue to the top of the linear layout button group. Below is the code for the main content view. This layout is set using the setContentView in the onCreate method
<?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"
android:orientation="vertical">
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
<LinearLayout android:id="#+id/call_log_select_host_button_group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<Button android:id="#+id/call_log_select_btnCancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Block"/>
<Button android:id="#+id/call_log_select_btnBlock"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
</LinearLayout>
</RelativeLayout>
I'm not sure if it makes any difference but just in case the below is how I am populating the list and setting the view.
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
CallInformation callInformation = (CallInformation)arrayList.get(position);
LayoutInflater inflator = (LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflator.inflate(R.layout.call_log_selection, parent, false);
ImageView imageView = (ImageView)rowView.findViewById(R.id.call_log_selector_image);
if (callInformation.type == android.provider.CallLog.Calls.INCOMING_TYPE)
{
imageView.setImageResource(android.R.drawable.sym_call_incoming);
}
else if (callInformation.type == android.provider.CallLog.Calls.OUTGOING_TYPE)
{
imageView.setImageResource(android.R.drawable.sym_call_outgoing);
}
else if (callInformation.type == android.provider.CallLog.Calls.MISSED_TYPE)
{
imageView.setImageResource(android.R.drawable.sym_call_missed);
}
CheckBox checkbox = (CheckBox)rowView.findViewById(R.id.call_log_selector_checkbox);
checkbox.setText(callInformation.content);
checkbox.setTag(callInformation.telephone);
return rowView;
}
}
The below code is the XML layout that is inflated by the adapter get view function
<?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="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView android:id="#+id/call_log_selector_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:layout_gravity="center"
android:src="#android:drawable/stat_sys_phone_call"/>
<CheckBox android:id="#+id/call_log_selector_checkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
Thanks for any help you can provide.
Try telling your ListView to stay above your LinearLayout
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/call_log_select_host_button_group> // Add this line here
</ListView>

Categories

Resources