I have a linear layout set to vertical where when a button is clicked items are added to it but they are all added vertically.
I would like the fields to be added horizontally each row such that on the next click the new fields are added below the current fields,
setting the layout to horizontal achieves this but on the next click the items are stacked onto each other
This is the xml file
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toplayout">
<Button <!--..onclick of this fields are added below-->
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add_household"
android:layout_alignParentRight="true"
android:layout_margin="5dp"
android:id="#+id/add_households_btn" />
</RelativeLayout>
<LinearLayout ..fields are added in this layout
android:orientation="vertical"
android:layout_below="#+id/toplayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/household_items_layout"
android:weightSum="1">
</LinearLayout>
Am adding the fields dynamically and this is the code:
public void add_houses(){ //this is called when the button is clicked
itemslayout.setWeightSum(1);
//sets widget items
Button removeitems = new Button(this);
EditText surname = new EditText(this);
EditText first_name = new EditText(this);
EditText middle_name = new EditText(this);
Spinner gender = new Spinner(this);
EditText dob = new EditText(this);
...other fields here
//set fields properties
surname.setBackgroundResource(android.R.drawable.edit_text);
first_name.setBackgroundResource(android.R.drawable.edit_text);
//set hint for edit texts
surname.setHint("Surname");
first_name.setHint("First name");
...
//set layouts params
LinearLayout.LayoutParams itemsdetails = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.3f
);
itemslayout.addView(surname, itemsdetails);
itemslayout.addView(first_name, itemsdetails);
itemslayout.addView(middle_name, itemsdetails);
itemslayout.addView(dob, itemsdetails);
itemslayout.addView(gender, itemsdetails);
}
The above generates this
How do i stack up the fields horizontally and yet on button click the new fields are set below(vertically)
Related
Problem: I'm having difficulty getting an EditText field to fit within a table cell dimensions.
What I've tried: I have tried a number of suggestions, like below in my code, stated here on StackOverflow but nearly all are static and not dynamic examples. I looked at a couple videos which were about the same.
What I'm trying to do: I'm trying to give the user an option to add a row of data to a table and fill in the fields. The following code works out well except I cannot seem to find the correct way of stating the LayoutParams to fit the EditText within the cell boundaries.
for(int r=1; r<5;r++) {
EditText editText = new EditText(this);
editText.setBackgroundColor(Color.WHITE);
editText.setTextSize(12);
editText.setSingleLine();
editText.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
row.addView(editText);
}
<TableLayout
android:id="#+id/table_authors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/colorDkGray"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/btn_add_author"
android:layout_marginTop="15dp"
android:layout_marginEnd="5dp"
android:layout_marginStart="5dp"/>
The code to setup table in the EditNote activity:
tableLayoutAuthors = findViewById(R.id.table_authors);
tableLayoutAuthors.addView(setupAuthorsTableRow("+", "Organization/First", "Middle Name", "Last Name","Suffix", true));
btnAddAuthor.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tableLayoutAuthors.addView(setupAuthorsTableRow("-","", "", "", "", false));
}
});
You need to make few adjustments:
In the xml TableLayout add android:stretchColumns="*" which means that stretch all columns.
<TableLayout
android:id="#+id/table_authors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="716dp"
android:orientation="vertical"
android:stretchColumns="*"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
You need to set editText width to 0. Because with WRAP_CONTENT it respects the content width it has to wrap & not the cell boundaries.
This is how sample code would look like:
TableLayout tableLayoutAuthors = findViewById(R.id.table_authors);
TableRow row = new TableRow(this);
for (int r = 1; r < 5; r++) {
EditText editText = new EditText(this);
editText.setBackgroundColor(Color.WHITE);
editText.setTextSize(12);
editText.setText("test");
editText.setSingleLine();
editText.setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT));
row.addView(editText);
}
tableLayoutAuthors .addView(row);
With the help of Mayur Gajra, I was able to figure out how to fix my issue. Posted below is what fixed my issue:
Adding this line to the XML:
android:stretchColumns="*"
Changing to creating an instance of TableRow.LayoutParams and setting the margins and setting the padding for the editText:
for(int r=1; r<5;r++) {
EditText editText = new EditText(this);
// Change:
TableRow.LayoutParams trLayoutParams = new TableRow.LayoutParams();
trLayoutParams.setMargins(3,3,3,3);
editText.setLayoutParams(trLayoutParams);
editText.setBackgroundColor(Color.WHITE);
editText.setTextSize(12);
editText.setGravity(Gravity.CENTER);
editText.setSingleLine();
// Change:
editText.setPadding(8, 8, 8, 8);
row.addView(editText);
row.setClickable(true);
}
Please bear with me as I am new to the use of Views and Layouts.
I am trying to create an application in which the user can enter a text field, press a button and have a new text field appear and they can continue adding text fields in this way.
My solution was to have the top level be a scrollview and then have a relative view as a child within that(this way I can then programatically insert more editviews in my code with the OnClick() listener.
I have seen and read a couple of other posts pertaining to relative views but it seems there is still something that I am missing. I have tried
Here is the xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_create_accounts"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.nic.mybudget.CreateAccountsActivity">
<RelativeLayout
android:id="#+id/activity_create_accounts_relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/activity_name"
android:inputType="textAutoComplete"
android:layout_alignParentTop="true"
android:id="#+id/activity_createAccounts_relativeLayout_activityName"/>
<Button
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="#+id/activity_createAccounts_relativeLayout_activityName"
android:id="#+id/activity_create_accounts_relativeLayout_activityName_addButton" />
<Button
android:text="Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/activity_create_accounts_relativeLayout_activityName_addButton"
android:layout_centerHorizontal="true"
android:id="#+id/activity_create_accounts_relativeLayout_activityName_saveButton" />
</RelativeLayout>
And here is the Code where I try to add new editviews.
public class CreateAccountsActivity extends Activity {
static private final String TAG = "MAIN-Activity";
int numAccounts = 0;
int lastAccountID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_accounts);
final RelativeLayout Relative = (RelativeLayout) findViewById(R.id.activity_create_accounts_relativeLayout);
final TextView oldAccount = (TextView) findViewById(R.id.activity_createAccounts_relativeLayout_activityName);
final TextView newAccount = new TextView(this);
final Button addNewAccountButton = (Button) findViewById(R.id.activity_create_accounts_relativeLayout_activityName_addButton);
addNewAccountButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "addNewAccountOnClick");
numAccounts = numAccounts+1;
int newAccountID = oldAccount.getId() + numAccounts;
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
newAccount.setLayoutParams(rlp);
newAccount.setHint("Hint" );
newAccount.setId(newAccountID);
Relative.addView(newAccount);
RelativeLayout.LayoutParams blp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
blp.addRule(RelativeLayout.BELOW, newAccountID-1);
addNewAccountButton.setLayoutParams(blp);
}
});
}
}
As you can see what I am trying (and failing) to do is add the new edit view at the top of the page and simply push everything else down the page. What am I getting wrong here with the relative layout?
Any help is appreciated.
First thing View with id activity_createAccounts_relativeLayout_activityName is EditText and you are casting it with TextView so that is wrong cast it to EditText.
And to your actual problem:
You are using same EditText instance with variable newAccount and adding it again in relative layout if you want to add one more EditText in relative layout you have to initialise EditText inside onclicklistener.
Just add one line newAccount= new EditText(context)in your onclicklistener code before line numAccounts = numAccounts+1;
Happy Coding !
Here is what I have:
LinearLayout horizontalLL = new LinearLayout(thisActivity);
horizontalLL.setOrientation(LinearLayout.HORIZONTAL);
horizontalLL.setLayoutParams(new LinearLayoutCompat.LayoutParams(LinearLayoutCompat.LayoutParams.MATCH_PARENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT,1));
TextView captionTV = new TextView(thisActivity);
captionTV.setText(element.description);
captionTV.setLayoutParams(new LinearLayoutCompat.LayoutParams(LinearLayoutCompat.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT));
editText.setLayoutParams(new LinearLayoutCompat.LayoutParams(LinearLayoutCompat.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT,0.6f));
horizontalLL.addView(captionTV);
horizontalLL.addView(editText);
LL.addView(horizontalLL);
but the result is bad: the editText is still very short and does not fill the horizontal linear layout. I've tried to set the weight for captionTV too, but still no result.
Please do the change as below
editText.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,0.6f));
You can use LinearLayout.LayoutParams.MATCH_PARENT or LinearLayout.LayoutParams.WRAP_CONTENT instead of 0 here.
If possible replace all LinearLayoutCompat to LinearLayout.
Try the following code, if you want that your EditText automatically cover the remaining space:
LinearLayout horizontalLL = new LinearLayout(this);
horizontalLL.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
horizontalLL.setOrientation(LinearLayout.HORIZONTAL);
TextView captionTV = new TextView(this);
captionTV.setText("element.description");
captionTV.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
editText.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT , 1));
horizontalLL.addView(captionTV);
horizontalLL.addView(editText);
LL.addView(horizontalLL);
You need to use below code :
LinearLayout ll1 = (LinearLayout)findViewById(R.id.ll1);
android.widget.LinearLayout horizontalLL = new android.widget.LinearLayout(this);
horizontalLL.setOrientation(android.widget.LinearLayout.HORIZONTAL);
horizontalLL.setLayoutParams(new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, 150,1f));
TextView captionTV = new TextView(this);
captionTV.setText("Hello textview ");
captionTV.setTextColor(Color.BLUE);
captionTV.setLayoutParams(new android.widget.LinearLayout.LayoutParams(0, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,0.4f));
horizontalLL.addView(captionTV);
EditText editText = new EditText(this);
editText.setText("Hello edittext ");
editText.setTextColor(Color.BLUE);
editText.setLayoutParams(new android.widget.LinearLayout.LayoutParams(0, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,0.6f));
horizontalLL.addView(editText);
ll1.addView(horizontalLL);
because in XML weight define like below:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1.0">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.40"
android:text="test"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.60"
android:text="test"/>
</LinearLayout>
I'm trying to generate a dynamic screen that contains a "textview", "edittext","button" and "listview". I'm trying to add what is written in the edittext to my listview. I'm not getting any errors and i can see added items inside my ArrayList when i debug the code. But listview shows only the very first item added into the list.
LayoutParams layoutMatchParent = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
LayoutParams layoutWrapContent = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LayoutParams layoutMatchParentWidth = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ScrollView scrollView = new ScrollView(this);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
scrollView.addView(linearLayout);
setContentView(scrollView, layoutMatchParent);
LinearLayout vgMul=new LinearLayout(this);
vgMul.setOrientation(LinearLayout.VERTICAL);
TextView tvMul=new TextView(this);
tvMul.setText("Some Label");
tvMul.setId(1);
tvMul.setLayoutParams(layoutWrapContent);
vgMul.addView(tvMul);
EditText edtMul=new EditText(this);
edtMul.setId(2);
edtMul.setLayoutParams(layoutMatchParentWidth);
vgMul.addView(edtMul);
Button btnAddMul=new Button(this);
btnAddMul.setText("Add");
btnAddMul.setLayoutParams(layoutMatchParentWidth);
vgMul.addView(btnAddMul);
ListView lvMul =new ListView(this);
lvMul.setId(3);
lvListItems =new ArrayList<String>();
lvArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lvListItems);
lvMul.setAdapter(lvArrayAdapter);
lvMul.setScrollContainer(false);
vgMul.addView(lvMul);
btnAddMul.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
EditText edtMul=(EditText)findViewById(2);
lvListItems.add(edtMul.getText().toString());
lvArrayAdapter.notifyDataSetChanged();}
});
linearLayout.addView(vgMul);
Is this because of the scrollview? I'm adding my controls as children of a linear layout but i'm stuck...Any help will be appreciated...Thanks...
Remove your scroll view, a listview already implements a scrollview.
Alright, so I assume you have an activity, some part of that activity has a listview, you want to scroll on the listview and also scroll the whole activity.
You can do it by creating the listview in another Relative Layout (or Linear layout). Let me write an example for you in xml
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
//Your text views and other stuff goes here
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/SecondRelativeLayoutDetailedPage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:background="#color/holoActionBarColor">
<ListView
android:id="#+id/myRewardsActiveList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/myActiveRewardsHeadingLinLayout">
</ListView>
</RelativeLayout>
</RelativeLayout>
Does this make more sense now?
I am making an alert dialog in an application.Because of more text present in the alert dialog,i want to put vertical scroll bar in the alert dialog.I have tried many options,But nothing is working.Please tell me how to solve this problem.This is the code :
AlertDialog.Builder HelpOnButtonDialog ;
HelpOnButtonDialog = new AlertDialog.Builder(this);
TextView HelpOnButtonView = new TextView(this);
HelpOnButtonView.setSingleLine(false);
HelpOnButtonView.setTextColor(getResources().getColor(R.color.dark_green));
HelpOnButtonView.setText("hello");
Button HelpOnButton = new Button(this);
HelpOnButton.setHeight(20);
HelpOnButton.setWidth(20);
HelpOnButton.setText("Ok");
HelpOnButton.setOnClickListener(HelpOnButtonClickListener);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams( new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(1);
linearLayout.setBackgroundColor(getResources().getColor(R.color.black));
linearLayout.addView(HelpOnButtonView);
linearLayout.addView(HelpOnButton);
ScrollView sv = new ScrollView(this);
sv.pageScroll(0);
sv.setBackgroundColor(0);
sv.setScrollbarFadingEnabled(true);
sv.setVerticalFadingEdgeEnabled(false);
sv.addView(linearLayout);
alertHelp = HelpOnButtonDialog.create();
alertHelp.setView(sv);
alertHelp.show();
Create a custom dialog you can customize it as you wish. In the dialog box layout xml file
surround your RelativeLayout or LinearLayout with scrollable like below code
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:orientation="vertical"
android:layout_weight="1">
<!--Your other text views, buttons... etc surrounded by RelativeLayout
or LinearLayout goes here-->
</ScrollView>
When the content exceeds dlalog box size it will display the scroll bar.
Hope this will help you :)