I have create an android app with Kotlin.This app contains an interface, which contains a recyclerview to get all the list of product.
To fill this list of product, i create an adapter, this adapter is a cardview : Imageview and two textview.
In this interface, i added two buttons, one when i click on all the product display with the list and the other one display with grid disposition.The default value for grid disposition.
I wsant to change the width and the height of a cardview, to display all the product in the List.
The following code is anadapter item:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardProductItem"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:visibility="visible"
android:layout_marginTop="#dimen/spacing_medium"
android:layout_marginBottom="#dimen/spacing_medium"
android:layout_marginRight="#dimen/spacing_middle"
android:layout_marginLeft="#dimen/spacing_middle"
android:background="#color/grey">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/productImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:layout_centerInParent="true"
android:layout_gravity="center"/>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="#dimen/spacing_middle">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/productName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="#dimen/spacing_medium"
android:fontFamily="#font/lato_regular"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#color/black"/>
<TextView
android:id="#+id/productPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="#dimen/spacing_medium"
android:textStyle="bold"
android:fontFamily="#font/lato_regular"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#color/black"/>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageView
android:id="#+id/arrowDetailProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:layout_margin="8dp"
android:visibility="gone"
android:layout_gravity="center|center_horizontal"
android:src="#drawable/ic_right_arrow"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
the following code is an extract from an xml file which cotains a recyclerview :
<ImageView
android:id="#+id/gridProducts"
android:layout_width="#dimen/spacing_mlarge"
android:layout_height="#dimen/spacing_mlarge"
android:scaleType="fitXY"
android:layout_toLeftOf="#+id/productNumber"
android:layout_margin="8dp"
android:src="#drawable/ic_divided_squares"/>
<ImageView
android:id="#+id/listProducts"
android:layout_width="#dimen/spacing_mlarge"
android:layout_height="#dimen/spacing_mlarge"
android:scaleType="fitXY"
android:layout_margin="8dp"
android:src="#drawable/ic_rounded_black_square_shape"/>
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerViewProductByCategory"
android:layout_below="#+id/recyclerViewCategories"
android:layout_marginTop="66dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/spacing_middle"
android:layout_marginEnd="#dimen/spacing_middle"
android:orientation="horizontal"/>
the following code contains the onclick acion into the two buttom(Imageview) :
gridProducts.setOnClickListener {
recyclerViewProductByCategory.layoutManager =
StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
recyclerViewProductByCategory.adapter = products?.let { it -> ProductAdapter(it) }
gridProducts.visibility = View.INVISIBLE
listProducts.visibility = View.VISIBLE
}
listProducts.setOnClickListener {
recyclerViewProductByCategory.layoutManager =
LinearLayoutManager(this#CategoryByProduct, RecyclerView.VERTICAL, false)
recyclerViewProductByCategory.adapter = products?.let { it -> ProductAdapter(it) }
val layoutParams = LayoutParams(
LayoutParams.MATCH_PARENT, // CardView width
LayoutParams.WRAP_CONTENT // CardView height
)
cardProductItem.layoutParams = layoutParams
listProducts.visibility = View.INVISIBLE
gridProducts.visibility = View.VISIBLE
}
after running my app an exception appear as the following :
java.lang.IllegalStateException: cardProductItem must not be null
could you please tell me where's the error and how can i correct it
Based on the code available, you are setting the value of cardProductItem outside of setOnClickListener. The error is stating that cardProductItem can not be set when a listProduct is clicked. Make sure that you are setting cardProductItem to a value before the UI is presented to the user.
For a more in-depth answer, I would need to look at the other places where cardProductItem is used.
Related
I have layouts which will be repetitive added in main layout:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/txt_temp_feels_like"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="sans-serif-medium"
android:text="-"
android:textColor="#color/gray"
android:textSize="18sp" />
I want it to be added to this container:
<LinearLayout
android:id="#+id/temp_feels_like_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:orientation="horizontal">
</LinearLayout>
And I want to do it in code. I'm using this method:
private fun setData(
layout: List<Int>,
container: List<ViewGroup>,
) {
for (i in layout.indices) {
container[i].addView(layoutInflater.inflate(layout[i], null))
}
}
where layout is link to resource of layout.
And it's fill it but i have some problems:
I can't set text to this added view
I can't set weight to this view
Maybe my algorithm is wrong. Can you help?
It depends on when should you set the text and gravity. If you can set it before adding to layout - you can do something like that:
val view = layoutInflater.inflate(layout[i], null) as TextView
view.apply {
text = "abc"
params = (params as LinearLayout.LayoutParams).apply {
weight = *value you need*
}
}
container[i].addView(view)
Or if you need to set text to view after views are added to layout, you can reference to view via container.children/container.getChildAt(i)
UPD
you can get the child in the following way:
val count = layout.getChildCount()
var v: TextView? = null
for (i in 0..count) {
val view = layout.getChildAt(i)
if (view is TextView) {
v = view
}
}
Or one more way - set tag to view while adding it to layout and then you can get it by tag in any time you need
Could something like this help?
<LinearLayout
android:id="#+id/temp_feels_like_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:orientation="horizontal"
>
<TextView
android:id="#+id/txt_temp_feels_like1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="sans-serif-medium"
android:visibility="gone"
android:text="-"
android:textColor="#color/gray"
android:textSize="18sp"
/>
<TextView
android:id="#+id/txt_temp_feels_like2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="sans-serif-medium"
android:visibility="gone"
android:text="-"
android:textColor="#color/gray"
android:textSize="18sp"
/>
<TextView
android:id="#+id/txt_temp_feels_like3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="sans-serif-medium"
android:visibility="gone"
android:text="-"
android:textColor="#color/gray"
android:textSize="18sp"
/>
<TextView
android:id="#+id/txt_temp_feels_like4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="sans-serif-medium"
android:visibility="gone"
android:text="-"
android:textColor="#color/gray"
android:textSize="18sp"
/>
</LinearLayout>
You can have the gravity set appropriately (and the text if it's not dynamic),
and then set the visibility in code rather than try to add the view.
findViewById<View>(R.id.txt_temp_feels_like1).visibility = View.VISIBLE
Suppose I have a cardView Layout which have a TextView and ImageView. I showed them in RecyclerView. If I click on the view the textColor and the drawable color will become red. and then if I click another view, that clicked view will be red and prevoius will convert to black. I did the same thing in bottomNavigationView using custom color.
Here is the layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/item_layout_card_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="10dp"
app:cardCornerRadius="4dp"
app:cardElevation="2dp"
app:cardMaxElevation="3dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/item_image_view"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:scaleType="centerCrop"
android:src="#mipmap/ic_launcher" />
<TextView
android:id="#+id/item_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/item_image_view"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:gravity="center"
android:text="Item"
android:textColor="#000000"
android:textSize="12sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
You want to change the color of the recycler view row
In your adapter define variable like
var coloredIndex = -1
In your onBindViewHolder() function
h.var.setOnClickListener {
coloredIndex = curPos
}
changeSelectedItemColor(h, curPos)
Define this below function to change the color.
private fun changeSelectedItemColor(h: YourViewHolder, curPos: Int) {
if (coloredIndex == curPos) {
h.var.setBackgroundColor(ContextCompat.getColor
(context, R.color.red))
} else {
h.var.setBackgroundColor(ContextCompat.getColor
(context, R.color.black))
}
notifyDataSetChanged()
}
This is a example. You didn't add your recycler view adapter code in your question. Please add it for further clarification.
I have a ListView with a TextView (which has an integer as string) and a Seekbar.
I need to extract the integer value and set it to the Seekbar.
Unfortunately, when I do this, I get a NullPointer exception because the content that populates the ListView item is not recognized by the onCreateView of the PlaceholderFragment.
String progressValue = percentileTextView.getText().toString();
bar.setProgress(Integer.parseInt(progressValue));
I have an activity layout for this activity, then a fragment layout which contains the listview and then I have a content.xml where the actual TextView is present. My question is how to reference this TextView? Am I doing this right, i.e. Activity layout contains a ConstraintLayout with Toolbars etc, then the Fragment Layout contains the ListView and I am using a separate content.xml and applying that to the ListView via a SimpleAdapter.
newAdapter = new SimpleAdapter(getContext(), newList,
R.layout.content, new String[] {
"name",
"percent"
},
new int[] {
R.id.name1,
R.id.percentile1
});
I hope you understand my problem.
When I do the following, it works (i.e. Android Studio understand that is the TextView) - but throws a null pointer exception upon build.
TextView percentileTextView = (TextView) rootView.findViewById(R.id.percentile1);
UPDATE 1 for pskink:
Newlist contents:
[{TA=Item1, IA=46}, {TA=Item2, IA=98}, {TA=Item3, IA=70}, {TA=Item4, IA=54}, {TA=Item5, IA=99}, {TA=Item6, IA=98}]
Row item layout (content.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="#dimen/activity_horizontal_margin">
<TextView
android:id="#+id/name1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:textStyle="bold"
android:textColor="#color/colorAccent" />
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:padding="5dp">
<TextView
android:id="#+id/greater_than"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="#string/greater_than" />
<TextView
android:id="#+id/percentile1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:layout_toRightOf="#id/greater_than"
android:layout_toEndOf="#id/greater_than"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:layout_toRightOf="#id/percentile1"
android:layout_toEndOf="#id/percentile1"
android:text="#string/percentile_text"/>
</RelativeLayout>
<SeekBar
android:id="#+id/seekBar2"
android:paddingTop="12dip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end|bottom"
android:paddingTop="12dip"
android:orientation="horizontal">
<ImageView
android:contentDescription="info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/someimg" />
</LinearLayout>
</LinearLayout>
On my activity with fragments, I've some pages that have an image and some that don't. The layout is like this: Textview-Imageview-Textview, so when I there isn't an image I want the Textviews to be below each other. This is my 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"
android:background="#006400">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/handling"
android:textColor="#ffffff"
android:textSize="20dp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/stilling"
android:layout_below="#id/handling"
android:layout_gravity="center_vertical" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAlignment="center"
android:text="New Text"
android:id="#+id/arabisk"
android:layout_below="#id/stilling"
android:layout_gravity="center"
android:textSize="18dp"
android:textColor="#ff0000" />
</RelativeLayout>
</ScrollView>
</LinearLayout>
This is what I get
EDIT:
My array of images
int[] billede = new int[18];
billede[1] = R.mipmap.sojle1;
billede[2] = R.mipmap.sojle4;
billede[3] = R.mipmap.sojle2;
billede[4] = R.mipmap.sojle3;
billede[6] = R.mipmap.sojle4;
billede[8] = R.mipmap.sojle6;
billede[10] = R.mipmap.sojle7;
billede[12] = R.mipmap.sojle8;
billede[15] = R.mipmap.sojle9;
stilling.setImageResource(billede[mPage]);
return view;
Set the visiblity to GONE
in your xml
android:visibility="GONE"
or via code
imageView.setVisibility(View.GONE);
Set ImageView visibility to Gone and your text view must be one below other like you want. Like this
stilling.setVisibility(View.GONE);
Use LinearLayout instead of RelativeLayout. And set visible View.GONE to hide ImageView.
I would like to set text for the spinners in the layout reused. However, only the first spinner is set. How to set text for all spinners with same id?
Also, I would like to ask how add another skillfield.xml to fragment_skill.xml when clicking the imageview?
Thank you.
fragment_skill.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/skillInfo" >
<RelativeLayout
android:id="#+id/OCC"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/occ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="#string/occSkill"
android:textSize="20sp" />
<TextView
android:id="#+id/occPt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_toEndOf="#id/occ"
android:text=""
android:textSize="20sp" />
<include
android:id="#+id/oocSkill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/occ"
layout="#layout/skillfield" />
<ImageView
android:id="#+id/addOccSkill"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_below="#id/oocSkill"
android:contentDescription="#string/delSkill"
android:src="#android:drawable/ic_input_add" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/ORI"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/OCC" >
<TextView
android:id="#+id/ori"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="#string/oriSkill"
android:textSize="20sp" />
<TextView
android:id="#+id/oriPt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_toEndOf="#id/ori"
android:text=""
android:textSize="20sp" />
<include
android:id="#+id/oriSkill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/ori"
layout="#layout/skillfield" />
<ImageView
android:id="#+id/addOriSkill"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_below="#id/oriSkill"
android:contentDescription="#string/delSkill"
android:src="#android:drawable/ic_input_add" />
</RelativeLayout>
</RelativeLayout>
skillfield.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/addSkillField" >
<Spinner
android:id="#+id/selectSkill"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp" />
<TextView
android:id="#+id/skillPt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:text=""
android:textSize="20sp" />
<ImageView
android:id="#+id/addSkill"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="bottom"
android:contentDescription="#string/delSkill"
android:src="#android:drawable/ic_delete" />
</LinearLayout>
In MainActivity.class
skill = (Spinner) findViewById(R.id.selectSkill);
ArrayAdapter<CharSequence> skillAdapter = ArrayAdapter.createFromResource(context, R.array.skills, R.layout.spinner);
skillAdapter.setDropDownViewResource(R.layout.spinner_down);
skill.setAdapter(skillAdapter);
how to setup Spinner with the same ID
It is true that if you do this:
skill = (Spinner) findViewById(R.id.selectSkill);
You will be only able to get the first Spinner. I don't think you can add new Spinner dynamically by using <include>.
Here's the main idea of how to achieve what you need:
In fragment_skill.xml, you need to add an empty container view. This container should be inserted in the place you want to add your Spinner every time the button is clicked. This container will hold your Spinners that are added.
Let's say you want to insert here:
<RelativeLayout...>
<TextView.../>
<TextView.../>
<!-- here is where you want to insert your Spinner -->
<ImageView.../>
</RelativeLayout...>
So you add a LinearLayout at that spot, like this:
<RelativeLayout...>
<TextView.../>
<TextView.../>
<LinearLayout
android:id="#+id/container"
android:orientation="vertical
... />
<ImageView.../>
</RelativeLayout...>
You can add any other type of layout, depending what you need, it doesn't have to be LinearLayout. After that, you grab that LinearLayout from your activity/fragment like this:
LinearLayout mContainer; // make this instance variable
mContainer = (LinearLayout)findViewById(R.id.container);
Also grab the button so that you can set up a listener, let's assume it's called Button mButton:
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addNewSpinner();
});
So when the button is clicked, you will call addNewSpinner(). Now, make a method called addNewSpinner(). Inside, the method, we will be inflating skillfield.xml. The method should look something like this:
public void addNewSpinner(){
View view = getLayoutInflater().inflate(R.layout.skillfield, null);
Spinner skill = (Spinner) view.findViewById(R.id.selectSkill);
ArrayAdapter<CharSequence> skillAdapter = ArrayAdapter.createFromResource(context, R.array.skills, R.layout.spinner);
skillAdapter.setDropDownViewResource(R.layout.spinner_down);
skill.setAdapter(skillAdapter);
mContainer.addView(skill);
}
If you are not sure what inflate means, here is the explanation:
Instantiates a layout XML file into its corresponding View objects.
Hope it helps!