change one cell in gridView - java

How can I add a int value to just one cell in a gridView? for example, using only the first cells and assigning them a value . Is there a way to do that?
This is my grid in the xml:
<GridView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:numColumns="3" >
</GridView>
this is my java:
int first = 5;
int second = 6
GridView grid = (GridView) findViewById(R.id.grid);
//I'm missing here a line something like: grid.setContentOfCell (1, first); grid.setContentOfCell (2, second)
I want to assign the int A to the first cell in the grid and B to the second.

In GridView getView Method Check the position is grater than 4 and also put other Cell value according to postion value that you want.
public View getView(int position, View convertView, ViewGroup arg2) {
if(position>3)
{
productPriceTextView.setText("1-4")
}else if(position==a)
Display your value
}else if(position==b)
Display your value
}
Thanks

Related

Dropdown size adjust according to displayed results

I use autoComplete in my project.
Here is the usage of autoComplete:
In Code:
val arrayAdapter = context?.let {
ArrayAdapter<String>(
it, // Context
android.R.layout.simple_list_item_1,
autocompleteOptions // Array
)
}
autoCompleteTextView.setAdapter(arrayAdapter)
// Set an item click listener for auto complete text view
autoCompleteTextView.onItemClickListener =
AdapterView.OnItemClickListener { parent, _, position, _ ->
//some logic
}
In XML:
<AutoCompleteTextView
android:id="#+id/actvSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true""
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:completionThreshold="0"
android:dropDownHeight="200dp"
android:layout_gravity="center_vertical" />
My question is there any way to adjust the size of the dropdown list according to the current options displayed in the dropdown?
You would need to set it manually in code, based on the number of entries in your arrayAdapter:
autoCompleteTextView.setDropDownHeight(arrayAdapter.getCount() * rowHeight)
where you either calculate each row height, or just have it pre-set as a static height

List view item background color

Hello how can i set the item liner Layout background color from this array that it have just 3 color , position one color one , position two color two , position three color three and then position one color one and so on..
the position of the list view
int[] androidColors = context.getResources().getIntArray(R.array.randomColor);
viewHolder.linearLayout.setBackgroundColor(androidColors[position]);
You can try to create your own custom adapter and implement getView function like this :
public View getView (int position, View convertView, ViewGroup parent){
if( convertView == null ){
//We must create a View:
convertView = inflater.inflate(R.layout.my_list_item, parent, false);
}
//Here we can do changes to the convertView, such as set a text on a TextView or set the color of every single item of your view.
//or an image on an ImageView.
return convertView;
}
try to have a look to this post:
Change background colour of current listview item in adapter getView method

Force RecyclerView to bottom of page in layout

I have a page that consists of an ImageView, and a RecyclerView. The RecyclerView contains a small number of items (currently three) and only takes up around a quarter of the screen on my test device. However, despite trying numerous layout options, I cannot get the RecyclerView to effectively wrap its content and take up just enough space required to contain these three rows, and leave the rest of the space for my ImageView.
To help illustrate what I mean, I have drawn two diagrams. The first shows what I would like to happen, and the second what is happening:
So far, I have tried several different combinations of RelativeLayout - for instance, I will set RecyclerView to layout:align_ParentBottom and the second RelativeLayout that contains the ImageView to layout:alignComponent so that its bottom matches the RecyclerView top (this would normally drag the ImageView layout so that it fills any reminaing space, which is what I would like to happen.)
The RecyclerView though just keeps occupying all the space it can, even though it only contains a few rows. The current "solution" I have is to place everything inside a LinearLayout and set less gravity to the RecyclerView, but it isn't ideal, because on different emulators it wont line up completely with the bottom of the screen, and in others there isn't enough room and the RecyclerView becomes scrollable.
Thanks in advance for any help and suggestions anyone can offer.
Many thanks to everyone who contributed, but I have found a programmatic solution outside of the layout files. In case anyone else is looking for a solution to this problem, I found one here.
It appears as if there is an issue with RecyclerView currently where it doesn't wrap content. The answer is to construct a custom class that extends LinearLayoutManager. I have posted the solution that worked for me below - most of it is copy and pasted from the answer given in the link I quoted. The only small issue is that it doesn't account for the extra space added by decorations, which is why I had to make a small tweak to the following line near the end of the code:
//I added the =2 at the end.
measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin + 2;
Here is the code in its entirety:
public class HomeLinearLayoutManager extends LinearLayoutManager{
HomeLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
private int[] mMeasuredDimension = new int[2];
#Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
int widthSpec, int heightSpec) {
final int widthMode = View.MeasureSpec.getMode(widthSpec);
final int heightMode = View.MeasureSpec.getMode(heightSpec);
final int widthSize = View.MeasureSpec.getSize(widthSpec);
final int heightSize = View.MeasureSpec.getSize(heightSpec);
int width = 0;
int height = 0;
for (int i = 0; i < getItemCount(); i++) {
measureScrapChild(recycler, i,
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
mMeasuredDimension);
if (getOrientation() == HORIZONTAL) {
width = width + mMeasuredDimension[0];
if (i == 0) {
height = mMeasuredDimension[1];
}
} else {
height = height + mMeasuredDimension[1];
if (i == 0) {
width = mMeasuredDimension[0];
}
}
}
switch (widthMode) {
case View.MeasureSpec.EXACTLY:
width = widthSize;
case View.MeasureSpec.AT_MOST:
case View.MeasureSpec.UNSPECIFIED:
}
switch (heightMode) {
case View.MeasureSpec.EXACTLY:
height = heightSize;
case View.MeasureSpec.AT_MOST:
case View.MeasureSpec.UNSPECIFIED:
}
setMeasuredDimension(width, height);
}
private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
int heightSpec, int[] measuredDimension) {
View view = recycler.getViewForPosition(position);
if (view != null) {
RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
getPaddingLeft() + getPaddingRight(), p.width);
int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
getPaddingTop() + getPaddingBottom(), p.height);
view.measure(childWidthSpec, childHeightSpec);
measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;
measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin + 2;
recycler.recycleView(view);
}
}
}
Thanks again.
Put the two in a RelativeLayout and make ImageView fill parent:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<RecyclerView
android:id="#+id/recyclerView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/recyclerView"
/>
</RelativeLayout>
Edit: Wrote TextView by accident. Fixed.
The only solution I can think of is using layout weight. Specify 70% of the screen for the image and 30% for the Recyclerview as you said you have just 3 rows. Use adjustViewByBounds to ensure the images aspect ratio is maintained.
My code below:
<?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">
<ImageView
android:src="#drawable/ic_round_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_weight=".9"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=".1"/>
</LinearLayout>

Automatic spacing in gridview columns?

I have a simple GridView. The following is the XML
<LinearLayout
android:id="#+id/calendar_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:orientation="vertical" >
<GridView
android:id="#+id/grid_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="7"
android:verticalSpacing="2dp"
android:rotationY="180" >
</GridView>
</LinearLayout>
I create a TextView and insert it as an item in the gridview. Basically the idea is to create a customized calendar.
In the adaptor I have
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = new TextView(mContext);
textView.setText(days.get(position).toString());
textView.setRotationY(180);
textView.setGravity(Gravity.CENTER);
textView.setBackground(mContext.getResources().getDrawable(R.drawable.grey_box));
int x = mContext.getResources().getDimensionPixelSize(R.dimen.calendar_slot);
textView.setLayoutParams(new GridView.LayoutParams(x, x));
return textView;
}
R.dimen.calendar_slot equals to 30dp.
What I fail to understand is , given the above why does my gridview appear like below ? . I need the columns to be merged together. But they have spaces between them.
Can anyone aid ?
The reason why there is a lot of space between the columns of gridview is that the textview in your layout is not occupying the complete column space provided by the grid view
To avoid this problem , you have to calculate the device's screen width and height and divide it by number of columns and rows respectively. This will give you the exact width and height you needed for your single textview.Set this as the dimensions of your text view. You will get equal space between your rows and columns
The code will be as follows
DisplayMetrics displayMetrics=getResources().getDisplayMetrics();
screen_width=displayMetrics.widthPixels; //width of the device screen
screen_height=displayMetrics.heightPixels; //height of device screen
int view_width=screen_width/columns; //width for text view
int view_height=screen_height/rows; //height for text view
textview.getgetLayoutParams().width=view_width;
textview.getgetLayoutParams().height=view_height;

How to multiply column by the number in ListView, Android

I select data from a database.
Data in one column, you must convert to kilograms, I need to multiply by a factor of one column. The data in column sets_weight in the pound, and it is necessary to display in kilograms, with without modifying to the database
How to do it correctly. Any ideas! Thanks!
onSets = db.getSets(exesIdsColExes, toprog_dif);
listSets.setAdapter(new SimpleCursorAdapter(this,
R.layout.itemsets, onSets,
new String[] {"sets_ids", "sets_weight", "sets_ones"},
new int[] {R.id.itemsets_ids, R.id.itemsets_weight, R.id.itemsets_ones}) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);
// Here we get the textview and set the color
Typeface fontTitleProg = Typeface.createFromAsset(getAssets(), "AGHELVETICA.TTF");
TextView itemsets_ids = (TextView) row.findViewById(R.id.itemsets_ids);
itemsets_ids.setTypeface(fontTitleProg, 1);
itemsets_ids.setGravity(0x05);
TextView itemsets_weight = (TextView) row.findViewById(R.id.itemsets_weight);
itemsets_weight.setTypeface(fontTitleProg, 1);
itemsets_weight.setGravity(0x01);
TextView itemsets_ones = (TextView) row.findViewById(R.id.itemsets_ones);
itemsets_ones.setTypeface(fontTitleProg, 1);
itemsets_ones.setGravity(0x01);
return row;
}
});
I've never done any android development, but the documentation of SimpleCursorAdapter says:
First, if a SimpleCursorAdapter.ViewBinder is available, setViewValue(android.view.View, android.database.Cursor, int) is invoked. If the returned value is true, binding has occured. If the returned value is false and the view to bind is a TextView, setViewText(TextView, String) is invoked. If the returned value is false and the view to bind is an ImageView, setViewImage(ImageView, String) is invoked.
So you just need to pass a custom instance of SimpleCursorAdapter which will, if the column index is 1 (second column), get the value in pounds from the cursor, multiply it to transform pounds into kg, set the result as the text of the view, and return true. For the other columns, return false.

Categories

Resources