I have the following XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/background"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="4dip"
android:paddingRight="4dip" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="32dip"
android:layout_height="32dip"
android:layout_marginLeft="4dip"
android:src="#drawable/test_imag" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/imageView1" />
</RelativeLayout>
and the following bindView in a cursor
public void bindView(View view, Context context, Cursor cursor) {
wrapper = (RelativeLayout) view.findViewById(R.id.background);
TextView tview2 = (TextView) view.findViewById(R.id.text2);
ImageView imgview = (ImageView) view.findViewById(R.id.imageView1);
tview2.setText("Test text");
if (cursor.getPosition() % 2 == 0) {
tview2.setBackgroundResource(R.drawable.bubble_left);
} else {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tview2.setBackgroundResource(R.drawable.bubble_right);
params.addRule(RelativeLayout.LEFT_OF, R.id.imageView1);
tview2.setLayoutParams(params);
wrapper.setGravity(Gravity.RIGHT);
}
}
I'm trying to do a bubble chat with an image, the IF part is fine, the image is on the rght side of the screen, and, the text is to the left of it.
In the else part I set the gravity to the right and add a rule to set tview2 to the left of the image.
The result of the above made the image go to the right side of the screen but the tview2 has gone, what did I do wrong?
You need to specify params for the imageview as well since you've declared it initially in the xml to be relative to the textview.
imageparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
The result would be something like this.
else {
RelativeLayout.LayoutParams imageparams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imageparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imgview.setLayoutParams(imageparams);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tview2.setBackgroundResource(R.drawable.bubble_right);
params.addRule(RelativeLayout.LEFT_OF, R.id.imageView1);
tview2.setLayoutParams(params);
wrapper.setGravity(Gravity.RIGHT);
}
Related
I wanted to re-create my XML layout programatically, but it doesn't seem to work. I found out how to (supposedly) create it programatically from another thread. Any help is appreciated, thanks.
The XML:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:background="#android:color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test1"
android:id="#+id/txt1"
android:paddingTop = "36dp"
android:paddingRight = "36dp"
android:paddingLeft = "18dp"
android:paddingBottom = "36dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test2"
android:layout_toRightOf="#id/txt1"
android:id="#+id/txt2"
android:paddingTop = "36dp"
android:paddingRight = "36dp"
android:paddingBottom = "36dp"/>
</RelativeLayout>
Here is the code I'm attempting to match the XML above:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creating a new RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
// Defining the RelativeLayout layout parameters.
// In this case I want to fill its parent
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
// Creating a new TextView
TextView txt1 = new TextView(this);
txt1.setText("Test1");
TextView txt2 = new TextView(this);
txt2.setText("Test2");
// Defining the layout parameters of the TextView
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.RIGHT_OF, txt1.getId());
txt1.setPadding(18,36,36,36);
txt2.setPadding(0,36,36,36);
// Setting the parameters on the TextView
txt1.setLayoutParams(lp);
txt2.setLayoutParams(lp2);
// Adding the TextView to the RelativeLayout as a child
relativeLayout.addView(txt1);
relativeLayout.addView(txt2);
// Setting the RelativeLayout as our content view
setContentView(relativeLayout, rlp);
}
}
What I see when I run it programmatically
The way my program works is that I take user input for 3 items, create a horizontal LinearLayout programmatically with those three items. Then place that LinearLayout inside a RelativeLayout that is defined in XML.
User can keep adding these 3-item horizontal LLs to the bottom of the RelativeLayout, which acts as a list in this case.
I am specifying a weight in the setLayoutParams method for each TextView object
TextView quantityText = new TextView(MainActivity.this);
quantityText.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
quantityText.setText("" + currentIngredientMeasurementQuantity);
TextView typeText = new TextView(MainActivity.this);
typeText.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
typeText.setText(currentIngredientMeasurementType);
TextView nameText = new TextView(MainActivity.this);
nameText.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 4f));
nameText.setText(currentIngredientName);
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setId(View.generateViewId());
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.ingredient_list_relativelayout);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//Find last member of RelativeLayout
View lastIngredient = (View) relativeLayout.getChildAt(totalIngredientQuantity - 1);
relativeParams.addRule(RelativeLayout.BELOW, lastIngredient.getId());
relativeParams.setMargins(0, 0, 0, 0);
ll.addView(quantityText);
ll.addView(typeText);
ll.addView(nameText);
relativeLayout.addView(ll, relativeParams);
The problem is, the TextViews aren't being displayed the way I'd like them to. I'd like each of the three items to line up vertically, like a proper list.
Instead, I get something like this. You can see that the length of the strings is somehow affecting the way the layout is stored/displayed.
I know it's a big block of code, but any help would be appreciated!
Try this:-
Create a custom layout insteadof creating controls as dynamically
custom.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="90">
<TextView
android:id="#+id/textview1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="30"
android:gravity="center"
android:text="text1"/>
<TextView
android:id="#+id/textview2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="30"
android:text="text2"/>
<TextView
android:id="#+id/textview3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="30"
android:text="text3"/>
</LinearLayout>
Add this view in RelativeLayout
View view = factory.inflate(R.layout.custom, null);
relativeLayout.addView(view);
You need to set the layout_width property to 0 for layout weight property to work well. I made a couple to changes to your code to make it work well for me.
Try this,
private void addNewRow(float currentIngredientMeasurementQuantity, String currentIngredientMeasurementType, String currentIngredientName) {
TextView quantityText = new TextView(MainActivity.this);
quantityText.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
quantityText.setText("" + currentIngredientMeasurementQuantity);
TextView typeText = new TextView(MainActivity.this);
typeText.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
typeText.setText(currentIngredientMeasurementType);
TextView nameText = new TextView(MainActivity.this);
nameText.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 4f));
nameText.setText(currentIngredientName);
LinearLayout ll = new LinearLayout(MainActivity.this);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
ll.setId(View.generateViewId());
} else {
ll.setId(totalIngredientQuantity);
}
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.ingredient_list_relativelayout);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//Find last member of RelativeLayout
View lastIngredient = (View) relativeLayout.getChildAt(totalIngredientQuantity - 1);
if (lastIngredient != null) {
relativeParams.addRule(RelativeLayout.BELOW, lastIngredient.getId());
relativeParams.setMargins(0, 0, 0, 0);
}
ll.addView(quantityText);
ll.addView(typeText);
ll.addView(nameText);
relativeLayout.addView(ll, relativeParams);
}
You have to set the Gravity to Left (or Start) on your TextViews before adding them to the LinearLayout, just like this:
LinearLayout.LayoutParams name_lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
name_lp.gravity = Gravity.LEFT;
nameText.setLayoutParams(name_lp);
Try to tell your your Textviews to use all the available space they can get. So they will fill the whole LinearLayout.
Instead of WRAP_CONTENT use MATCH_PARENT or FILL_PARENT
In XML you could use 0dip like described here: In android layouts what is the effect/meaning of layout_height="0dip"
Another idea would be to use TableLayout or GridLayout http://developer.android.com/reference/android/widget/GridLayout.html
http://developer.android.com/reference/android/widget/TableLayout.html
Have Fun
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);
When i used this XML file...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
..I see that background color wrap content
http://img716.imageshack.us/img716/8225/rlayout.jpg
But if i write this RelativeLayout by code...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layout.setBackgroundColor(Color.parseColor("#FF0000"));
TextView text = new TextView(this);
text.setText("Hello world");
//AƱado texto
layout.addView(text);
layout.setLayoutParams(params);
setContentView(layout);
}
...I see that background color match parent instead of wrap content
http://img404.imageshack.us/img404/1427/rlayoutfull.jpg
Any ideas?
Thanks!
you are never setting the layout params:
layout.setLayoutParams(params);
and I think that you should place the:
TextView text = new TextView(this);
text.setText("Hello world");
and all other view you will add to your layout before you setting the layout params, otherwise there will be no content to wrap-around.
Edit:
what happens when you set:
tried to replace between the two:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
?
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"
/>