Add multiple views in ScrollView? - android

I am working on an Android App. I have some problem..
Here is my app :
The pink rectangle is a ScrollView. I want to add TextView to the same ScrollView. I know I need a LinearLayout and add my Views inside this layout. After I need to add the LinearLayout to my ScrollView. Here is my ScrollView class with my methods setClassicLabel to implement TextViewand setClassicButton to implement Button in my ScrollView.
Here is my ScrollView.java :
public class MyScrollView extends ScrollView {
private JSONParser jParser;
private JSONObject contenuScrollView;
private Context context;
#SuppressLint("NewApi")
public MyScrollView(Context context, JSONArray listScrollView) throws JSONException {
super(context);
this.context = context;
}
public void onCreate(Bundle savedInstanceState) throws JSONException {
}
/* Création de TextView */
public void setClassicLabel(JSONArray listLabel, LinearLayout ll) throws JSONException {
if (listLabel.length() > 0)
{
DisplayMetrics metrics = new DisplayMetrics();
ll = new LinearLayout(getContext());
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams paramsll = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < listLabel.length(); i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) (getContext().getResources().getDisplayMetrics().widthPixels * listLabel.getJSONObject(i).getDouble("size_x")),
(int) (getContext().getResources().getDisplayMetrics().heightPixels * listLabel.getJSONObject(i).getDouble("size_y")));
params.leftMargin = (int) (getContext().getResources().getDisplayMetrics().widthPixels * listLabel.getJSONObject(i).getDouble("position_x"));
params.topMargin = (int) (getContext().getResources().getDisplayMetrics().heightPixels * listLabel.getJSONObject(i).getDouble("position_y"));
TextView tv = new TextView(context);
tv.setText(listLabel.getJSONObject(i).getString("text"));
tv.setTextSize(listLabel.getJSONObject(i).getInt("police_size"));
tv.setTextColor(Color.parseColor(listLabel.getJSONObject(i).getString("colortext")));
tv.setBackgroundColor(Color.parseColor(listLabel.getJSONObject(i).getString("color")));
ll.addView(tv, params);
}
this.addView(ll, paramsll);
}
}
#SuppressWarnings("deprecation")
public void setClassicButton(JSONArray listButton, LinearLayout ll) throws JSONException {
if (listButton.length() > 0)
{
DisplayMetrics metrics = new DisplayMetrics();
ll = new LinearLayout(getContext());
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams paramsll = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < listButton.length(); i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) (getContext().getResources().getDisplayMetrics().widthPixels * listButton.getJSONObject(i).getDouble("size_x")),
(int) (getContext().getResources().getDisplayMetrics().heightPixels * listButton.getJSONObject(i).getDouble("size_y")));
params.leftMargin = (int) (getContext().getResources().getDisplayMetrics().widthPixels * listButton.getJSONObject(i).getDouble("position_x"));
params.topMargin = (int) (getContext().getResources().getDisplayMetrics().heightPixels * listButton.getJSONObject(i).getDouble("position_y"));
int dest = listButton.getJSONObject(i).getInt("dest");
MyButton myButton = new MyButton(context, dest);
RoundRectShape rect = new RoundRectShape(
new float[] {50,50, 50,50, 50,50, 50,50},
null,
null);
ShapeDrawable bg = new ShapeDrawable(rect);
bg.getPaint().setColor(Color.parseColor(listButton.getJSONObject(i).getString("color")));
myButton.setBackgroundDrawable(bg);
myButton.setTextColor(Color.parseColor(listButton.getJSONObject(i).getString("colortext")));
//BitmapDrawable bdra = new BitmapDrawable(downloadBitmap(listButton.getJSONObject(i).getString("http")));
myButton.setText(listButton.getJSONObject(i).getString("text"));
myButton.getBackground().setAlpha(30);
myButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
MyButton button = (MyButton)view;
Intent intent = new Intent(context, ClassicView.class);
Content content = new Content(button.getDestination());
intent.putExtra("CONTENT", content);
context.startActivity(intent);
}
});
ll.addView(myButton, params);
}
this.addView(ll, paramsll);
}
}
}
MyScrollView heritates from ScrollView. The problem is the next :
When I create my ScrollView with the method setClassicScrollView, i call my methods setClassicLabel and setClassicButton to implements theses two views in my ScrollView. But i can only set Button or Label. I can't set one label and one button. I must have a problem who comes from a layout.. I am on it for two days
Here is my method setClassicScrollView :
public void setClassicScrollView(JSONArray listScrollView, RelativeLayout rl) throws JSONException {
if (listScrollView.length() > 0) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
for (int i = 0; i < listScrollView.length(); i++) {
MyScrollView myScrollView = new MyScrollView(this, listScrollView);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams paramsll = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) (metrics.widthPixels * listScrollView.getJSONObject(i).getDouble("size_x")), (int) (metrics.heightPixels * listScrollView.getJSONObject(i).getDouble("size_y")));
params.leftMargin = (int) (metrics.widthPixels * listScrollView.getJSONObject(i).getDouble("position_x"));
params.topMargin = (int) (metrics.heightPixels * listScrollView.getJSONObject(i).getDouble("position_y"));
myScrollView.setBackgroundColor(Color.RED);
myScrollView.setLayoutParams(params);
myScrollView.setHorizontalScrollBarEnabled(true);
myScrollView.getMaxScrollAmount();
myScrollView.getBackground().setAlpha(50);
//myScrollView.setClassicLabel(listScrollView.getJSONObject(i).getJSONArray("Label"), ll);
myScrollView.setClassicButton(listScrollView.getJSONObject(i).getJSONArray("Button"), ll);
rl.addView(myScrollView);
}
}
}
Someone has an idea of how I can set TextViews and Buttons in the same ScrollView ?

Here's the layout for your scroll view.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Spinner
android:id="#+id/spending_report_cycle_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:id="#+id/SomeView2"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_gravity="center_horizontal"
android:background="#008080"
android:orientation="vertical" />
<fragment
android:id="#+id/fragment_content_1"
android:name="com.mike.passintents.Fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:id="#+id/SomeView2"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_gravity="center_horizontal"
android:background="#008080"
android:orientation="vertical" />
<ListView
android:id="#+id/spending_report_listview"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:background="#333333" >
</ListView>
</LinearLayout>
</ScrollView>
Here.. In the scroll view, I have one linear layout as one child and after that I have other views inside the linear layout.

Related

Scroll horizontally in programmatically created ScrollView

I'm creating a ScrollView, nesting in a RelativeLayout (which contains tables). Everything is properly added to the layout. The problem is: vertically scrolling works fine, but horizontal scrolling does NOT work at all. I already tested various combinations of MATCH_PARENT, WRAP_CONTENT and FILL_PARENT. No try even worked close. So my question is: What am I missing here?
Here is the code:
public class PlayerView extends View {
private RelativeLayout relativeLayout;
private TableLayout tableLayout;
private int player_loop;
private int player_count;
public PlayerView(Context context, int player_count){
super(context);
setPlayer_count(player_count);
}
public ScrollView create_scrollView(){
ScrollView scrollView = new ScrollView(getContext());
ScrollView.LayoutParams scroll_params = new ScrollView.LayoutParams(
ScrollView.LayoutParams.MATCH_PARENT,
ScrollView.LayoutParams.MATCH_PARENT
);
scrollView.setLayoutParams(scroll_params);
scrollView.addView(create_relativeLayout());
return scrollView;
}
public RelativeLayout create_relativeLayout(){
relativeLayout = new RelativeLayout(getContext());
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
relativeLayout.setLayoutParams(relativeParams);
create_Player_Table_Layout();
return relativeLayout;
}
public void create_Player_Table_Layout(){
for(player_loop = 1; player_loop <= player_count; player_loop++){
relativeLayout.addView(player_table(getResources().getString(R.string.dummy_player_name) + player_loop, player_loop));
}
}
public TableLayout player_table(String playername, int playernumber){
tableLayout = new TableLayout(getContext());
tableLayout.setId(playernumber * 1000);
if (playernumber > 1) {
//TABLE PLACEMENT
RelativeLayout.LayoutParams tbl_params_New = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
tbl_params_New.addRule(RelativeLayout.RIGHT_OF, (playernumber - 1) * 1000);
tableLayout.setLayoutParams(tbl_params_New);
}
//Add Playername
TableRow row_playername = new TableRow(getContext());
TextView view_name = new TextView(getContext());
TableRow.LayoutParams view_name_params = new TableRow.LayoutParams();
view_name_params.setMargins(20,20,20,20);
view_name.setLayoutParams(view_name_params);
view_name.setGravity(Gravity.CENTER);
view_name.setText(playername);
view_name.setTextSize(20);
view_name.setId(playernumber * 100);
row_playername.addView(view_name);
tableLayout.addView(row_playername);
//Add Lifepoints
TableRow row_lifepoints = new TableRow(getContext());
TextView view_lifepoints = new TextView(getContext());
TableRow.LayoutParams view_lifepoints_params = new TableRow.LayoutParams();
view_lifepoints_params.setMargins(20, 0, 20, 20);
view_lifepoints.setText("40");
view_lifepoints.setTextSize(40);
view_lifepoints.setGravity(Gravity.CENTER);
view_lifepoints.setId(playernumber * 100 + 10);
view_lifepoints.setLayoutParams(view_lifepoints_params);
row_lifepoints.addView(view_lifepoints);
tableLayout.addView(row_lifepoints);
Log.d("Test", "Player count:" + player_count);
for(int opponent_loop = 1; opponent_loop <= player_count; opponent_loop++){
tableLayout.addView(commander_damage_from_player(player_loop, opponent_loop));
}
return tableLayout;
}
private TableRow commander_damage_from_player(int player_loop, int opponent_loop){
TableRow row = new TableRow(getContext());
TextView textView = new TextView(getContext());
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams();
layoutParams.setMargins(20, 0, 40, 20);
textView.setLayoutParams(layoutParams);
textView.setText("0 | " + getResources().getString(R.string.dummy_player_name)+ " " + opponent_loop);
textView.setTextSize(20);
textView.setId(player_loop + 100 + opponent_loop);
row.addView(textView);
return row;
}
private void setPlayer_count(int player_count){
this.player_count = player_count;
}
}
And for documentation the calling class:
public class Home extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PlayerView playerView = new PlayerView(this, 8);
setContentView(playerView.create_scrollView());
}
}
The only way you can do this is by creating a horizontalscrollview to be the child of the scrollview. If you make the minor modification to your create_relativeLayout() to create a horizontalscroll view then it will work correctly. I have seen this happen in other examples.
public HorizontalScrollView create_relativeLayout(){
HorizontalScrollView horizontalScrollView = new HorizontalScrollView(getContext());
relativeLayout = new RelativeLayout(getContext());
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
relativeLayout.setLayoutParams(relativeParams);
create_Player_Table_Layout();
horizontalScrollView.addView(relativeLayout);
return horizontalScrollView;
}

GridView Dynamically 2:3 layout data set

I am using one one layout and include 5 times on other layout. but i want to user Grid View instance of incluide layout. Here I am sharing Screen shot. please help me to make this layout in android
This is Flipboard layout. and second one is my layout.
GridView does not support this. One has to use a custom linearlayout and add views to it dynamically by setting the layout parameters.
Use the class as layout back bone
public class CompositeView extends LinearLayout {
private ImageView imageView;
private TextView textView;
public CompositeView(Context context) {
super(context);
imageView = new ImageView(context);
imageView.setClickable(true);
imageView.setPadding(3, 3, 3, 3);
LayoutParams params = new LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(params);
imageView.setScaleType(ScaleType.FIT_XY);
textView = new TextView(context);
textView.setTextColor(getResources().getColor(android.R.color.white));
textView.setClickable(true);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
textView.setSingleLine(true);
textView.setEllipsize(TruncateAt.END);
textView.setPadding(3, 3, 3, 3);
textView.setBackgroundResource(R.drawable.strip_bg);
this.setGravity(Gravity.CENTER);
// this.setClickable(true);
this.addView(imageView);
this.addView(textView);
}
public void setText(int resid) {
textView.setText(resid);
}
public void setText(CharSequence text) {
textView.setText(text);
}
public void setIcon(int resid) {
imageView.setImageDrawable(getResources().getDrawable(resid));
}
public ImageView getImageView() {
return imageView;
}
public TextView getTextView() {
return textView;
}
}
In activity or fragment
names = new String[] { "Cloud Bingo", "New Bingo Games", "Politricks",
"Seam Group", "Regular Cloud Bingo" };
urls = new String[] { "http://www.cloudbingo.co.uk/",
"http://www.newbingogames.co.uk/",
"http://www.politricks.co.in", "http://veenagupta.in/",
"http://www.regularbingo.co.uk/" };
for (int i = 0; i < images.length; i++) {
view = new CompositeView(getActivity());
view.setText(names[i]);
// view.setOnClickListener(this);
view.setOrientation(LinearLayout.VERTICAL);
view.setIcon(images[i]);
view.setId(i); // id of whole linear view
view.getTextView().setGravity(Gravity.CENTER);
view.getImageView().setId(i); // id of just this imageview
view.getTextView().setId(i);
}
Use following logic for your purpose
for (int i = 0; i < images.length; i++) {
flag++;
if (flag % 2 == 0) {
// number is even
mainLayout.addView(compositeList.get(i));
}
else {
Log.v("in", "else");
secondaryLayout = new LinearLayout(getActivity());
LayoutParams params = new LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 15, 0, 15);
secondaryLayout.setLayoutParams(params);
secondaryLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout leftLayout = new LinearLayout(getActivity());
leftLayout.setLayoutParams(new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.MATCH_PARENT, 1.0f));
leftLayout.addView(compositeList.get(i));
secondaryLayout.addView(leftLayout);
i++;
if (i < images.length) {
LinearLayout rightLayout = new LinearLayout(getActivity());
LayoutParams rParams = new LayoutParams(0,
LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
rParams.setMargins(10, 0, 0, 0);
rightLayout.setLayoutParams(rParams);
rightLayout.addView(compositeList.get(i));
secondaryLayout.addView(rightLayout);
}
else {
LinearLayout rightLayout = new LinearLayout(getActivity());
LayoutParams rParams = new LayoutParams(0,
LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
rParams.setMargins(10, 0, 0, 0);
rightLayout.setLayoutParams(rParams);
secondaryLayout.addView(rightLayout);
}
mainLayout.addView(secondaryLayout);
}
}
flag is equal to -1 initially , I show one image (with text at bottom) for even rows, two images for odd rows
Here is the layout
<?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="match_parent"
android:background="#drawable/splash_bg"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:layout_weight="5"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp" >
<LinearLayout
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>

Views within custom layout won't show

I'm creating a custom linear layout to hold two image views.
I first thought that the layout is not showing at all but I then set its background to black to check if the layout is being inflated and it did. Then understood that the problem is the image views, they simply not showing.
Thanks in advance :-)
This is the class:
public class LoginIcons extends LinearLayout {
private ImageView mImageViewLogo;
private ImageView mImageViewIcons;
private View mView;
boolean test = false;
public LoginIcons(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public LoginIcons(Context context) {
super(context);
init();
}
private void init() {
setOrientation(LinearLayout.VERTICAL);
mImageViewLogo = new ImageView(this.getContext());
mImageViewIcons = new ImageView(this.getContext());
mView = new View(getContext());
LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mImageViewLogo.setLayoutParams(params);
params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.topMargin = (int)LocalSettingsHelper.dpToPx(getContext(), 20);
mImageViewIcons.setLayoutParams(params);
params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.topMargin = (int)LocalSettingsHelper.dpToPx(getContext(), 10);
mView.setLayoutParams(params);
mImageViewLogo.setImageResource(R.drawable.splash_logo_placeholder);
mImageViewIcons.setImageResource(R.drawable.login_icons);
mImageViewIcons.setBackgroundColor(Color.BLACK);
mImageViewLogo.setBackgroundColor(Color.BLACK);
addView(mView);
addView(mImageViewLogo);
addView(mImageViewIcons);
}
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
setViewsDimensions();
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
}
private void setViewsDimensions() {
LinearLayout parent = (LinearLayout) mImageViewLogo.getParent();
int screenWidth = LocalSettingsHelper.getScreenWidth(getContext());
// 0.375 percent
int imgDim = (int) ((double) screenWidth * 0.32);
int iconsWidth = (int) ((double) screenWidth * 0.5);
LinearLayout.LayoutParams params = (LayoutParams) mImageViewLogo
.getLayoutParams();
params.width = imgDim;
params.height = imgDim;
mImageViewLogo.setLayoutParams(params);
params = (LayoutParams) mImageViewIcons.getLayoutParams();
params.width = iconsWidth;
mImageViewIcons.setLayoutParams(params);
}
}
And this is the 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="match_parent"
android:background="#color/HeaderBackground"
android:orientation="vertical" >
<Button
android:id="#+id/button_ok"
style="#style/LoginOkButtonStyle"
android:background="#drawable/blue_button_background_selector"
android:text="#string/ok" />
<com.example.me.entities.views.LoginIcons
android:id="#+id/loginIcons"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</com.example.me.entities.views.LoginIcons>
</LinearLayout>
It seems that the View object acted like match_parent and had height of nearly 400 pixels. This caused the image views to drop outside the boundaries of the screen.
Once I removed the View the image views turned up.

Add a dynamic tablelayout in a relativelayout above a existing button

i'm trying to add a tablelayout in a relativelayout, but i'm havind some problems to put this tablelayout above a existing button, i can only but it below it.
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
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:stretchColumns="0,1"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Button" />
the lp.addRule(RelativeLayout.ABOVE, idx); isn't working
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the size of the screen to set the size of the buttons according
// to the level
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int level = 3;
int width = size.x - (size.x / level);
int btsize = width / level;
// Get the main Layout
RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.main);
//
TableLayout tLayout = new TableLayout(this);
int id = 0;
for (int i = 0; i < level; i++) {
// Create the TableRow
TableRow tRow = new TableRow(this);
for (int j = 0; j < level; j++) {
id++;
Button bt = new Button(this);
bt.setId(id);
bt.setWidth(btsize);
bt.setHeight(btsize);
bt.setMinimumWidth(0);
bt.setMinimumHeight(0);
tRow.addView(bt); // add button into the row
}
tLayout.addView(tRow);// add row into the table
}
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
int idx = findViewById(R.id.button1).getId();
lp.addRule(RelativeLayout.ABOVE, idx);
rLayout.addView(tLayout,lp);// add table into the relativelayout
}
you can try this...
put this code in your oncreate() method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
relay = (RelativeLayout)findViewById(R.id.rlayout);
LayoutParams layoutprams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
TableLayout table=new TableLayout(this);
relay.addView(table);

Android RelativeLayout problem

I have some problem with RelativeLayout positioning:
Now I have this: http://dl.dropbox.com/u/18411570/now.png
And I would like this: http://dl.dropbox.com/u/18411570/expectation.png
Here's my code:
public class ActivityTwo extends Activity {
RelativeLayout myRelativeLayout = null;
ArrayList<ArrayList<EditText>> spreadsheet = new ArrayList<ArrayList<EditText>>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.relativesprsheet);
myRelativeLayout = (RelativeLayout)findViewById(R.id.relativeLayout);
//FIRSTROW
ArrayList<EditText> firstRow = new ArrayList<EditText>();
char columnTitle = 'A';
int prev_id = 0;
for(int i = 0; i < 5; ++i) {
EditText eText = new EditText(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
eText.setWidth(100);
eText.setId(getUniqueCellId(columnTitle,i + 1));
if(i==0) {
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT,eText.getId());
eText.setText("A1");
}
else {
params.addRule(RelativeLayout.RIGHT_OF,prev_id);
eText.setText(Character.toString(columnTitle)+Integer.toString(1));
}
prev_id = getUniqueCellId(columnTitle,i + 1);
firstRow.add(eText);
myRelativeLayout.addView(eText,params);
columnTitle++;
}
spreadsheet.add(firstRow);
//SECONDROW
ArrayList<EditText> second_row = new ArrayList<EditText>();
EditText a2 = new EditText(this);
a2.setId(getUniqueCellId('A',2));
a2.setWidth(100);
a2.setText("A2");
RelativeLayout.LayoutParams a2params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
a2params.addRule(RelativeLayout.BELOW, getUniqueCellId('A', 1));
myRelativeLayout.addView(a2, a2params);
second_row.add(a2);
EditText bigView = new EditText(this);
bigView.setId(getUniqueCellId('B', 2));
bigView.setWidth(300);
bigView.setText("B2");
RelativeLayout.LayoutParams b2params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
b2params.addRule(RelativeLayout.RIGHT_OF, getUniqueCellId('A', 2));
b2params.addRule(RelativeLayout.ALIGN_TOP,getUniqueCellId('A', 2));
b2params.addRule(RelativeLayout.ALIGN_BOTTOM,getUniqueCellId('A', 4));
b2params.addRule(RelativeLayout.ALIGN_RIGHT,getUniqueCellId('D',5));
myRelativeLayout.addView(bigView, b2params);
second_row.add(bigView);
spreadsheet.add(second_row);
//THIRDROW
ArrayList<EditText> third_row = new ArrayList<EditText>();
EditText a3 = new EditText(this);
a3.setId(getUniqueCellId('A',3));
a3.setWidth(100);
a3.setText("A3");
RelativeLayout.LayoutParams a3params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
a3params.addRule(RelativeLayout.BELOW, getUniqueCellId('A', 2));
myRelativeLayout.addView(a3, a3params);
third_row.add(a3);
spreadsheet.add(third_row);
//FOURTHROW
ArrayList<EditText> fourth_row = new ArrayList<EditText>();
EditText a4 = new EditText(this);
a4.setId(getUniqueCellId('A',4));
a4.setWidth(100);
a4.setText("A4");
RelativeLayout.LayoutParams a4params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
a4params.addRule(RelativeLayout.BELOW, getUniqueCellId('A', 3));
myRelativeLayout.addView(a4, a4params);
fourth_row.add(a4);
spreadsheet.add(fourth_row);
//FIFTHROW
ArrayList<EditText> fifth_row = new ArrayList<EditText>();
EditText a5 = new EditText(this);
a5.setId(getUniqueCellId('A',5));
a5.setWidth(100);
a5.setText("A5");
RelativeLayout.LayoutParams a5params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
a5params.addRule(RelativeLayout.BELOW, getUniqueCellId('A', 4));
myRelativeLayout.addView(a5, a5params);
fifth_row.add(a5);
char c2 = 'B';
for(int i = 1; i < 5; ++i) {
EditText temp5Text = new EditText(this);
temp5Text.setId(getUniqueCellId(c2,5));
temp5Text.setWidth(100);
temp5Text.setText(Character.toString(c2)+"5");
RelativeLayout.LayoutParams temp5Textparams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
temp5Textparams.addRule(RelativeLayout.RIGHT_OF, getUniqueCellId((char)(c2-1), 5));
temp5Textparams.addRule(RelativeLayout.ALIGN_TOP, getUniqueCellId((char)(c2-1), 5));
myRelativeLayout.addView(temp5Text, temp5Textparams);
fifth_row.add(temp5Text);
c2++;
}
spreadsheet.add(fifth_row);
}
private int getUniqueCellId(char c, int i) {
return Integer.valueOf(c) * 10369 + i;
}
}
And here is the layout xml:
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</RelativeLayout>
</ScrollView>
</HorizontalScrollView>
I can't understand, why the first row is broken and the fifth is not, and how could i fix it... please help me if you can, thx!
Edit1: I didn't mention that I would like to do it programmatically Edit2: I changed the links, I hope it will work for everybody
I suggest you use your XML layout file for this. Set up things so they are laid out properly, and then hook up non-layout stuff like click handlers or changing text later.

Categories

Resources