The following XML implementation seems to work fine:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<WebView
android:id="#+id/chatView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<EditText
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
</LinearLayout>
However, the following Java implementation doesn't (there's a tiny space at the bottom after the WebView, but the TextView isn't visible.)
Context mContext = getActivity();
LinearLayout view = new LinearLayout(mContext);
view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
view.setOrientation(LinearLayout.VERTICAL);
WebView web (new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f));
web.loadUrl("http://www.google.com");
TextView text = new TextView(mContext);
text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
view.addView(web);
view.addView(text);
Example:
TextView should be where the black space at the bottom is, but taller of course. Any help would be very appreciated, thanks.
If you're trying to make the WebView resize around the TextView (I'm assuming this is what you want since you have the android:weight property), make sure that you set the height to "0dp" instead of "fill_parent". Otherwise, the WebView WILL fill the parent and your TextView won't be displayed.
In addition, since the TextView's height is set to "wrap_content," you actually need content there if you want to see it. See if it shows up once you set the text.
I think u have to set some text on TextView "text"
Similar like this text.setText("This is text ")
Related
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.
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?
I have in main.xml TitlePageIndicator and ViewPager, and I want to add admob (right into LinearLayout) at the bottom of RelativeLayout. How can I do this?
When I launch it, the log says nothing about errors (since there is no place to put admob), but admob is invisible, I can`t see it. (looks like admob is outside the screen because I tried to set specific sizes to ViewPager and it works perfect)
I do not want to set specific sizes to ViewPager (becouse of the different screen sizes)
Thanks.
My main.xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.viewpagerindicator.TitlePageIndicator
android:id="#+id/indicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/indicator"/>
<LinearLayout
android:id="#+id/for_ads"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/viewpager"/>
</RelativeLayout>
UPD.
solved
I used this answer and it works perfect for me
First, your RelativeLayout needs an ID if you want to reference it:
RelativeLayout rLayout = (RelativeLayout)findViewById(R.id.yourRelativeId);
Then create some LayoutParams for the object (in this case, your admob adview) that tell it to align itself to the bottom (and not align to any other views, this way it isn't pushed off-screen or moved by the other views):
RelativeLayout.LayoutParams rLParams =
new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
rLParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
Next, add the view to your RelativeLayout with your LayoutParams:
rLayout.addView(yourAdView, rLParams);
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.