I create checkboxes dynamically within the Constraintlayout. I need to use this layout, not the Linearlayout. So I created the given below methods
order.xml file
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/clReasons"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/clDepartments">
</androidx.constraintlayout.widget.ConstraintLayout>
orderstatus.java
linearMain =(ConstraintLayout) findViewById(R.id.clReasons);
for(int intReasonCount=0;intReasonCount<reasonsList.size();intReasonCount++)
{
checkBox=new CheckBox(OrderStatus.this);
checkBox.setId(Integer.valueOf(reasonsList.get(intReasonCount)
.get(ReasonInfo.ReasonID))
);
checkBox.setText(String.valueOf(reasonsList.get(intReasonCount)
.get(ReasonInfo.ReasonDesc)));
linearMain.addView(checkBox
);
The output is
All checkboxes are aligned at one position. I need to set this dynamic checkbox are 2 columns at each row(2 checkboxes at each row). Please refer to the image.
You need to set constaints for child views in code.
For example check this answer: ConstraintLayout: change constraints programmatically
Related
I have two Buttons nested in a LinearLayout. Between these Buttons are two TextViews. In the Xml, I have set the foreground to an image for each of these Buttons.
It runs fine on my device for Api 23. But on other devices below Api 23, the foreground image does not display and instead results in a default white solid color. Is there any way to make these images show using foreground below Api 23?
We have tried FrameLayout but it does not do what we want it to do. Would ImageButtons be a better way to solve this issue?
One of the core functions of our app is that every time a user taps a Button, the size increases and the image stretches accordingly. This is done dynamically in code. If I were to use ImageButtons, I would need to set the layout parameters every time for height and width, rather than one line of code that sets the height.
Any tips would be appreciated!
EDIT: Code I am working with -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="11"
android:background="#android:color/black">
<Button
android:layout_weight="5"
android:id="#+id/firstP"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:foreground="#drawable/icebutton"
android:scaleX="1"
android:scaleY="1"/>
<TextView
android:layout_weight="0.5"
android:id="#+id/firstPlayer"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rotation="180"
android:textColor="#android:color/white"
android:background="#android:color/transparent"/>
<TextView
android:layout_weight="0.5"
android:id="#+id/secondPlayer"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:background="#android:color/transparent"/>
<Button
android:layout_weight="5"
android:id="#+id/secondP"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:foreground="#drawable/firebutton"
android:scaleX="1"
android:scaleY="1"/>
</LinearLayout>
We found out that there were two issues causing the images to not be shown.
1. The size of the image file was too big, creating an outOfMemory error which in turn resulted in the buttons not displaying the images.
2. The foreground attribute does not work for API 22 and below.
Steps to solving these issues:
1. We reduced the size of the image files.
2. We replaced Button with ImageButton
3. In the XML file we removed the foreground attribute, added a black background, and added the image via the src attribute. The following is a snippet.
<ImageButton
android:layout_weight="5"
android:id="#+id/firstP"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:src="#drawable/icebutton"
android:scaleType="fitXY"
android:background="#android:color/black"/>
We then had to change our code to dynamically adjust the height of the buttons to match the new image buttons with the help of this link by setting the LayoutParams:
how to change size of button dynamic in android
Now everything works perfectly!
I have an activity with a layout. After a GET request to a server, I want to dynamically add new elements to that layout.
I want to add those elements multiple times, using a for-structure.
The elements I want to add are the following:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="#drawable/outer_border"
android:padding="2dp"
android:layout_marginTop="20dp">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#color/orange"
android:height="40dp"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="TW"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textSize="70px"
android:width="60dp" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView3"
android:layout_toLeftOf="#+id/checkBox1"
android:text="inca 6 zile"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
I've tried this:
for(int i = 0; i < homeworkList.size(); i++){
LinearLayout linearLayout = (LinearLayout) currentActivity.findViewById(R.id.linearLayout2);
RelativeLayout newLayout = new RelativeLayout(currentActivity, null, R.style.HomeworkLayout);
TextView text = new TextView(currentActivity);
TextView text1 = new TextView(currentActivity);
text1.setText("da");
text.setText("nu");
newLayout.addView(text1);
newLayout.addView(text);
linearLayout.addView(newLayout, relativeParams);
}
But no result, those textview were added but on top of each other, and the relative layout I just added in that for doesn't have any of the style I added using R.style.HomeworkLayout.
What is the best way to add the elements with so much styling? Why isn't this working?
those textview were added but on top of each other
That's what you told RelativeLayout to do. If you wanted to specify positioning rules, you would have passed instances of RelativeLayout.LayoutParams to addView() when you were adding the TextView widgets.
What is the best way to add the elements with so much styling?
Well, probably, the answer is to use ListView or RecyclerView. That being said, the simplest solution that keeps your vertical LinearLayout would be to inflate the rows:
LinearLayout linearLayout = (LinearLayout) currentActivity.findViewById(R.id.linearLayout2);
for(int i = 0; i < homeworkList.size(); i++){
View row=getLayoutInflater().inflate(R.layout.row, linearLayout, false);
// call findViewById() to retrieve your TextView widgets and fill them in
linearLayout.addView(row);
}
This assumes that the layout you show in your question is named R.layout.row; adjust the inflate() call as needed if that is not the name. This also assumes that the code snippet is in a method on the activity that is hosting this UI.
If you want to use a layout which is repeating why don't you prefer using a custom liner layout.
A simple and basic solution is mentioned on this link
http://android-coding-tuts.blogspot.in/2012/02/custom-listview-with-sliding-view-for.html
You should look up Fragments for this. They have a separate control-view structure and you can just create a new fragment for each subview.
Check it out here:
http://developer.android.com/guide/components/fragments.html
I am creating an app that needs to dynamically generate an XML file. The layout starts essentially empty (other than a linear layout), then I loop through a JSON array and create the XML code shown below for EACH element in the array (the only thing different for each element in the array is the ids for the views).
I am not really understanding how I can use Java to create these layouts, edit their attributes, and add views to them.
I know I can create a GridLayout object and give it the number of rows and columns with
GridLayout doc = new GridLayout(3, 3);
But I can't figure out how to edit all of the specific attributes, and then add views inside of the layout.
What is the best way to create a layout, edit it's attributes, and add views within that layout through Jave code?
Thank you.
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="3"
android:layout_marginBottom="10dp"
android:background="#3c37ff00"
android:id="#+id/doctor1"
android:longClickable="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Dr. Sam"
android:id="#+id/doctor1_name"
android:layout_row="0"
android:layout_column="0"
android:textStyle="bold"
android:textSize="26dp"
android:typeface="sans" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/doctor1_profile"
android:layout_row="0"
android:layout_column="1"
android:src="#drawable/no_pic"
android:scaleType="fitXY"
android:layout_rowSpan="3"
android:layout_columnSpan="1"
android:layout_gravity="right"
android:background="#00ffffff" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/doctor1_action"
android:layout_row="0"
android:layout_column="2"
android:src="#drawable/abc_ic_menu_paste_mtrl_am_alpha"
android:scaleType="centerInside"
android:layout_rowSpan="3"
android:layout_columnSpan="1"
android:background="#47ffffff"
android:clickable="true"
android:onClick="setContentView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Very Close"
android:id="#+id/doctor1_distance"
android:layout_row="1"
android:layout_column="0"
android:textStyle="bold" />
</GridLayout>
You could add your views in any viewGroup by:
YourViewGroup.addView(yourChildView);
But I think ListView will serve you better because:
You don't have to worry about each view's handling;
You will have automatic scroll;
You can simply manage your data via Adapter.
EDIT:
As of most recent APIs, you should use a RecyclerView for various reasons. It works similar to the previous adapters and you can accomplish any list/grid behavior you want.
You should start with a single xml document and a parent layout. Then you can give that an id and call it dynamically. You can add to this layout whatever you want. Heres some code I had from doing it. My parent linear layout is my_ll:
LinearLayout ll = (LinearLayout) myInflatedView.findViewById(R.id.my_ll);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
// Use this for each image in the list
final ImageView image = new ImageView(getActivity());
image.setAdjustViewBounds(true); // Scales it to the screen
image.setId(i); // Sets each with unique id
ll.addView(image, params); // Adds the ImageView to screen BEFORE adding image (important)
Picasso.with(getActivity()) // THEN you add the image from photosList
.load(photosList.get(i))
.into(image);
You can add onClickListeners to each view also dynamically. The addView is the part that will add it dynamically.
I have the following layout which displays an image and a text string:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<ImageView
android:id="#+id/ivIcon"
android:layout_width="#dimen/number_icon"
android:layout_height="#dimen/number_icon"
android:scaleType="fitXY"
android:layout_marginTop="#dimen/letter_icon_margin_top" />
<Button
android:id="#+id/btnNumber"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="3"
android:text=""
android:gravity="center"
android:textSize="#dimen/word_size_text"
android:textColor="#000000"
android:textStyle="bold"
android:background="#drawable/borderaction" />
</LinearLayout>
The ivIcon is displaying the images based on user input. It worked great when displaying one image per letter. But how do i fix the above layout so it displays the number of images based on user selection.
#dimen is used to change the view size based on the screen.
For example this is what is looking like when I use it for letter:
For the number, if the user chooses 5 the display should be:
If the user chooses 1 the display should be:
The code that is setting the image for the letter is:
ivLetterIcon = (ImageView) findViewById(R.id.ivIcon);
ivLetterIcon.setImageResource(R.drawable.apple);
How would I accomplish the number images?
Would it be best to have a blank layout without the images and add the ImageView at runtim depending on the number and have it centered? This way the images(s) are centered to the parent view layout?
Well I got an idea maybe you can try this out.
Try using GridView for displaying the number of apples by dynamically changing the column numbers and row numbers depending upon the screen.
For ex, if gridView is the GridView and the entered number is 11, you can do this.
gridView.setNumberOfColumns(5);
gridView.setNumberOfRows(3);
so 5x3 can accomodate 15 elements which can fit in 11 easily.
Then inflate the custom view containing the Apple into the columns.
Make the layout_height for the ImageView a fixed height
--edit--
int dp = TheFixedSize;
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
Imageview.setHeight(px);
I have an XML RelativeLayout snippet that I would like to include several times (from a loop) in my main View. The problem seems to be -- is there a way to avoid hard-coding the parent of the RatingBar, since each time I include the RelativeLayout snippet my elements will need to have different ids?
As far as I can tell, the recommended way is to get the layout snippet and then override the android:id for each element to be unique, and then override the android:layout_below manually for each element that has relative positioning. This seems a little kludgy -- is there any way to have these bindings get done automatically?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:id="#+id/relativeView">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="Label"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView1" />
</RelativeLayout>
you just need to change the id of the RelativeLayout
like
int BASEID=200;
View v = mLayoutInflator.inflate(R.layout.myRelativeLayout, null);
for (int i;i<10;i++){
v.findViewById(R.id.relativeView).setId(i+BASEID);
}
mRootView.addView(v,...);
then when you need to get the RatingBar for suppose the 4th RelativeLayout you added you can call
RatingBar mRatingBar = (RatingBar)mRootView.findViewById(BASEID+3).findViewById(R.id.ratingBar1);