Chip programmatically generated not responding to click in ChipGroup - java

I've made a chipgroup and I want to dynamically generate in java its content, populating it with filter chips made from strings. I've made the Chipgroup but trying to add chips I notice that, no matter what I do, the chips are not responding to clicks. I also tried to make a single chip in the design editor and moving it in the same chipgroup, the results is that the design editor created chip is working as intended (checking and unchecking it), the generated ones are static (in the same group).
the code I use for the single working one:
<com.google.android.material.chip.ChipGroup
android:id="#+id/iChipGroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:chipSpacing="8dp"
app:singleSelection="true" >
<com.google.android.material.chip.Chip
android:id="#+id/chip2"
style="#style/Widget.MaterialComponents.Chip.Filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cwae" />
</com.google.android.material.chip.ChipGroup>
the way I create the dynamic ones
Chip chip = new Chip(iChipGroup.getContext());
ChipDrawable chipDrawable = ChipDrawable.createFromAttributes(getContext(), null, 0, R.style.Widget_MaterialComponents_Chip_Filter);
chip.setChipDrawable(chipDrawable);
chip.setText(myText);
iChipGroup.addView(chip);
I've also tried to set an OnCheckedhangeListener both on the single chips and ChipGroup, got nothing.
If it helps, my hierarchy is (inside a fragment):
frameLayout
-scrollView
--LinearLayout
---chipGroup
----chip...

managed to make the chip respond by changing the way I made them this way
LayoutInflater layoutInflater = getLayoutInflater();
Chip chip = (Chip)layoutInflater.inflate(R.layout.my_filter_chip,ingredientsChipGroup, false);

Related

How to change the size of vector drawable programmatically using java

I want to change the size of the icons that are displayed in my recycler view. My recycler view basically shows categories of expenses. I have hardcoded some of the categories with icons in the XML layout that I have created using the android studio, but the user can also define their own categories and can select an icon for that, I am using https://github.com/maltaisn/icondialoglib/wiki
dialogue to allow users to select icons of their choice while defining a new category. I save the IDs of drawable icons from the dialogue to the database and in the onBindViewHolder method of adapter class I retrieve the ID and using that ID I get the icon. If I display a vector drawable that is defined in XML, the size(48x48) of the icon is showing perfect but the size of the icons that are selected from the dialogue is smaller(Image is shared below). I have tried to change it to 48x48 but it is not working.
<ImageView
android:id="#+id/single_column_item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
></ImageView>
vector drawable that is defined in the XML.
<vector android:autoMirrored="true" android:height="48dp"
android:viewportHeight="32" android:viewportWidth="32"
android:width="48dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#333332" android:pathData="M24.832,11.445C24.646,11.167 24.334,11 24,11h-1c-0.553,0 -1,0.447 -1,1v6c0,0.553 0.447,1 1,1h4c0.553,0 1,-0.447 1,-1v-1.5c0,-0.197 -0.059,-0.391 -0.168,-0.555L24.832,11.445zM27,18h-4v-6h1l3,4.5V18z"/>
<path android:fillColor="#333332" android:pathData="M31.496,15.336l-4,-6C26.938,8.499 26.004,8 25,8h-4V6c0,-1.654 -1.346,-3 -3,-3H3C1.346,3 0,4.346 0,6v11c0,1.654 1.346,3 3,3h0v3c0,1.654 1.346,3 3,3h1.142c0.447,1.721 2,3 3.859,3c1.857,0 3.41,-1.279 3.857,-3h5.282c0.447,1.721 2,3 3.859,3c1.857,0 3.41,-1.279 3.857,-3H29c1.654,0 3,-1.346 3,-3v-6C32,16.406 31.826,15.83 31.496,15.336zM3,18c-0.552,0 -1,-0.447 -1,-1V6c0,-0.553 0.448,-1 1,-1h15c0.553,0 1,0.447 1,1v2v2v7c0,0.553 -0.447,1 -1,1H3zM11.001,27c-1.105,0 -2,-0.896 -2,-2s0.895,-2 2,-2c1.104,0 2,0.896 2,2S12.104,27 11.001,27zM24,27c-1.105,0 -2,-0.896 -2,-2s0.895,-2 2,-2c1.104,0 2,0.896 2,2S25.104,27 24,27zM30,23c0,0.553 -0.447,1 -1,1h-1.143c-0.447,-1.721 -2,-3 -3.857,-3c-1.859,0 -3.412,1.279 -3.859,3h-5.282c-0.447,-1.721 -2,-3 -3.857,-3c-1.859,0 -3.412,1.279 -3.859,3H6c-0.552,0 -1,-0.447 -1,-1v-3h13c1.654,0 3,-1.346 3,-3v-7h4c0.334,0 0.646,0.167 0.832,0.445l4,6C29.941,16.609 30,16.803 30,17V23z"/>
</vector>
Java code
#Override
public void onBindViewHolder(#NonNull CategoryViewHolder holder, int position) {
String categoryName=addCategoriesList.get(position).getColumn_value();
holder.textView.setText(categoryName);
if(categoryName.equals("Transport")){
holder.imageView.setImageResource(R.drawable.ic_transport);
}else{
if(IconHelper.getInstance(holder.imageView.getContext()).getIcon(addCategoriesList.get(position).getIconID())!=null){
Drawable drawable=IconHelper.getInstance(holder.imageView.getContext()).getIcon(addCategoriesList.get(position).getIconID()).getDrawable(holder.imageView.getContext());
drawable.setBounds(0,0,100,100);
holder.imageView.setImageDrawable(drawable);
}}
}
This line looks like a likely culprit:
drawable.setBounds(0,0,100,100);
These are pixel values, and so they will cause the icon to appear at different physical sizes depending on the pixel density of the device you're using to test. If your device is xxxhdpi, 100px will be much smaller than 48dp.
You could convert px to dp in order to get the right values to use here, but I think you can go even simpler. Once you know that you're using a user-selected image, you don't actually have to build the Drawable object. Instead, you can use the resource value directly:
int iconId = addCategoriesList.get(position).getIconID();
holder.imageView.setImageResource(iconId);

Is it possible to modify xml in java programatically. specifically layout_alignBottom, layout_alignLeft and layout_alignStart

A few ImageViews have been already created in xml
eg:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/blue_dot1"
android:src="#drawable/checkers_blue1"
android:layout_alignBottom="#+id/y1"
android:layout_alignLeft="#+id/x1"
android:layout_alignStart="#+id/x1"/>
Is it possible to update the xml later on programatically in Java for the alignbottom, left and start variables (so that the blue_dot1 image can be moved along the x/y grid of images)
For example I am going to make a method which has a X and Y arg to allow movement in a simple grid, its just the xml part i cant figure out.
yes this is very much possible. but you are not modifying the XML instead you are modifying properties of a specific view:
check this question
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
button.setLayoutParams(params); //causes layout update
You can not change the xml-file. But you can set LayoutParams of your view programatically.
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.removeRule(RelativeLayout.ALIGN_BOTTOM);
No, modifying the XML is not possible to do from Java code. However, you can modify the Views layout properties programatically even after the XML properties have been declared and the view has been drawn. Its fairly simple to do. You just get a reference to that image view:
ImageView myImg = (ImageView)findViewById(R.id.myImg);
myImg.setLayoutParams([Your params here]);

Android cardView click callback

I am trying to use this library for card view
https://github.com/DenisMondon/material-design-library
<com.blunderer.materialdesignlibrary.views.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:mdl_title="CardView"
app:mdl_description="A Left Image CardView"
app:mdl_normalButton="Normal"
app:mdl_highlightButton="Highlight"
app:mdl_imagePosition="left"
app:mdl_image="#drawable/image" />
I can not figure out how to add a onClickListener for the labels normalButton and highlightButto.
Can someone please give me a hand. thank you
Also how to set an Image programmatically into this cardView?
It has following two listeners
private OnClickListener mOnNormalButtonClickListener;
private OnClickListener mOnHighlightButtonClickListener;
You need to implement CardView.mOnNormalButtonClickListener, CardView.mOnHighlightButtonClickListener in your activity and override their methods to implement the click listeners for these buttons and set them using these functions.
setOnNormalButtonClickListener(OnClickListener onNormalButtonClickListener)
setOnHighlightButtonClickListener(OnClickListener onHighlightButtonClickListener)
#Joolah
With the latest version of the library, you can have a cardview url image.
Just write this:
myCardView.setImageUrl("http://your_image_url");

custom rows in ListView

i have the following problem, I am writing an app that repeats the http://www.rottentomatoes.com/mobile/ website, if you look on the site there are deviders the "Opening this Week", "Top Box Office" and "Also in Theaters" so my question is, how do i add this custom rows to the ListView that holds all the movies
here is what i have so far, please pitch me some ideas so it will look exactly like on the site
The easiest way to achieve this would be to include the dividers in the layout of item views. Then make them VISIBLE only when the view maps to the first item of a new category, otherwise leave them as GONE.
Example (in semi-pseudocode):
First, make your item layout like this:
<LinearLayout orientation=vertical>
<TextView id=divider>
<your original item layout>
</LinearLayhout>
Then, in the adapter's getView():
Film item = getItem(position);
boolean needsDivider = (position == 0 || getItem(position - 1).filmCategory != film.filmCategory);
if (needsDivider)
{
dividerView.setVisibility(View.VISIBLE);
dividerView.setText(getFilmCategoryName(film.filmCategory));
}
else
dividerView.setVisibility(View.GONE);
A more fancy solution would be to use a library like StickyListHeaders. It doesn't work as your mobile site example, but it's probably even better (e.g. the headers don't scroll up out of the screen).

NullPointerException when acting on a View

For some reason I'm getting a nullPointerException when working with any new View I place in my XML. The view type (TextView, EditText, etc) doesn't matter. Any views I originally had work - it's isolated to any newly added views.
I've tried cleaning the project numerous times, deleted the entire XML file, restarted eclipse, then re-pasted the XML back into a new file, no luck. Appears to be similar as this question, but nothing has gotten this working.
I really don't want to have to re-create the entire project, but I'm not sure what else to do if recreating the XML and cleaning isn't enough.
Code is as follows:
XML
...
<TextView
android:id="#+id/dlg_add_proj_test_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Test Text" />
...
Activity
...
public void fireDlg() {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_add_proj);
dialog.setTitle("Add Project");
//Other previous views
TextView newTxtView = (TextView) findViewById(R.id.dlg_add_proj_test_text);
newTxtView.setText("New Text"); //Null Pointer Here
}
...
you should be using dialog.findViewById(R.id.dlg_add_proj_test_text) instead of just findViewById. The findViewById uses the activity's method while dialog.findViewById uses the method in the dialog.

Categories