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 :)
Related
I have a relative layout which contain a linearLayout with two imageView as below.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rlParent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:layout_centerInParent="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_marginRight="5dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"/>
</LinearLayout>
</RelativeLayout>
Then i have added custom surfaceView programmatically. After adding the surfaceView linearLayout is not showing in center.I tried to set layout_centerInParent property through the code but it is not working.
Please help me.
mPreview = new CustomSurfaceView(getActivity(),1, CameraPreview.LayoutMode.FitToParent, false,this);
LayoutParams previewLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
mLayout.addView(mPreview, 0, previewLayoutParams);
mLayout is Releativelayout and mPreview is custom surfaceview
Your parent layout(Here RelativeLayout) width must be match_parent. Then only your child layout, the centerInParent will work.
Try this...
<RelativeLayout
android:id="#+id/rlParent">
<LinearLayout
android:id="#+id/rlChild">
......
</LinearLayout>
View mPreview = findViewById(R.id.rlChild);
RelativeLayout.LayoutParams previewLayoutParams = (RelativeLayout.LayoutParams)mPreview.getLayoutParams();
previewLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
mPreview.setLayoutParams(previewLayoutParams);
mLayout.addView(mPreview);
The second and the third lines, you have to write match_parent. That's why centerinparent doesn't work.
Try this android:layout_centerHorizontal="true" instead of android:layout_centerInParent="true"
I am trying to add a new LinearLayout with a EditText element in it whenever a button on my app gets pushed. The xml I have is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/app"
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:orientation="vertical"
android:weightSum="1">
<EditText android:id="#+id/dish_name"
android:layout_width="match_parent"
android:layout_height="58dp"
android:hint="#string/dish_name"
android:background="#drawable/my_border"
android:paddingLeft="10dip"/>
<LinearLayout android:id="#+id/ingredient_list"
android:layout_width="match_parent"
android:layout_height="58dp"
android:weightSum="1"
android:layout_marginTop="20dp">
<EditText android:id="#+id/edit_message"
android:layout_width="0dp"
android:layout_height="42dp"
android:hint="#string/edit_message"
android:background="#drawable/my_border"
android:layout_weight="1"
android:paddingLeft="10dip"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
</LinearLayout>
I am trying to duplicate the second LinearLayout along with the EditText inside of it. The java code I have so far is:
public void sendMessage(View view) {
LinearLayout layout = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setWeightSum(1f);
params.weight = 1.0f;
params.setMargins(0,20,0,0);
EditText editText = new EditText(this);
editText.setBackgroundResource(R.drawable.my_border);
editText.setHint(R.string.edit_message);
editText.setLayoutParams(params);
LinearLayout container = (LinearLayout)findViewById(R.id.app);
container.addView(editText);
}
The problem I am running into is that when I click the button I get the following:
AppImage
I want the EditText box to be the same height as the one I have defined in the xml but it takes up the entire screen.
Any ideas?
No need to re-define the layout entirely within Java. You should define it as its own XML file. Call it ingredient_input.xml
For example, feel free to modify. This is one "row" for the EditText and button, so a horizontal layout. Could also be a RelativeLayout.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="58dp"
android:weightSum="1"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<EditText android:id="#+id/edit_message"
android:layout_width="0dp"
android:layout_height="42dp"
android:hint="#string/edit_message"
android:background="#drawable/my_border"
android:layout_weight="1"
android:paddingLeft="10dip"/>
<Button
android:id="#+id/btnAddMore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"/>
</LinearLayout>
First, you need to get the parent linear layout
LinearLayout ll = (LinearLayout) findViewById(R.id.ingredient_list);
Then, you can inflate the XML file for the "input row"
LayoutInflater inflater = LayoutInflater.from(MainActivity.this); // some context
View row = inflater.inflate(R.layout.ingredient_input, null);
You can also find the button, then set its click listener
row.findViewById(R.id.btnAddMore).setOnClickListener(...); // TODO
Then, just add that view onto the parent linear layout
ll.addView(row);
Realistically, you really could be using a ListView with an Adapter instead.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
In the code above, you have the height set to match_parent and widht set to wrap_content. Instead, try setting the height and the width to match your xml. The height should be 58dp and the width should be match parent.
Hope this helps!
I haven't the time to test this but you can try:
// assuming you have variables for ingredient_list and edit_message
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)ingredientList.getLayoutParams();
LinearLayout.LayoutParams editTextParams = (LinearLayout.LayoutParams)editMessage.getLayoutParams();
You can then use these instead of creating the parameters.
P.S. you don't have a specified orientation in your ingredient_list in the xml.
I try to add a PlotView (custom View) into a LinearLayout. My LinearLayout has a weightSum of 8. Inside my .xml I defined a Space (weight of 3) that has to appear above my PlotView and a Button (weight of 1) that should follow my plot underneath. So far, so good.
Now the PlotView is added programmatically, with a weight of 4. However, it will always consume almost the entire screen, and I am not sure what I am doing wrong here.
My Code:
main_activity.xml (snippet)
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_alignParentEnd="true"
android:id="#+id/linlayout"
android:weightSum="8">
<Space
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/calibrate"
android:id="#+id/calibration"
android:layout_weight="1"
android:layout_gravity="center_horizontal" />
</LinearLayout>
main_activity.java (snippet)
LinearLayout layout = (LinearLayout) findViewById(R.id.linlayout);
plotView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 4f));
layout.addView(plotView, 1);
Any idea what I am doing wrong?
The solution is to set the height, which is affected by the weight, to 0.
Code in .xml:
android:layout_height="0dp"
Code in .java:
plotView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 0, 4f));
I have a problem with a linear layout inside a scrollview
<ScrollView
android:layout_width="fill_parent"
android:scrollbarStyle="outsideOverlay"
android:layout_height="420px">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="bottom">
<TextView
android:text="1st"
android:gravity="right"
android:textSize="100px"
android:layout_width="fill_parent"
android:layout_height="100px" />
<TextView
android:text="2nd"
android:gravity="right"
android:textSize="100px"
android:layout_width="fill_parent"
android:layout_height="100px" />
</LinearLayout>
</ScrollView>
The Textview that has text of "1st" shows on the top of the textview that has text of "2nd", is there a way that the "1st" shows on the bottom without changing the textview? because I plan to add textviews programmatically.
Adding views programmatically to a LinearLayout is fairly easy:
LinearLayout yourLayout = (LinearLayout) findViewById(R.id.your_layout);
TextView view = new TextView(/* Context and other params */);
yourLayout.addView(view);
Rather than invoking new TextView directly, it is recommended to specify an XML for your text view and use a layout inflater:
TextView view = (TextView) LayoutInflater.from(getBaseContext())
.inflate(R.id.your_textview_layout, yourLayout, false);
I have the following main.xml file with a LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1" android:id="#+id/llid">
<TextView android:text="Client profile"
android:id="#+id/ProfileName"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
<TextView android:text="Specs"
android:id="#+id/Specs"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
</LinearLayout>
I add an image to the LinearLayout via code at runtime like so
ImageView image = new ImageView(this);
image.setImageBitmap(bmp);
LinearLayout ll = (LinearLayout) findViewById(R.id.llid);
ll.addView(image);
However, I want to add the ImageView between the 2 TextViews in my LinearLayout. I can't seem to find a way in the android docs to add a view before another view, or after. How can I do this?
NB I call
setContentView(R.layout.main);
Before I add the ImageView to the LinearLayout.
When adding a View to a ViewGroup, you can specify an index which sets the position of the view in the parent.
You have two views and so (counting from zero) you would want to add at the 1st position; just call ll.addView(image, 1); to have it placed in between the two TextViews.
The docs state you can use the index to insert it where you want. I see you are using the signature of the view only, did you try the signature with the index parameter?
public void addView(View child, int index)
I faced a similar problem. In my case I wanted to add a LinearLayout at last position of another LinearLayout. To accomplish it, I did:
LinearLayout parentLayout = (LinearLayout) findViewById(R.id.parentLayout);
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// add others views to your linear layout
parentLayout.addView(layout, parentLayout.getChildCount());
setContentView(R.layout.main);
ImageView img = (ImageView) findViewById(R.id.imagefield);
img.setImageResource(your_image_here);
and in the xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
android:id="#+id/llid">
<TextView android:text="Client profile"
android:id="#+id/ProfileName"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
<ImageView android:id="#+id/imagefield"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView android:text="Specs"
android:id="#+id/Specs"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
</LinearLayout>
Add an ImageView into the xml, and if its not being used, make it invisible (image.setVisibility(View.INVISIBLE)). It may not show anything anyway when no image is set.
To get position of view in a view group
val targetPosition = oldLL.indexOfChild(viewToAdd)
To add a view at a position in a view group
newLL.addView(viewToAdd,targetPosition)