This is my xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
<Button
android:id="#+id/button_weather9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Πίσω" />
<ImageView
android:id="#+id/weather_icon"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:scaleType="fitXY"
android:src="#drawable/weather1" />
</LinearLayout>
But in my application I want inside my class, to set a new xml dynamically with the same things as above. I do no know what exactly will be the parameters. So far I have written this. Can you help me with it and complete what is missing:
final LinearLayout test2 = new LinearLayout(this);
test2.setOrientation(LinearLayout.VERTICAL);
test2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
final Button Back = new Button(this);
Back.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
Back.setText("Back");
test2.addView(Back);
final LoaderImageView image = new LoaderImageView(this, ImageUrl);
image.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
I am more interested in ScaleType because the image.ScaleType(FILL) doesn't work.
Can you clarify what do you want to achieve. A java code that construct exactly the same layout as the XML? If so why? Or, are do you want to modify at run time some aspect of the XML layout, if so what aspect?
Possibly there are simpler ways to achieve what you need.
Related
I want to change linearlayout that inside of ReletiveLayout.
But when I use settop it doesn't work!!!
and I can't find good tutorial
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:padding="5dp" >
<LinearLayout
android:id="#+id/remotebox"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/remote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WWWWWWWWWWW"
android:textSize="30dp"
android:layout_gravity="left"
/>
<TextView
android:id="#+id/ver"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TTTTTT"
android:layout_gravity="right"
/>
</LinearLayout>
i want change "#+id/remotebox" layout position and width and hight.
any suggestion about this problem or link of good tutorial?
sorry for poor english
try this
LinearLayout linearLayout= (LinearLayout)findViewById(R.id.remotebox);
linearLayout.getLayoutParams().width=50;//dimensions
linearLayout.requestLayout();
linearLayout.setTranslationY(800);//position
hope this will help you .
in my case, the following code works :
// step1. get the target view
LinearLayout thisView = (LinearLayout)rootView.findViewById(R.id.tab3_bind_device_page);
// step2. get the LayoutPrams instance.
// notice: in this case, the LinearLayout is wrapped by this RelativeLayout,
// so here we have to use the RelativeLayout.LayourParams
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
// change the margins
// in fact you should convert -25f from "px" to "dp", if possible.
lp.setMargins(0, -25f, 0, 0);
thisView.setLayoutParams(lp);
I'm trying to create a LinearLayout programmatically but some things are just not working as expected.
This is the code:
LinearLayout djCard = (LinearLayout)getLayoutInflater().inflate(R.layout.cardtemplate, null);
ImageView djimage = new ImageView(this);
djimage.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 2));
djimage.setAdjustViewBounds(true);
djimage.setScaleType(ImageView.ScaleType.FIT_START);
bmpDj = BitmapFactory.decodeStream(am.open("djs/horger.jpg"));
djimage.setImageBitmap(bmpDj);
RobotoTextView djtext = new RobotoTextView(this);
djtext.setText(R.string.title_horger);
djtext.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 8));
djtext.setGravity(Gravity.CENTER_VERTICAL);
djtext.setTypeface(RobotoTypefaceManager.obtaintTypeface(this,12));
djtext.setTextSize(R.dimen.textSizeLarge);
djCard.addView(djimage);
djCard.addView(djtext);
container.addView(djCard);
cardtemplate:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="-7dp"
android:clickable="true"
android:orientation="horizontal"
style="#style/nowCardStyle"/>
And look at this screenshot: The above card is what I have in xml, the other is my dynamically created card. Even the TextView (Custom one) is not showing...
This is my card layout in xml (which is what i want to have):
<LinearLayout
android:id="#+id/djCard1"
style="#style/nowCardStyle"
android:clickable="true"
android:padding="-7dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/djImg1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:scaleType="fitStart"
android:adjustViewBounds="true"/>
<com.mikebdev.douala.widget.RobotoTextView
android:id="#+id/djText1"
android:layout_gravity="center"
android:layout_marginLeft="#dimen/activity_vertical_margin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:typeface="roboto_condensed_light"
android:textSize="#dimen/textSizeLarge"
android:layout_weight="8"/>
</LinearLayout>
This might not be the answer to your question, but it might solve you problem:
You inflate cardtemplate.xml with the LinearLayout, and you try to add the ImageView and RobotoTextView programmatically.
Why don't you inflate the second XML file you provided, with the ImageView and RobotoTextView included as children and retrieve those children from the parent?
For example:
ImageView djimage = (ImageView) djCard.findViewById(R.id.djImg1);
RobotoTextView djtext = (RobotoTextView) djCard.findViewById(R.id.djText1);
Then you could just inflate the layout from XML, and skip the hassle with trying to create a layout programmatically :)
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);
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 ")
(note that I'm a total beginner in android programming)
I have a class that derives from GLSurfaceView.
What I would like is to place some views (image,text) on top of it.
I managed to get the text view to position properly by using textView.setPadding(300,0,0,0);
The problem is that I cannot get the image view to position properly. I've tried imageView.layout(), imageView.setPadding().
Here is the code:
ImageView imageView=new .... // Create and set drawable
// Setting size works as expected
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(200,200);
imageView.setLayoutParams(lp);
surfaceView = new MySurfaceViewDerivedFromOpenGLSurface(...);
setContentView(surfaceView);
addContentView(textView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
addContentView(textView2, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
addContentView(imageView, lp); // Add the image view
Is it even possible to position it properly this way without specifing the stuff in the XML file?
I saw an example in the sdk dev website that shows how to create views on the openGL surface view, but the problem is that I have a derived class and I don't know if I can specify it in the XML file (I have 0% experience in the XML, Eclipse handled everything for me so far).
You'll save a ton of headaches later by learning how to use xml layout. Specifying a layout for a custom view tripped me up too. This is how it works:
<view class="complete.package.name.goes.here.ClassName"
android:id="#+id/workspace"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</view>
So a really simple vertical layout for your app would be:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ImageView android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<view class="package.name.to.MySurfaceViewDerivedFromOpenGLSurface"
android:id="#+id/mySurfaceView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</view>
</LinearLayout>
You can get a reference to anything in your layout files as long as there's an id:
ImageView myImageView = (ImageView ) findViewById(R.id.imageView1);