How to get value of material dialog box input field in Android? - java

I have a material alert dialog box like this:
Here is the java code for the dialog box:
public void openAddNoteDialog() {
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(MainActivity.this, R.style.dialogBoxTheme);
builder.setTitle("Add note");
builder.setBackground(getResources().getDrawable(R.drawable.dialog_box, null));
builder.setIcon(R.drawable.ic_add_note);
builder.setView(R.layout.addnote_dialog);
builder.setPositiveButton("Add", ((dialogInterface, i) -> handleAddNote()));
builder.setNegativeButton("Cancel", (dialogInterface, i) -> dialogInterface.dismiss());
builder.show();
}
And here is the layout that is in the dialog box:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/notenameFLD"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="260dp"
android:layout_height="90dp"
android:layout_gravity="center"
android:layout_marginTop="25dp"
android:hint="#string/notename"
android:textColorHint="#color/white"
app:boxStrokeColor="#color/white"
app:counterEnabled="true"
app:counterMaxLength="20"
app:counterTextColor="#color/white"
app:helperTextTextColor="#color/white"
app:hintTextColor="#color/white">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/notenameEDITFLD"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/opensans_regular"
android:maxLength="20"
android:textColor="#color/white"
android:textSize="20sp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/notecontentFLD"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="260dp"
android:layout_height="90dp"
android:layout_gravity="center"
android:layout_marginBottom="25dp"
android:hint="#string/notecontent"
android:textColorHint="#color/white"
app:boxStrokeColor="#color/white"
app:counterEnabled="true"
app:counterMaxLength="250"
app:counterTextColor="#color/white"
app:helperTextTextColor="#color/white"
app:hintTextColor="#color/white">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/notecontentEDITFLD"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/opensans_regular"
android:maxLength="250"
android:textColor="#color/white"
android:textSize="20sp" />
</com.google.android.material.textfield.TextInputLayout>
Now, I'm trying to get the value of these input fields in other function called handleAddNote().
final TextInputEditText noteNameFLD = findViewById(R.id.notenameEDITFLD);
final TextInputEditText noteContentFLD = findViewById(R.id.notecontentEDITFLD);
The problem is, when I'm trying to get the values of these input fields as strings, they return nothing. What is the problem here? Is this because the findViewById() can't find the fields as they are inside of the alert dialog box and not in the actual activity?
Ps. sorry if this is confusing, I can add more info if needed.

Is this because the findViewById() can't find the fields as they are
inside of the alert dialog box and not in the actual activity?
Yes, you are correct :) you need to get them on the view you set to your dialogBuilder. Change the way you set your dialog view to:
final View dialogView = LayoutInflater.from(context).inflate(R.layout. addnote_dialog, null);
alertDialogBuilder.setView(dialogView);
Then, you can find the views using dialogView.findViewById().

Related

setVisibility(View.GONE) shrinking display size

Android version 11.0.4
I am trying to use a ListView to display a series of elements with different types of information to display in the same list using a custom ListAdapter.
To make the same ListAdapter able to display different types of information I'm creating a custom XML template with all possible elements that I want to display and hiding the ones I don't want to show programmatically.
However, when I hide the extra elements I don't want to show the size of the view displaying them shrinks.
When the extra elements are shown
When the extra elements are gone
The code that hides the elements:
TextView catagory = (TextView) convertView.findViewById(R.id.title);
TextView time1 = (TextView) convertView.findViewById(R.id.time1);
TextView time2 = (TextView) convertView.findViewById(R.id.time2);
TextView location = (TextView) convertView.findViewById(R.id.location);
TextView description = (TextView) convertView.findViewById(R.id.description);
View separator1 = (View) convertView.findViewById(R.id.separator_vertical1);
View separator2 = (View) convertView.findViewById(R.id.separator_horizontal1);
View separator3 = (View) convertView.findViewById(R.id.separator_vertical2);
View separator4 = (View) convertView.findViewById(R.id.separator_horizontal2);
catagory.setText(item.name);
Class type = item.getType();
if (EventItem.class.equals(type)) {
time1.setText(String.valueOf(((EventItem)item).timeStart));
time2.setText(String.valueOf(((EventItem)item).timeEnd));
location.setText(((EventItem)item).location);
description.setText(((EventItem)item).description);
} else if (TaskItem.class.equals(type)) {
time1.setText(String.valueOf(((TaskItem)item).timeDue));
description.setText(((TaskItem)item).description);
//hide unused elements
separator2.setVisibility(View.GONE);
location.setVisibility(View.GONE);
separator3.setVisibility(View.GONE);
time2.setVisibility(View.GONE);
} else if (ReminderItem.class.equals(type)) {
time1.setText(String.valueOf(((ReminderItem)item).time));
//hide unused elements
separator2.setVisibility(View.GONE);
location.setVisibility(View.GONE);
separator3.setVisibility(View.GONE);
time2.setVisibility(View.GONE);
separator4.setVisibility(View.GONE);
description.setVisibility(View.GONE);
} else if (SubjectItem.class.equals(type)) {
description.setText(((SubjectItem)item).description);
} else if (NoteItem.class.equals(type)) {
description.setText(((TaskItem)item).description);
//hide unused elements
separator1.setVisibility(View.GONE);
time1.setVisibility(View.GONE);
separator2.setVisibility(View.GONE);
location.setVisibility(View.GONE);
separator3.setVisibility(View.GONE);
time2.setVisibility(View.GONE);
} else if (ContactItem.class.equals(type)) {
//hide unused elements
separator1.setVisibility(View.GONE);
time1.setVisibility(View.GONE);
separator2.setVisibility(View.GONE);
location.setVisibility(View.GONE);
separator3.setVisibility(View.GONE);
time2.setVisibility(View.GONE);
}
The XML template:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/rectangle">
<TextView
android:id="#+id/title"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textAlignment="textStart"
android:textSize="16sp"
android:textColor="#ffffffff"/>
<View
android:id="#+id/separator_vertical1"
android:layout_width="1dp"
android:layout_height="23dp"
android:layout_toStartOf="#id/time1"
android:background="#android:color/white"/>
<TextView
android:id="#+id/time1"
android:layout_width="94dp"
android:layout_height="wrap_content"
android:layout_toStartOf="#id/title"
android:layout_alignParentEnd="true"
android:textAlignment="center"
android:textSize="16sp"
android:textColor="#ffffffff"/>
<View
android:id="#+id/separator_horizontal1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#id/title"
android:background="#android:color/white"/>
<TextView
android:id="#+id/location"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#id/separator_horizontal1"
android:textAlignment="textStart"
android:textSize="16sp"
android:textColor="#ffffffff"/>
<View
android:id="#+id/separator_vertical2"
android:layout_width="1dp"
android:layout_height="23dp"
android:layout_toStartOf="#id/location"
android:layout_below="#id/separator_horizontal1"
android:background="#android:color/white"/>
<TextView
android:id="#+id/time2"
android:layout_width="94dp"
android:layout_height="wrap_content"
android:layout_toStartOf="#id/location"
android:layout_below="#id/separator_horizontal1"
android:layout_alignParentEnd="true"
android:textAlignment="center"
android:textSize="16sp"
android:textColor="#ffffffff"/>
<View
android:id="#+id/separator_horizontal2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#id/location"
android:background="#android:color/white"/>
<TextView
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/separator_horizontal2"
android:textAlignment="textStart"
android:textSize="20sp"
android:textColor="#ffffffff"/>
The ListView the elements are displayed in:
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
android:id="#+id/text_overview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
android:textColor="#ffffffff"
android:divider="#android:color/transparent"
android:dividerHeight="8dp"
android:headerDividersEnabled="true"
android:footerDividersEnabled="true"
android:scrollbars="none"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
The answer to your question is to use View.INVISIBLE
However, when I hide the extra elements I don't want to show the size of the view displaying them shrinks.
View.GONE: This view is invisible, and it doesn't take any space for layout purposes.
View.INVISIBLE: This view is invisible, but it still takes up space for layout purposes.
Here's the link of Documentation.
Use View.INVISIBLE which takes up space and invisible the view.
while you are using View.GONE which doesn't takes up space for the view and invisible the view.
For more description, you can go with:https://developer.android.com/reference/android/transition/Visibility
I eventually got it working, not sure how but setting each element as visible before going into the if statements stoped the shrinking of the view window. It also solved the issue of some list items missing elements that shouldn't have been hidden. I can only assume some resources were being reused somewhere and arriving pre-hidden.

Edittext unable to receive input from keyboard while in dialog box for above android N

I'm setting up a dialog to get the pin number from the user. While the dialog does appear with the keyboard but edittext receive no input from keyboard. The same dialog box work on the android 6. Below is the code
Code for dialog
LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View promptsView = layoutInflater.inflate(R.layout.activity_password,
null);
Dialog dialog = new Dialog(getApplicationContext()
, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
}
else {
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE);
}
dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT);
dialog.setContentView(promptsView);
dialog.getWindow().setGravity(Gravity.CENTER);
dialog.show();
XML Layout Code:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorAccent">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="32dp"
android:text="Enter Your Pin"
android:textSize="16sp"
android:gravity="center"
android:textColor="#fff"
android:layout_marginTop="#dimen/activity_vertical_margin"
/>
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_centerInParent="true"
android:hint="Enter Pin"
android:id="#+id/enter_pin"
android:inputType="numberPassword"
android:textAlignment="center"
/>
<Button
android:id="#+id/confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/enter_pin"
android:text="Confirm"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:textSize="12dp"
android:textColor="#fff"
android:background="#android:color/transparent"
/>
</RelativeLayout>
Please try below code for in Java file for dialog:
Dialog dialog = new Dialog(MainActivity.this);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.activity_password);
dialog.setCanceledOnTouchOutside(true);
dialog.getWindow().setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
Window window = dialog.getWindow();
window.setGravity(Gravity.CENTER);
dialog.show();
This code worked into above android N and as well as in below android N.
I hope its work for you.

What are the parameters for AlertDialog.Builder()?

So I am working on creating my first test app using Android Studio and Kotlin.
Also, should I just not use Kotlin when developing apps? I was told to use Kotlin..
Anyways back to my problem.
I wanted to know what the parameters were for AlertDialog.Builder(this)
I know it's supposed to be this or this#mainactivity but I don't know what the parameter is.
There is no intellisense and I couldnt find any documents.
How about a custom Alert Dialog that yes it is boiler plate code but oh the style!
You need this XML
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="#+id/imgDI"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:src="#drawable/caution" />
<TextView
android:id="#+id/tvDAT"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="30dp"
android:text="Delete Note"
android:textColor="#color/color_Black"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvDAC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="80dp"
android:gravity="center"
android:text="Do You Want to DELETE this Note"
android:textColor="#color/color_Black"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/btnYES"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="240dp"
android:layout_marginTop="110dp"
android:background="#color/color_Transparent"
android:text="DELETE"
android:textColor="#color/color_deepBlue"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/btnNO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="110dp"
android:background="#color/color_Transparent"
android:text="CANCEL"
android:textColor="#color/color_deepBlue"
android:textSize="18sp"
android:textStyle="bold" />
here is the button that calls the view
btnDelete.setOnClickListener{
if(etPerson.text.toString().equals("")){
message("No Match Found")
return#setOnClickListener
}
doCustom()
}
And now doCustom IT is a little funky to call the other function
fun doCustom() {
/* This method uses the custom_dialog.xml file created for greater control over
the styling of the Custom Alert Dialog for various screen sizes and to be
able to set the text size of the dialog message text
*/
val makeDialog = LayoutInflater.from(this).inflate(R.layout.custom_dialog,null)
val mBuilder = AlertDialog.Builder(this).setView(makeDialog)
val mAlertDialog = mBuilder.show()
val btnYES = makeDialog.findViewById<Button>(R.id.btnYES)
val btnNO = makeDialog.findViewById<Button>(R.id.btnNO)
mAlertDialog.setCancelable(false)
btnYES.setOnClickListener {
removePerson()
mAlertDialog.dismiss()
}
btnNO.setOnClickListener {
message("Record NOT Deleted")
etPerson.setText("")
Timer().schedule(800){
thisACTIVITY()
}
mAlertDialog.dismiss()
}
mAlertDialog.show()
}
private fun removePerson() {
val dbHandler = DBHelper(this)
val result = dbHandler.deletePerson(etPerson.text.toString())
if (result) {
etPerson.setText("")
message("Record Removed")
Timer().schedule(1000){
thisACTIVITY()
}
}else{
etPerson.setText("NO MATCH -> click View Person List")
btnViewList.visibility = View.VISIBLE
btnEdit.visibility = View.INVISIBLE
btnDelete.visibility =View.INVISIBLE
btnAdd.visibility = View.INVISIBLE
message("NO Match Found")
}
}

Android Studio Buttons not Displaying

I made this dialog for my app that shows up allows you to delete the items from db. It used to be a textView and a button which called the function, all in a linear layout, which worked good. Now I changed to a Relative Layout so I could add another button on the same line as the other, I also added an editText.
My problem is that since I added those objects I am not able to see the buttons anymore. the editText and textView are visible.
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/dialog_deletefood"
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="com.example.paulcosma.app2.SecondActivity">
<TextView
android:text="TextView"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lblFoodDetails"
android:textAlignment="center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp" />
<Button
android:text="Şterge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnDeleteEatenFood"
android:layout_alignBaseline="#+id/btnModifyEatenFood"
android:layout_alignBottom="#+id/btnModifyEatenFood"
android:layout_toStartOf="#+id/lblFoodDetails"
android:visibility="visible" />
<Button
android:layout_marginTop="100dp"
android:text="Modifică"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnModifyEatenFood"
android:layout_below="#+id/lblFoodDetails"
android:layout_toEndOf="#+id/lblFoodDetails"
android:visibility="visible" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="#+id/txtEditValue"
android:maxLength="6"
style="#style/Widget.AppCompat.AutoCompleteTextView"
android:maxLines="1"
android:textAppearance="#style/TextAppearance.AppCompat"
android:fontFamily="sans-serif"
android:textSize="14sp"
android:textAlignment="center"
android:textColorLink="#android:color/holo_blue_light"
android:inputType="numberDecimal"
android:layout_marginTop="21dp"
android:layout_below="#+id/lblFoodDetails"
android:layout_centerHorizontal="true" />
</RelativeLayout>
This dialog is supposed to show on listView item click. Below is the code I used to show it.
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HashMap<String, String> hmap = (HashMap<String, String>)parent.getItemAtPosition(position);
final String _name = hmap.get("food");
final int _id = foodDB.getFoodId(_name);
AlertDialog.Builder mBuiler = new AlertDialog.Builder(SecondActivity.this);
View mView = getLayoutInflater().inflate(R.layout.dialog_deletefood,null);
final TextView lblFoodDetails = (TextView) mView.findViewById(R.id.lblFoodDetails);
final Button btnDeleteEatenFood = (Button) mView.findViewById(R.id.btnDeleteEatenFood);
lblFoodDetails.setText("Aţi ales produsul " + _name + ". Aici puteţi adăuga sau scădea cantitatea printr-o anumită valoare sau puteţi şterge apariţia produsului.");
mBuiler.setView(mView);
final AlertDialog dialog = mBuiler.create();
btnDeleteEatenFood.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
foodDB.deleteEatenFood(_id);
Toast.makeText(SecondActivity.this,_name+" a fost sters",Toast.LENGTH_SHORT).show();
dialog.dismiss();
updateFoodList(foodDB.getAllEatenFood());
}
});
dialog.show();
}
This layout file renders the buttons in the Android layout designer as described (see below)
The problem may reside in code.
The problem was that both buttons where placed based on textView's size. When it changed, buttons got placed out of the screen
Bug:
android:layout_toEndOf="#+id/lblFoodDetails"
android:layout_toEStartOf="#+id/lblFoodDetails"

Unable to dynamically add EditText to LinearLayout

I have read many post similar to this, but none of them resolved my problem. Another funny thing is, on the emulator the place for the edittext is held (meaning other widgets are shifted down), but is not shown, on the other hand the same implementation on the real phone doesn't even show a place for the widget. I have the following LinearLayout inside which I need to dynamically add an EditText widget when the TextView Add a Team Member is clicked.
<LinearLayout
android:id="#+id/layout_add_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/separator"
android:layout_margin="5dp"
android:background="#color/yellow_background"
android:orientation="vertical"
android:padding="5dp">
<LinearLayout
android:id="#+id/layout_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/iv_check_research_tasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_check_empty" />
<EditText
android:id="#+id/et_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:selectAllOnFocus="true"
android:text="Task Name"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tv_add_team_member"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="5dp"
android:text="Add a team member"
android:textColor="#color/text_green"
android:textSize="15dp"
android:textStyle="bold" />
<LinearLayout
android:id="#+id/ll_add_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_add_team_member"
android:orientation="horizontal"
android:weightSum="100" >
<ImageView
android:id="#+id/iv_create_assignment_attach"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="10"
android:src="#drawable/icon_flag_alt" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:layout_weight="15"
android:text="Due"
android:textSize="15dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="65"
android:hint="None"
android:selectAllOnFocus="true" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="10"
android:src="#drawable/icon_calendar_empty" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_create_assignment_trash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:layout_marginTop="5dp"
android:text="Trash" />
<Button
android:id="#+id/btn_create_assignment_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="5dp"
android:text="Done" />
</LinearLayout>
</LinearLayout>
Here is the Java code, the onClickListener implementation of the TextView.
tvAddTeamMember.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout) getActivity().findViewById(R.id.layout_task_name);
EditText et = new EditText(getActivity());
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setText("Text");
ll.addView(et);
}
You may proceed this way ! This is not what you're looking for. But your problem will get resolved.
1) By default add the EditText view in your XML and make it invisible.
2) On Clicking the TextView, make it visible.
enjoy !
Hi #Anas Azeem: your code is correct.through your code we can add editetxt to linear layout dynamically. problem is in the layout file only.in the layout mentioned the orientation for added layout as "horizontal" ,instead of that one use "vertical" like below
<LinearLayout
android:id="#+id/layout_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/iv_check_research_tasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<EditText
android:id="#+id/et_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:selectAllOnFocus="true"
android:text="Task Name"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
Try to change the line :
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
With this:
android.widget.LinearLayout.LayoutParams p= new android.widget.LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//Then do your work
If you know you might be adding a layout, you could always declare ViewStub. After that you simply find the viewStub and call inflate() on it:
ViewStub stub = (ViewStub) findViewById(R.id.stub);
View inflated = stub.inflate();
This is the promoted way to do that in Android SDK. Thanks to that you can define the infalte layout in an xml file.
Try with this code.
replace ur line ll.addView(et);
by::
ll.addView(et,p);
and delete ur line et.setLayoutParams(p);
A simple way to do this.
Button add = (Button) findViewById(R.id.add_checkpoint);
final LinearLayout layout = (LinearLayout) findViewById(R.id.addLayout);
// layout has assigned weight = "1.0" in xml & it's a child of ScrollView
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater oLayoutInflater = getLayoutInflater();
// oView is declared globally & Layout "row_checkpoint" contains a EditText
oView = oLayoutInflater.inflate(R.layout.row_checkpoint, null);
final EditText speciesEditText = (EditText) oView
.findViewById(R.id.checkpoint);
speciesEditText.setId(layout.getChildCount());
// Perform whatever required over EditText, same way you can add more elements.
layout.addView(oView);
}
});
Hope it will help.

Categories

Resources