I'm trying to add to the first group of the new layout. But the disclosure of the groups, the new layout appears in the other groups. I want a new layout was added only in the first group expandblelistview. But my clumsy code adds it in all groups, and these groups do not clickable.
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.mylistview, parent, false);
}
if(groupPosition == 0)
{
LinearLayout fl = (LinearLayout)convertView.findViewById(R.id.mylistview);
View fl2 = inflater.inflate(R.layout.infoday, fl, true);
//fl.addView(fl2);
}
TextView textChild = (TextView)convertView.findViewById(R.id.textView22);
Group group = (Group)getGroup(groupPosition);
textChild.setText(group.string);
return convertView;
}
My list
<?xml version="1.0" encoding="utf-8"?><FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ExpandableListView
android:id="#+id/expListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true">
</ExpandableListView>
My group in list
<?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="fill_parent"
android:layout_height="80dp"
android:orientation="vertical"
tools:context=".MainActivity"
android:id="#+id/mylistview">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/textView22"
android:textSize="14sp"
android:layout_marginTop="20dp"
android:layout_marginLeft="35dp" />
My new layout, which i want to add
<?xml version="1.0" encoding="utf-8"?><FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/frameinfoday"
android:paddingBottom="10dp">
<ImageView
android:layout_width="150dp"
android:layout_height="25dp"
android:id="#+id/imageView2"
android:background="#drawable/day" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="СУББОТА"
android:id="#+id/textView25"
android:textColor="#ffffffff"
android:layout_marginLeft="25dp"
android:layout_marginTop="4dp"
android:textIsSelectable="true"
android:textSize="11sp" />
View headerView = View.inflate(this, R.layout.infoday, null);
listView.addHeaderView(headerView);
This is work for me. I add my layout in header listview!
More information: add to header and footer in listview
Related
I have a custom layout for the list's cells where you have a checkbox and 3 text elements. I found on Stackoverflow that I should add android:descendantFocusability = "blocksDescendants" on my LinearLayout where the list is but it doesn't change anything.
Here is my XML for the page with the list :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/cl_fragment_detail_apero"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
tools:context=".ui.home.AperoDetailFragment"
android:descendantFocusability="blocksDescendants">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:id="#+id/name_apero"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingHorizontal="10dp"
android:gravity="left"
android:textSize="18sp" />
<TextView
android:id="#+id/date_apero"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingHorizontal="10dp"
android:ems="10"
android:gravity="right"
android:textSize="18sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#DDDDDD" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:id="#+id/txtView_shopping_list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:paddingHorizontal="10dp"
android:text="#string/shopping_list_title" />
<Button
android:id="#+id/btn_add_ingredients"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/colorAccent"
android:clickable="true"
android:focusable="true"
android:padding="15dp"
android:src="#android:drawable/ic_menu_add"
android:text="#string/btn_add_ingredient"
android:textColor="#FFFFFF" />
</LinearLayout>
<ListView
android:id="#+id/list_ingredient"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal" />
</LinearLayout>
then in my Java code I set the listener like this :
#Override
public View onCreateView(#NonNull final LayoutInflater inflater,
final ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_detail_apero, container, false);
final TextView name = (TextView) root.findViewById(R.id.name_apero);
name.setText(detailApero.name);
TextView date = (TextView) root.findViewById(R.id.date_apero);
date.setText(detailApero.date);
// some code that aren't useful right now
//-------------- List of ingredients for the detailed apero
final ListView list_ingredient = (ListView) root.findViewById(R.id.list_ingredient);
LaperoDatabase db = Room.databaseBuilder(root.getContext(),
LaperoDatabase.class, "lapero_db").allowMainThreadQueries().build();
IngredientDao dbIngredient = db.getIngredientDao();
//#TODO control if we get the right ingredients
ArrayList<Ingredient> ingredient_of_this_apero = (ArrayList<Ingredient>) dbIngredient.getIngredientsFromApero(detailApero.aid);
IngredientAdapter adapter = new IngredientAdapter(root.getContext(), R.layout.ingredient_cell_layout, ingredient_of_this_apero);
list_ingredient.setAdapter(adapter);
list_ingredient.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
Ingredient selected_ingredient = (Ingredient) adapter.getItemAtPosition(position);
Log.e("test","okasodkdaso");
}
});
return root;
}
I also tried using a OnItemLongClickListener but it also does nothing. How can I put a listener for the list items ?
I have a custom ListView where each element has an icon and 3 TextViews. I am able to change the background to white but now I don't see the dynamic effect when I click on the text. I mean the gray color on touch (ripple effect).
this is the MainActivity
ListView mListView = findViewById(R.id.list_layout);
names = new ArrayList<>();
macs = new ArrayList<>();
adapter= new CompleteListAdapter(this, names, macs);
mListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
Method in my CompleteListAdapter which extends BaseAdapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = LayoutInflater.from(mContext).inflate(R.layout.two_line_icon, parent, false);
TextView text1 = (TextView) rowView.findViewById(R.id.text1);
TextView text2 = (TextView) rowView.findViewById(R.id.text2);
ImageView icon = (ImageView) rowView.findViewById(R.id.icon);
text1.setText(names.get(position));
text2.setText(macs.get(position));
return rowView;
}
This is part of the main_activity.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
tools:context="com.example.myapp.MainActivity">
<ListView
android:id="#+id/list_layout"
android:layout_width="match_parent"
android:layout_height="400dp"></ListView>
</LinearLayout>
And this is the single entry of the ListView: two_line_icon.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:background="#android:color/white"
android:layout_height="match_parent">
<ImageView android:id="#+id/icon"
android:foregroundTint="#android:color/transparent"
android:foregroundTintMode="add"
android:scaleType="fitStart"
android:src="#drawable/img_logo"
android:tintMode="add"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Texto 1"/>
<TextView android:id="#+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Tet2"/>
<TextView android:id="#+id/text3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="text3"/>
</LinearLayout>
</LinearLayout>
I'm trying to load views with image and text by using CardView in the list. However, there is a divider between the cards.
This is my output:
I'm trying to do like in this video, no divider lines.
Here's my XML code.
activity_cat_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/lvCat"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
item_cat
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FEFEFE"
android:layout_margin="8dp"
app:cardCornerRadius="4dp"
card_view:cardCornerRadius="4dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
card_view:cardElevation="8dp">
<!-- Main Content View -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/ivCat"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"/>
<TextView
android:id="#+id/tvCatName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ivCat"
android:text="Name"
android:maxLines="3"
android:padding="8dp"
android:textColor="#222"
android:textStyle="bold"
android:textSize="22dp" />
<TextView
android:id="#+id/tvDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvCatName"
android:text="Description"
android:maxLines="3"
android:padding="8dp"
android:textColor="#666"
android:textSize="14dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
edit 1: add CatAdapter.java
public class CatAdapter extends ArrayAdapter<Cat> {
public CatAdapter(Context context, ArrayList<Cat> cats) {
super(context,0,cats);
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//get data item from this position
Cat cats = getItem(position);
//check if existing view is being reused, otherwise inflate the views
if (convertView == null)
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_cat, parent, false);
//lookup view for data population
TextView tvName = (TextView)convertView.findViewById(R.id.tvCatName);
TextView tvDescription = (TextView)convertView.findViewById(R.id.tvDescription);
ImageView ivCat = (ImageView)convertView.findViewById(R.id.ivCat);
//populate into template view from data source
tvName.setText(cats.getName());
tvDescription.setText(cats.getDescription());
Glide.with(getContext()).load(cats.getImages()).into(ivCat);
return convertView;
}
}
You can remove divider with xml :
<ListView
android:id="#+id/lvCat"
android:dividerHeight="0dp"
android:divider="#null"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
I want to create a square buttons in a fragment. My fragment layout file is:
<?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:weightSum="2"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
android:weightSum="2"
>
<Button
android:id="#+id/main_menu_news"
android:text="#string/main_menu_news_text"
style="#style/buttons"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginRight="5dp"
/>
<Button
android:id="#+id/main_menu_insta"
android:text="#string/main_menu_insta_text"
style="#style/buttons"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginLeft="5dp"
/>
</LinearLayout>
I changed the height of buttons in onCreateView. This function implementation is:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
super.onCreateView(inflater, container, savedInstanceState);
View view=inflater.inflate(R.layout.main_menu, container, false);
ViewGroup layout = (ViewGroup) getActivity().getLayoutInflater().inflate(R.layout.main_menu, null);
for(int i=0;i<layout.getChildCount();i++){
View v=layout.getChildAt(i);
if(v instanceof ViewGroup){
ViewGroup vg=(ViewGroup)v;
for(int j=0;j<vg.getChildCount();j++){
View v2=vg.getChildAt(j);
if(v2 instanceof Button){
Button btn=(Button)v2;
btn.setHeight(btn.getMeasuredWidth());
}
}
}
}
return view;
}
I cannot change height of buttons in onCreateView and getMeasuredWidth function always returns 0.
How can I fix it?
To resolve this you can create a linear layout with wrap content dimension and add this button to that layout. Then set the Height of button and i think then, getMeasuredWidth() should not return 0.
This is my piece of code, you can customize it according to you
LinearLayout newLL = new LinearLayout(mContext);
newLL.setLayoutParams(newLinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
newLL.setOrientation(LinearLayout.HORIZONTAL);
newLL.setGravity(Gravity.LEFT);
newLL.addView(btn);
widthSoFar = btn.getMeasuredWidth();
button.setLayoutParams(new LinearLayout.LayoutParams(10, 100));
Example here.
You can do it from 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="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/colorAccent"
android:gravity="center">
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="btn1" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/colorPrimary"
android:gravity="center">
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="btn2" />
</LinearLayout>
I have an ExpandableListView setup working very properly and as I wanted but with a minor problem I want the group indicator (the arrow) to expand and collapse when I open or close the group. This function is although partially working but not fully correct.
The problem is that when I expand lets say group1 the indicator of group2 automatically gets invisible (Image at the end of post). and if I continue to expsnd group3 then the indicator of group4 hides. at the end only the terminal groups i.e ending groups have the indicator, the indicators of all other groups are invisible. I tried debugging this very hard but could find any reason as to why this is happening. Maybe you all can take a look!
This is the activity_main.xml (Yes I am Using Android Navigation Drawer Here)
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ExpandableListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:groupIndicator="#null"
android:divider="#F3F3F3"
android:padding="1dp"
android:dividerHeight="1dp"
android:background="#FFFFFF" />
</android.support.v4.widget.DrawerLayout>
drawer_group_item.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" >
<TextView
android:id="#+id/group_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="24sp"
android:gravity="left"
android:minHeight="30dp"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textColor="#616161"
android:text="Hello"
android:paddingRight="40dp" />
<ImageView
android:id="#+id/indicator"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingRight="10dp"
android:src="#drawable/ic_action_expand" />
</RelativeLayout>
drawer_list_item
<?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="horizontal" >
<ImageView
android:id="#+id/store_image"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:padding="5dp"
android:scaleType="fitXY" />
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:textColor="#616161"
android:textSize="24sp" />
</LinearLayout>
getGroupView from the adapter
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.drawer_group_item, null);
holder.imageView = (ImageView) convertView.findViewById(R.id.indicator);
holder.textView = (TextView) convertView.findViewById(R.id.group_title);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(parentItems.get(groupPosition));
if(groupPosition == 0 || groupPosition == 5){
holder.imageView.setVisibility(View.GONE);
}
if(isExpanded){
holder.imageView.setImageResource(R.drawable.ic_action_collapse);
}
if(!isExpanded){
holder.imageView.setImageResource(isExpanded?R.drawable.ic_action_collapse:R.drawable.ic_action_expand);
}
return convertView;
}
Image: http://imgur.com/JzsVdQz
Image order is right to left (sorry about that)
EDIT: I also want some categories to not expand. like if there is a group which doesnot have subitems I want to hide the indicator for that group. I am using this code currently
if(groupPosition == 0 || groupPosition == 5){
holder.imageView.setVisibility(View.GONE);
}
but how do i hide the indicator in the statelist xml and setIndicatorBounds method (as stated by user3664308)
You should instead create a drawable with different states for an expanded and collapsed group like:
drawable/group_indicator.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_expanded="true"
android:drawable="#drawable/ic_action_expand" />
<item
android:drawable="#drawable/ic_action_collapse" />
</selector>
And then set the indicator:
mExpList.setIndicator(R.drawable.group_indicator.xml);
Then adjust the indicators position by setting its bounds:
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
mListView.setIndicatorBounds(myLeft, myRight);
} else {
mListView.setIndicatorBoundsRelative(myLeft, myRight);
}