I want to get Values from EditText dynamically.. I have a lot of EditText generated when user press add button..When User presses add Button it generates 3 Edittext each time. I dont know how to get the values from this dynamically generated EdiTtext . Now My question is how can i get the values from The 3 Edittext on each row. ALso I need to verify that if user removed the view or not. Please help I am new android development.This should happen when a user presses on Save Button. Thanks in advance!
This is class .
public class SecondActivity extends Activity {
Button saveBtn,cancelBtn,addBtn;
RelativeLayout layout;
EditText third,first,second;
LinearLayout Container;
int counter=0;
int all=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
saveBtn=(Button) findViewById(R.id.save);
cancelBtn=(Button) findViewById(R.id.cancel);
Container=(LinearLayout) findViewById(R.id.container);
addBtn=(Button) findViewById(R.id.addBtn);
saveBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(SecondActivity.this,MainActivity.class);
startActivity(intent);
Toast.makeText(SecondActivity.this, "The Result: "+all,Toast.LENGTH_LONG).show();
finish();
}
});
cancelBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(SecondActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
});
addBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
// EditText ed1=(EditText) findViewById(R.id.editText1);
// EditText ed2=(EditText) findViewById(R.id.editText2);
// EditText ed3=(EditText) findViewById(R.id.editText3);
// all=all+Integer.parseInt(ed1.getText().toString())+Integer.parseInt(ed2.getText().toString())+Integer.parseInt(ed3.getText().toString());
buttonRemove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
}});
Container.addView(addView);
}});
}
}
This is my row.xml .which i am using as a view in java 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:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dip" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Field 1" />
<EditText
android:id="#+id/editText1"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:inputType="numberPassword" >
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText1"
android:layout_alignBottom="#+id/editText1"
android:layout_toRightOf="#+id/editText1"
android:inputType="numberPassword" />
<Button
android:id="#+id/remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText2"
android:layout_alignParentRight="true"
android:text="Remove" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText2"
android:layout_alignLeft="#+id/editText2"
android:text="Field 2" />
<EditText
android:id="#+id/editText3"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText2"
android:layout_alignBottom="#+id/editText2"
android:layout_toRightOf="#+id/editText2"
android:editable="false"
tools:ignore="Deprecated" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText3"
android:layout_alignLeft="#+id/editText3"
android:text="Field 3" />
</RelativeLayout>
This is my layout which is attached with my activity class.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_alignParentTop="true"
android:orientation="vertical"
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=".SecondActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Add" />
</RelativeLayout>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="300dp" >
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/save"
android:layout_width="146dip"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Save" />
<Button
android:id="#+id/cancel"
android:layout_width="146dip"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Cancel" />
<EditText
android:id="#+id/editText1"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_above="#+id/save"
android:layout_alignParentLeft="true"
android:editable="false"
android:hint="T 1"
tools:ignore="Deprecated" >
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText1"
android:layout_toRightOf="#+id/editText1"
android:editable="false"
android:hint="T 2"
tools:ignore="Deprecated" >
</EditText>
<EditText
android:id="#+id/editText3"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText2"
android:layout_toRightOf="#+id/editText2"
android:editable="false"
android:hint="T 3"
tools:ignore="Deprecated" />
<EditText
android:id="#+id/editText4"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText3"
android:layout_alignParentRight="true"
android:editable="false"
android:hint="T 4"
tools:ignore="Deprecated,HardcodedText" />
</RelativeLayout>
</LinearLayout>
This is your edited SecondActivity.java
public class SecondActivity extends Activity {
Button saveBtn, cancelBtn, addBtn;
RelativeLayout layout;
EditText third, first, second;
LinearLayout Container;
int counter = 0;
int all = 0;
int tag = 0;
ArrayList<Integer> dynamicLayoutsTags;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
saveBtn = (Button) findViewById(R.id.save);
cancelBtn = (Button) findViewById(R.id.cancel);
Container = (LinearLayout) findViewById(R.id.container);
addBtn = (Button) findViewById(R.id.addBtn);
saveBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (dynamicLayoutsTags.size() > 0) {
for (int i = 0; i < dynamicLayoutsTags.size(); i++) {
View getView = Container
.findViewWithTag(dynamicLayoutsTags.get(i));
EditText editText1 = (EditText) getView
.findViewById(R.id.editText1);
EditText editText2 = (EditText) getView
.findViewById(R.id.editText2);
EditText editText3 = (EditText) getView
.findViewById(R.id.editText3);
Toast.makeText(
SecondActivity.this,
"Row " + i + " : " + "editext 1 is : "
+ editText1.getText()
+ " editext 2 is : "
+ editText2.getText()
+ " editext 3 is : "
+ editText3.getText(),
Toast.LENGTH_LONG).show();
}
}
Intent intent = new Intent(SecondActivity.this,
MainActivity.class);
startActivity(intent);
Toast.makeText(SecondActivity.this, "The Result: " + all,
Toast.LENGTH_LONG).show();
finish();
}
});
cancelBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(SecondActivity.this,
MainActivity.class);
startActivity(intent);
finish();
}
});
addBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
Button buttonRemove = (Button) addView
.findViewById(R.id.remove);
addView.setTag(tag);
buttonRemove.setTag(tag);
dynamicLayoutsTags.add(tag);
Container.addView(addView);
tag++;
buttonRemove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// ((LinearLayout) addView.getParent())
// .removeView(addView);
Integer removeTag = (Integer) v.getTag();
View deleteView = Container.findViewWithTag(removeTag);
Container.removeView(deleteView);
dynamicLayoutsTags.remove(removeTag);
}
});
}
});
}
#Override
protected void onResume() {
super.onResume();
tag = 0;
dynamicLayoutsTags = new ArrayList<Integer>();
}
}
Related
I am trying to make a simple app. The program should create a table with given row and column numbers. I'm adding my Java and xml files below:
public class MainActivity extends AppCompatActivity {
private Button btnCreate, btnCalculate, btnReset, btnExit;
private EditText txtColumn, txtRow;
private LinearLayout linLayout, mainLayout;
private TextView txtResult;
private Random random;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCreate = (Button) findViewById(R.id.btnCreate);
btnCalculate = (Button) findViewById(R.id.btnCalculate);
btnReset = (Button) findViewById(R.id.btnReset);
btnExit = (Button) findViewById(R.id.btnExit);
txtColumn = (EditText) findViewById(R.id.txtColumn);
txtRow = (EditText) findViewById(R.id.txtRow);
txtResult = (TextView) findViewById(R.id.txtResult);
txtColumn.requestFocus();
random = new Random();
LinearLayout mainLayout = findViewById(R.id.mainLayout);
TableLayout table = new TableLayout(this);
btnCalculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
btnCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
table.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
table.setShrinkAllColumns(true);
table.setStretchAllColumns(true);
int columnNumber = Integer.parseInt(txtColumn.getText().toString());
int rowNumber = Integer.parseInt(txtRow.getText().toString());
for (int i=0; i < rowNumber; i++) {
TableRow row = new TableRow(MainActivity.this);
for (int j=0; j < columnNumber; j++) {
int value = random.nextInt(100) + 1;
TextView tv = new TextView(MainActivity.this);
tv.setText(String.valueOf(value));
row.addView(tv);
}
table.addView(row);
}
mainLayout.addView(table);
}
});
btnExit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
System.exit(0);
}
});
btnReset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
txtColumn.setText(null);
txtRow.setText(null);
txtResult.setText(null);
}
});
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/txtRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter row number"
android:inputType="textCapWords"
android:textSize="18sp" />
<EditText
android:id="#+id/txtColumn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter column number"
android:inputType="textCapWords"
android:textSize="18sp" />
<TextView
android:id="#+id/txtResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Result"
android:background="#E91E63"
android:textSize="18sp"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/btnCreate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CREATE"/>
<Button
android:id="#+id/btnCalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CALCULATE" />
<Button
android:id="#+id/btnReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RESET" />
<Button
android:id="#+id/btnExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EXIT" />
</LinearLayout>
<LinearLayout
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
This is the error message that I got:
The specified child already has a parent. You must call removeView() on the child's parent first.
Therefore, whenever I tried to add mainLayout.removeView(table); to the code nothing happens when I click the button create. Thank you all. I will be appreciated for any kinds of comments.
You have the following errors:
1: The layout above the mainLayout layout is letting the height be match_parent. Make mainLayout not displayable.
2: The layout mainLayout is also setting the height to match_parent which is also wrong. I wonder do you understand what match_parent means?
3: The first time you add successfully, but because you are setting the height to match_parent, it makes it not visible, in fact it successfully added the table. The second click because it has been added to the table, it will crash.
The code below I have fixed for you, let's try it.
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/txtRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter row number"
android:inputType="textCapWords"
android:textSize="18sp"
android:text="3"/>
<EditText
android:id="#+id/txtColumn"
android:text="2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter column number"
android:inputType="textCapWords"
android:textSize="18sp" />
<TextView
android:id="#+id/txtResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Result"
android:background="#E91E63"
android:textSize="18sp"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/btnCreate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CREATE"/>
<Button
android:id="#+id/btnCalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CALCULATE" />
<Button
android:id="#+id/btnReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RESET" />
<Button
android:id="#+id/btnExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EXIT" />
</LinearLayout>
<LinearLayout
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
JAVA:
public class MainAct extends AppCompatActivity {
private Button btnCreate, btnCalculate, btnReset, btnExit;
private EditText txtColumn, txtRow;
private LinearLayout linLayout, mainLayout;
private TextView txtResult;
private Random random;
TableLayout table;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCreate = (Button) findViewById(R.id.btnCreate);
btnCalculate = (Button) findViewById(R.id.btnCalculate);
btnReset = (Button) findViewById(R.id.btnReset);
btnExit = (Button) findViewById(R.id.btnExit);
txtColumn = (EditText) findViewById(R.id.txtColumn);
txtRow = (EditText) findViewById(R.id.txtRow);
txtResult = (TextView) findViewById(R.id.txtResult);
txtColumn.requestFocus();
random = new Random();
LinearLayout mainLayout = findViewById(R.id.mainLayout);
btnCalculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
btnCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (table != null) {
mainLayout.removeView(table);
}
table = new TableLayout(getBaseContext());
table.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
table.setShrinkAllColumns(true);
table.setStretchAllColumns(true);
int columnNumber = Integer.parseInt(txtColumn.getText().toString());
int rowNumber = Integer.parseInt(txtRow.getText().toString());
for (int i=0; i < rowNumber; i++) {
TableRow row = new TableRow(MainAct.this);
for (int j=0; j < columnNumber; j++) {
int value = random.nextInt(100) + 1;
TextView tv = new TextView(MainAct.this);
tv.setText(String.valueOf(value));
row.addView(tv);
}
table.addView(row);
}
if (table != null) {
mainLayout.addView(table);
}
}
});
btnExit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
System.exit(0);
}
});
btnReset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
txtColumn.setText(null);
txtRow.setText(null);
txtResult.setText(null);
}
});
}
}
After press SignUp button, Display error : unfortunately app has stopped
LogCat error : java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lolacupcakes/com.example.lolacupcakes.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
SignUpActivity: Java Code
public class SignUpActivity extends AppCompatActivity {
EditText emailId, password;
Button btnSignUp;
TextView tvSignIn;
FirebaseAuth mFirebaseAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
mFirebaseAuth = FirebaseAuth.getInstance();
emailId = findViewById(R.id.emailIDSignUp);
password = findViewById(R.id.pwdIDSignUp);
btnSignUp = findViewById(R.id.btnIDSignUp);
tvSignIn = findViewById(R.id.textView);
btnSignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email = emailId.getText().toString();
String pwd = password.getText().toString();
if(email.isEmpty())
{
emailId.setError("Please Enter Email Id");
emailId.requestFocus();
}
else if (pwd.isEmpty())
{
password.setError("Please Enter Email Id");
password.requestFocus();
}
else if (email.isEmpty() && pwd.isEmpty())
{
Toast.makeText(SignUpActivity.this, "Fields are Empty",Toast.LENGTH_SHORT).show();
}
else if (!(email.isEmpty() && pwd.isEmpty()))
{
mFirebaseAuth.createUserWithEmailAndPassword(email,pwd).addOnCompleteListener(SignUpActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (!task.isSuccessful())
{
Toast.makeText(SignUpActivity.this, "SignUp Unsuccessful, Please Try Again",Toast.LENGTH_SHORT).show();
}
else
{
startActivity(new Intent(SignUpActivity.this,HomeActivity.class));
}
}
});
}
else
{
Toast.makeText(SignUpActivity.this, "Error Occurred!",Toast.LENGTH_SHORT).show();
}
}
});
tvSignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(SignUpActivity.this, LoginActivity.class);
startActivity(i);
}
});
}
}
SignUpActivity XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SignUpActivity">
<Button
android:id="#+id/btnIDSignUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="132dp"
android:text="Sign Up" />
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp"
android:src="#drawable/logo_lola" />
<EditText
android:id="#+id/pwdIDSignUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="206dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" />
<EditText
android:id="#+id/emailIDSignUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="269dp"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="87dp"
android:text="Already have an account? Sign in here." />
</RelativeLayout>
HomeActivity Java Code:
public class HomeActivity extends AppCompatActivity {
Button btnlogout;
FirebaseAuth mFirebaseAuth;
private FirebaseAuth.AuthStateListener mAuthStateListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
btnlogout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
Intent intoMain = new Intent(HomeActivity.this, SignUpActivity.class);
startActivity(intoMain);
}
});
}
}
HomeActivityXML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Welcome"
android:textSize="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<Button
android:id="#+id/btnLogout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Logout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="424dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
You missed initializing btnlogout. Add this line in HomeActivity onCreate before setting View.OnClickListener
btnlogout = findViewById(R.id.btnLogout);
You need to create btnSignUp object before setonlicklistener for button
btnSignUp = findViewById(R.id.btnLogout);
You need to create the btnSignUp object before setonclicklistener.
I'm self-taught and have fallen in love. This is my first android app which in theory is fairly simple.
The issue: I've been successful in dynamically creating new EditTexts with a click of a button using a layoutinflator method. My problem is being able to identify the new id's of those EditTexts, or simply retrieving the new input values from the new EditTexts that the user has created... so I can display them to their respective TextViews.
Thank you in advance. I sincerely appreciate it.
MainActivity.java code:
public class MainActivity extends AppCompatActivity {
LinearLayout parentLinearLayout;
Button button;
TextView result;
EditText editName;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parentLinearLayout = findViewById(R.id.parent_linear_layout);
editName = findViewById(R.id.editName);
result = findViewById(R.id.textViewName);
button = findViewById(R.id.addName);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = editName.getText().toString();
result.setText(name);
}
});
}
public void onAddField(View v) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.activity_row, null);
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
}
public void onDelete(View v) {
parentLinearLayout.removeView((View) v.getParent());
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent_linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:orientation="vertical" >
<Button
android:id="#+id/add_field_button"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#555"
android:onClick="onAddField"
android:text="Add Player"
android:textColor="#FFF"
/>
<TextView
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:id="#+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name 1"
/>
<TextView
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:id="#+id/textViewName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name 2"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<EditText
android:id="#+id/editName"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5" />
<Button
android:id="#+id/delete_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:drawable/ic_delete"
android:onClick="onDelete" />
</LinearLayout>
<Button
android:id="#+id/addName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
activity_row.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<EditText
android:id="#+id/number_edit_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5" />
<Button
android:id="#+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#android:drawable/ic_delete"
android:onClick="onDelete"/>
</LinearLayout>
Try to get EditText and TextView from your inflated view and add TextChangedListener on EditText and update TextView inside onTextChanged
public void onAddField(View v) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.activity_row, null);
EditText editText = rowView.findViewById(R.id.number_edit_text);
TextView textView = rowView.findViewById(R.id.your_text_view);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
textView.setText(s);
}
#Override
public void afterTextChanged(Editable s) {
}
});
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
}
I'm trying to click on row in list view but my code only works when i pressed between two rows
you can see this :
http://www.screencast.com/t/2vei8yOQh
note in rep.xml i using table layout and inside it rowTable
i uses this code :
public class LastDaysActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_last_days);
final Button newFourm = (Button) findViewById(R.id.button1);
LastDaysAdapter adapter = new LastDaysAdapter(this, getDates(),
getApplicationContext(), getBusID());
setListAdapter(adapter);
newFourm.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(LastDaysActivity.this,
TabHostActivity.class);
startActivity(i);
}
});
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
TextView myView = (TextView) v.findViewById(R.id.textView2);
String text = myView.getText().toString();
Toast.makeText(this, text + " selected", Toast.LENGTH_LONG).show();
}
edit xml :
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/TableLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow
android:id="#+id/TableRow05"
android:layout_width="fill_parent"
android:layout_height="27dp"
android:layout_marginTop="4dp"
android:background="#drawable/trans01" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="رقم اللوحة" />
<TextView
android:id="#+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="200dp"
android:text="التاريخ"
android:textSize="12sp" />
</TableRow>
</TableLayout>
<LinearLayout
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/btn_image" />
<TextView
android:layout_width="150dp"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:text="something" />
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
btn1 = (LinearLayout) findViewById(R.id.btn1);
btn1.setOnClickListener(this);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(this);
I can not click on the button. Always called click on LinearLayout.
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
// set something parametr
break;
case R.id.btn1:
// go to activity
break;
}
}
Seems to work for me like this,
Your xml file,
<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="fill_parent" >
<LinearLayout
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_width="150dp"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:text="something" />
<Button
android:id="#+id/btn"
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
And your Activity class,
public class MainActivity extends Activity implements OnClickListener{
LinearLayout btn1 =null;
Button btn =null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (LinearLayout) findViewById(R.id.btn1);
btn1.setOnClickListener(this);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onClick(View v) {
if(v==btn)
{
Toast.makeText(getApplicationContext(), "btn clicked", Toast.LENGTH_SHORT).show();
}
if(v==btn1)
{
Toast.makeText(getApplicationContext(), "btn1 clicked", Toast.LENGTH_SHORT).show();
}
}
}
You can still differentiate in the onclick method:
#Override
public void onClick(View v) {
if (v.getId() == R.id.btn1)
{
// Linear was clicked
}
else if (v.getId() == R.id.btn){
// Button was clicked
}
}
But you should still remove the button from the linear
put clickable true in tag..
file android:clickable="true"
<LinearLayout
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical" android:onClick="" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/actionbar_background" />
<TextView
android:layout_width="150dp"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:text="something" />
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true" />
</LinearLayout>
...................................
btn1 = (LinearLayout) findViewById(R.id.btn1);
btn1.setOnClickListener(this);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(this);
...................................
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
// set something parametr
break;
case R.id.btn1:
// go to activity
break;
}
}