Does anyone know how to wrap a GridView?
Despite setting layout_width to wrap_content both on the GridView and parent LinearLayout the GridView is still bigger than its 3 column content (approx. double the content width). Tried also to apply fixed layout_width = ... dp. That did not work either.
Any ideas how to wrap the GridView? See below.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp" >
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnWidth="90dp"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="none"
android:gravity="center" />
</LinearLayout>
Setting the LinearLayout and the GridView to "wrap_contents" won't work, as the GridView pretty much defaults to "fill_parent" if you don't give it a specific size.
I did some testing, and you have several solutions depending on how you create your view :
Setting the LinearLayout to android:layout_width="100dip"
View view = getLayoutInflater().inflate(R.layout.grid_dialog, null);
dialog.setContentView(view);
=> doesn't work as expected : the inflater doesn't have a parent view when inflating, so it just doesn't take all the android:layout_XXX attributes into account.
dialog.setContentView(R.layout.grid_dialog);
=> works as expected, because this time the dialog will do the inflate and know the parent view. You can use dialog.findViewById to retrieve the gridview if needed....
Setting the GridView to android:layout_width="100dip"
Apparently works all the time, as it has a parent when it's inflated, so the layout_XXX attributes actually work.
All this isn't perfect as it doesn't do a real wrap_content on the GridView and you have to manually set the layout_width, but it should work.
Thanks Gregory. I'm not sure how to do the dialog.setContentView(R.layout.grid_dialog) in an AlertDialog builder. The AlertView dialog has only SetView, not setContentView available. Any hints on how to inflate the AlertDialog so it knows its parent view? See the AlertDialog code I'm using below, that does not wrap the GridView:
protected Dialog onCreateDialog(int id) {
switch(id) {
case CATEGORY_ID:
AlertDialog.Builder builder;
Context mContext = this;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.categorydialog_3_cols, (ViewGroup) findViewById(R.id.layout_root));
GridView gridview = (GridView) layout.findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(FS_90x90.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
dialog = builder.create();
break;
default:
dialog = null;
}
return dialog;
}
In addition the
View layout = inflater.inflate(R.layout.categorydialog_3_cols, (ViewGroup) findViewById(R.id.layout_root));
has the parent View (for the Grid view) defined as
(ViewGroup) findViewById(R.id.layout_root)
You can try setting the width at runtime, like this:
dialog.show();
LayoutParams params = dialog.getWindow().getAttributes();
params.width = 100;
dialog.getWindow().setAttributes(params);
Reference: How can I set the AlertDialog width, etc.
Related
I faced with a problem that i cannot put my dataGridView into scrollView and in case I have a lot of columns they just become so thin that it's impossible to see something there. That's why I decided to remake it and create LinearLayout with Vertical Layout for each column and each of them will have another LinearLayout with Horizontal Layout just to simulate GridView. (Hope it's a good idea)
But unfortunately I'm facing some problems during its creation. It is not being created and my application turning off. Ask you for your help
Here is my code: grid_container.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">
<ScrollView
android:id="#+id/GridScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/main_grid_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
PageFragment.java (place where LinearLayout should be fill out)
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.grid_container, container, false);
LinearLayout mainLayout = (LinearLayout) view.findViewById(R.id.main_grid_layout);
int colCount = mPage.get(0).split(",").length;
int rowCount = mPage.size();
ArrayList<ArrayList<String>> tempNormList = createNormList(mPage);
for(int currCol=0;currCol<colCount;currCol++){
LinearLayout linearLayout = new LinearLayout(view.getContext());
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(llParams);
for(int currRow=0; currRow<rowCount;rowCount++){
TextView textView = new TextView(view.getContext());
textView.setText(tempNormList.get(currCol).get(currRow));
if(currRow==0){
textView.setBackgroundResource(R.drawable.header_borders);
}
linearLayout.addView(textView);
}
mainLayout.addView(linearLayout);
}
return view;
}
Thank you in advance for your help
try my linked Answer, it will provide your solution, but you have do some changes like below
Don't Use Custom Adapter, use for loop with getView() method of
CustomAdapter to Set Data.
Cast your Linearlayout which id is main_grid_layout by `findViewById().
add convertView of getView() method to main_grid_layout
Skip Item Android Grid View in condition
hope it will help you
I have created a custom layout with 3 buttons for alertdialog which is working fine. I am trying to make the layout so that width and height layout will be only wrap its content i.e. no extra width/height but I am unable to do so. Can you help me on this please?
Here is my custom layout for alertdialog:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#00ffffff"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/rectangle_menu"
>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/wowButtonId"
android:layout_marginLeft="5dp"
android:src="#drawable/love_icon"
android:background="#drawable/round_button_for_round_menu_like_button"
android:layout_gravity="center"
/>
<ImageButton
android:layout_width="40dp"
android:layout_height="35dp"
android:id="#+id/blehButtonId"
android:layout_marginLeft="5dp"
android:src="#drawable/bleh"
android:background="#drawable/round_button_for_round_menu_like_button"
android:layout_gravity="center"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/dislikeButtonId"
android:layout_marginLeft="5dp"
android:src="#drawable/dislike_icon"
android:background="#drawable/round_button_for_round_menu_like_button"
android:layout_gravity="center"
/>
</LinearLayout>
</LinearLayout>
alertDialog implement in adapter code:
AlertDialog.Builder builder=new AlertDialog.Builder(context);
AlertDialog alertDialog=builder.create();
View view1=LayoutInflater.from(context).inflate(R.layout.layout_for_long_like_button_option,null);
ImageButton wow=(ImageButton) view1.findViewById(R.id.wowButtonId);
ImageButton disLike=(ImageButton) view1.findViewById(R.id.dislikeButtonId);
ImageButton bleh=(ImageButton) view1.findViewById(R.id.blehButtonId);
wow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context,"wow",Toast.LENGTH_SHORT).show();
}
});
disLike.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context,"dislike",Toast.LENGTH_SHORT).show();
}
});
bleh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context,"bleh",Toast.LENGTH_SHORT).show();
}
});
alertDialog.setView(view1);
alertDialog.show();
try this on your class file where you inflate your alert dialog...
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = alertDialog.getWindow();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
Summary: Use RelativeLayout tag at the root in your custom layout
I had two alert dialogs in the application I was writing. One of them did not wrap content, while the other did. The one with the LinearLayout tag at its root, was expanding a tad more in height, than the space its contents really occupied.
I tried setting the width and height properties to wrap_content on the LinearLayout but to no avail.
The one with the RelativeLayout tightly wrapped its contents. The code structure to achieve this would look like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<!-- More widgets -->
</LinearLayout>
</RelativeLayout>
Although care must be taken, that the inner LinearLayout should still specify wrap_content. You can of course specify match_parent if your intention is to present an elongated dialog, the intentions for doing so, which I would leave it to the developer.
Android Studio still warns me that The LinearLayout layout or its RelativeLayout parent is useless. I do not consider this a solution to the problem, rather it's just a fix. And, I should mention that I've always had these layout problems working with Android. This, I feel, is a more saner approach as it keeps the code modifications down to the layout.
You can use below code for set layout and show dialog
public void showDialog() {
LayoutInflater li = LayoutInflater.from(MainActivity.this);
View promptsView = li.inflate(R.layout.layout_for_long_like_button_option, null);
final TextView txtOk = (TextView) promptsView.findViewById(R.id.txtOk);
final TextView txtCancel = (TextView) promptsView.findViewById(R.id.txtCancel);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(BaseActivity.this);
alertDialogBuilder.setView(promptsView);
final AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
txtOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
txtCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
You can try setting the width of the inflated view directly when the dialog is shown and therefore has a window
val dialog = AlertDialog.Builder(context)
.setView(myView)
.create()
dialog.setOnShowListener {
dialog.window?.setLayout(
myView.width,
ViewGroup.LayoutParams.WRAP_CONTENT
)
}
I am trying to build an Application where there is a list-view with many items but I am not being able to change or set the width and height of single items.I have searched everywhere and the answer I got is making the width fill_parent,but its not working for me...
Kindly help.... thanks in advance... here are codes:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="3dp"
tools:context=".CustomListViewAndroidExample" >
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</RelativeLayout>
If you want to change the height of list view dynamically, you can use
list.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, theSizeIWant));
or
import android.view.ViewGroup.LayoutParams;
ListView mListView = (ListView) findViewById(R.id.listviewid);
LayoutParams list = (LayoutParams) mListView.getLayoutParams();
list.height = set the height acc to you;//like int 200
mListView.setLayoutParams(list);
This link shows you how to do it in java with your own custom adapter.
When overriding the getView() in your adapter, your can modify the height before supplying your view to the framework for render. Also, note that you do not have to use a SimpleCursorAdapter, an ArrayAdapter can also be used in the same fashion.
final SimpleCursorAdapter adapter = new SimpleCursorAdapter (context, cursor) {
#Override
public View getView (int position, View convertView, ViewGroup parent) {
final View view = super.getView(position, convertView, parent);
final TextView text = (TextView) view.findViewById(R.id.tvRow);
final LayoutParams params = text.getLayoutParams();
if (params != null) {
params.height = mRowHeight;
}
return view;
}
}
<?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"
android:orientation="vertical" >
<ImageView
android:id="#+id/venueImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
I have an ordinary listview and a custom adapter using the xml above for the row.
I will only have 3 rows, but I want those 3 rows to have equal heights to fit the height of the screen's resolution. At the moment I don't even know how to set the height of the rows because setting the LinearLayout's height doesnt do anything.
You can set the height of your ImageView to get what you want. To set it programmatically, you can do this on the getView() of your custom adapter:
public View getView(int position, View convertView, ViewGroup parent) {
View cv = convertView;
if (convertView == null) {
cv = inflater.inflate(R.layout.listview_item, null);
}
ImageView venueImage = (ImageView) cv.findViewById(R.id.venueImage);
LinearLayout.LayoutParams vi_params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, (int)(screenHeight*0.33);
venueImage.setLayoutParams(vi_params);
return cv;
}
You can get the screen height by adding this code on your main activity:
int screenHeight = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight();
I'm pretty new to Android, and I'm looking for a little help.
I have a GridView with the following XML in a linear layout.
<GridView android:id="#+id/gridview"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:numColumns="2"
android:columnWidth="90dp"
android:stretchMode="columnWidth"/>
I'm adding a series of CheckBoxes to the view with...
public View getView(int position, View convertView, ViewGroup parent)
{
CheckBox checkView;
if (convertView == null) {
checkView = new CheckBox(context);
checkView.setPadding(5, 5, 5, 5);
} else {
checkView = (CheckBox) convertView;
}
checkView.setText(checkNames[position]);
checkView.setWidth(400);
return checkView;
}
No matter how many different widths I try, including fill_parent and
wrap_content, the text winds up behind the box instead of next to it.
Any hints or tricks?
Thanks,
Andrew
Try using only wrap content for both layout width and layout height. Because the fill_parent will fill the full layout, where as wrap_content will fill only to the size of your text entered.