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"
/>
Related
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.
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 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 implement an imageview and a textview side by side. I achieved this by using XML. However i wanted to achieve this programmatically but had no luck so far. My XML and Java code are below. Please help me to execute programmatically.
I'm executing the Java code in a fragment.
XML CODE:
<RelativeLayout
android:id="#+id/rl1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<com.loopj.android.image.SmartImageView
android:id="#+id/my_image1"
android:layout_width="160dip"
android:layout_height="100dip"
android:adjustViewBounds="true"
android:scaleType="fitXY"
/>
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/my_image1"
android:layout_alignTop="#+id/my_image1"
android:layout_toRightOf="#+id/my_image1"
android:gravity="center_vertical"
/>
</RelativeLayout>
JAVA CODE:
RelativeLayout rl1 = new RelativeLayout(getActivity());
RelativeLayout.LayoutParams rlParams;
rlParams = new RelativeLayout.LayoutParams(
newLayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
rlParams.addRule(LinearLayout.VERTICAL);
rl1.setLayoutParams(rlParams);
SmartImageView siv1 = new SmartImageView(getActivity());
siv1.setId(rand.nextInt(50000) + 1);
siv1.setLayoutParams(new LayoutParams(width,height));
siv1.setAdjustViewBounds(true);
siv1.setScaleType(ScaleType.FIT_XY);
siv1.setImageUrl(Uri);
TextView tv1 = new TextView(getActivity());
RelativeLayout.LayoutParams relativeLayoutParams;
relativeLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
tv1.setLayoutParams(relativeLayoutParams);
relativeLayoutParams.setMargins(10, 0, 0, 0);
tv1.setGravity(Gravity.CENTER_VERTICAL);
tv1.setText("Sample Text");
rl1.addView(siv1);
rl1.addView(tv1);
ll.addView(rl1);
By executing the above code, i'm getting the image but the text is inside the image. But, i want to get the image on the left and the text on the right.
Thanks in advance
Add below code to your Activity:
rlParams.addRule(RelativeLayout.ALIGN_TOP, siv1);
rlParams.addRule(RelativeLayout.ALIGN_BOTTOM, siv1);
rlParams.addRule(RelativeLayout.RIGHT_OF, siv1);
tv1.setLayoutParams(rlParams);
and then do:
rl1.addView(siv1);
rl1.addView(tv1);
Hope this helps.
In your case it is better to use a horizontal LinearLayout:
LinearLayout ll = new LinearLayout(getActivity());
ll.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(param);
TextView textview = new TextView(getActivity());
...
SmartImageView siv1 = new SmartImageView(getActivity());
...
ll.addView(textview);
ll.addView(siv1);
To align the views in RelativeLayout is much difficult. I will suggest you to use the TableRows or LinearLayouts. Both these give much easier way to align views side by side. Here is a source in which one TextView is aligned-left with 4 ImageViews aligned on its right. You can get idea from this
TextView taking/consuming too much space above and below the text
You can set the margin of your text like this
relativeLayoutParams.setMargins(200, 0, 0, 0);
but this is not the best practice so do one thing take linear layout set orientation to horizontal add both imageview and textview to it and then add this linearlayout to your relative layout.
Hope this helps.
Here is my code:
//define tablerows. each table row has max 3 buttons
LinearLayout llRow1 = (LinearLayout)findViewById(R.id.llRow1); llRow1.removeAllViews();
float scale = getResources().getDisplayMetrics().density;
int padding_5dp = (int) (5 * scale + 0.5f);
//Define FrameLayout
FrameLayout flTmp = new FrameLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT, 1f);
lp.setMargins(0, 0, padding_5dp, padding_5dp);
flTmp.setLayoutParams(lp);
//Add dynamically TextView
TextView tvTmp = new TextView(this);
tvTmp.setText("Test");
LinearLayout.LayoutParams tvPara=new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT );
tvPara.gravity = Gravity.BOTTOM | Gravity.LEFT;
tvPara.setMargins(padding_5dp, 0, 0, 0);
tvTmp.setLayoutParams(tvPara);
//Add dynamically Buttons for juice
ImageButton btnTmp = new ImageButton(this);
[...]
//Add Button and TextView to FrameLayout
flTmp.addView(btnTmp);
flTmp.addView(tvTmp);
llRow1.addView(flTmp);
What i try to do is create an imagebutton dynamically with a textview description like following xml code:
<FrameLayout>
<ImageButton android:background="#null" android:id="#+id/button_x" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="fitXY" android:src="#drawable/button_graphic"></ImageButton>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:clickable="false" android:text="TEST TEST"></TextView>
</FrameLayout>
works fine with my code, but the textview ignores margin and gravity parameters.
its located on upper left corner without margins.
Any hint?
The LayoutParams you use need to be of the type of parent that the child is contained in. You're FrameLayout contains the TextView, but you're assigning a LinearLayout.LayoutParams reference to it. Make it a FrameLayout.LayoutParams or change to a LinearLayout. If I had to guess, it's silently catching this error and just ignoring it.