I am trying to position a TextView and an ImageView within a LinearLayout programmatically. The problem is that the textview is always on top of the imageview, and I would like to it be underneath. I cannot find a way to imitate the android:layout_below= xml attribute in the java UI approach.
You should simply switch the order of your two views.
For example:
linear.addView(image);
linear.addView(text);
instead of:
linear.addView(text);
linear.addView(image);
LinearLayout lin = new LinearLayout(context);
lin.setOrientation(LinearLayout.VERTICAL);
ImageView imageView = new ImageView(context);
TextView textView = new TextView(context);
lin.addView(imageView);
lin.addView(textView);
the first component ImageView should be placed in the LinearLayout first, before the TextView.
Edit: oops forgot about "programmatically"
Related
I am trying to create a layout with nested linear layouts and textviews which are placed vertically in a linear layout. They are created programatically. While I can set the height of the linear layout using layout Params I am not able to do this for textview. Using Textview setLayoutParams or setHeight function provides no response. The text is always wrapped around the content (though this is no where mentioned by me in the code). Can someone help me fix this?
this code might help you:
LinearLayout.LayoutParams paramsLayoutExpire = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
TextView txtView=new TextView(this);
txtView.setLayoutParams(paramsTextViewExpire);
LayoutParams.MATCH_PARENT: Height
LayoutParams.WRAP_CONTENT: Width
I hope this might help
TextView textView = new TextView(this);
textView.setText("Hellow World");
textView.setHeight(prefered_height_in_pixel);
this.addView(textView)
I've designed a RelativeLayout with children elements such as TextView and ImageView. What I want to do is to create a RelativeLayout object from my created RelativeLayout in XML, that way I can access to its children elements and modify them (change image from ImageView and change the text from TextView). How can I do this? It would be kind of like this.
RelativeLayout lay = new "myrelativelayout";
ImageView img = lay.children[0];
TextView txt = lay.children[1];
Thanks!
XML:
<RelativeLayout
android:id="#+id/relative_layout_id"
...>
<TextView
android:id="#+id/textview_id"
.../>
</RelativeLayout>
Activity onCreate:
RelativeLayout lay = (RelativeLayout) findViewById(R.id.relative_layout_id);
To change children use:
TextView child = (TextView) findViewById(R.id.textview_id);
child.setText(text);
or:
View child = lay.getChildAt(i);
To access the layout we use the findViewById method. Therefore to change the image in the ImageView you do not really need access to the RelativeLayout.
The way we access any element through its id is as follows:
View v = (View)findViewById(R.id.view_id);
where view_id is the ID of the view.
To access the RelativeLayout, therefore, the piece of code would be:
RelativeLayout lay = (RelativeLayout) findViewById(R.id.relative_layout_id);
But if you only want to change the text in the TextView, you really don't need the above code. It is sufficient if you do a similar access for the TextView:
TextView textView = (TextView) findViewById(R.id.id_of_textview);
textView.setText(text);
A general method of accessing any resource can be found here.
I have a LinearLayout with horizontal orientation in which I add some TextView and EditText dynamically. My problem is when the TextView is long, it is cut off (by height) and the EditText is invisible.
Adding padding to the views didn't solve it. When setting the LinearLayout minHeight value, the TextView is displayed correctly but the EditText is still invisible.
<LinearLayout
android:id="#+id/content_content"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"></LinearLayout>
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < cntCount; i++) {
TextView cntView = new TextView(getActivity());
cntView.setLayoutParams(params);
cntView.setText(cnt.get(i));
contentContentView.addView(cntView);
EditText answerView = new EditText(getActivity());
answerView.setLayoutParams(params);
answerView.setId(i);
answerView.setHint("......");
contentContentView.addView(answerView);
}
EDIT
I have found the solution and created a library. For those interested you can download it here
Your LinearLayout has an orientation of "horizontal" - it will add the next view to the right of the current view. This means that if the TextView wraps (fills the parent) there is no room for any additional views in the LinearLayout that will be visible on the screen (they will be added to the right of the visible layout).
If you are trying to place an EditText in the same line with a series of TextViews, then you will need to create a custom TextView and layout. This requires doing measurements about the placement and size of the EditText that are not standard with Android.
This post may help: How to overlay EditText on TextView just after the end of text
You can set up layout_weights for the EditText and the TextView instead of using wrap_content for the layout_width. That would make sure that the EditText will always be shown.
I have a RelativeLayout in my Android App. Now I want to show an ImageView in front of that Layout. The problem is that the ImageView is not in the front, it's a bit transparent and I can see things like EditText and Button. I can't change the Layout (setContentView), because the Layout is created dynamically and after setContentView, the Controls are away.
You can add view programmatically, and in the way that it will be on the top!
create id for you top level layout
Now some code (in my case it's relative layout):
RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.relative_layout_id);
ImageView imageView = new ImageView(context)
Drawable rightArrowBlackDrawable = getResources().getDrawable(R.drawable.image);
imageView.setLayoutParams(getLayoutParams());
relativeLayout.addView(imageView);
imageView.bringToFront();
//here just example layout params, use yours params ;-)
private RelativeLayout.LayoutParams getLayoutParams() {
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
return layoutParams;
}
You can bring it to the front once you insert it.
imageView.bringToFront();
If the image is transparent, you can set a white background to prevent things below it from showing.
imageView.setBackgroundColor(0xFFFFFF);
I was just wondering what kind of relative layout parameters would I need to use to place a text view in the middle of an Image View. If this is not possible how else would I do so.
You can use RelativeLayout, set the image as the background and use Center_In_parent for the textview.
android:centerInParent="true"
or
android:layout_centerHorizontal="true"
or
android:layout_centerVertical="true"
You can use any of them depending on your requirement
ImageView is not a view group, so you can't do that exactly.
What you need to do is to put both your ImageView and your textview inside a framelayout. Then center your textview using android:centerInParent="true".
RelativeLayout relativeLayout;//set the relative layout
text=new TextView(context);
ImageView image=new ImageView(context);
RelativeLayout.LayoutParams imageParams = new
RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams textParams = new
RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textParams.addRule(RelativeLayout.CENTER_IN_PARENT);
imageParams.addRule(RelativeLayout.CENTER_IN_PARENT);
relativeLayout.addView(image,imageParams);
relativeLayout.addView(text,textParams);