This is the layout XML I am using.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/tabTransparent"
android:id="#+id/aLayoutRelative">
<LinearLayout
android:id="#+id/linearLayout8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout7"
android:layout_below="#+id/linearLayout7"
android:layout_marginTop="14dp" >
<Button
android:id="#+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/text_accounts_aHistoryNotes" />
</LinearLayout>
</RelativeLayout>
I want to add the button programmatically in java source. This is what I tried.
RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.aLayoutRelative);
LinearLayout lastLinearLayout = (LinearLayout)findViewById(R.id.linearLayout8);
lastLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.LEFT_OF, lastLinearLayout.getId());
layoutParams.addRule(RelativeLayout.BELOW, lastLinearLayout.getId());
layoutParams.setMargins(0, 14, 0, 0);
linearLayout.setLayoutParams(layoutParams);
Button button9 = new Button(this);
Log.i("tab0", recordTabs.get(0));
button9.setText(""+recordTabs.get(0));
button9.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
linearLayout.addView(button9);
relativeLayout.addView(linearLayout);
But in the view, the button is vertical with no text on it; aligned towards left of the page (in effect towards relative layout). XML version works well. I reckon the orientation settings turn out to be the culprit. Having googled in various documentations hardly helped.
Any thoughts?
Thanks in advance!
What are you trying to achieve?
for start, remove the
layoutParams.addRule(RelativeLayout.LEFT_OF, lastLinearLayout.getId());
so you can at least see the button below.
Why to you place the buttons in linear layouts?
Related
I try to add a Button to my RelativLayout.
It works, but the Buttons are overlapping each other.
I guess I'm overriding the LayoutParams, or I'm using the wrong methods, but I don't know.
This is only a test. In the final version, I want to add an undefined number of Buttons to a scrollView with a relativLayout in it.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Button myButton = new Button(MainActivity.this);
myButton.setText("Push Me");
RelativeLayout ll = (RelativeLayout)findViewById(R.id.relative_main);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
lp.topMargin = R.id.button;
myButton.setLayoutParams(lp);
ll.addView(myButton);
}
}
);
}
}
The XML-File:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.freddy.test.MainActivity"
android:id="#+id/relative_main">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add new Button"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
The buttons are overlapping each other because you are using a RelativeLayout without telling at which place is the button supposed to go relative to the Layout.
Something that may help you would be to use a LinearLayout instead of the RelativeLayout and add the Buttons to it.
Try to use this instead of FrameLayout.LayoutParams
This gives you more control over relative layout children (since your parent layout is a RelativeLayout)
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
You can align to another view by this:
params.addRule(RelativeLayout.BELOW, R.id.putyourviewhere);
This is if you stick to RelativeLayout. But the solution of LinearLayout is ok also. But RelativeLayout has more control
As you are using a RelativeLayout you have to use toRightOF if you want the buttons to be lined horizontally or below if you want the buttons to be lined vertically, so to add any of them programmatically, just add it as a rule to your layoutParams:
lp.addRule(RelativeLayout.RIGHT_OF, R.id.button);
OR
lp.addRule(RelativeLayout.BELOW, R.id.button);
But this is only a test, in the Final version i want to add an
undefined number of buttons to a scrollview with a relativLayout in
it.
If your problem is that
the Buttons are overlapping each other
It is relatively simple to solve. Use <LinearLayout> instead
In your root Tag just add a attribute called android:orientation="vertical" or android:orientation="horizontal".
There are also attributes to solve this for a RelativeLayout but it is a little more complex.
If these are problems u have often, I suggest you take this course.
example:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</ScrollView>
OK, now try to ADD another parameters to control the layout like centering CENTER_HORIZONTAL
params.addRule(RelativeLayout.CENTER_HORIZONTAL, -1); // -1 means that CENTER_HORIZONTAL doesn't take a view as a parameter
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
lp.topMargin = R.id.button;
// use some meaningful margin, R.id.button can return any integer value.
// you have issue with alignment of button
ll.addView(myButton);
// with this call your button should be in view hierarchy now you have to align your button. best is use RelativeLayout.LayoutParams and make your button relative(top/bottom/left/right) to some other component in viewgroup(RelativeLayout).
I've been searching around in Google for a bit but I can't seem to find what I want to do. I want to be able to programatically add an icon as an overlay in an activity at a specified position without using any xml.
An example of what I mean: http://cdn9.staztic.com/app/a/2326/2326236/pollfish-demo-2-1-s-307x512.jpg
Any ideas?
It depends at layout you are using. If you are using RelativeLayout, you can do it this way:
<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:id="#+id/main" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:src="#android:drawable/ic_"/>
</RelativeLayout>
Which is equal with this Java code (except root RelativeLayout):
RelativeLayout layout = (RelativeLayout) findViewById(R.id.main);
ImageView child = new ImageView(this);
child.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
child.setLayoutParams(params);
layout.addView(child);
I am trying to add a text view to an imageview inside a horizontal scrollview programatically. However, this does not seem to work.
Sample Image on in RelativeLayout without scrolling:
Here is a sample image in horizontal scrolling:
Here is my xml layout:
<HorizontalScrollView android:id="#+id/home_horizontal_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/middle_menu_title" >
<LinearLayout android:id="#+id/home_linear_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
Inside my test code:
LinearLayout layout = (LinearLayout)findViewById(R.id.home_linear_layout);
for(int i = 0 ; i < 10; i++){
ImageView myView = new ImageView(this);
myView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
myView.setAdjustViewBounds(true);
myView.setScaleType(ScaleType.FIT_XY);
myView.setPadding(0, 2, 2, 0);
myView.setImageResource(R.drawable.render);
layout.addView(myView);
TextView text = new TextView(this);
text.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 50));
text.setBackgroundColor(Color.parseColor("#4000"));
text.setTextColor(Color.WHITE);
text.setGravity(Gravity.CENTER);
text.setText("Header Title");
layout.addView(text);
I have also tried using Relative Layout inside the horizontal scrollview without any success.
Inside a simple relative layout like below , I am able to display the title and image but not when it is in the horizontal scrollview
<RelativeLayout android:id="#+id/top_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/menu_title"
android:background="#drawable/background_gradient">
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="2dip"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/render" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#4000"
android:gravity="center"
android:text="Image Title"
android:textColor="#FFFFFF" />
</RelativeLayout>
Any advise?
There is a problem in your layout :
you say to the LinearLayout parent view to take a width according to its children :
using android:layout_width="wrap_content"
then you say the children to take a width according to the parent :
using LayoutParams.MATCH_PARENT
you have to understand that it can't give a predictible result since they both depend to each other.
I think if you set the width to LayoutParams.WRAP_CONTENT on the children it will solve the issue :
ImageView myView = new ImageView(this);
myView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
myView.setAdjustViewBounds(true);
myView.setScaleType(ScaleType.FIT_XY);
myView.setPadding(0, 2, 2, 0);
myView.setImageResource(R.drawable.render);
layout.addView(myView);
TextView text = new TextView(this);
text.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 50));
text.setBackgroundColor(Color.parseColor("#4000"));
text.setTextColor(Color.WHITE);
text.setGravity(Gravity.CENTER);
text.setText("Header Title");
layout.addView(text);
EDIT : seen the edit from the question, LinearLayout can't be the good answer because it doesn't allow children overlapping.
You can easily add an image to a TextView without putting it in a new parent layout by using the compoud drawables :
myTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.left, 0, 0, 0);
Put 0 to remove drawable,
You can put a drawable on any side (left, top, right, bottom)
see the documentation here, it may help you.
the size of the drawable has to match your needs since it use (as it says) the drawable intrinsic bounds.
if you don't have any other view in your LinearLayout than a image and a text, it's advised to use compound drawables for optimizations.
EDIT : seen the edit in the question : the compound drawable can't be the answer if you need to overlap your image and your text.
On a Dialog I want a MATCH_PARENT EditText left of a ImageButton with WRAP_CONTENT.
This would look like the picture below.
Layout picture http://goo.gl/dxuS5
I dont know how to solve this problem!
It's must programming only in java! I have no chance to write this in XML.
I have just use with LinearLayout. But the MATCH_PARENT swaps the ImageButton with WRAP_CONTENT.
Also I use RelativeLayout but this doesn't looks like so I want.
try this:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageButton
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:layout_gravity="left"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Assuming you are putting these into a horizontally oriented LinearLayout:
EditText temp = new EditText(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); LayoutParams.WRAP_CONTENT, 1);
temp.setLayoutParams(params);
ImageView imageView = new ImageView(this);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(vp);
imageView.setImageResource(id);
someLinearLayout.addView(temp);
someLinearLayout.addView(imageView);
Problem solved
now i want to define one edit text to left and other to right...
i tried doing that but didnt work.. when i do fill_parent.. it just show one edit text.. on one line where it shows both of them side by side when i do wrap_content..
now what i want to do is.. have both edit text boxes define certain size and position left and right.. which i tried implementing didnt work??
You need to change the layout from vertical to horizontal in your layout XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
...
You can also position elements within the layout using the gravity setting. So to position the button in the middle you could use:
<Button android:layout_width="wrap_content"
android:gravity="center"
...
Update: to put two buttons in each row, side by side, simply nest the LinearLayout elements, e.g.
<LinearLayout android:orientation="vertical" ...>
<LinearLayout android:orientation="horizontal" ...>
<Button ... />
<Button ... />
</LinearLayout>
<LinearLayout android:orientation="horizontal" ...>
<Button ... />
<Button ... />
</LinearLayout>
</LinearLayout>
Just create a new linear layout, add the EditTexts to that, then add the new linear layout to your existing layout.
public void onClick(View v) {
EditText t = new EditText(PlusbuttonActivity.this);
EditText a = new EditText(PlusbuttonActivity.this);
LinearLayout l = new LinearLayout(PlusbuttonActivity.this);
t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
a.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
l.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
l.addView(t);
l.addView(a);
addViewToRoot(l);
}
Basically, you're just reproducing the same structure you would need to produce the layout in your XML.