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>
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 am trying to resize the inflated view but not working
I am adding this view to AlertDialog
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.popup_util, null,false);
view.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 200)); //Set params here
......
dialog.setView(view);
popup_util
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/setting_password_popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/colorPrimary"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtPopUpTitle"
android:padding="10dp"
android:layout_centerHorizontal="true"
android:textColor="#000000" android:textSize="24dp" android:background="#color/colorAccent"/>
<TextView android:padding="10dp" android:layout_width="match_parent" android:layout_below="#+id/txtPopUpTitle" android:id="#+id/txtPopUpMessage" android:layout_height="wrap_content" android:textSize="22dp"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center_horizontal|bottom">
<Button android:id="#+id/btnPopUpCancel"
android:layout_width="match_parent" android:layout_height="50dp"
android:layout_weight="1"
android:textSize="24dp"
android:visibility="gone"
android:layout_centerHorizontal="true"
android:background="#android:color/transparent"
android:text="#string/btn_cancel_text" />
<Button android:id="#+id/btnPopUpOk"
android:layout_width="match_parent" android:layout_height="50dp"
android:textColor="#color/colorAccent"
android:textSize="24dp"
android:layout_centerHorizontal="true"
android:text="#string/btn_ok_text"
android:background="#android:color/transparent"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
But it is not working.. What i m missing ?
u cane try this code:
View view = inflater.inflate(R.layout.active_slide, this);
view.setMinimumWidth(200);
You can use this code its works good in my case
this.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, theSizeIWant));
NOTE: Be sure to use the parent Layout's LayoutParams. Mine is LinearLayout.LayoutParams!
You can do that by setting the width and height to the window of the dialog itself.
alertDialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, 200);
You must do that after showing the dialog using dialog.show()
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'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