I have an ImageView in my app. My question is how I can set params to it?
ImageView header = new ImageView(getActivity());
I used this way but I face with classCastException
ImageView header = new ImageView(getActivity());
header.setImageResource(R.drawable.ahsan_hadis_img);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(200, 1500);
header.setLayoutParams(layoutParams);
Try adding this in your XML file:
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout"
android:orientation="vertical">
</LinearLayout>
Add this in your JAVA file
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(
android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,
android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
ImageView subImage = new ImageView(this);
subImage.setLayoutParams(params);
layout.addView(subImage);
ClassCastException might occur if you are adding your ImageView to a layout other than LinearLayout. Choose your LayourParams class according to the ImageView's parent.
Related
I'm trying to add a few images to a LinearLayout programmatically.
the xml of the image looks like this:
<ImageView
android:id="#+id/iv_card27"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:scaleType="centerInside"
app:srcCompat="#drawable/back" />
and this is my the java code I tried already:
ImageView card = new ImageView(this);
card.getLayoutParams().width = 50;
card.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
card.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
card.setImageResource(R.drawable.back);
bottomRow.addView(card);
However I have been struggling to add the Margins, Also Im worried about the width which I set to 50. But it should actually be 50dp. How can I accomplish this?
You can use ViewGroup.MarginLayoutParams , RelativeLayout.LayoutParams or LinearLayout.LayoutParams to set layout margin.
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
card.setLayoutParams(params);
https://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams
You have to arrange margins using particular code:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(xxx,xxx,xxx,xxx);
card.setLayoutParams(params);
for setting margin to your ImageView:
MarginLayoutParams marginParams = new MarginLayoutParams(image.getLayoutParams());
marginParams.setMargins(left_margin, top_margin, right_margin, bottom_margin);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
card.setLayoutParams(layoutParams);
I am new in android so I spent hours of trying to make this look like this in code. Could someone help on this ?
Here is my try :
RelativeLayout photo = new RelativeLayout(this);
photo.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, densityToPixels(80)));
photo.setGravity(Gravity.CENTER_VERTICAL);
allphotos.addView(photo);
TextView textView1 = new TextView(this);
textView1.setLayoutParams(new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
textView1.setGravity(Gravity.CENTER_VERTICAL);
textView1.setText("IDasdfasdfasdfasdfasdfd");
photo.addView(textView1);
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
ImageView img = new ImageView(this);
img.setLayoutParams(new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
img.setLayoutParams(params);
photo.addView(img);
ImageView del_img = new ImageView(this);
img.setLayoutParams(new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
img.setLayoutParams(params);
photo.addView(del_img);
Why do you do all that in code? Use xml. If you need the view constructed from xml use View.inflate passing your xml to it.
There's really no need to do that in code...
Just use a TextView and add two compound drawables to it:
<?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="wrap_content"
>
<TextView
android:id="#+id/txtText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="8dp"
android:textSize="14sp"
android:textColor="#color/white"
android:drawableLeft="#drawable/icon_left"
android:drawableRight="#drawable/icon_rite"
android:drawablePadding="8dp"
/>
</RelativeLayout>
[EDIT]
To set the compounds in code:
1 - You don't need these lines in the TextView definition, anymore:
android:drawableLeft="#drawable/icon_left"
android:drawableRight="#drawable/icon_rite"
2 - You need to set the drawables in code:
// given that you retrieved your TextView as txt and you retrieved your drawables as drwLeft and drwRite
// Parameter order: left, top, right, bottom
txt.setCompoundDrawablesWithIntrinsicBounds(drwLeft, null, drwRite, null);
I am trying to create a dialog window in Android where I am dynamically creating the layout and adding the views in to it. But I want to add my view to a specific position in layout.
Here is my code snippet
final Dialog dialog = new Dialog(this);
FrameLayout fl=new FrameLayout(this);
TextView et = new TextView(this);
et.setText("asdas");
fl.addView(et,100,1200);
ColorDrawable cd=new ColorDrawable(android.graphics.Color.TRANSPARENT);
dialog.getWindow().setBackgroundDrawable(cd);
dialog.setContentView(fl);
I used this to place a image button at a specified location in my relative layout. hope this helps:
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.leftMargin = (int)(marginLeft);
layoutParams.topMargin = (int)(marginTop);
imageButton.setLayoutParams(layoutParams);
EDIT:
You can do this for other layouts that support positioning of child views. This method works even for lower API versions. Atleast it worked on API 10. :)
yes you can do this ...something like this
AdView ad = new AdView(this, AdSize.SMART_BANNER, getString(R.string.admob_id));
LinearLayout control_container =(LinearLayout)findViewById(R.id.ad_container);
control_container.addView(ad);
AdRequest adRequest = new AdRequest();
ad.loadAd(adRequest);
and in your xml file...
<?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"
android:background="#E9E5E6" >
<LinearLayout
android:id="#+id/ad_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="bottom"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
suppose if you want to add TextView at (200,200) position
try this...
FrameLayout frameLayout = new FrameLayout(this);
TextView textView = new TextView(this);
textView.setTop(200);
textView.setLeft(200);
frameLayout.addView(textView, textViewWidth, textViewHeight);
I think this should work.
Dialog dialog = new Dialog(this);
FrameLayout frm_layout = new FrameLayout(this);
android.widget.FrameLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.leftMargin = 100;
params.topMargin = 1200;
TextView et = new TextView(this);
et.setText("asdas");
frm_layout.addView(et, params);
dialog.setContentView(frm_layout, new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,
android.view.ViewGroup.LayoutParams.FILL_PARENT));
I am new in android and I have done so much googling for this issue. Actually I want to create below UI in Code of android:
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/tab_background">
<RelativeLayout
android:id="#+id/event_1"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/color_001">
<TextView
android:id="#+id/txt_event_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="1dp"
android:gravity="center"
android:text="j1"
android:textStyle="bold"
android:textColor="#ffffff" />
</RelativeLayout>
</RelativeLayout>
Can anybody help me, please?
I have written a quick example code based on your xml layout. I tried make it clear by using comments but if you don't understand something, I can try to explain it a little more.
// Creating the outer RelativeLayout which has id "relativeLayout2" in your xml layout.
RelativeLayout outerRelativeLayout = new RelativeLayout(context);
// ------------------------------------------------------------------
// Creating the inner RelativeLayout which has id "event_1" in your xml layout.
RelativeLayout innerRelativeLayout = new RelativeLayout(context);
// Creating the TextView which has id "txt_event_1" in your xml layout.
TextView textView = new TextView(context);
textView.setGravity(Gravity.CENTER);
textView.setText("j1");
// Defining the layout parameters of the TextView
RelativeLayout.LayoutParams textViewParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
textViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
textViewParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
// Adding the TextView to the inner RelativeLayout as a child
innerRelativeLayout.addView(textView, new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
// ------------------------------------------------------------------
// Adding the inner RelativeLayout to the outer RelativeLayout as a child
outerRelativeLayout.addView(innerRelativeLayout, new RelativeLayout.LayoutParams(convertDptoPx(40), convertDptoPx(40)));
// ------------------------------------------------------------------
// Defining the layout parameters of the outer RelativeLayout
RelativeLayout.LayoutParams rootParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rootParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
// Set the layout parameters associated with the outer RelativeLayout.
outerRelativeLayout.setLayoutParams(rootParams);
Although this code snippet doesn't include some of the xml attributes defined in your xml layout, you can find the related methods in the documentation of the view.
For instance, if you look at the TextView documentation (http://developer.android.com/reference/android/widget/TextView.html), you can see the "XML Attributes" table which shows the attribute name and related method.
use this code.
RelativeLayout layout1= new RelativeLayout(this);
LayoutParams param1= new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
RelativeLayout layout2= new RelativeLayout(this);
LayoutParams param2= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
TextView text= new TextView(this);
text.setText("your text");
layout2.addView(text, param2);
layout1.addView(layout2, param1);
setContentView(layout1);
RelativeLayout.LayoutParams childVw = new RelativeLayout.LayoutParams(40, 40);
TextView textVw = new TextView(this);
textVw.setLayoutParams(childVw);
Then add this to your main relative layout
setContentView(R.layout.layoutfilename);
RelativeLayout layout1= (RelativeLayout)findViewById(R.id.yourlayoutid);
LayoutParams param1= new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
RelativeLayout layout2= new RelativeLayout(this);
LayoutParams param2= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
TextView text= new TextView(this);
text.setText("your text");
layout2.addView(text, param2);
layout1.addView(layout2);
I want to place a TextBox over an image dynamically in java code. Here is my code:
ImageView image2 = new ImageView(this);
image2.setPadding(25, 25, 0, 0);
image2.setId(2001);
image2.setImageResource(R.drawable.img);
LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
image2.setLayoutParams(layoutParams);
linear.addView(image2);
its my image :
how i place a textview over this image ??? help me out plz..!!!
Wrap your ImageView and EditText in a RelativeLayout (wrap_content for height and width). Try first with layout Editor and get the values to set this programatically.
Try this code as demo:
//Main Rel_Layout
RelativeLayout scrollHolder = new RelativeLayout(this);
scrollHolder.setId(++myid);
RelativeLayout.LayoutParams scrollHolderParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
scrollHolder.setLayoutParams(scrollHolderParams);
scrollHolder.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
// Image Holder Layout
RelativeLayout imgHolder = new RelativeLayout(this);
imgHolder.setId(++myid);
RelativeLayout.LayoutParams imgHolderParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imgHolder.setLayoutParams(imgHolderParams);
imgHolder.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
//imgHolder.setBackgroundColor(Color.BLUE);
imgHolder.setLayoutParams(imgHolderParams);
// Image Object
ImageView image2 = new ImageView(this);
image2.setId(++myid);
image2.setBackgroundColor(Color.BLUE);
//int resId = HomePage.this.getResources().getIdentifier("img2", "drawable", HomePage.this.getPackageName());
image2.setImageResource(R.drawable.img2);
// set image to its holder
imgHolder.addView(image2);
// set imgHolder to main Layout
scrollHolder.addView(imgHolder);
// set main layout as content-view
setContentView(scrollHolder);
// this will sure help you.
OLDER
Simplest way is:
[1] Add EditText in your Relative-layout, with
layout-center-horizontal = true and layout-center-verticall = true
[2] In xml set its Visibility=GONE
[3] In code file take its object, and set its "text-value" and Visibility = VIEW.Visible
Why are you trying to do this i java? Think you should look at FrameLayout and z-index The z-index is defined by the order the items are added:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_drawable"
android:scaleType="fitCenter"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:padding="5dp"
/>