android interface error ImageButton - android

Hi i have an app that is a gallery of images.
The problem is that, i have buttons to move between images on the center of the app and i want them at 75% of the margin of top.
The question is : Can i put it on % of the screen instead of dp?
The error is that if i use margin_bottom it crashes.
Here is my code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/color_fondo"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/barra"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY"a
android:src="#drawable/cabecera" />
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/barra"
android:paddingTop="#dimen/activity_vertical_margin"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher" />
<ProgressBar
android:id="#+id/pb1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/img1"
android:layout_centerHorizontal="true"
android:visibility="invisible" />
<ImageButton
android:id="#+id/imgbtnshare1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#null"
android:paddingBottom="5dp"
android:src="#drawable/compartir4" />
<ImageButton
android:id="#+id/imgbtn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imgbtnshare1"
android:layout_alignParentLeft="true"
android:layout_marginBottom="100dp"
android:background="#null"
android:src="#drawable/izquierda42" />
<ImageButton
android:id="#+id/imgbtn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/imgbtn2"
android:background="#null"
android:src="#drawable/derecha42" />
</RelativeLayout>

You may get the Layout size and then calculate your buttons top margin as proportion of that:
NOTE: You cannot get the layout size in onCreate() since it's not set yet. Do it in onWindowFocusChanged method of your Activity.
RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.YourXML);
#Override
public void onWindowFocusChanged (boolean hasFocus) {
// The layout has been drawn here
// so the Height and Width have been computed
int width = relativeLayout.getWidth();
int height = relativeLayout.getHeight();
}
int topMargin = height * 0.75;
And set it programmatically:
LayoutParams layoutParams = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
layoutParams.setMargins(left, top, right, bottom);
yourbutton.setLayoutParams(layoutParams);

Related

Not able to set width of a horizontal linear layout or height of vertical linear layout

I have made a fragment layout for which code is shown below.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bill_display_id"
android:layout_gravity="start"
tools:context=".HomePage">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/zoom">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bill_text_id"
/>
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scroll_horiz_id">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:drawable/btn_default"
android:orientation="horizontal"
android:id="#+id/zoom_lin">
<View
android:layout_height="match_parent"
android:layout_width="1dp"
android:background="#android:color/black" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp"
android:id="#+id/prd_id">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product"
android:layout_gravity="center"
/>
</LinearLayout>
<View
android:layout_height="match_parent"
android:layout_width="1dp"
android:background="#android:color/black" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp"
android:id="#+id/rate_id">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rate"
android:layout_gravity="center"
/>
</LinearLayout>
<View
android:layout_height="match_parent"
android:layout_width="1dp"
android:background="#android:color/black" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp"
android:id="#+id/qty_id">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Qty"
android:layout_gravity="center"
/>
</LinearLayout>
<View
android:layout_height="match_parent"
android:layout_width="1dp"
android:background="#android:color/black" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp"
android:id="#+id/price_id">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price"
android:layout_gravity="center"
/>
</LinearLayout>
<View
android:layout_height="match_parent"
android:layout_width="1dp"
android:background="#android:color/black" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
Now i have a function setZoom (shown below) in which i am trying to set height and width of horizontal linear layout (id = zoom_lin).
But i am seeing that only height is getting set and not width.
protected void setZoom(float val)
{
View horz_lin = rootView.findViewById(R.id.zoom_lin);
ViewGroup.LayoutParams lp = horz_lin.getLayoutParams();
lp.width = 300; // 300 is a random number for testing only
lp.height = 600; // 600 is a random number for testing only
horz_lin.requestLayout();
}
Similarly if i try to set height and width for vertical linear layout (id = zoom), i see that only width is getting set and not height.
Is there any rule which forbids us from setting the width for horiizontal linear layout and height for vertical linear layout.
Please excuse me if am missing something trivial as i am a beginner in android development.
You need to set the updated layoutParams back to the linear layout:
protected void setZoom(float val)
{
LinearLayout horz_lin = (LinearLayout)rootView.findViewById(R.id.zoom_lin);
ViewGroup.LayoutParams lp = horz_lin.getLayoutParams();
lp.width = 300; // 300 is a random number for testing only
lp.height = 600; // 600 is a random number for testing only
horz_lin.setLayoutParams(lp); // ADD THIS LINE
horz_lin.requestLayout();
}

Android dynamicly added button doesnt fit into layout

I have a RelativeLayout with 5 buttons :
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#212121"
tools:context="com.example.test.MainActivity"
android:id="#+id/menu"
android:gravity="center">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="#+id/main_menu"
android:gravity="center">
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="#string/singleplayer_text"
android:id="#+id/singleplayer_button"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:onClick="initializeGame"
android:clickable="true"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/multiplayer_button"
android:layout_alignStart="#+id/multiplayer_button"
android:background="#drawable/button"
android:textColor="#212121" />
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="#string/multiplayer_text"
android:id="#+id/multiplayer_button"
android:layout_below="#+id/singleplayer_button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="#drawable/button"
android:textColor="#212121" />
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="#string/deck_name"
android:id="#+id/deck_button"
android:layout_below="#+id/multiplayer_button"
android:layout_alignLeft="#+id/multiplayer_button"
android:layout_alignStart="#+id/multiplayer_button"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="#drawable/button"
android:textColor="#212121" />
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="#string/options_text"
android:id="#+id/options_button"
android:layout_below="#+id/deck_button"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="#drawable/button"
android:textColor="#212121" />
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="#string/exit_text"
android:id="#+id/exit_button"
android:layout_below="#+id/options_button"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="#drawable/button"
android:textColor="#212121" />
</RelativeLayout>
Sometime in my game , i come to the point where i want to save it.And when it is saved, there should be another button added at my menu.
resume= new Button(this);
resume.setId((int) Math.random() * 10);
resume.setText("Resume");
resume.setTextColor(Color.parseColor("#212121"));
resume.setBackgroundResource(R.drawable.button);
int margin = (int) (8 * scale + 0.5f);
RelativeLayout main_menu = (RelativeLayout)findViewById(R.id.main_menu);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
lp.setMargins(0, margin, 0,margin);
main_menu.addView(resume, lp);
The problem is, that this new button resume covers the singleplayer button, but i want it on top of it and all buttons should still be centered.
Is this possible?
Hope this helps.
lp.addRule(RelativeLayout.BELOW, R.id.below_id);
Replace below_id with your specific id.
Adds a layout rule to be interpreted by the RelativeLayout. This
method should only be used for constraints that don't refer to another
sibling (e.g., CENTER_IN_PARENT) or take a boolean value (TRUE for
true or 0 for false). To specify a verb that takes a subject, use
addRule(int, int) instead.
According to your requirement doesnt fit into layout , You can remove
Remove this First Then rectify your addRule
Read RelativeLayout add rule
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"

Android: soft keyboard pushes up whole layout, need to resize the listview

I have a full screen chat layout where I have a header with a logo on the top, then follows a listView with all chat messages and at the bottom I have the editText and the buttom to send the chat message. Now what happens when I click in the editText is, that the whole layout gets pushed up. What I need is, the header with the logo to always stay where they are and the listView to gets resized.
this does not do the trick:
android:windowSoftInputMode="adjustResize"
this neither:
android:windowSoftInputMode="adjustPan"
maybe because I have a full screen layout?
I need to find a different way to resize my listView once a keyboard is present, any ideas? Thanks in advance!
Here is my layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#drawable/ss99_yellow_area_navigation_bar"
android:baselineAligned="false"
android:orientation="horizontal" >
<ImageView
android:id="#+id/back"
android:layout_width="45dp"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:adjustViewBounds="true"
android:clickable="true"
android:src="#drawable/ss99_button_back" />
<ImageView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:src="#drawable/ss99_logo_small" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="-2dp"
android:background="#drawable/ss9_yellow_bar_chat_new" >
<TextView
android:id="#+id/userOpponent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/userUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/userOpponent"
android:layout_alignBottom="#+id/userOpponent"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<ListView
android:id="#+id/chatList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:divider="#00000000"
android:listSelector="#00000000"
android:layout_weight="1"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll">
</ListView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#drawable/ss9_yellow_area_for_bottom_and_chat_string" >
<ImageView
android:id="#+id/field1"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:src="#drawable/ss1_field_big" />
<EditText
android:id="#+id/chatEdit"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/field1"
android:layout_alignLeft="#+id/field1"
android:layout_alignTop="#+id/field1"
android:layout_marginBottom="3dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="1dp"
android:background="#null"
android:inputType="textAutoCorrect|textAutoComplete"
android:textAppearance="?android:attr/textAppearanceMedium" >
</EditText>
<ImageView
android:id="#+id/chatOk"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:adjustViewBounds="false"
android:baselineAlignBottom="false"
android:clickable="true"
android:cropToPadding="false"
android:scaleType="fitEnd"
android:src="#drawable/ss4_button_go_small" />
</RelativeLayout>
</LinearLayout>
Try to add this to your activity in Manifest.xml
<activity
android:windowSoftInputMode="stateVisible|adjustNothing">
</activity>
okay so I actually solved this myself, but I think this is not more than a hack so I might be revisit this at a later point. but the point is it works (tested this on a Galaxy S4)
I resize my listView according to the height of the displayed softKeyboard and resize it back once the keyboard is gone.
private static boolean keyboardHidden = true;
private static int reduceHeight = 0;
//...
//...
final View decorView = this.getWindow().getDecorView();
decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect rect = new Rect();
decorView.getWindowVisibleDisplayFrame(rect);
int displayHeight = rect.bottom - rect.top;
int height = decorView.getHeight();
boolean keyboardHiddenTemp = (double)displayHeight / height > 0.8 ;
int mylistviewHeight = mylistview.getMeasuredHeight();
if (keyboardHiddenTemp != keyboardHidden) {
keyboardHidden = keyboardHiddenTemp;
if (!keyboardHidden) {
reduceHeight = height - displayHeight;
LinearLayout.LayoutParams mParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,mylistviewHeight - reduceHeight);
mylistview.setLayoutParams(mParam);
mylistview.requestLayout();
} else {
LinearLayout.LayoutParams mParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,mylistviewHeight + reduceHeight);
mylistview.setLayoutParams(mParam);
mylistview.requestLayout();
}
}
}
});
Add this to your activity
<activity
android:windowSoftInputMode="adjustResize"
</activity>
But for this to effect put the height of listview fill_parent

Android - horizontal images to fit screen

I have an Android horizontal LinearLayout with 5 images in it. I want the 5 images to fit the screen horizontally while keeping the aspect ratio. Each image is 128x128 pixel.
The problem is that while the width is set like what I wanted the height of the images remain the same, so they look like this on a bigger screen (this is the Nexus 7):
What I have now:
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:cropToPadding="true"
android:scaleType="fitXY"
android:src="#drawable/image01" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/image02" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/image03" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/image04" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/image05" />
</LinearLayout>
Thanks for the help
change the scaleType to centerInside or fitCenter and height to match parent.
try to surround the imageview with a horizontal LinearLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_gravity="center">
<ImageView
android:id="#+id/btnHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="btnhome" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_gravity="center">
<ImageView
android:id="#+id/btnGames"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="btngames" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_gravity="center">
<ImageView
android:id="#+id/btnNews"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="btnnews"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_gravity="center">
<ImageView
android:id="#+id/btnPayment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="btnpayment"/>
</LinearLayout>
then, change the scaleType to fitCenter.
it will cause your pictures to have a correct ratio event you view it in a wider screen.
I found some code to create a square view that I could then use to render my own custom graphics in. You should be able to something like this:
public class SquareImageView extends ImageView {
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
int height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
int length = Math.min(width, height);
setMeasuredDimension(length, length);
}
}
I haven't tried this squaring code in a LinearLayout but when use RelativeLayout where it was positioned first and attached to three sizes of the parent it worked nicely. This is hoping that ImageView does the right thing with the image, otherwise you're going to have to override the onDraw() method too.

Every View stick together when loop LayoutInflater

I have a layout problem. my code have a LayoutInflater set the following xml as layout, and will loop few time to show my school time table. Eventhough i have set the relativelayout's marginBottom and Top to 50dp, but why every layout still stick together?
here's the xml:
<RelativeLayout
android:id="#+id/left_part"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TextView
android:id="#+id/table_day"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_above="#+id/table_date"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="#string/table_day"
android:textSize="#dimen/table_day_size" />
<TextView
android:id="#+id/table_date"
android:layout_width="match_parent"
android:layout_height="14dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="#string/table_date"
android:textSize="#dimen/table_date_size" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/center_part"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/left_part"
android:layout_alignTop="#+id/left_part"
android:layout_toRightOf="#+id/left_part" >
<RelativeLayout
android:id="#+id/center_part1"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<TextView
android:id="#+id/table_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="#string/table_time"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="#dimen/table_timelocationclass_size" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/center_part2"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/center_part1" >
<TextView
android:id="#+id/table_subject"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:padding="1dp"
android:gravity="center_vertical"
android:text="#string/table_subject"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="#dimen/table_subject_size" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/center_part3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/center_part2" >
<TextView
android:id="#+id/table_lecturer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="#string/table_lecturer"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="#dimen/table_lecturer_size" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/right_part"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/center_part"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/center_part"
android:layout_toRightOf="#+id/center_part" >
<RelativeLayout
android:id="#+id/right_part1"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<TextView
android:id="#+id/table_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="#string/table_location" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/right_part2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/right_part1" >
<TextView
android:id="#+id/table_class"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="#string/table_class" />
</RelativeLayout>
</RelativeLayout>
Here is the print screen:
here is the loop:
LinearLayout ll = (LinearLayout)findViewById(R.id.main_table);
ll.removeAllViews();
//set table layout
for(int i=0;i<tableRow.size();i++)
{
LayoutInflater inflater = getLayoutInflater();
View perClassTable = inflater.inflate(R.layout.perclass_table, null);
TextView table_day = (TextView)perClassTable.findViewById(R.id.table_day);
TextView table_date = (TextView)perClassTable.findViewById(R.id.table_date);
TextView table_time = (TextView)perClassTable.findViewById(R.id.table_time);
TextView table_location = (TextView)perClassTable.findViewById(R.id.table_location);
TextView table_class = (TextView)perClassTable.findViewById(R.id.table_class);
TextView table_subject = (TextView)perClassTable.findViewById(R.id.table_subject);
TextView table_lecturer = (TextView)perClassTable.findViewById(R.id.table_lecturer);
table_day.setText(date[i].substring(0, 3));
table_date.setText(date[1].substring(4,13));
table_time.setText(time[i]);
table_location.setText(loca[i]);
table_class.setText(clas[i]);
table_subject.setText(subj[i]);
table_lecturer.setText(lect[i]);
//testing
ll.addView(perClassTable);
}
Set some LayoutParams with some margins for your inflated view at the end of the for loop:
//...
table_subject.setText(subj[i]);
table_lecturer.setText(lect[i]);
// set some proper LayoutParams for the inflated View which is going to be added
// to the ll LinearLayout
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
// set some margins to distance the rows
lp.topMargin = 100;
lp.bottomMargin = 100;
// add the inflated view with to ll with the LayoutParams above.
ll.addView(perClassTable, lp);
}
The LayoutParams is a special class designed to be use with a ViewGroup subclass which generally holds layout attributes like width/height, margins, layout positioning rules etc.
Also, you could improve the layout file by removing all those wrappers RelativeLayouts and set the TextViews directly in the parent RelativeLayout. If the number of rows is big you may also want to have a look at the ListView widget as galex said in his answer.
A RelativeLayout evaluates it's children from top to bottom. So, if you use something like android:layout_above="#+id/table_date", it must come after table_date.
<RelativeLayout
android:id="#+id/left_part"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TextView
android:id="#+id/table_date"
android:layout_width="match_parent"
android:layout_height="14dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="#string/table_date"
android:textSize="#dimen/table_date_size" />
<TextView
android:id="#+id/table_day"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_above="#+id/table_date"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="#string/table_day"
android:textSize="#dimen/table_day_size" />
</RelativeLayout>
Here, I have swapped the textviews.
Looks like a list to me, look into ListView.
It will even recycle views if you implement your adapter correctly!

Categories

Resources