I have a ViewPager and a GridView to show a monthly calendar. The GridView has about 30 elements and swipe to left and right is very slow. I made a sample project to test without any database access and it is slow, too. Maybe someone sees the problem or do I have to make an asynchronous load of the pages?
That is the layout for one element in the GridView. I want to show 9 little 5x5 pixel icons in any element, but without them, it is slow, too.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/Layout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="1dp">
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:id="#+id/date" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginLeft="5dp" android:gravity="center_vertical" android:textSize="14sp" android:textStyle="bold"/>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff">
<ImageView android:id="#+id/dot1" android:layout_width="wrap_content" android:drawingCacheQuality="auto" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:background="#ffffff" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:paddingBottom="1dp" android:paddingLeft="1dp" android:paddingTop="5dp"/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff">
<ImageView android:id="#+id/dot2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:background="#ffffff" android:paddingBottom="1dp" android:paddingLeft="1dp"/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff">
<ImageView android:id="#+id/dot3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_gravity="right" android:background="#ffffff" android:paddingBottom="1dp" android:paddingLeft="1dp"/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff">
<ImageView android:id="#+id/dot4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_gravity="center_horizontal" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:background="#ffffff" android:paddingBottom="1dp" android:paddingLeft="1dp"/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff">
<ImageView android:id="#+id/dot5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_gravity="left" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:background="#ffffff" android:paddingBottom="1dp" android:paddingLeft="1dp"/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff">
<ImageView android:id="#+id/dot6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_gravity="left" android:background="#ffffff" android:paddingBottom="1dp" android:paddingLeft="1dp"/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff">
<ImageView android:id="#+id/dot7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_gravity="left" android:background="#ffffff" android:paddingBottom="1dp" android:paddingLeft="1dp"/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff">
<ImageView android:id="#+id/dot8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_gravity="left" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:background="#ffffff" android:paddingBottom="1dp" android:paddingLeft="1dp"/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff">
<ImageView android:id="#+id/dot9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_gravity="left" android:background="#ffffff" android:paddingBottom="5dp" android:paddingLeft="1dp"/>
</RelativeLayout>
</LinearLayout>
That's the instantiateItem of GridView adapter:
public Object instantiateItem(ViewGroup container, int position) {
GridView gv = new GridView(m_context);
gv.setAdapter(new BaseAdapter() {
#Override
public View getView(int pos, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
// if it's not recycled, initialize some attributes
v = mInflater.inflate(R.layout.calendar_month_item, null);
} else {
v = convertView;
}
TextView dayView = (TextView) v.findViewById(R.id.date);
dayView.setText(pos + "");
return v;
}
#Override
public long getItemId(int p) {
return 0;
}
#Override
public Object getItem(int p) {
return null;
}
#Override
public int getCount() {
return 30;
}
});
gv.setNumColumns(5);
((ViewPager) container).addView(gv);
return gv;
}
I looked around for a bit and I think I found exactly what you are looking for:
content XML, you will probably have to change or redo your layout as it is turns out quite large, as you said.
CalendarAdapter.java, in line 112 you can see that they added an icon there. Just change the code around so you can add your nine icons programmatically instead of having all of these layouts.
How to implement CalendarView in Android
Which version of Android do you use? This is important, because there is a Calendar in Android 3.0, is has bugs, but not so critical.
About your question, if I well understood it. Look at your XML, it contains 11 layouts, and this is just for only one element. If you have them many, imagine the amount of work the Android system must do, to inflate all your elements. And after that, when you swipe, you reuse the elements, and Android must refresh the ones that are not valid. This is a lot of work. (11 layouts * 30 = 330 layouts to be refreshed or inflated). As Android developer documentation says, you must always use as less layouts to wrap your elements, as possible!
Anyway, your approach is not correct. I can suggest you to look at the source code of the CalendarView in Android 3+, but to give you a hint:
Create a layout for a week, not for every day(like in CalendarView). By doing this, Android will have to refresh only n elements(You'll choose how many weeks you want to display at a moment of time), not 30. This layout must contain 7 views for every day.
Hope you get some idea from this.
First of all, your layout is a bit strange, as others have stated.
You really don't need the schema for any layout sub elements.
Having multiple nested relative or linear layouts when not needed will be quite expensive, especially when wrapped in a GridView wrapped in a ViewPager.
Second of all, the ViewPager and PagerAdapter are a little bit tricky.
When you have the default number of offscreen pages, you will layout all three of them at the same time. If you set it higher, you will be layouting even more views.
Make sure that you correctly remove views in destroyItem.
Implement the isViewFromObject method correctly, since otherwise your views will be discarded when you page, causing more cost of recreating them.
Going with #CommonsWare's advice is probably a good idea. TraceView is a bit difficult to use, but even so it will probably yield some clues.
Related
My problem is this, I have multiple RelativeLayouts in a LinearLayout inside of a ScrollView that is in the main RelativeLayout. When trying to obtain the id's in the fragment class I have it keeps giving me errors.
I've looked around for an answer for this for about 2 days now and have only come across an idea of using
<include name="some_name" layout="#layout/some_layout"/>
and it doesn't really help me with my app. I'm trying to develop a calendar week view which I already have the numbers working and the time on the left.
Here is my xml file:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/day_labels_linear_layout"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<!-- not important but this holds the numbers up top -->
</LinearLayout>
<ScrollView
android:id="#+id/calendar_scroll_view"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_below="#+id/day_labels_linear_layout"
android:fadingEdge="none"
android:overScrollMode="never"
android:scrollbars = "none">
<!-- used to space each day with the day_labels_linear_layout weights -->
<LinearLayout
android:id="#+id/calendar_spacer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!--This has all of the hours, I just didn't include them
because it would take up so much room-->
<RelativeLayout
android:id="#+id/hours_relative_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2.5"
android:background="#color/scheduler_background" >
<TextView
android:id="#+id/time_12_am"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:paddingBottom="25dp"
android:paddingTop="25dp"
android:text="#string/text_time_12_am" />
<TextView
android:id="#+id/time_1_am"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/time_12_am"
android:layout_centerHorizontal="true"
android:paddingBottom="25dp"
android:paddingTop="25dp"
android:text="#string/text_time_1_am" />
<TextView
android:id="#+id/time_2_am"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/time_1_am"
android:layout_centerHorizontal="true"
android:paddingBottom="25dp"
android:paddingTop="25dp"
android:text="#string/text_time_2_am" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/sunday_relative_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" >
<View
android:id="#+id/divider_sunday_left"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="#color/divider_scheduler" />
<Button
android:id="#+id/sunday_day"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="#color/transparent" />
<View
android:id="#+id/divider_sunday_right"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="#color/divider_scheduler" />
</RelativeLayout>
<!-- there is a Relative layout for each day-->
</LinearLayout>
</ScrollView>
</RelativeLayout>
So when I try to access the id's this is what I get:
RelativeLayout sundayRL;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_scheduler, null);
sundayRL = (RelativeLayout) view.findViewById(R.id.sunday_relative_layout);
}
It gives me the error: sunday_relative_layout cannot be resolved or is not a field, I added a button to see if I could get the id for that and the same result happened. Yet I was able to find the id's for each of the day numbers and day names that is in the top LinearLayout.
Does anyone have a better solution to this? I'm trying to register when the user clicks the view so I can send them to a scheduling page.
EDIT:
So changing null to container allowed me to find my Ids
View view = inflater.inflate(R.layout.fragment_scheduler, container);
But it caused another issue with a map fragment on one of the other fragments saying I didn't have the right API key which it does.
If you have nested layouts
eg:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout >
<RelativeLayout
android:id="#+id/rl2>
<Button
android:id="#+id/getStarted"
/>
</RelativeLayout>
</RelativeLayout>
And you want to press a button in the nested one.
Add ID on the nested Layout.
and use this code in onCreate()
View view = (View) findViewById(R.id.rl2);
Button button = (Button) view.findViewById(R.id.getStarted);
I'm trying to register a context menu for my gridview. At the moment nothing happens.
Amazingly I couldn't find the answer here.
My gridview is inside main.xml relativelayout:
<?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">
<LinearLayout
android:id="#+id/bottom_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="horizontal">
<ImageButton
android:id="#+id/cameraButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_menu_camera"
android:onClick="cameraButtonOnClick"/>
<ImageButton
android:id="#+id/addGalleryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/add_gallery"
android:onClick="addGalleryButtonOnClick"/>
<ImageButton
android:id="#+id/addDirButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/add_dir"
android:onClick="addDirButtonOnClick"/>
</LinearLayout>
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="spacingWidthUniform"
android:gravity="center"
android:layout_above="#id/bottom_bar"
/>
The gridview is filled with thmbnail.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res/com.ninovanhooff.projectv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageButton
android:id="#+id/thumbview"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="fitCenter"
android:background="#drawable/thumb_selector"
android:src="#drawable/sample_0" />
<!-- <TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:focusable="false"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:background="#drawable/label_bg"/>
<com.ninovanhooff.projectv.ThreeStateCheckBox
android:id="#+id/tricheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
app:label=""
app:shadow="false"
app:state="0"
android:layout_alignParentBottom="true">
</com.ninovanhooff.projectv.ThreeStateCheckBox>
-->
</RelativeLayout>
Note that only the imagebutton is preserved to simplify problem
Contextmenu is registered like this:
adapter = new ThumbnailAdapter(this);
gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(adapter);
registerForContextMenu(gridView);
I suspect it's got something to do with android:focusable or android:clickable. But don't know which to try. I tried focusable = false and clickable = false on both the relativelayout and linearlayout of main.xml, but no effect.
So, I got this to work and my suspicion was right. Other elements where stealing the touch events away from intended gridview element. Make sure to remove clickable objects like buttons and checkboxes from your layout to find the culprit. In my case, I had to remove both ImageButton and ThreeStateCheckbox. Another mistake was that I added onclicklisteners to the ImageButton rather than the direct child of gridview (thumbnail.xml). The recommended approach is to use gridview.setOnItemClickListener(listener)).
Now, this works, but I still need my image and checkbox back. ImageButton was replaced by an ImageView, and I added android:focusable="false" to the checkbox to prevent it from consuming the click event. And everything works fine.
If you can't figure this stuff out, the hierarchyviewer can be enlightening. Maybe you forgot about some tiny element which could steal your touch events. Good luck!
I'm having issues with the addView method from LinearLayout. I don't know why, but if I add three views only the first one is displayed. Here is the code:
Comment[] comments = posts[position].getComments();
LinearLayout layout = (LinearLayout)convertView.findViewById(R.id.post_list_item_comments);
layout.removeAllViews();
for(int i=0; i<comments.length; i++) {
View comment = inflater.inflate(R.layout.post_list_item_comment,null);
((TextView)comment.findViewById(R.id.post_list_item_comment_name)).setText(comments[i].getFrom().getName());
((TextView)comment.findViewById(R.id.post_list_item_comment_message)).setText(comments[i].getText());
layout.addView(comment,i);
}
I've tried with addView(comment) too, but with the same result.
This is the code of the View that I retrieve when to use the findViewById mehthod.
<LinearLayout
android:id="#+id/post_list_item_comments"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="40dip"
android:layout_below="#id/post_list_item_footer_text"
android:visibility="gone"/>
And this is the XML that I inflate:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<View
android:id="#+id/post_list_item_comment_divider"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#drawable/divider"
android:layout_marginTop="2dip"
android:layout_marginBottom="2dip"/>
<ImageView
android:id="#+id/post_list_item_comment_photo"
android:layout_width="40dip"
android:layout_height="wrap_content"
android:layout_below="#id/post_list_item_comment_divider"
android:adjustViewBounds="true"/>
<TextView
android:id="#+id/post_list_item_comment_name"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toRightOf="#id/post_list_item_comment_photo"
android:layout_below="#id/post_list_item_comment_divider"
android:maxLines="2"
android:ellipsize="end"/>
<TextView
android:id="#+id/post_list_item_comment_message"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toRightOf="#id/post_list_item_comment_photo"
android:layout_below="#id/post_list_item_comment_name"
android:textSize="13sp"
android:maxLines="2"
android:ellipsize="end"/>
</RelativeLayout>
you do not declare the LinearLayout orientation... by default is Horizontal, try setting the orientation Vertical
<LinearLayout
android:id="#+id/post_list_item_comments"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="40dip"
android:layout_below="#id/post_list_item_footer_text"
android:visibility="gone"/>
Use the hierarchy viewer to check if there really is only 1 view added, or that you can see only one view. For instance, this line android:layout_below="#id/post_list_item_footer_text" might be troublesome if you repeat it? I don't know the expected behaviour for that...
The answer to this question might be really obvious but its giving me a headache. I have a simple LinearLayout with one single ListView in it. I do this: onCreate
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.friends);
ListView listView = (ListView) findViewById(R.id.friend_list);
listAdapter = new CheckinListAdapter(checkins, listView, R.layout.checkin_list_item);
listView.setAdapter(listAdapter);
if (getLastNonConfigurationInstance() != null) {
FriendsActivity last = (FriendsActivity) getLastNonConfigurationInstance();
this.checkins.addAll(last.checkins);
this.sort = last.sort;
} else {
refresh();
}
registerForContextMenu(listView);
}
But for some reason onCreateContextMenu never gets called! So I did some research and since I am loading the list after the register, perhaps it doesn't register it correctly. If I go in my ListAdapter and do registerForContextMenu it does show up. But it doesn't behave correctly with the keyboard. So I am now confused on what can be the error because it seems a little non intuitive for me to have to register each child item. All the examples I find online are using ArrayAdapter. :(
Any suggestions?
Edit
Here is more detail, in case it something I don't see:
My XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:text="#string/check_in"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onCheckInClicked"/>
<ListView android:id="#+id/friend_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
List item xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dip"
android:paddingBottom="5dip">
<ImageView android:id="#+id/user_photo"
android:layout_width="40dip"
android:layout_height="40dip"
android:scaleType="centerCrop"/>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="8dip">
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:id="#+id/user" style="#style/TextButton"/>
<TextView android:text="#string/at"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button android:id="#+id/venue"
android:singleLine="true"
android:ellipsize="end"
style="#style/TextButton"/>
</LinearLayout>
<TextView android:id="#+id/venue_address" style="#style/GreyLarge"/>
<LinearLayout android:id="#+id/checkin_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip">
<ImageView android:id="#+id/checkin_image"
android:layout_width="70dip"
android:layout_height="60dip"
android:layout_marginRight="8dip"
android:scaleType="centerCrop"/>
<TextView android:id="#+id/checkin_shout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView android:id="#+id/elapsedTime" style="#style/GreySmall"/>
</LinearLayout>
</LinearLayout>
This took me 6 hours to figure out but it turns out I had to add:
android:focusable="false"
to all my <Button/> tags.
Related post: TextView and Button in each row and onListItemClick()
Do, registerForContextMenu(listView);
before setting the adapter, that is before:
listView.setAdapter(listAdapter);
You have mentioned that the menu is not responding correctly with the keyboard actions. Can you tell me what exactly the problem is?
I am trying to create a screen (in portrait mode) that shows 4 images (same size, intended to scale down to fit screen), taking up the entire screen, breaking up the screen into quadrants (a tall, 2x2 grid). This will act as a main menu type of activity and each image should be clickable, in order to take the user to a different activity.
I have tried using a GridView inside a LinerLayout (using a lot from Google's GridView tutorial) but cannot get the images to all scale properly to fill the entire screen. I get extra margins around the images and/or scrolling of the entire screen.
I have also tried using a TableLayout, placing 2 images in each of the 2 rows. Visually, that worked perfectly. Unfortunately when using that, I cannot seem to reference the ImageView items in the TableLayout in my activity code (findViewById always returns null).
I feel like a TableLayout is really not the "right thing to do" but I would like to hear what others have to say. Either way, what should be done to accomplish my desired functionality?
Thanks.
Edit 1.1:
The relative layout works much better for getting things lined up. Now I'm just left with the issue where findViewById always returns null. Here is my code so far:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/homescreen_bgcolor"
>
<ImageView id="#+id/one"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:src="#drawable/item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView id="#+id/two"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:src="#drawable/item2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView id="#+id/three"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:src="#drawable/item3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView id="#+id/four"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/item4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
public class HomeScreenActivity2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homescreen2);
ImageView imageView = (ImageView) findViewById(R.id.one);
imageView.setClickable(true);
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Log.i("Test", "test");
}
});
}
}
Here is a sample layout showing how you can achieve a 2 X 2 grid that covers the entire screen using just a RelativeLayout.
<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" >
<View
android:id="#+id/centerVerticalShim"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_centerVertical="true"
android:visibility="invisible" />
<View
android:id="#+id/centerHorizontalShim"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:visibility="invisible" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/centerVerticalShim"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/centerHorizontalShim"
android:background="#42A5F5"
android:gravity="center"
android:text="#string/one"
android:textColor="#FFFFFF" >
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/centerVerticalShim"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/centerHorizontalShim"
android:background="#EF5350"
android:gravity="center"
android:text="#string/two"
android:textColor="#FFFFFF" >
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/centerVerticalShim"
android:layout_toLeftOf="#+id/centerHorizontalShim"
android:background="#66BB6A"
android:gravity="center"
android:text="#string/three"
android:textColor="#FFFFFF" >
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/centerVerticalShim"
android:layout_toRightOf="#+id/centerHorizontalShim"
android:background="#5C6BC0"
android:gravity="center"
android:text="#string/four"
android:textColor="#FFFFFF" >
</TextView></RelativeLayout>
The above layout results in this:
I think a TableLayout could work for you, but I'd recommend trying out RelativeLayout as well. You can basically pin your images to the four quadrants by using combinations of
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"`
on your images.
I'm doing something similar in my app where I have multiple buttons on a homepage that can launch corresponding activities. RelativeLayout works fine, and it avoids nested Layout objects, which can hamper performance during render and layout procedures (if it gets out of hand).