I have founded many solutions for this problem but none of them have worked for me. I want to show a PopupWindow inside a Fragment. This is my code
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View popupView = layoutInflater.inflate(R.layout.pop_up_cargando, null,false);
this.popupWindow = new PopupWindow(popupView, popupView.getWidth(),popupView.getHeight());
this.popupWindow.setFocusable(true);
int location[] = new int[2];
this.btnInventario.getLocationOnScreen(location);
this.popupWindow.showAtLocation(this.btnInventario, Gravity.NO_GRAVITY, location[0], location[1] + btnInventario.getHeight()); // this.btnInventario is the button that calls this code
this.popupWindow.update(0, 0, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
But the PopupWindow never appears.
Edit: This is the content of pop_up_cargando
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_recomendar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical" >
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:id="#+id/cargando"/>
<TextView
android:id="#+id/txtTexto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_gravity="center"
android:text="#string/vacio"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
Any suggestions? What I'm doing wrong? Your help will be very appreciated.
Your popupView.getWidth() and popupView.getHeight() values are equals to 0 because the view has not been drawn yet.
Before asking for the width and the height of your view, you have to ensure that it has been drawn. For that you can call the following method after it has been inflated:
popupView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
After that, the size of your view is available with the methods getMeasuredWidth() and getMeasuredHeight().
Related
I have been trying to do such a pop up menu. However, I am not able to do it since I can not put pictures and give design in pop-up menu. Does any one know to do such a pop menu?
Go to this link. I hope this will help you achieving what you want.
An example for pop up menu is here.
EDIT
Create an PopUpWindow layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llSortChangePopup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/sort_popup_background"
android:orientation="vertical" >
<TextView
android:id="#+id/tvDistance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/distance"
android:layout_weight="1.0"
android:layout_marginLeft="20dp"
android:paddingTop="5dp"
android:gravity="center_vertical"
android:textColor="#color/my_darker_gray" />
<ImageView
android:layout_marginLeft="11dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sort_popup_devider"
android:contentDescription="#drawable/sort_popup_devider"/>
<TextView
android:id="#+id/tvPriority"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/priority"
android:layout_weight="1.0"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:clickable="true"
android:onClick="popupSortOnClick"
android:textColor="#color/my_black" />
<ImageView
android:layout_marginLeft="11dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sort_popup_devider"
android:contentDescription="#drawable/sort_popup_devider"/>
<TextView
android:id="#+id/tvTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/time"
android:layout_weight="1.0"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:clickable="true"
android:onClick="popupSortOnClick"
android:textColor="#color/my_black" />
<ImageView
android:layout_marginLeft="11dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sort_popup_devider"
android:contentDescription="#drawable/sort_popup_devider"/>
<TextView
android:id="#+id/tvStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/status"
android:layout_weight="1.0"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:textColor="#color/my_black"
android:clickable="true"
android:onClick="popupSortOnClick"
android:paddingBottom="10dp"/>
</LinearLayout>
and then create the PopUpWindow in your Activity:
// The method that displays the popup.
private void showStatusPopup(final Activity context, Point p) {
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.llStatusChangePopup);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.status_popup_layout, null);
// Creating the PopupWindow
changeStatusPopUp = new PopupWindow(context);
changeStatusPopUp.setContentView(layout);
changeStatusPopUp.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
changeStatusPopUp.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
changeStatusPopUp.setFocusable(true);
// Some offset to align the popup a bit to the left, and a bit down, relative to button's position.
int OFFSET_X = -20;
int OFFSET_Y = 50;
//Clear the default translucent background
changeStatusPopUp.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
changeStatusPopUp.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
}
finally pop it it up onClick of a button or anything else:
imTaskStatusButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
int[] location = new int[2];
currentRowId = position;
currentRow = v;
// Get the x, y location and store it in the location[] array
// location[0] = x, location[1] = y.
v.getLocationOnScreen(location);
//Initialize the Point with x, and y positions
point = new Point();
point.x = location[0];
point.y = location[1];
showStatusPopup(TasksListActivity.this, point);
}
});
I am trying to align a linear layout inside another linear layout programmatically, but setGravity(Gravity.RIGHT) doesn`t work.
I know the differences between gravity and layout_gravity.
Here's the 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="fill_parent">
<LinearLayout
android:id="#+id/initial_wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:id="#+id/conversationwrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/bubble_yellow"
>
<TextView
android:id="#+id/messagecontent_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:paddingLeft="10dip"
android:text="Message_content"
android:textSize="15dp" />
<TextView
android:id="#+id/timestamp_tv"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="HH:MM"
android:ems="2"
android:textSize="12dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
And the code of getView() in my Adapter:
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.listitem_conversation, parent, false);
}
wrapper = (LinearLayout) row.findViewById(R.id.conversationwrapper);
initial_wrapper = (LinearLayout) row.findViewById(R.id.initial_wrapper);
time = (TextView) row.findViewById(R.id.timestamp_tv);
Message coment = getItem(position);
content = (TextView)row.findViewById(R.id.messagecontent_tv);
content.setText(coment.content);
time.setText(getCurrentTimeStamp());
wrapper.setBackgroundResource(!coment.mine ? R.drawable.bubble_yellow : R.drawable.bubble_green);
initial_wrapper.setGravity(!coment.mine ? Gravity.LEFT : Gravity.RIGHT);
return row;
}
But it always happen to be on the left, even when the image is green.
The strange thing is that if I type
android:gravity="right"
inside initial_wrapper, It works in the preview but not in the device.
I'm using a Moto G and a Nexus 5, but neither of them work
The problem can be here : android:layout_width="fill_parent", how you supose to set android:gravity="right" here, if your layout is filling whole parent? You should try to use
<LinearLayout
android:id="#+id/initial_wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
instead of :
<LinearLayout
android:id="#+id/initial_wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
do this
<LinearLayout
android:id="#+id/initial_wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity = "left"
>
in your getView() do this
LinearLayout.LayoutParams lp = initial_wrapper.getLayoutParams();//will need to casting
lp.gravity= Gravity.RIGHT; // this is the important part
initial_wrapper.setLayoutParams(lp);
hope it helps
So, my program works fine. I only have three warnings left (one for each popupwindow) and it annoys me alot, I've been serching around for a solution that would fit my needs but I can't seem to find one.
This is my code (the other two popupwindows are similar)
else if(id == R.id.action_resetstats){
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.resetpop, null);
final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button yesDismiss = (Button)popupView.findViewById(R.id.yesDismiss);
yesDismiss.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v){
SharedPreferences prefs = getSharedPreferences(savedData, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.clear();
editor.commit();
counterW = 0;
counterL = 0;
counterT = 0;
counterTot = 0;
timerTime = 3;
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(yeDismiss, 50, 50);
Button noDismiss = (Button)popupView.findViewById(R.id.noDismiss);
noDismiss.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v){
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(naDismiss, 50, 50);
xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:background="#android:color/black"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="230dp"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="2dp"
android:background="#android:color/darker_gray"
android:orientation="vertical" >
<Button
android:id="#+id/noDismiss"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="NO!" />
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Reset stats?"
android:textSize="22sp" />
</RelativeLayout>
<Button
android:id="#+id/yesDismiss"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Yes" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/relativeLayout1"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:text="Are you sure you want " />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:text="to reset your statistics?" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="39dp"
android:text="!CANNOT BE UNDONE!" />
</RelativeLayout>
</RelativeLayout>
The problem is here to clear things up:
View popupView = layoutInflater.inflate(R.layout.resetpop, null);
and the warning says:
Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout's root element)
Any ideas how to solve this the best way? Thanks in advance!
You could just use:
context = popupView.getContext();
inflater.inflate(R.layout.resetpop, new LinearLayout(context), false);
That should solve the warning.
When you write below code blog, you mean inflate this layout with parent, attach it to parent, but because of pass null, inflater wont be able to attach new parent
View popupView = layoutInflater.inflate(R.layout.resetpop, null);
LayoutInflater class -->
350
351 public View More ...inflate(int resource, ViewGroup root) {
352 return inflate(resource, root, root != null);
353 }
But this, it will inflate with given parent, but wont attach it to the new parent.
View popupView = layoutInflater.inflate(R.layout.resetpop, new LinearLayout(context), false);
PopupWindow Performance Issues and Unable Dismiss
In case someone stumbled upon here, because of some performance issues on the PopupWindow.
It is when your had the PopupWindow launched, but just freezes the entire screen and without able proceed unless restarting the app. Happens on older devices, mine happened to be Samsung GT-N7100 (API 19).
Well, I've tried with solving the warning when using the LayoutInflater. But, didn't solve the lagging issue.
Step 1 - Set View to Render Using Software Acceleration
According to this forum post, all I need is to set the view to use software acceleration for the view instead. Then, everything is just right as rain.
https://androidforums.com/threads/android-popupwindow-performance-issues.882794/
val popupView = inflater.inflate(R.layout.popup_tooltip, null)
popupView.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
Step 2 - Set PopupWindow Focusable Attributes to Dismiss
Immediately after that, I still have issues dismissing the PopupWindow. If only the focusable is set to true, the performance issue will still persist.
I have to manually set the attributes to allow it to be dismissed when a click is detected.
https://stackoverflow.com/a/45408724/2867351
val popupWindow = PopupWindow(popupView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
popupWindow.isFocusable = true
popupWindow.isOutsideTouchable = true
popupWindow.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
That's about it. Cheers mate 🎉
How Do i make this view. I checked Spinner and PopUp window but i failed to do... Please suggest me any examples...
you should go for Android QuickAction widget.
This is an open source project of GithUb.
https://github.com/lorensiuswlt/NewQuickAction3D
https://github.com/alhneiti/Android-QuickAction
Hope this will help you.
I have achived this kind of behavior using a PopUpWindow, here is my code:
// The method that displays the popup.
private void showStatusPopup(final Activity context, Point p) {
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.llStatusChangePopup);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.status_popup_layout, null);
// Creating the PopupWindow
changeStatusPopUp = new PopupWindow(context);
changeStatusPopUp.setContentView(layout);
changeStatusPopUp.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
changeStatusPopUp.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
changeStatusPopUp.setFocusable(true);
// Some offset to align the popup a bit to the left, and a bit down, relative to button's position.
int OFFSET_X = -20;
int OFFSET_Y = 50;
//Clear the default translucent background
changeStatusPopUp.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
changeStatusPopUp.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
}
and this is the PopUp layout:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:layout_marginLeft="11dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/devider_popup_task_status_change"
android:contentDescription="#drawable/devider_popup_task_status_change"/>
<TextView
android:id="#+id/tvInProgress"
android:paddingLeft="10dp"
android:layout_weight="1.0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/my_black"
android:gravity="center_vertical"
android:clickable="true"
android:onClick="popupStatusChangeOnClick"
android:layout_marginLeft="11dp"
android:layout_marginRight="4dp"
android:text="#string/inprogress"/>
<ImageView
android:layout_marginLeft="11dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/devider_popup_task_status_change"
android:contentDescription="#drawable/devider_popup_task_status_change"/>
<TextView
android:id="#+id/tvOnTheWay"
android:paddingLeft="10dp"
android:layout_weight="1.0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/my_black"
android:gravity="center_vertical"
android:clickable="true"
android:onClick="popupStatusChangeOnClick"
android:layout_marginLeft="11dp"
android:layout_marginRight="4dp"
android:text="#string/ontheway"/>
<ImageView
android:layout_marginLeft="11dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/devider_popup_task_status_change"
android:contentDescription="#drawable/devider_popup_task_status_change"/>
<TextView
android:id="#+id/tvComplete"
android:paddingLeft="10dp"
android:layout_weight="1.0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/my_black"
android:gravity="center_vertical"
android:clickable="true"
android:onClick="popupStatusChangeOnClick"
android:layout_marginLeft="11dp"
android:layout_marginRight="4dp"
android:text="#string/complete"/>
<ImageView
android:layout_marginLeft="11dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/devider_popup_task_status_change"
android:contentDescription="#drawable/devider_popup_task_status_change"/>
<TextView
android:id="#+id/tvFailed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:layout_weight="1.0"
android:gravity="center_vertical"
android:clickable="true"
android:onClick="popupStatusChangeOnClick"
android:layout_marginLeft="11dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="11dp"
android:text="#string/failed"
android:textColor="#color/my_black" />
When the ImageView's are only a 1 pixel deviders.
What you ask about is a Spinner, but you can also make your own popup list, using "ListPopupWindow"
you can use spinner also-
String[] branch={"NEW","prev",...};
ArrayAdapter<String> list;
spinner=(Spinner) findViewById(R.id.spinner);
list=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,branch);
spinner.setAdapter(list);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
}
The grid view of my application has 3 rows and 3 columns. I want this to fill the screen ,irrespective of the screen size of the device.
I tried getting window size and setting the layoutparams accordingly. But this is not giving a perfect alignment as it works with weightsum in linearlayout.
So can we use weightsum for gridview or is there any other android property that can be used.
Thanks a lot for time and response.
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/text1view"
android:background="#drawable/home_bg"
android:gravity="center"
android:horizontalSpacing="10dip"
android:numColumns="3"
android:stretchMode="columnWidth" />
each grid item is
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="75dip"
android:layout_margin="0dip"
android:padding="0dip" >
</ImageView>
<TextView
android:id="#+id/grid_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/grid_item_image"
android:gravity="center_horizontal"
android:padding="0dip"
android:textColor="#000000"
android:textSize="10sp" >
</TextView>
code of the adapter :
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.griditems, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView
.findViewById(R.id.grid_item_text);
holder.image = (ImageView) convertView
.findViewById(R.id.grid_item_image);
// if it's not recycled, initialize some attributes
holder.image.setImageResource(gridItemIds[position]);
holder.text1.setText(gridTitles[position]);
int h = mContext.getResources().getDisplayMetrics().densityDpi;
// holder.image.setLayoutParams(new LayoutParams(h-50,h-50));
convertView.setLayoutParams(new GridView.LayoutParams(h - 45,
h - 39));
// holder.image.setScaleType(ImageView.ScaleType.CENTER_CROP);
// holder.image.setScaleType(ImageView.ScaleType.FIT_XY);
// holder.image.setPadding(20, 20, 20, 20);
// convertView.setLayoutParams(new GridView.LayoutParams(Utils
// .getLayoutParameter(), Utils.getLayoutParameter()+50));
holder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new ModuleManager().startModuleACtivity(position, mContext);
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
static class ViewHolder {
TextView text1;
ImageView image;
}
pls see the line
convertView.setLayoutParams(new GridView.LayoutParams(h-45, h-39));
i arrived at this value with some trials on different size emulator.
But i dont think its a right way to define height and width.
As far as i understood your question, as you have not posted any code, what you are trying to do is, you want gridview to appear on full screen.
Firstly you must be using some layout to put your gridview in. Do the following:
Make height and width of the layout to fill parent:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
then, set height and width of your gridview to fill_parent as well.
Well, this is just a supposed solution that you might have been working this way. putting some code would be much better. Hope this works for you.
Update:
Well the problem may lie here:
convertView.setLayoutParams(new GridView.LayoutParams(h-45, h-39));
change to:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
convertView.setLayoutParams(new GridView.LayoutParams(params));
Hope this works for you.
#preetha: first of all look at this example i used this and my layout did not affect irrespective of devise height and width....
http://developer.android.com/resources/tutorials/views/hello-gridview.html
set the following in xml
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
hope this helps...
Here I only changed width and height as match parent and wrap content.
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:id="#+id/textView5"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="5dp"
android:text="Category"
android:textSize="20dp" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="170dp"
android:layout_height="170dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:src="#drawable/iconclothcolor" />
<TextView
android:layout_width="wrap_content"
android:id="#+id/textView3"
android:layout_marginTop="2dp"
android:textSize="16dp"
android:textColor="#F37E98"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Shirt" />
<TextView
android:layout_width="wrap_content"
android:id="#+id/textView4"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Gender - Male" />
<TextView
android:layout_width="wrap_content"
android:id="#+id/textView8"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Quantity - 10" />
<TextView
android:layout_width="wrap_content"
android:id="#+id/textView6"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Size - S" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/textView10"
android:textSize="20dp"
android:text="Offer" />
<TextView
android:layout_width="wrap_content"
android:id="#+id/textView7"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20dp"
android:textColor="#F37E98"
android:text="Price - 1500 Rs" />
</LinearLayout>
</androidx.cardview.widget.CardView>
And in the Mainclass add this -->
GridLayoutManager gridLayoutManager=new GridLayoutManager(this,2);
recyclerView.setLayoutManager(gridLayoutManager);
To adjusting Gridview to screen size
In onCreate take rowHeight :
root = binding.root
view.onGlobalLayout {
rowHeight =
root.measuredHeight / currentPageNumberOfRow
fillView(appsList)
}
In adapter:
override fun getView(position: Int, ConvertView: View?, parent: ViewGroup?): View? {
var mConvertView = ConvertView
if (layoutInflater == null) {
layoutInflater =
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as
layoutInflater!!.inflate(R.layout.item_app_shortcut, null)
}
if (mConvertView == null) {
mConvertView =
layoutInflater!!.inflate(R.layout.item_child_launcher_app, null)
//Add this:
mConvertView.layoutParams = AbsListView.LayoutParams(GridView.AUTO_FIT, rowHeight)
}
Dont forget set row image:
android:layout_width="0dp"
android:layout_height="0dp"
To fit on screen