How can I programatically create a ScrollView with RelativeLayout boxes like this? - android

I am trying to create an Activity with a ScrollView in it. In the ScrollView will be "boxes" (sort of like a button) that is just a bit smaller than the width of the parent view and spaced slightly apart vertically. Because the boxes will have a couple different size text in a couple areas on them, I think I need to make the out of RelativeLayouts. (I would post a picture but can't with my reputation point so hopefully this makes sense).
I tried create an XML for the activity and than programatically adding the relativeviews. I got it to work, sort of, but am having trouble with the box spacing.
My test XML looks like this...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- scrolling section -->
<ScrollView
android:layout_width="fill_parent"
android:paddingTop="5dp"
android:layout_height="0dp"
android:layout_weight="1.30"
>
<LinearLayout
android:id="#+id/llScroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
</ScrollView>
</LinearLayout>
My Activity looks like this...
public class TestActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
LinearLayout llAccts=(LinearLayout)findViewById(R.id.llScroll);
for (int i=1;i<20;i++) {
RelativeLayout rlView = new RelativeLayout(this);
rlView.setBackgroundColor(Color.BLUE);
TextView test=new TextView(this);
test.setText("Test #"+i);
test.setTextColor(Color.WHITE);
rlView.addView(test);
llAccts.addView(rlView,new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
}
}
I get the scrollable section, no problem. Problem is my blue "boxes" appear to be touching one another and I can't figure out how to make them look like buttons with padding left and right and between them. I tried
rlView.setPadding(20,20,20,20);
right after the setBackground color. All that appears to do is pad the text... not the RelativeViews.
Any suggestions no how to get this to work?
Thanks
===========
NEW INFORMATION:
Here is some additional information after following some suggestions and trial an error. I was also able to post a pic for easier reference.
===========
What I am trying to achieve, programmatically, is something that looks like this.
I achieved this example using the straight XML which looks like this...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- header: non-scrolling -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UPDATE ALL"
android:id="#+id/btnUpdate" />
</RelativeLayout>
<!-- scrolling bottom pane -->
<ScrollView
android:layout_width="fill_parent"
android:paddingTop="5dp"
android:layout_height="0dp"
android:layout_weight="1.30"
>
<LinearLayout
android:id="#+id/llGroups"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
// individual groups start here - this section will be controlled by a database
// #1 group
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:background="#color/blue"
android:padding="5dp"
android:clickable="true"
android:id="#+id/grp1"
>
<TextView
android:id="#+id/text1"
android:text="Item #1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/leftext1"
android:text="123.00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="30dp" />
</RelativeLayout>
// #2 group
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#color/blue"
android:padding="5dp"
android:clickable="true"
android:id="#+id/grp2"
>
<TextView
android:id="#+id/text2"
android:text="Item #2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/leftext2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/leftext2"
android:text="456.00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="30dp"
/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
I took this apart and created an XML file and and activity that will programmatically create the middle groups.
The modified XML (activity_main2) looks like this...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- header: non-scrolling -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UPDATE ALL"
android:id="#+id/btnUpdate" />
</RelativeLayout>
<!-- scrolling bottom pane -->
<ScrollView
android:layout_width="fill_parent"
android:paddingTop="5dp"
android:layout_height="0dp"
android:layout_weight="1.30"
>
<LinearLayout
android:id="#+id/llGroups"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
</ScrollView>
</LinearLayout>
I started rewriting my activity code to test some of the suggestions. It currently looks like this...
public class Main2Activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
LinearLayout llGroups=(LinearLayout)findViewById(R.id.llGroups);
for (int i=1;i<20;i++) {
RelativeLayout rlView = new RelativeLayout(this);
rlView.setBackgroundColor(Color.BLUE);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.setMargins(10,10,10,0);
TextView test=new TextView(this);
test.setText("Test #"+i);
test.setTextColor(Color.WHITE);
rlView.addView(test);
// other textviews will go here
llGroups.addView(rlView,rlp);
}
}
}
This compiles and runs, but the setMargins is not working. My screen has the "UPDATE ALL" button at the top like it should and (19) "Text #" with a blue background, but they are full width and touching one another so it is a solid blue background.
I am getting close, but not sure how to get the margins to set correctly for the rlView relativeview (not the textviews).
Hopefully this helps explain what I am seeing now..
SOLUTION
After suggestions, and some trial and error... here is what I had to do to achieve the desired layout in case someone else is interested.
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams tvp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams tvp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
LinearLayout mTvContainer = (LinearLayout) findViewById(R.id.llAccts);
for (int i = 0; i < 20; i++) {
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundColor(Color.BLUE);
RelativeLayout rl = new RelativeLayout(this);
TextView tv = new TextView(this);
tv.setTextColor(Color.WHITE);
tv.setPadding(5, 5, 5, 5);
tv.setText("Test #" + i);
tvp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
TextView tv2 = new TextView(this);
tv2.setTextColor(Color.WHITE);
tv2.setPadding(5, 5, 5, 5);
tv2.setText(String.valueOf(i));
tv2.setTextSize(30);
tvp2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rl.addView(tv, tvp);
rl.addView(tv2,tvp2);
ll.addView(rl);
llp.setMargins(32,16,32,16);
mTvContainer.addView(ll,llp);
}
}
For each entry in the FOR, I had to use a LINEARLAYOUT to be able to set the margins properly, and in that LINEARLAYOUT I had to use a RELATIVELAYOUT to use the addRules to get the proper positioning of the TextViews.
If there is an easier way, let me know. Thanks for everyones help!!

You are using too much nested layouts needed, here is stripped version.
xml:
<ScrollView 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">
<LinearLayout
android:id="#+id/ll_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</ScrollView>
Activity code:
mTvContainer = (LinearLayout) findViewById(R.id.ll_main);
int p = (int) getResources().getDimension(R.dimen.activity_horizontal_margin);
for (int i = 0; i < 20; i++) {
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(this);
tv.setBackgroundColor(Color.BLUE);
tv.setTextColor(Color.WHITE);
tv.setPadding(p, p / 2, p, p / 2);
tv.setText("TextView " + i);
TextView tv2 = new TextView(this);
tv2.setBackgroundColor(Color.RED);
tv2.setTextColor(Color.WHITE);
tv2.setPadding(p, p / 2, p, p / 2);
tv2.setText("TextView " + i);
ll.addView(tv);
ll.addView(tv2);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(p, p / 2, p, p / 2);
mTvContainer.addView(ll, params);
}

Related

Adding Image views to the horizontal scroll dynamically (through code)

I just started working on android a day ago and I'm working on scrolls. I have already made one but I would now like to do the same thing dynamically.
This is the code for my activity_main.xml
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="horizontal">
<LinearLayout
android:id= "#+id/linearlayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:onClick="onTouch">
</LinearLayout>
</HorizontalScrollView>
<LinearLayout
android:id="#+id/bottomlinear"
android:layout_width="match_parent"
android:layout_height="400px"
android:gravity="center"
android:background="#00ffff"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="46dp">>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/drop"
android:textSize="30sp"
android:text="Drop Zone" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Total"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Success"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Fail"
android:textSize="20sp" />
</LinearLayout>
Basically I would like to add 10 images from the drawable into the horizontal scroll as image views dynamically. Any help or ideas are much appreciated.
You can just do like this.
Create an xml layout which holds the imageView.
image_item.xml
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="200dp"
android:layout_height="200dp"
android:visibility="visible"
android:adjustViewBounds="true"
/>
Now find the container where want into java file, like this.
LinearLayout containerLayout = (LinearLayout)findViewById(R.id.linearlayout1);
Now just run a for loop till 10 and add the views at runtime.
for(int a = 0 ; a < 10 ; a++)
{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflatedView = inflater.inflate(R.layout.image_item, null);
containerLayout.addView(inflatedView);
}
Hope this helps, Feel free to discuss if found problem.
Happy Coding :-)
I solved the problem. I'm gonna post the solution hoping that it helps someone else having a similar issue. I originally made 3 horizontal scroll views and the xml for one of them is like this
<HorizontalScrollView
android:id="#+id/HorizontalScrollView1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginBottom="1dp"
android:background="#FFF"
android:scrollbars="none">
<LinearLayout
android:id="#+id/imgLayout1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
The code I wrote to create Image Views inside Linear Layout of horizontal scroll view:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int j=1; j<=10; j++)
{
b1=j;
create_img1("drawable/a"+j, b1);
}
}
void create_img1(String ss, int ID)
{
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.imgLayout1);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(200, 200);
parms.gravity = Gravity.CENTER;
parms.setMargins(20, 20, 20, 20);
final ImageView imageView = new ImageView(this);
imageView.setLayoutParams(parms);
int id = getResources().getIdentifier(ss, "id", getPackageName());
imageView.setImageResource(id);
linearLayout.addView(imageView);
imageView.setId(ID);
}
I made one for multiple scroll views with drag and drop functionality but I filtered it out and if you want to create image views inside scroll views dynamically, this is what you are looking for. Hope this helps someone else with a similar issue.
I think you are trying to do something like this:
mHScrollContentView = (ViewGroup) findViewbyId(R.id.linearlayout1);
ImageView iv1 = new ImageView(this);
iv.setImageResource(R.drawable.image_1);
ImageView iv2 = new ImageView(this);
iv.setImageResource(R.drawable.image_2);
ImageView iv3 = new ImageView(this);
iv.setImageResource(R.drawable.image_3);
ImageView iv4 = new ImageView(this);
iv.setImageResource(R.drawable.image_4);
mHScrollContentView.addView(iv1);
mHScrollContentView.addView(iv2);
mHScrollContentView.addView(iv3);
mHScrollContentView.addView(iv4);
http://sandyandroidtutorials.blogspot.in/2013/06/horizontal-listview-tutorial.html
You might get an answer here!

Different view edittext in Android

When I create EditText programmatically I have no underline (in the second EditText). Like this: http://oi41.tinypic.com/2ut427d.jpg
When I create an EditText in XML everything is fine, see http://oi44.tinypic.com/nmxdnm.jpg. Where is the error?
This is my XML layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
<include layout="#layout/input_title_layout" />
<TableLayout
android:id="#+id/relativeLayout12"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp" >
<TextView
style="#style/tvTitleStyle"
android:text="#string/diameter" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text=":" />
<EditText
android:id="#+id/diameterValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:digits="0123456780."
android:inputType="numberDecimal" />
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
And code to add EditText programatically:
final TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT, 1f);
final TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
rowParams.bottomMargin = 0;
TableLayout tableLayout = new TableLayout(context);
tableLayout.setLayoutParams(tableParams);
inputLinearLayout.addView(tableLayout);
TableRow tableRowEdit = new TableRow(context);
TextView titleTv = new TextView(context);
titleTv.setTextAppearance(context, R.style.tvTitleStyle);
titleTv.setText(context.getString(context.getResources().getIdentifier(inputTvs[i] , "string", context.getPackageName())));
TextView separatorTv = new TextView(context);
separatorTv.setGravity(Gravity.LEFT);
separatorTv.setPadding(5, 0, 5, 0);
separatorTv.setText(":");
separatorTv.setTextColor(Color.BLACK);
EditText editText = new EditText(context);
editText.setId(i);
tableRowEdit.addView(titleTv);
tableRowEdit.addView(separatorTv);
tableRowEdit.addView(editText);
tableLayout.addView(tableRowEdit);
It's hard to tell exactly what the issue might be given the code you supplied.
-Have you tried setting the layout parameters of the TableRow/TextView/EditText? The TextViews might be taking up all the visible space.
-Are you sure inputLinearLayout has been inflated?

How can I give top blank when I add textview programmatically?

I am doing an app like whatsup. My question is about interface. I can get a message desc and parse it. After that I can add layout programmatically but it is writing same coordinate all the time. I tried tv.layout(l, t, r, b) and tv.setTop but doesn't work.
Where is my issue?
bubbleLayout=(RelativeLayout)findViewById(R.id.layoutbubble);
for(int i=0;i<msgid.length;i++){
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
// Creating a new TextView
TextView tv = new TextView(this);
tv.setText(msgdesc[i]);
tv.setTop(20);
// Defining the layout parameters of the TextView
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
// Setting the parameters on the TextView
tv.setLayoutParams(lp);
// Adding the TextView to the RelativeLayout as a child
bubbleLayout.addView(tv);
My XML File:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="1300dip"
android:layout_height="80dip"
android:background="#084B8A" >
<TextView
android:id="#+id/messagetitle"
android:layout_width="wrap_content"
android:layout_height="90dip"
android:layout_centerInParent="true"
android:layout_marginTop="30dip"
android:textColor="#ffffff"
android:textSize="12pt" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/layoutbubble"
android:layout_width="1300dip"
android:layout_height="500dip"
android:layout_marginTop="10dip" >
</RelativeLayout>
<RelativeLayout
android:layout_width="1300dip"
android:layout_height="80dip"
android:layout_marginTop="10dip" >
<EditText
android:id="#+id/replytext"
android:layout_width="1000dip"
android:layout_height="90dip"
android:layout_marginLeft="10dip" />
<Button
android:id="#+id/sendbutton"
android:layout_width="200dip"
android:layout_height="90dip"
android:layout_marginLeft="1050dp"
android:background="#084B8A"
android:text="Send"
android:textColor="#ffffff"
android:textSize="12pt" />
</RelativeLayout>
</LinearLayout>
Screen Shot
I solved my problem.Changed my RelativeLayout to LinearLayout in the xml
<LinearLayout
android:id="#+id/layoutbubble"
android:layout_width="1300dip"
android:layout_height="500dip"
android:layout_marginTop="10dip"
android:orientation="vertical">
</LinearLayout>
And change my code as follows:
bubbleLayout=(LinearLayout)findViewById(R.id.layoutbubble);
for(int i=0;i<msgid.length;i++){
// Creating a new TextView
TextView tv = new TextView(this);
tv.setText(msgdesc[i]);
tv.setPadding(30, 10, 0, 0);
bubbleLayout.addView(tv);
}
That is it.
I think you are using RelativeLayout or a normal LinearLayout?
Try using a LinearLayout with android:orientation="vertical"

how to make the textview visible in button click event in android

I developed one Simple app, which contains one textview.and my problem is that I want visible in invisible this text view on button click event.
At loadtime I do this
myTextView.setVisible(View.GONE);
and after this at Button Click event, I do this.
myTextView.setVisible(View.VISIBLE);
The textview is visible but it is overlap on below TextView means myTextView cannot contain space. So what can I do now?
use
myTextView.setVisible(View.INVISIBLE);
instead of
myTextView.setVisible(View.GONE);
to persist the space in layout.........
If you are using relativelayout, specify android:layout_below="id_of_above_text_view" in the second textview.
If you dont specify the relation to the views in relative layout, it will appear one above other
try this:
RelativeLayout layout = new RelativeLayout(this);
TextView tv1 = new TextView(this);
tv1.setText("A");
tv1.setId(1);
TextView tv2 = new TextView(this);
tv2.setText("B");
tv2.setId(2);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.BELOW, tv1.getId());
layout.addView(tv1);
layout.addView(tv2, lp);
and when you change visibility, call invalidate() on parent view of myTextView (layout here) or root view of your layout ( layout.getRootView() )
A slight change in your code should make it work fine I believe.
myTextView.setVisibility(View.INVISIBLE);
and after the button click,
myTextView.setVisibility(View.VISIBLE);
For doing it in the java code, have a go at this(haven't tried it out myself though)...
RelativeLayout rl = new RelativeLayout(this);
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.xxx);
TextView txt = new TextView(this);
txt.setText("XXX");
rl.addView(iv,0);
rl.addView(txt,1);
This will show and hide the text(s) when the corresponding buttons are pressed.
Very useful if you want to control multiple text with buttons
1)XML File:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="vertical"
android:gravity="center_vertical">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:text="The Origin"
android:id="#+id/btnOrigin"
android:onClick="buttonOnClick"/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:id="#+id/txtOrigin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/about"
android:textColor="#000"
android:textSize="#dimen/text_body"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:visibility="gone"/>
</TableRow>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="vertical"
android:gravity="center_vertical">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:text="Vision"
android:id="#+id/btnVision"
android:onClick="buttonOnClick"/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:id="#+id/txtVision"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/vision"
android:textColor="#000"
android:textSize="#dimen/text_body"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:visibility="gone"/>
</TableRow>
</LinearLayout>
2) JAVA code
private TextView txtOrigin, txtVision;
public void buttonOnClick(View view) {
switch (view.getId()) {
case R.id.btnOrigin:
txtOrigin = (TextView) findViewById(R.id.txtOrigin);
txtOrigin.setVisibility(View.VISIBLE);
txtVision = (TextView) findViewById(R.id.txtVision);
txtVision.setVisibility(View.INVISIBLE);
break;
case R.id.btnVision:
txtVision = (TextView) findViewById(R.id.txtVision);
txtVision.setVisibility(View.VISIBLE);
txtOrigin = (TextView) findViewById(R.id.txtOrigin);
txtOrigin.setVisibility(View.INVISIBLE);
break;
}
}

Kinda circular dependency in RelativeLayout with embedded ScrollView

Basically, an Activity should display on screen:
1. A logo on top. Must always be displayed. Must center both vertically and horizontally, when choices leaves extra space on screen.
2. A ScrollView at bottom. Hosts vertical LinearLayout, which in turn hosts a set of choices a user can choose from. Must take minimal space necessary to display all choices (So logo is vertically centered). Can take up up to (Screen - Logo) space on screen. Choices are added programmatically.
Now, I have defined it in layout as:
<?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">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:padding="5dp"
android:src="#drawable/logo" />
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#id/imageView"
>
<LinearLayout
android:id="#+id/ChoicesPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</RelativeLayout>
If ImageView is layed out using layout_above=scrollView, ScrollView is layed out before, NOT taking into consideration minimum size of ImageView.
If ScrollView is layed out using layout_below=ImageView, ImageView just stays on top. When there are only 2-3 choices, screen looks pretty ugly.
Is there a way I can satisfy the constraints? Or should I define two different layout xml files, and switch to the correct one programmatically?
<?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">
<ImageView
android:id="#+id/imageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:padding="5dp"
android:src="#drawable/ic_launcher" />
<ScrollView
android:id="#+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/imageView" >
<LinearLayout
android:id="#+id/ChoicesPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_text_1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_button_1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_text_2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_button_2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_text_3"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_button_3"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_text_4"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_button_4"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_text_5"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_button_5"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_text_6"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_button_6"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_button_1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_text_2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_button_2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_text_3"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_button_3"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_text_4"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_button_4"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_text_5"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_button_5"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_text_6"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="scroll_view_1_button_6"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
this seems to work for me. its like 14 items in the scrollview
public class TestActivity extends Activity {
private RelativeLayout mRelativeLayout;
private RelativeLayout mRelativeLayout2;
private ImageView mImageView;
private ScrollView mScrollView;
private TextView TV1;
private TextView TV2;
private TextView TV3;
private TextView TV4;
private Button B1;
private Button B2;
private Button B3;
private Button B4;
private RelativeLayout.LayoutParams lp;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mRelativeLayout = new RelativeLayout(this);
setContentView(mRelativeLayout);
mImageView = new ImageView(this);
mImageView.setId(1);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
mImageView.setImageResource(R.drawable.ic_launcher);
mRelativeLayout.addView(mImageView, lp);
mScrollView = new ScrollView(this);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.BELOW, mImageView.getId());
mRelativeLayout.addView(mScrollView, lp2);
mRelativeLayout2 = new RelativeLayout(this);
int itemcounts = mRelativeLayout2.getChildCount();
mScrollView.addView(mRelativeLayout2);
TV1 = new TextView(this);
TV1.setId(2);
TV1.setTextSize(20);
TV1.setText("Im a Text View. The First");
mRelativeLayout2.addView(TV1);
B1 = new Button(this);
B1.setId(3);
RelativeLayout.LayoutParams bl1 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
bl1.addRule(RelativeLayout.BELOW, TV1.getId());
B1.setText("Im a Button. The First");
B1.setTextSize(25);
mRelativeLayout2.addView(B1, bl1);
TV2 = new TextView(this);
RelativeLayout.LayoutParams tvl2 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tvl2.addRule(RelativeLayout.BELOW, B1.getId());
TV2.setId(4);
TV2.setTextSize(20);
TV2.setText("Im a Text View. The Second");
mRelativeLayout2.addView(TV2, tvl2);
B2 = new Button(this);
B2.setId(5);
RelativeLayout.LayoutParams bl2 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
bl2.addRule(RelativeLayout.BELOW, TV2.getI());
B2.setText("Im a Button. The Second");
B2.setTextSize(25);
mRelativeLayout2.addView(B2, bl2);
TV3 = new TextView(this);
TV3.setId(6);
RelativeLayout.LayoutParams tvl3 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tvl3.addRule(RelativeLayout.BELOW, B2.getId());
TV3.setTextSize(20);
TV3.setText("Im a Text View. The Third");
mRelativeLayout2.addView(TV3, tvl3);
B3 = new Button(this);
B3.setId(7);
RelativeLayout.LayoutParams bl3 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
bl3.addRule(RelativeLayout.BELOW, TV3.getId());
B3.setText("Im a Button. The Third");
B3.setTextSize(25);
mRelativeLayout2.addView(B3, bl3);
TV4 = new TextView(this);
TV4.setId(8);
RelativeLayout.LayoutParams tvl4 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tvl4.addRule(RelativeLayout.BELOW, B3.getId());
TV4.setTextSize(20);
TV4.setText("Im a Text View. The Fourth");
mRelativeLayout2.addView(TV4, tvl4);
if (itemcounts < 4) {
lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
}
if (itemcounts > 4) {
lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
}
}
}
try this

Categories

Resources