How to onclick on pop up fragment ? Android studio - android-studio

I have a popup.xml that have sign in and sign up button. This pop up will be shown when i click on certain function. Now the problem is i cant OnClick on the sign in and sign up button. I want to redirect to login activity when i click on sign in on the pop up fragment. I tried to create a class to the popup.xml and OnClick on sign in button but it seem like not working at all. It doesnt even triggered the OnClick function because Log.d("dwfsd","wefwef"); doesnt show at all.
Pop Up Image
My loginpopup.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="350dp"
android:layout_height="300dp"
xmlns:tools="http://schemas.android.com/tools"
android:layout_gravity="center"
android:background="#drawable/circlebackground"
tools:context=".loginpopup">
<ImageView
android:id="#+id/loginLogo"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:src="#drawable/foxsplash" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/loginLogo"
android:paddingTop="30dp"
android:gravity="center">
<Button
android:id="#+id/signIn"
android:layout_width="150dp"
android:layout_height="55dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="56dp"
android:textColor="#color/white"
android:textSize="24sp"
android:padding="3dp"
android:focusable="true"
android:textAllCaps="false"
android:background="#drawable/rounded_rectangle"
android:text="Sign In"/>
<Button
android:id="#+id/signUp"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="56dp"
android:textColor="#color/white"
android:textSize="24sp"
android:padding="3dp"
android:focusable="true"
android:textAllCaps="false"
android:background="#drawable/rounded_rectangle"
android:text="Sign Up"
android:layout_toEndOf="#+id/signIn"/>
</RelativeLayout>
</RelativeLayout>
loginpopup.java:
public class loginpopup extends AppCompatActivity {
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginpopup);
button =(Button) findViewById(R.id.signIn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("dwfsd","wefwef");
openActivity();
}
});
}
private void openActivity() {
Intent intent = new Intent(this, login.class);
startActivity(intent);
}
}

Related

How to open new activity on click text view?

I am very new in application development. I am trying to open new activity when I click on TextView in Android Studio. But i am getting error from android studio which is below
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null
object reference at
com.example.etechnomateapp.MainActivity.onCreate(MainActivity.java:41)
The LAYOUT page
<?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"
android:background="#drawable/screenbg"
tools:context=".loginActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10sp"
android:orientation="vertical">
<ImageView
android:id="#+id/img"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20sp"
android:src="#drawable/shop_logo"
tools:ignore="ContentDescription"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40sp"
android:layout_marginBottom="40sp"
android:gravity="center_horizontal"
android:text="#string/user_login"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="#color/colorDarkRed"
android:textSize="22sp"
/>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/userEmailWreapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorRed"
android:layout_marginTop="5dp"
>
<EditText
android:id="#+id/emailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:layout_marginTop="5dp"
android:inputType="textEmailAddress"
android:hint="#string/enter_email"
android:textColor="#color/colorRed"
android:textSize="18sp"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/userPasswordWreapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorRed"
android:layout_marginTop="5dp"
>
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:layout_marginTop="5dp"
android:hint="#string/enter_password"
android:textColor="#color/colorRed"
android:inputType="textPassword"
android:textSize="18sp"/>
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="#+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp"
android:text="#string/user_login"
android:background="#drawable/btnloginbg"
android:textColor="#color/colorWhite"
android:textSize="20sp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:layout_marginBottom="10sp"
android:gravity="center_horizontal"
android:text="Or"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="#color/colorDarkRed"
android:textSize="22sp"
/>
<TextView
android:id="#+id/forgetPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/forgetPass"
android:textAlignment="center"
android:textAllCaps="false"
android:layout_marginLeft="45dp"
android:textColor="#color/colorDarkRed"
android:textSize="16sp"
/>
<TextView
android:id="#+id/UserSignup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/signup"
android:textAlignment="center"
android:textAllCaps="false"
android:layout_marginLeft="250dp"
android:layout_marginTop="-20dp"
android:textColor="#color/colorDarkRed"
android:textSize="16sp"
/>
</LinearLayout>
</RelativeLayout>
The Main Acxtivity Page
package com.example.etechnomateapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btnUsersignup, btnUserlogin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnUserlogin=findViewById(R.id.btnUserLogin);
btnUsersignup=findViewById(R.id.btnSignup);
final TextView register = (TextView) findViewById(R.id.UserSignup);
btnUsersignup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent= new Intent(MainActivity.this, RegistrationActivity.class);
startActivity(intent);
}
});
btnUserlogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent= new Intent(MainActivity.this, loginActivity.class);
startActivity(intent);
}
});
register.setOnClickListener(new View.OnClickListener() { /// errors occuring here
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, RegistrationActivity.class);
startActivity(myIntent);
}
});
}
}
This is a result of activity_main.xml not containing Textview by id UserSignup.
It seems like you have posted some other xml. Because there is no "btnUserLogin" in xml you have written in java code.

Android Listview not displaying added items

I had a listview that was working fine and displaying Items which we're being created after taking a photo and then added. Now, for some reason, it doesn't.
What's particulary strange is that if I add the items in the same place where I initially create and set the adapter, the phone DOES show them. But when I do so in a later stage, it doesn't!
EDIT - After some more trial and error, it looks as though the database related commands are problematic! See commented below.
RELEVANT JAVA CODE
Cart _cart;
private AppDataBase _db;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart_review);
init(getIntent().getStringExtra("calling activity"));
}
private void init(String callingActivity)
{
_cart = new Cart();
_pricingServices = new PricingServices(this);
ArrayAdapter<Item> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, _cart.items);
ListView itemListView = findViewById(R.id._dynamic_item_list);
itemListView.setAdapter(adapter);
_cart.items.add(new Item(5, "blame", 4)); //these work!!!!
_cart.items.add(new Item(5, "bloom", 4)); //!!!!!!
_db = Room.databaseBuilder(getApplicationContext(), AppDataBase.class, "class").fallbackToDestructiveMigration().allowMainThreadQueries().build();
if(callingActivity.equals(MainActivity.class.getSimpleName()))
{
_cart = new Cart(); //When these are commented out
_cart.setId((int)(_db.cartDao().insert(_cart))); //it seems to work
}
else
_cart.items.addAll(_db.itemDao().getItemsByCartId(getIntent().getIntExtra("cart id", -1)));
}
public void OnItemDescriptionDone(String description)
{
_description = description;
Item item = new Item(_convertedPrice, _description, _cart.getId());
_cart.items.add(item); //This doesn't update the UI !!!
}
And here is the layout of the activity
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CartReviewActivity">
<ListView
android:id="#+id/_dynamic_item_list"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="24dp"
android:layout_marginTop="24dp"
app:layout_constraintBottom_toTopOf="#+id/button_capture_image"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textview_cart_name" />
<ImageButton
android:id="#+id/button_capture_image"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_marginBottom="16dp"
android:onClick="OnCameraButtonClick"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="#drawable/camera_icon" />
<TextView
android:id="#+id/textview_cart_name"
android:layout_width="0dp"
android:layout_height="31dp"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:text="The Cart name will be"
android:textColor="#android:color/darker_gray"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

Radio Button and TextField Android-studio

How can I make a radio button group and a TextField, so that whenever I click on TextField the radio button automatically changes from one to another?
Please give me a block of code for this.
If I understood your question correctly, you want the radioButton checked status change once the textView click. You can achieve by this way.
public class MainActivity extends Activity {
private RadioGroup radioSexGroup;
private RadioButton radioSexButton,male,female;
private TextView btnDisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioSexGroup=(RadioGroup)findViewById(R.id.radioGroup);
btnDisplay=(TextView)findViewById(R.id.textView3);
male = (RadioButton)findViewById(R.id.radioButton);
female=(RadioButton)findViewById(R.id.radioButton2);
btnDisplay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int selectedId=radioSexGroup.getCheckedRadioButtonId();
radioSexButton=(RadioButton)findViewById(selectedId);
if(radioSexButton.getText().toString().equals("Male"))
{
female.setChecked(true);
}
else
{
male.setChecked(true);
}
}
});
}
}
activity_main
<?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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="90dp"
android:layout_marginTop="58dp"
android:weightSum="1"
android:id="#+id/radioGroup"
android:layout_alignRight="#+id/textView3"
android:layout_alignEnd="#+id/textView3">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="55dp"
android:text="Male"
android:id="#+id/radioButton"
android:layout_gravity="center_horizontal"
android:checked="false"
android:textSize="25dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:id="#+id/radioButton2"
android:layout_gravity="center_horizontal"
android:checked="false"
android:textSize="25dp"
android:layout_weight="0.13" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me "
android:id="#+id/textView3"
android:textSize="35dp"
android:layout_below="#+id/radioGroup"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"></TextView>
</RelativeLayout>
https://www.tutorialspoint.com/android/android_radiogroup_control.htm
check this link , to use radio group with textviews

how to retrieve string value from dynamic edit text..?

In my layout I added two edit text by click on the add button and its added(works) and when I click on delete button it also delete the dynamic edit text.My question is how to get string value from it suppose I dynamically add two views and also delete when not needed.
here is my code..
public class PartDetails extends AppCompatActivity {
// Parent view for all rows and the add button.
private LinearLayout mContainerView;
// The "Add new" imageButton
private ImageButton mAddImageButton;
Button submit_part_details;
private View mExclusiveEmptyView;
EditText prequired,pnumber;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_part_details);
mContainerView = (LinearLayout) findViewById(R.id.parentView);
submit_part_details = (Button) findViewById(R.id.submit_part_details);
mAddImageButton = (ImageButton) findViewById(R.id.add_et_parts);
prequired= (EditText) findViewById(R.id.et_Prequired);
pnumber= (EditText) findViewById(R.id.et_Pnumber);
mAddImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
inflateEditRow();
}
});
submit_part_details.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String parts=prequired.getText().toString();
String pnum =pnumber.getText().toString();
}
});
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// TODO: Handle screen rotation:
// encapsulate information in a parcelable object, and save it
// into the state bundle.
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// TODO: Handle screen rotation:
// restore the saved items and inflate each one with inflateEditRow;
}
// Helper for inflating a row
private void inflateEditRow() {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row_add_parts, null);
final EditText parts_required = (EditText) addView
.findViewById(R.id.et_parts);
final EditText parts_number = (EditText) addView
.findViewById(R.id.et_Pnumber);
final ImageButton deleteButton = (ImageButton) addView
.findViewById(R.id.delete_et_parts);
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((LinearLayout) addView.getParent()).removeView(addView);
}
});
mContainerView.addView(addView);
}
}
this is parent XML layout
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#android:color/black"
android:layout_height="match_parent"
android:focusableInTouchMode="true">
<LinearLayout
android:id="#+id/parentView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:layout_below="#+id/tv"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/relative_layout1"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/parts_required"
android:layout_width="110dp"
android:layout_height="40dp"
android:text="Parts Required"
android:textColor="#fff"
android:textSize="15sp"
android:textStyle="bold"
android:background="#drawable/shape"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/parts_required"
android:layout_marginLeft="20dp"
android:orientation="horizontal">
<EditText
android:id="#+id/et_Prequired"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.8"
android:hint="text"
android:singleLine="true"
android:inputType="text"
android:textSize="20sp"
android:paddingLeft="20dp"
android:imeOptions="actionNext"
android:background="#drawable/shape_edit_text"
/>
<ImageButton
android:id="#+id/add_et_parts"
android:layout_width="0dp"
android:layout_height="40dp"
android:background="#null"
android:onClick="onAddNewClicked"
android:src="#android:drawable/ic_input_add"
android:layout_weight="0.2"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relative_layout2"
android:layout_marginTop="5dp"
android:layout_below="#+id/relative_layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/parts_number"
android:layout_width="110dp"
android:layout_height="40dp"
android:text="Parts Number"
android:textColor="#fff"
android:textSize="15sp"
android:textStyle="bold"
android:background="#drawable/shape"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/parts_number"
android:layout_marginLeft="20dp"
android:orientation="horizontal">
<EditText
android:id="#+id/et_Pnumber"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:hint="text"
android:singleLine="true"
android:inputType="text"
android:textSize="20sp"
android:paddingLeft="20dp"
android:imeOptions="actionNext"
android:background="#drawable/shape_edit_text"
/>
<!-- <ImageButton
android:id="#+id/add_et_parts_number"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="#null"
android:src="#android:drawable/ic_input_add"
android:layout_weight="0.2"/>-->
</LinearLayout>
</RelativeLayout>
<Button
android:layout_marginTop="50dp"
android:id="#+id/submit_part_details"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="#+id/snap"
android:textAllCaps="false"
android:textSize="20sp"
android:textColor="#fff"
android:background="#1c4648"
android:text="Submit All Details" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
this is Add Button XML layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#android:color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/relative_layout1"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/parts_required"
android:layout_width="110dp"
android:layout_height="40dp"
android:text="Parts Required"
android:textColor="#fff"
android:textSize="15sp"
android:textStyle="bold"
android:background="#drawable/shape"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/parts_required"
android:layout_marginLeft="20dp"
android:orientation="horizontal">
<EditText
android:id="#+id/et_parts"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.8"
android:singleLine="true"
android:inputType="text"
android:textSize="20sp"
android:hint="text"
android:paddingLeft="20dp"
android:imeOptions="actionNext"
android:background="#drawable/shape_edit_text"
/>
<ImageButton
android:id="#+id/delete_et_parts"
android:layout_width="0dp"
android:layout_height="40dp"
android:background="#null"
android:src="#android:drawable/ic_delete"
android:layout_weight="0.2"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relative_layout2"
android:layout_marginTop="5dp"
android:layout_below="#+id/relative_layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/parts_number"
android:layout_width="110dp"
android:layout_height="40dp"
android:text="Parts Number"
android:textColor="#fff"
android:textSize="15sp"
android:textStyle="bold"
android:background="#drawable/shape"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/parts_number"
android:layout_marginLeft="20dp"
android:orientation="horizontal">
<EditText
android:id="#+id/et_Pnumber"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:hint="text"
android:singleLine="true"
android:inputType="text"
android:textSize="20sp"
android:paddingLeft="20dp"
android:imeOptions="actionNext"
android:background="#drawable/shape_edit_text"
/>
<!-- <ImageButton
android:id="#+id/add_et_parts_number"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="#null"
android:src="#android:drawable/ic_input_add"
android:layout_weight="0.2"/>-->
</LinearLayout>
</RelativeLayout>
</LinearLayout>
ArrayList<EditText> edtParts_required=new ArrayList<EditText>();
ArrayList<EditText> edtParts_number=new ArrayList<EditText>();
// Helper for inflating a row
private void inflateEditRow() {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row_add_parts, null);
final EditText parts_required = (EditText) addView
.findViewById(R.id.et_parts);
final EditText parts_number = (EditText) addView
.findViewById(R.id.et_Pnumber);
//Add EditText object to Collection
edtParts_required.add(parts_required);
edtParts_number.add(parts_required);
final ImageButton deleteButton = (ImageButton) addView
.findViewById(R.id.delete_et_parts);
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((LinearLayout) addView.getParent()).removeView(addView);
}
});
mContainerView.addView(addView);
}
When you delete the inflated row remove the element from collection too
Loop through your Collection and get the text from dynamic EditText
for(int i=0;i<edtParts_required.size();i++){
String enterdText=edtParts_required.get(i).getText().toString();
}
you can do it in many ways . here are two of them:
make an ArrayList and whenever you add new edit text add it to this array too and when you delete remove it from here too. when you want to get their text put it in a for loop
BETTER WAY : you have a linear layout. get its child count and put it in for loop. something like this :
for (int i=0;i<mContainerView.getChildCount();i++){
View mView=mContainerView.getChildAt(i);
EditText myEditText=(EditText) mView.findViewById(R.id.et_parts);
String txt=myEditText.getText().toString();
EditText myEditText2=(EditText) mView.findViewById(R.id.et_Pnumber);
String txt2=myEditText2.getText().toString();
}

Android input boxes on the next line

Okay, so I decided to take the Google classes for learning droid devving. I decided instead of the simple input box and button I would make multiple ones, and have each display a set color. The issue is it is jamming all input boxes on one line, rather than their own separate lines. This is what I have in the activity_color.xml
<?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:orientation="horizontal" >
<EditText android:id="#+id/edit_message_blue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message_blue" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send_blue"
android:onClick="sendMessageBlue" />
<EditText android:id="#+id/edit_message_orange"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message_orange" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send_orange"
android:onClick="sendMessageOrange" />
</LinearLayout>
This is the main java class
package com.example.color.texts;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class ColorTexts extends Activity {
public final static String EXTRA_MESSAGE = "com.example.colortexts.MESSAGE";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_texts);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_color_texts, menu);
return true;
}
/** Called when the user clicks the Send button for Blue */
public void sendMessageBlue(View view) {
Intent intent = new Intent (this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message_blue);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
/** Called when the user clicks the Send button for Red */
public void sendMessageRed(View view) {
Intent intent = new Intent (this, DisplayMessageActivityRed.class);
EditText editText = (EditText) findViewById(R.id.edit_message_red);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
/** Called when the user clicks the Send button for Orange */
public void sendMessageOrange(View view) {
Intent intent = new Intent (this, DisplayMessageActivityOrange.class);
EditText editText = (EditText) findViewById(R.id.edit_message_orange);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
Now I guess the question is how to either tell it to start a new line, like android:layout_next="1" or something (I KNOW THAT DOESN'T EXIST), or would I have to add to the public class? Such as make another layout file and reference to that? I highly doubt the latter would work.
EDIT: Per the instructions laid out, I think this is how I was supposed to do it, but it only know shows the first line :(
<?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:orientation="vertical" >
<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" >
<EditText android:id="#+id/edit_message_blue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message_blue" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send_blue"
android:onClick="sendMessageBlue" />
</LinearLayout>
<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:orientation="horizontal" >
<EditText android:id="#+id/edit_message_red"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message_red" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send_red"
android:onClick="sendMessageRed" />
</LinearLayout>
<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:orientation="horizontal" >
<EditText android:id="#+id/edit_message_orange"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message_orange" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send_orange"
android:onClick="sendMessageOrange" />
</LinearLayout>
</LinearLayout>
Your parent layout is set to be android:orientation="horizontal". This needs to be set to android:orientation="vertical".
EDIT: Here is the reference in the Android docs:
http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:orientation

Resources