I am using the following to add EditTexts along with cancel buttons dynamically to my main activity.
JAVA
LinearLayout root = (LinearLayout) findViewById(R.id.root_layout);
for(int i=0;i<numberOfEditTexts;i++)
{
LinearLayout editText = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.edittexts,root,false);
editText.setId(View.generateViewId());
root.addView(editText);
}
edittexts XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ems="10"
android:hint="#string/hint_email_id" >
<requestFocus />
</EditText>
<ImageButton
android:id="#+id/buttonDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/cancel_button"
android:onClick="deleteRow"
android:layout_gravity="center"/>
</LinearLayout>
I am already using generateViewId(). Does that ensure unique ids?
More importantly, I want to get/set texts individually on all dynamically created EditTexts. How can I access them?
Related
Good day.
I have an android application. In my application, I have a custom listView with 5 columns that are textViews. The user can click the rows, once he does, the row layout will change, changing the last 2 textViews to EditTexts. I then register the new EditTexts onto my custom keyboard taken from this example - kindly note that I did a functional copy-paste of his example with regards to the custom keyboard class and how to make it work in the main layout. However, when I click the EditText in the row, my custom keyboard does not show up at all.
I have a global variable as such:
CustomKeyboard mCustomKeyboard;
And in my onCreate() method in the activity, I do:
mCustomKeyboard= new CustomKeyboard(this, R.id.keyboardview, R.xml.custom_keyboard);
This is my layout, I have the KeyboardView at the bottom of the Layout:
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".SearchResult" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- A lot of views go here, enclosed in my linear layout -->
</LinearLayout>
<android.inputmethodservice.KeyboardView
android:id="#+id/keyboardview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:visibility="gone" />
</RelativeLayout>
Here is the code that changes the layout. What I do is that I take the row values from the old layout, get the new layout search_result_inflate, then set the texts of the new layout using the values I got. Kindly note the mCustomKeyboard.registerEditText(R.id.qtyInputSearchResult); line after inflating the layout:
private void changeLayout(final View view){
//get views from old layout
TextView textViewQuantity = (TextView)view.findViewById(R.id.qtyInput);
TextView textViewDiscountReq = (TextView)view.findViewById(R.id.discInput);
TextView textViewName = (TextView)view.findViewById(R.id.dialogItemName);
TextView textViewPrice = (TextView)view.findViewById(R.id.price);
TextView textViewDiscount = (TextView)view.findViewById(R.id.discount);
//store values in strings
String itemName = textViewName.getText().toString();
String itemPrice = textViewPrice.getText().toString();
String itemDiscount = textViewDiscount.getText().toString();
String itemQty = textViewQuantity.getText().toString();
String itemDisc = textViewDiscountReq.getText().toString();
//set the view to gone
textViewQuantity.setVisibility(View.GONE);
textViewDiscountReq.setVisibility(View.GONE);
textViewName.setVisibility(View.GONE);
textViewPrice.setVisibility(View.GONE);
textViewDiscount.setVisibility(View.GONE);
//get the old layout
LinearLayout ll_inflate = (LinearLayout)view.findViewById(R.id.search_result_layout);
//get the inflate/new view
View child = getLayoutInflater().inflate(R.layout.search_result_inflate, null);
//get the views in the new view, populate them
TextView newName = (TextView)child.findViewById(R.id.dialogItemName);
newName.setText(itemName);
TextView newDiscount = (TextView)child.findViewById(R.id.discount);
newDiscount.setText(itemDiscount);
TextView newPrice = (TextView)child.findViewById(R.id.price);
newPrice.setText(itemPrice);
EditText qtyInput = (EditText)child.findViewById(R.id.qtyInputSearchResult);
qtyInput.setText(itemQty);
EditText discInput = (EditText)child.findViewById(R.id.discInputSearchResult);
discInput.setText(itemDisc);
//show new layout
ll_inflate.removeAllViews();
ll_inflate.removeAllViewsInLayout();
ll_inflate.addView(child);
mCustomKeyboard.registerEditText(R.id.qtyInputSearchResult);
}
Here is my search_result_inflate.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/search_result_inflate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:minHeight="50dp"
android:orientation="horizontal"
android:padding="1dip"
android:weightSum="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="horizontal"
android:padding="1dip" >
<TextView
android:id="#+id/dialogItemName"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:layout_weight="0.54"
android:gravity="center"
android:text="Item Name"
android:textSize="23sp" />
<TextView
android:id="#+id/price"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:layout_weight="0.14"
android:gravity="center"
android:text="Price"
android:textSize="23sp" />
<TextView
android:id="#+id/discount"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:layout_weight="0.10"
android:gravity="center"
android:text="Discount"
android:textSize="23sp" />
<EditText
android:id="#+id/qtyInputSearchResult"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:layout_weight="0.14"
android:background="#layout/edittext_selector"
android:gravity="center"
android:hint="qtyInput"
android:textColorHighlight="#color/white_opaque"
android:textSize="23sp" >
</EditText>
<EditText
android:id="#+id/discInputSearchResult"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.11"
android:background="#layout/edittext_selector"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:hint="discInput"
android:textColorHighlight="#color/white_opaque"
android:textSize="23sp" />
</LinearLayout>
</LinearLayout>
As you can see, I have 2 editTexts and I registered qtyInputSearchResult to the custom keyboard class. However, the custom keyboard does not show up.
I also tried to use the custom keyboard class on an editText in another activity and it works just fine. Am I missing something here? I'm confused as to why the custom keyboard does not show up properly.
Any help is very much appreciated, thank you.
Got it, I placed the keyboard layout in the search_result_inflate.xml like so:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/search_result_inflate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:minHeight="50dp"
android:orientation="horizontal"
android:padding="1dip"
android:weightSum="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="horizontal"
android:padding="1dip" >
<!-- a lot of components here -->
</LinearLayout>
<android.inputmethodservice.KeyboardView
android:id="#+id/keyboardview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:visibility="gone" />
</RelativeLayout>
I realize there many related problems, but I'm looking for this specific answer. The below code is my XML for my mainactivity in an app. Now my question. I would like to be able to alter the contents of the horizontal view, dynamically. This will involve clearing it of Views as well as adding new ones. How exactly do I go about doing this, as well as accessing the Linear Layout within it, when I tried to assign an ID to the Linear Layout I was given an error saying it could not accept a id of type string.
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/txtClassification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Classification:<>"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<Spinner
android:id="#+id/spinGoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="56dp" />
<Button
android:id="#+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Go Back"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/btnSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Select"
tools:ignore="HardcodedText" />
<HorizontalScrollView
android:id="#+id/gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtClassification"
android:layout_alignRight="#+id/spinGoto"
android:layout_below="#+id/txtClassification" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
<ImageView
android:id="#+id/myImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
All help is greatly appreciated!
You will have to work with the LinearLayout because a HorizontalScrollView can only have one direct child.
<HorizontalScrollView
android:id="#+id/gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtClassification" >
<LinearLayout
android:id="#+id/yourLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
Then you access it programmatically to add or remove views
public void addView(View v) {
LinearLayout yourLayout = (LinearLayout) findViewById(R.id.yourLayout);
yourLayout.addView(v);
}
public void clearLayout() {
LinearLayout yourLayout = (LinearLayout) findViewById(R.id.yourLayout);
yourLayout.removeAllViews();
}
public void removeView(View v) {
LinearLayout yourLayout = (LinearLayout) findViewById(R.id.yourLayout);
yourLayout.removeView(v);
}
hope this helps
The id has a predifined format. Enter the name you want after the "#+id/" syntax. Example android:id="#+id/layout".
Then in code get the LinearLayoutinside of the HorizontalScrollView:
LinearLayout layout = findViewById(R.id.layout);
To add a View to layout call the adView() of your layout. Example:
//add 5 buttons
for (int i = 1; i <= 5 ; i++){
Button btn = new Button(this);
btn.setId(i);
btn.setText(i+"");
layout.addView(btn);
}
To remove a View from the layout call removeView()
//remove button nr. 3
Button btn3 = layout.findViewById(3);
layout.removeView(btn3);
The HorizontalScrollView takes care of everything, you just have to populate and organize the elements inside the nested LinearLayout.
Good luck!
I have below layout i.e 1 spinner and a linear layout containing some edit box's and spinner's.this layout maps/represent to an object (Lets call it X).
The main spinner will be having n number of entries and each entry maps to the layout object (X).So, In all i will need to have n number of layouts
I want to allow user to fill only 1 object at a time ,so, I would keep only 1 layout visible.In order to solve this , One way would be having n number of layouts in .xml and playing with the visibility in onitemselected of the listener.Is there any other better/optiomized way of solving this.
How can i make this dynamic i.e if i dont know the value of n initially ?
`<Spinner
android:id="#+id/linesspinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/linView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/linename1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/linecffiltext" />
<Spinner
android:id="#+id/trospinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/Tro_arrays"
android:prompt="#string/linetrotext" />
<EditText
android:id="#+id/line1troval"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
</EditText>
<Spinner
android:id="#+id/cfspinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/cf_arrays"
android:prompt="#string/linecffiltext" />
<EditText
android:id="#+id/line1cfval"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
</EditText>
</LinearLayout>
try this, put that LinearLayout in another xml and call that xml as many times you want inside loop.
sample xml, name attrib_row.xml
<?xml version="1.0" encoding="utf-8"?>
<Linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/lable"
android:layout_width="150dip"
android:layout_column="0"
android:padding="3dip"
android:textColor="#000033" />
</Linearlayout>
code to call that in loop
Linearlayout row11 = (Linearlayout) LayoutInflater.from(this).inflate(
R.layout.attrib_row, null);
((TextView) row11.findViewById(R.id.lable)).setText("example");
I'm new to android application development. I'm trying to create a calculator application, and I got 2 questions to ask.
1.
How can I create a new row in LinearLayout?
It's currently looking like this:
[textview][textview][button]
And I want it to look like this:
[textview][textview]
[button]
This is my xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="#+id/number1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/number1" />
<TextView
android:id="#+id/number2"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/number2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_calculate" />
2. How can I process the sum of "number1" and "number2" Textviews and display it on the Main Activity (where the TextViews are)?
Linear layout can be horizontal or vertical, if you need rows and columns you need to work with relative layout or just add more then one linear layout such as..
TextView txtvar = (TextView) findViewById(R.id.textViewIdFromXml);
txtvar.setText("blah");
this how you connect to textview element whit id textViewIdFromXml and set blah for text
Nested linear layouts.
For example (pseudo xml):
<Linear Layout : Vertical>
<Linear Layout : Horizontal>
<TextView />
<TextView />
</Linear Layout>
<Button />
</Linear Layout>
Edit: I tried the following in Eclipse and for me it does what I think you ask (though it doesn't look fancy):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
1st Ans: create a Vertical Linear Layout & use a Horinzental one nested in the Vertical one. But I prefer relative layout.
2ns Ans: use onClickListener() for the buttons. & under every onClickListener() change the text of Number display textbox using setText(). but on = button's onClickListener() use the value of Number display textbox using getText() and use it for + - or in * /
I have a layout where I put i rows dynamically in a tablelayout that is inside a scrollview. Everything is working smoothly except that I don't can get the checkbox text to wordwrap (See xml below). The text is also set at runtime. I have searched the internet and has made a lot of trying to set different properties with no luck so far.
Is this task possible to achieve?
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dip">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="#+id/bes_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<EditText
android:id="#+id/bes_kommentar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/bes_checkbox" />
</RelativeLayout>
</TableRow>
CheckBox extends TextView, so I assume you can wrap its text like they solve the issue in this post :
Android TextView Text not getting wrapped
If you cannot get it to work like that, you can maybe also try, using match_parent in the checkbox.
<CheckBox
android:id="#+id/bes_checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
hope it helps.
use table row inside the Linear layout instead of table layout..
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dip" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/bes_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/bes_kommentar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bes_checkbox" />
</RelativeLayout>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dip" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/bes_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/bes_kommentar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bes_checkbox" />
</RelativeLayout>
</TableRow>
</LinearLayout>
I have made dynamic layouts with checkboxes in the past and used this approach:
// Create new view to add layout to
sv = new ScrollView(this);
// Create new layout to add check boxes to
ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
// Add layout to view
sv.addView(ll);
// Create 6 check boxes
int itemSize = 6;
for(int j=0; j<itemSize; j++){
CheckBox cb = new CheckBox(this);
cb.setText("number " + j);
ll.addView(cb);
}
// Finalize view
this.setContentView(sv);
Is this what you are looking for?