I would like to know how I could dynamically write the data below in Java. I am stuck on how you write the "layout_alignParent" can set the gravity of each text view....
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center_vertical" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView3"
android:layout_alignBottom="#+id/textView3"
android:layout_alignParentRight="true"
android:gravity="center_vertical" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />
I know that the code below would not work. But how could I re-write it for it to work?
RelativeLayout m = (RelativeLayout)findViewById(R.id.tileContainerME);
EditText et1 = new EditText(this);
EditText et2 = new EditText(this);
EditText et3 = new EditText(this);
EditText et4 = new EditText(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
et1.setLayoutParams(params);
params.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
et2.setLayoutParams(params);
params.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.removeRule(RelativeLayout.CENTER_VERTICAL);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
et3.setLayoutParams(params);
params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
et4.setLayoutParams(params);
params.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
params.removeRule(RelativeLayout.CENTER_HORIZONTAL);
et1.setGravity(Gravity.CENTER_HORIZONTAL);
et1.setHint("Test");
et2.setGravity(Gravity.CENTER_VERTICAL);
et2.setHint("Test");
et3.setGravity(Gravity.CENTER_VERTICAL);
et3.setHint("Test");
et4.setGravity(Gravity.CENTER_HORIZONTAL);
et4.setHint("Test");
m.addView(et1);
m.addView(et2);
m.addView(et3);
m.addView(et4);
I think this code will help you:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
textView.setLayoutParams(params);
Edited:
params.gravity = Gravity.CENTER_HORIZONTAL;
tv2.setLayoutParams(params);
Related
I have a problem to display all data in a AlertDialog where the data just last records displayed. I tried adding a scrollview in a AlertDialog but scrollview doesn't show and still last records displayed which actually the qty all data more than 13rd rows.
TransactionActivity.java
private void inputUser() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setGravity(Gravity.CLIP_VERTICAL);
layout.setPadding(30, 30, 30, 30);
View view2 = getLayoutInflater().inflate(R.layout.books_list,null);
final ScrollView scrollView = new ScrollView(this);
//showing all data
for (Books list : booksList) {
String id = String.valueOf(list.getId());
String Item_name = list.getItem_name();
String Price = String.valueOf(list.getPrice());
String file = list.getFile();
final TextView txtId = (TextView) view2.findViewById(R.id.txtId);
txtId.setText(id);
final TextView txtItem_name = (TextView) view2.findViewById(R.id.txtItem_name);
txtItem_name.setText(Item_name);
final TextView txtPrice = (TextView) view2.findViewById(R.id.txtPrice);
txtPrice.setText(Price);
final ImageView img = (ImageView) view2.findViewById(R.id.file);
Picasso.with(this).load(URI_SEGMENT_UPLOAD + file).into(img);
}
scrollView.addView(view2);
builder.setView(scrollView);
builder.setTitle("PRODUCTS LIST");
builder.setCancelable(false);
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
});
final AlertDialog dialog = builder.create();
dialog.show();
}
books_list.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:layout_weight="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#e6e6e6"
android:orientation="vertical"
android:padding="1dp"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical"
android:padding="1.5dp">
<ImageView
android:id="#+id/file"
android:layout_width="124dp"
android:layout_height="96dp"
android:layout_gravity="top|center"
android:padding="5dp"
android:src="#drawable/ic_product" />
<View
android:layout_width="match_parent"
android:layout_height="1.5dp"
android:background="#e6e6e6" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/txtId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="2dp"
android:text="id" />
<TextView
android:id="#+id/txtItem_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Item Name" />
<TextView
android:id="#+id/txtPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/txtItem_name"
android:text="0" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
I'm trying to add multiple ConstraintLayouts below each other. But they appear to be on the same position. (See picture).
Question: How do I get the ConstraintLayouts to be below each other?
What's happpening right now:
Example 1:
Example 2:
How it should look like:
Activity.java
String response = (String) databaseManager.execute(phpLocation, parameters).get();
List<Review> listOfReviews = (List<Review>) EntityConverter.getInstance().jsonToObject(response,
new TypeToken<Collection<Review>>(){}.getType());
int totalscore = 0;
int amountOfReviews = 0;
RelativeLayout relativeLayoutReviews = (RelativeLayout) findViewById(R.id.relativeLayoutReviews);
for(Review r : listOfReviews) {
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.reviewtemplate, null);
v.setId(amountOfReviews);
TextView name = (TextView) v.findViewById(R.id.textViewReviewName);
name.setText(r.getName());
RatingBar ratingBar = (RatingBar) v.findViewById(R.id.ratingBarReviewScore);
ratingBar.setRating(r.getScore());
totalscore += r.getScore();
TextView ratingText = (TextView) v.findViewById(R.id.textViewReviewText);
ratingText.setText(r.getReviewtext());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (amountOfReviews != 0) {
// add the rule that places your button below your EditText object
params.addRule(RelativeLayout.BELOW, v.getId() -1);
}
relativeLayoutReviews.addView(v, amountOfReviews);
amountOfReviews++;
}
reviewtemplate.xml (The thing which I'm trying to insert)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:id="#+id/reviewTemplate"
android:layout_height="match_parent">
<TextView
android:id="#+id/textViewReviewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Pietje"
android:textSize="24sp"
android:textColor="#000000"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent" />
<RatingBar
android:id="#+id/ratingBarReviewScore"
android:layout_width="243dp"
android:layout_height="58dp"
android:numStars="5"
android:rating="0"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/textViewReviewName"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="#+id/textViewReviewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="Text"
android:textColor="#000000"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/ratingBarReviewScore" />
</android.support.constraint.ConstraintLayout>
RelativeLayout which will contain all the reviews.
<RelativeLayout
android:id="#+id/relativeLayoutReviews"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="1dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</RelativeLayout>
That happens because the root (parent) layout is RelativeLayout instead of LinearLayout.
Child views added into a RelativeLayout do not position one after another because their parent lays out their positioning depending on their child attributes (hence why the positioning is relative).
Add them into a LinearLayout with vertical orientation instead.
You should use a LinearLayout for your Layout Reviews, with a orientation vertical.
EDIT:
Use Chains attibute
Can you try ?
<RelativeLayout
android:id="#+id/relativeLayoutReviews"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="1dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="spread" >
</RelativeLayout>
Hope this helps.
I am trying to create the rows below the table heading programmatically, but they are spilling over and I am not sure how to split them up to only take up 25% of the width each?
Here is the code for the row creation:
TableRow row = new TableRow(activity);
lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT, 1.0f);
tl = new TableLayout.LayoutParams();
tl.weight = 1;
lp.height = 100;
row.setLayoutParams(lp);
row.setWeightSum(4);
tv = new TextView(getActivity());
tv.setLayoutParams(tl);
tv.setTextSize(14);
tv.setText(whotos.get(i));
tv2 = new TextView(getActivity());
tv2.setLayoutParams(tl);
tv2.setTextSize(14);
tv2.setText(documents.get(i));
tv3 = new TextView(getActivity());
tv3.setLayoutParams(tl);
tv3.setTextSize(14);
tv3.setText(directions.get(i));
tv4 = new TextView(getActivity());
tv4.setLayoutParams(tl);
tv4.setTextSize(14);
tv4.setText(thedates.get(i));
row.addView(tv); //tp name
row.addView(tv2); //document type
row.addView(tv3); //direction
row.addView(tv4); //date
ll.addView(row, i + 1 + tableHeadingRows);
Here is my code for the layout above the programmatically created rows:
<TableRow
android:id="#+id/tableRow2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="5dip"
android:stretchColumns="1"
android:weightSum="4">
<TextView
android:id="#+id/tp"
android:layout_weight="1"
android:layout_width="0dp"
android:text="TP"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/doc"
android:layout_weight="1"
android:layout_width="0dp"
android:text="Doc"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/dir"
android:layout_weight="1"
android:layout_width="0dp"
android:text="Direction"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/date"
android:layout_weight="1"
android:layout_width="0dp"
android:text="Date"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<!-- draw a red line -->
<View
android:layout_height="2dip"
android:background="#color/tableHorizontalRule" />
Does anyone have a solution to this issue?
I changed this over to a LinearLayout, as it simplified things a bit and allowed me to easily set column weights programmatically.
I've been trying to get the control arrows in the standard D-Pad orientation centered and underneath the video stream but this is the best I've been able to do so far based on what I believe should be the correct code. Notice that the up and down arrows are in the correct location but the Left arrow doesn't show and the Right arrow is on the left. Either I'm not understanding something correctly or there is something affecting the arrangement of the arrows that I'm not catching.
RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
setContentView(rl, relativeLayoutParams);
mv = new MjpegView(this);
rl.addView(mv);
Button panLeft = new Button(this);
panLeft.setBackgroundResource(R.drawable.arrow_left);
int panLeftID = View.generateViewId();
panLeft.setId(panLeftID);
Button panRight = new Button(this);
panRight.setBackgroundResource(R.drawable.arrow_right);
int panRightID = View.generateViewId();
panRight.setId(panRightID);
Button tiltUp = new Button(this);
tiltUp.setBackgroundResource(R.drawable.arrow_up);
int tiltUpID = View.generateViewId();
tiltUp.setId(tiltUpID);
Button tiltDown = new Button(this);
tiltDown.setBackgroundResource(R.drawable.arrow_down);
int tiltDownID = View.generateViewId();
tiltDown.setId(tiltDownID);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
relativeLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
rl.addView(tiltDown, relativeLayoutParams);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.LEFT_OF, tiltUpID);
relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rl.addView(panLeft, relativeLayoutParams);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF, tiltDownID);
relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rl.addView(panRight, relativeLayoutParams);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.ABOVE, tiltDownID);
relativeLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
rl.addView(tiltUp, relativeLayoutParams);
And here is what it looks like:
http://s23.postimg.org/otbyon2a3/screenie.png
What about a grid layout?
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:columnCount="3"
android:orientation="horizontal"
android:id="#+id/dpad_grid"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Up"
android:id="#+id/button4"
android:layout_row="0"
android:layout_column="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right"
android:id="#+id/button3"
android:layout_row="1"
android:layout_column="2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Down"
android:id="#+id/button2"
android:layout_row="2"
android:layout_column="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left"
android:id="#+id/button5"
android:layout_row="1"
android:layout_column="0"/>
</GridLayout>
To echo #Dave S you should really use layouts for something like this.
I'm a newbie.
I want to add 3 EditText's when a user click on a button.
I try to realize it, but it isn't work.
Widgets do not appear.
P.S. Widgets must be in others layout's.
Thank you for help.
Sorry for my bad English.
<?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:background="#drawable/bg_fill"
android:id="#+id/simple_main_layout">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/for_main_text">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Buy List"
android:id="#+id/textView" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="67dp"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/do_new_list"
android:layout_marginLeft="153dp"
android:layout_marginRight="2dp"
android:background="#drawable/btn_yes" android:textColor="#804f29" android:textStyle="bold"/>/>
</LinearLayout>
<EditText
android:layout_width="305dp"
android:layout_height="wrap_content"
android:id="#+id/editText" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="91dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText3" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Code:
private Button do_new_field;
private EditText[] text, many, cost;
private final int fields = 50;
private int last_field = 0;
private LinearLayout layoutik;
LinearLayout.LayoutParams layoutParams;
LinearLayout.LayoutParams layoutParams2;
#Override
public void onBackPressed() {
super.onBackPressed();
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_buy);
do_new_field = (Button) findViewById(R.id.do_new_list);
layoutik = (LinearLayout) findViewById(R.id.for_main_text);
do_new_field.setOnClickListener(this);
text = new EditText[fields];
many = new EditText[fields];
cost = new EditText[fields];
layoutParams =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT,0f);
layoutParams2 =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT,0f);
}
#Override
public void onClick(View view) {
if(view == do_new_field)
{
if(last_field <= fields)
{
text[last_field] = new EditText(this);
many[last_field] = new EditText(this);
cost[last_field] = new EditText(this);
text[last_field].setWidth(305);
text[last_field].setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
text[last_field].setMaxLines(3);
many[last_field].setWidth(91);
many[last_field].setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
many[last_field].setInputType(InputType.TYPE_CLASS_NUMBER);
many[last_field].setMaxLines(1);
cost[last_field].setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
cost[last_field].setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
cost[last_field].setInputType(InputType.TYPE_CLASS_NUMBER);
cost[last_field].setMaxLines(1);
text[last_field].setId(100 + last_field);
many[last_field].setId(200 + last_field);
cost[last_field].setId(300 + last_field);
text[last_field].setLayoutParams(layoutParams);
many[last_field].setLayoutParams(layoutParams);
cost[last_field].setLayoutParams(layoutParams);
layoutik.addView(text[last_field]);
layoutik.addView(many[last_field]);
layoutik.addView(cost[last_field]);
last_field++;
}
}
}
}
You might have to add "setId" to the editText and check.
TableLayout.LayoutParams params = new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0f);
editText.setId(EDT_ID + sub_count + i);
editText.setTag(insertAdAl.get(i).getmApiAppend());
editText.setLayoutParams(params);
editText.setTextAppearance(NewAdverts.this,
android.R.style.TextAppearance_Small);
editText.setGravity(Gravity.LEFT | Gravity.CENTER);
editText.setPadding(4, 4, 4, 4);
editText.setTextAppearance(NewAdverts.this,
android.R.attr.textAppearanceSmall);
editText.setTextColor(Color.BLACK);
editText.setBackgroundResource(R.drawable.notification_field);