I have a GridView which has a ViewFlipper in each tile. Each view in the ViewFlipper has a different size, but when the tile is flipped, both views are forced to the same size as the GridView column. What i've tried:
Googled
Set android:measureAllChildren to false on ViewFlipper(only works with ListView)
Left android:columnWidth unspecified
Made ViewFlipper root tag in grid tile layout file
grid_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="180dp" android:layout_height="150dp">
<ViewFlipper
android:layout_width="match_parent"
android:layout_height="match_parent"
android:measureAllChildren="false"
android:id="#+id/view_flipper_3"
>
<RelativeLayout
android:layout_width="180dp"
android:layout_height="150dp"
android:id="#+id/front2"
android:background="#088980"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/commitmentNumber"
android:textSize="25dp"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="220dp"
android:layout_height="190dp"
android:background="#000000"
android:id="#+id/back_2"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="#+id/commitmentPartOne"
android:paddingBottom="5dp"
android:textSize="25sp"
android:text="Part One"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/commitmentPartTwo"
android:textSize="25sp"
android:text="Part Two"
android:paddingBottom="5dp"
android:layout_below="#+id/commitmentPartOne"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/commitmentPartThree"
android:textSize="25sp"
android:text="Part Three"
android:layout_below="#+id/commitmentPartTwo"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
</ViewFlipper>
</RelativeLayout>
fragment_with_gridview.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<GridView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/sixCommitmentsGridView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:numColumns="2"
android:gravity="center"
android:columnWidth="179dp"
android:stretchMode="columnWidth"
/>
</RelativeLayout>
GridViewAdapter.java:
public class SixCommitmentsGridAdapter extends BaseAdapter {
Context context;
String[] numbers;
public SixCommitmentsGridAdapter(Context context, String[] numbers) {
this.context = context;
this.numbers = numbers;
}
#Override
public int getCount() {
return numbers.length;
}
#Override
public Object getItem(int position) {
return numbers[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view;
final ViewFlipper flipper;
TextView commitmentNumber;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.six_commitments_grid_item, parent, false);
}else{
view = convertView;
}
flipper = (ViewFlipper) view.findViewById(R.id.view_flipper_3);
commitmentNumber = (TextView) view.findViewById(R.id.commitmentNumber);
commitmentNumber.setText(numbers[position]);
flipper.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(position % 2 == 0 || position == 0){
AnimationFactory.flipTransition(flipper, AnimationFactory.FlipDirection.RIGHT_LEFT, 200);
}else {
AnimationFactory.flipTransition(flipper, AnimationFactory.FlipDirection.LEFT_RIGHT, 200);
}
}
});
return view;
}
}
Managed to achieve the same grid-style layout with my intented ViewFlipper effect using a StaggeredGridLayoutManager and a RecyclerView.
In Main RelativeLayout contain ViewFlipper, you set height and width!
set them "wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
maybe worked.
good luck.
Update
Use this library:
https://github.com/etsy/AndroidStaggeredGrid
Related
I'm trying to recreate the following layout in Android:
The grid does not have to grow dynamically, it will always be 5 columns & 2 rows. The orientation is fixed to landscape. Each cell has the exact same layout, just the data in the TextViews can change.
My current approach is to fill a GridLayout with TableLayouts and setting appropriate layout_row & layout_column for each TableLayout. Like so :
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="2"
android:columnCount="4"
android:orientation="vertical">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="0"
android:background="#color/colorAccent">
Which results in this:
Code from picture above : https://pastebin.com/khr9aGWf
I'm running into a few problems.
I cannot get the cells to automatically scale to fit the screen.
The size of this layout file is starting to become very large and hard to manage
Perhaps my approach is completely wrong and I need to go towards something where I define the layout of a cell once and dynamically fill a grid with it?
Any help would be greatly appreciated.
You can create this kind of layout using GridView
<GridView
android:id="#+id/list"
android:numColumns="5"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"/>
your gridview item layout
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="0"
android:background="#color/colorPrimary">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
</TableLayout>
GridviewAdapter.java
public class GridviewAdapter extends BaseAdapter {
Activity context;
private LayoutInflater mInflater;
TableLayout.LayoutParams params;
public GridviewAdapter(Activity context)
{
this.context=context;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
params=new TableLayout.LayoutParams(new TableLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
(int) ((getDeviceHeight(context)-getStatusBarHeight()) / 2)));
}
#NonNull
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(position%2==0)
{
convertView.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
}else{
convertView.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
}
convertView.setLayoutParams(params);
return convertView;
}
#Override
public int getCount() {
return 10;
}
#Override
public String getItem(int position) {
return "";
}
#Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
}
public static int getDeviceHeight(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
Point point = new Point();
wm.getDefaultDisplay().getSize(point);
return point.y;
} else {
return wm.getDefaultDisplay().getHeight();
}
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
}
and in your activity class add code like this
GridviewAdapter itemsAdapter = new GridviewAdapter(this);
GridView listView = (GridView) findViewById(R.id.list);
listView.setAdapter(itemsAdapter);
your output look like this
I have made a custom ArrayAdapter and tried to add the another TextView "Breed" of the animals, but when i execute the program , its not showing the Breed. Where am i doing wrong ? I am scratching my head since long. please help where is the mistake ?
MainActivity.Java
public class MainActivity extends AppCompatActivity {
ListView simpleList;
ArrayList<Item> animalList=new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
simpleList = (ListView) findViewById(R.id.simpleListView);
animalList.add(new Item("Lion",R.drawable.lion,"Khatarnak"));
animalList.add(new Item("Tiger",R.drawable.tiger,"Fudu"));
animalList.add(new Item("Monkey",R.drawable.monkey,"Lallu"));
animalList.add(new Item("Elephant",R.drawable.elephant,"Jabardast"));
animalList.add(new Item("Dog",R.drawable.dog,"ItemDog"));
animalList.add(new Item("Cat",R.drawable.cat,"MeeMee"));
MyAdapter myAdapter=new MyAdapter(this,R.layout.list_view_items,animalList);
simpleList.setAdapter(myAdapter);
}
}
MyAdapter.Java
public class MyAdapter extends ArrayAdapter<Item> {
ArrayList<Item> animalList = new ArrayList<>();
public MyAdapter(Context context, int textViewResourceId, ArrayList<Item> objects) {
super(context, textViewResourceId, objects);
animalList = objects;
}
#Override
public int getCount() {
return super.getCount();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_view_items, null);
TextView textView = (TextView) v.findViewById(R.id.textView);
ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
TextView breedView = (TextView)v.findViewById(R.id.breed);
textView.setText(animalList.get(position).getAnimalName());
imageView.setImageResource(animalList.get(position).getAnimalImage());
breedView.setText(animalList.get(position).getBreed());
return v;
}
}
Item.Java
public class Item {
String animalName;
int animalImage;
String breedName;
public String getBreed() {
return breedName;
}
public void setBreed(String breed) {
this.breedName = breedName;
}
public Item(String animalName,int animalImage,String breedName)
{
this.animalImage=animalImage;
this.animalName=animalName;
this.breedName = breedName;
}
public String getAnimalName()
{
return animalName;
}
public int getAnimalImage()
{
return animalImage;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="#+id/simpleListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#000"
android:dividerHeight="2dp"/>
</RelativeLayout>
list_view_items.xml
<?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="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp" />
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Demo"
android:textColor="#000" />
<TextView
android:id="#+id/breed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Breed"
android:textColor="#000" />
</LinearLayout>
This is most likely a problem with the linear layout for each item. The orientation is horizontal, which lays each view one after the other horizontally. The problem is that the text view for the animal type has a width of fill_parent leaving no space for the breed view. If you change it to wrap_content it'll probably work for some of the cases, but might not work with everything.
In any case I think this is a problem with the views' positions and sizes. Try and move them around. Perhaps even a simple change would be placing them vertically:
<?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="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Demo"
android:textColor="#000" />
<TextView
android:id="#+id/breed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Breed"
android:textColor="#000" />
</LinearLayout>
</LinearLayout>
There's perhaps a more efficient way of doing this, but this is just an example. The inner linear layout places one text view on to of the other.
PS: your getCount method is not really doing anything. You can remove it.
replace your layout with my code your issue will resolve.
<?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="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp" />
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Demo"
android:textColor="#000" />
<TextView
android:id="#+id/breed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Breed"
android:textColor="#000" />
</LinearLayout>
Hi I am working with custom GridView.
The GridView placed in the following xml/layout file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<View
android:id="#+id/music_home_ad_view"
android:layout_width="match_parent"
android:layout_height="50dp" />
<ScrollView
android:id="#+id/music_home_root_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ScrollView>
<FrameLayout
android:id="#+id/music_home_promo_frame"
android:layout_width="match_parent"
android:layout_height="#dimen/music_home_promo_height"
android:layout_marginTop="20dp" >
<android.support.v4.view.ViewPager
android:id="#+id/music_home_promo_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.raaga.home.CirclePageIndicator
android:id="#+id/music_home_page_indicator"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_gravity="bottom"
android:padding="10dp" />
</FrameLayout>
<GridView
android:id="#+id/music_home_grid_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/music_home_promo_frame"
android:numColumns="3" >
</GridView>
</RelativeLayout>
The GridView contains the following views
music_grid_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
>
<ImageView
android:id="#+id/music_grid_image"
android:layout_width="60dp"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:src="#drawable/new_releases"
/>
<TextView
android:id="#+id/music_grid_category_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/music_grid_image"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="New Releases"
android:textColor="#color/text_white"
/>
</RelativeLayout>
I have used the custom adapter class and followed this link
MusicCategoryAdapter.java
public class MusicCategoryAdapter extends BaseAdapter{
ArrayList<Integer> categoryImageList;
ArrayList<String> categoryNameList;
Context mContext;
public MusicCategoryAdapter(Context mContext, ArrayList<Integer> categoryImageList, ArrayList<String> categoryNameList) {
this.mContext= mContext;
this.categoryImageList = categoryImageList;
this.categoryNameList = categoryNameList;
}
#Override
public int getCount() {
//send the list length
return categoryImageList.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
GridHolder holder;
View grid = convertView;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
convertView = inflater.inflate(R.layout.music_grid_item, null);
holder = new GridHolder();
holder.categoryTitle = (TextView) convertView.findViewById(R.id.music_grid_category_name);
holder.categoryImage = (ImageView)convertView.findViewById(R.id.music_grid_image);
holder.categoryTitle.setText(categoryNameList.get(position));
holder.categoryImage.setImageResource(categoryImageList.get(position));
convertView.setTag(holder);
} else {
holder = (GridHolder) convertView.getTag();
}
return convertView;
}
public class GridHolder{
ImageView categoryImage;
TextView categoryTitle;
}
}
I am facing the problem that the items are repeating by changing the position when scrolling.
Could anyone help on this problem? What mistake I did in this?
Put the scrollview at the starting of relative layout and end the tag in the end of the relative layout.
This would make your entire layout scrollable and you will overcome the problem.
What I'm trying to do
I'm trying to create a chat which has to diffrent row's. For every row I made a own layout file, but the problem is that the layoutfile of one row dosn't fit the screen.
Question
What do I need to change in the row layout that it fits like it should. You'll find the code and also a printscreen of what I'm trying.
Code
ListAdapter
public class ChatListAdapter extends BaseAdapter {
private ArrayList<String> ContentList;
private ArrayList<Integer> ChatUserList;
private static LayoutInflater inflater = null;
public ChatListAdapter(Activity activity, ArrayList<String> _ContentList,
ArrayList<Integer> _ChatUserList) {
ContentList = _ContentList;
ChatUserList = _ChatUserList;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return ContentList.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
TextView tv_text;
TextView tv_date;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
if (ChatUserList.get(position) == 1) {
// I'm RECIVER -> Message is left align
vi = inflater.inflate(R.layout.rowlayout_leftalign, null);
tv_text = (TextView) vi.findViewById(R.id.tv_chat_leftalign);
tv_date = (TextView) vi.findViewById(R.id.tv_chat_leftalign_date);
} else {
// I'm SENDER -> Message is right align
vi = inflater.inflate(R.layout.rowlayout_rightalign, null);
tv_text = (TextView) vi.findViewById(R.id.tv_chat_rightalign);
tv_date = (TextView) vi.findViewById(R.id.tv_chat_rightalign_date);
}
tv_date.setText(sdf.format(new Date()));
tv_text.setText(ContentList.get(position));
tv_text.setMaxWidth(((Chat.display.getWidth() / 4) * 3));
return vi;
}
RowLayout XML Left (Blue bubbles)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#android:color/transparent" >
<RelativeLayout
android:id="#+id/ly_rf_row_r"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginBottom="2.5dp"
android:layout_marginTop="2.5dp"
android:background="#drawable/shape_rightalign"
android:paddingBottom="2.5dp"
android:paddingTop="2.5dp" >
<TextView
android:id="#+id/tv_chat_rightalign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="right"
android:paddingBottom="2dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/tv_chat_rightalign_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="-3dp"
android:layout_marginLeft="-3dp"
android:layout_toLeftOf="#id/tv_chat_rightalign"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:textColor="#android:color/black"
android:textSize="8.5dp" />
</RelativeLayout>
</RelativeLayout>
RowLayout XML right (red bubbles)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#android:color/transparent" >
<RelativeLayout
android:id="#+id/ly_rf_row_l"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginBottom="2.5dp"
android:layout_marginTop="2.5dp"
android:background="#drawable/shape_leftalign"
android:paddingBottom="2.5dp"
android:paddingTop="2.5dp" >
<TextView
android:id="#+id/tv_chat_leftalign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="left"
android:paddingBottom="2dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/tv_chat_leftalign_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="-3dp"
android:layout_marginLeft="-3dp"
android:layout_toRightOf="#id/tv_chat_leftalign"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:textColor="#android:color/black"
android:textSize="8.5dp" />
</RelativeLayout>
</RelativeLayout>
for both XML add android:layout_width="fill_parent" and android:layout_height="wrap_content" like followings
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#android:color/transparent"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
and so on...
I have a GridView and each cell has an ImageView with TextView under it. Unfortunately if the TextView has more than one line the text gets cut off. I have tried everything but I cant find a solution.
It seems that the row height of the GridView is the problem and not the actual text because you can see half of the text in the Textview.
Here is my code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridView = (GridView) findViewById(R.id.gridView1);
gridView.setAdapter(new ImageAdapter());
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
startActivity(new Intent(Main.this, Acronyms.class));
}
});
}
public class ImageAdapter extends BaseAdapter {
private Integer[] iconImg = {
R.drawable.acronyms, R.drawable.acronyms,
R.drawable.acronyms
};
private String[] iconTitle = {
"Acronyms", "Cardiac Strips",
"Test 3"
};
public int getCount() {
return iconImg.length;
}
public Object getItem(int arg0) {
return null;
}
public long getItemId(int arg0) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflater = getLayoutInflater();
view = inflater.inflate(R.layout.icon, null);
TextView textView = (TextView) view.findViewById(R.id.icon_text);
textView.setText(iconTitle[position]);
ImageView imageView = (ImageView) view.findViewById(R.id.icon_image);
imageView.setImageResource(iconImg[position]);
} else {
view = convertView;
}
return view;
}
}
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#a3e6ff"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="25"
android:text="Example"
android:textColor="#248fb7"
android:textSize="40dp"
android:typeface="serif" android:gravity="center_horizontal"/>
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="15dp" >
</GridView>
</LinearLayout>
and my icon.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid_icon_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="#+id/icon_image"
android:layout_width="90dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/icon_text"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:maxLines="2"
android:singleLine="false"
android:text="Samples Samples"
android:textColor="#FFFFFF"
android:textSize="15dp"
android:typeface="serif" />
</LinearLayout>
And here is a screenshot:
I resolved using, when i define it
TextView.setLines(2);
in the xml the textview is
android:layout_width="wrap_content"
android:layout_height="wrap_content"
[EDIT1]
You could try using a RelativeLayout instead of a Linear Layout for the icon.xml.
If this doesnt work then I would then move to a static height TextView. From looking at your screenshot, it looks like you will always use the same image, and the text is either going to be 1 line or 2. Just make the text height static to allow for 2 lines.
[ORIGINAL]
I think the problem is in your linear layout definition for your icon.xml. In your definition, you have the layout having "match_parent" as the width and height parameters. You should, since these are to essentially be subviews within the gridview be "wrap_content". Here is what I think it should be
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid_icon_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView android:id="#+id/icon_image"
android:layout_width="90dp"
android:layout_height="wrap_content" />
<TextView android:id="#+id/icon_text"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:maxLines="2"
android:singleLine="false"
android:text="Samples Samples"
android:textColor="#FFFFFF"
android:textSize="15dp"
android:typeface="serif" />
</LinearLayout>
Change ConstraintLayout for LinearLayout.
Make sure you are using ConstraintLayout and if possible change by LinearLayout. For long texts, Constraint is not very good.