Radio Button and TextField Android-studio - 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

Related

How to onclick on pop up fragment ? 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);
}
}

After starting another Activity and inflating new Layout, Layout of MainActivity still visible

I want to use a Navigation Drawer within my App by using activities instead of fragments. By using Layout inflater in the different activities I tried to keep the same navigation drawer as in the MainActivity, but to change the inner layout. My problem is that somehow the layout of the MainActivity cant be replaced. When clicking on a item in the navigation drawer the new Activtiy opens and starts the layoutinflater. But the Layout Of the Main Activity remains, while the layout of the other Activity is just added.
public class MainActivity extends AppCompatActivity implements
TabLayout.OnTabSelectedListener{
public static String TAG = "MainActivity";
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "CREATED");
NavigationView navigationView = (NavigationView)
findViewById(R.id.navigation_view);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, ^
toolbar, R.string.drawer_open, R.string.drawer_closed);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
navigationView.setNavigationItemSelectedListener(new
NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Mensa_in_der_Nähe:
Intent anIntent = new Intent(getApplicationContext(),
LocationActivity.class);
startActivity(anIntent);
drawerLayout.closeDrawers();
break;
}
return false;
}
});
Then the xml file of main activity:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusableInTouchMode="true">
<android.support.design.widget.CoordinatorLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/overview_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content_frame">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="enterAlways|scroll"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="#+id/menu_tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:tabMode="fixed" />
<android.support.design.widget.TabLayout
android:id="#+id/canteen_tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:tabMode="scrollable" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/dish_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</android.support.v4.view.ViewPager>
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginTop="?attr/actionBarSize"
android:background="#android:color/white"
app:menu="#menu/drawer_menu" />
the other activity:
public class LocationActivity extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout contentFrameLayout = (FrameLayout)
findViewById(R.id.content_frame);
getLayoutInflater().inflate(R.layout.activity_location,
contentFrameLayout);
}}
And the xml file of the other activity:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TimePicker
android:id="#+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>

Android Scrollview and layout problems

I have a question regarding android layouts. I cannot achieve what I am looking for and I need some direction.
I am looking for a layout that will be inside a Scroll View layout. its hard to explain but a picture is worth thousand words.enter image description here
I want this inside a scroll view, can any help please?
If you do not want to add images dynamically than this is the layout you want Try this.
activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="#+id/sv"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linMain"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<RelativeLayout
android:id="#+id/lin1"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_margin="10dp">
<ImageView
android:id="#+id/imgView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#android:color/holo_purple" />
<ImageView
android:id="#+id/imgView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#id/imgView1"
android:layout_marginTop="20dp"
android:background="#android:color/holo_red_light" />
<ImageView
android:id="#+id/imgView3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#id/imgView2"
android:layout_marginTop="20dp"
android:background="#android:color/holo_green_light" />
<ImageView
android:id="#+id/imgView4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#id/imgView3"
android:layout_marginTop="20dp"
android:background="#android:color/holo_blue_light" />
</RelativeLayout>
<LinearLayout
android:id="#+id/lin2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/linImages"></WebView>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Here i am attaching screenshot of this layout UI.
Try this,I have made two layouts ..
1) activity_main that contains gridview and webview.
2)activity_gridview which contains imageview which will inflate in gridview.
I have added images dynamically to Gridview using BaseAdapter.
In MainActivity i have set that Adapter to GridView. Try this code once.
activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:fillViewport="false"
android:orientation="vertical">
<LinearLayout
android:id="#+id/linImages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid"
android:layout_width="100dp"
android:layout_height="match_parent"
android:numColumns="1"></GridView>
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/linImages"></WebView>
</LinearLayout>
</ScrollView>
</RelativeLayout>
activity_gridview.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imgView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/star2" />
<ImageView
android:id="#+id/imgView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/star1" />
<ImageView
android:id="#+id/imgView3"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/star3" />
<ImageView
android:id="#+id/imgView4"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/star1" />
</LinearLayout>
CustomAdapter.java :
public class CustomAdapter extends BaseAdapter {
Context context;
int flags[];
LayoutInflater inflter;
public CustomAdapter(Context applicationContext, int[] flags) {
this.context = applicationContext;
this.flags = flags;
inflter = (LayoutInflater.from(applicationContext));
}
#Override
public int getCount() {
return flags.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.activity_gridview, null);
ImageView imgView1 = (ImageView) view.findViewById(R.id.imgView1);
ImageView imgView2 = (ImageView) view.findViewById(R.id.imgView2);
ImageView imgView3 = (ImageView) view.findViewById(R.id.imgView3);
ImageView imgView4 = (ImageView) view.findViewById(R.id.imgView4);
imgView1.setImageResource(flags[0]);
imgView2.setImageResource(flags[1]);
imgView3.setImageResource(flags[2]);
imgView4.setImageResource(flags[3]);
return view;
}
}
MainActivity.java :
public class MainActivity extends AppCompatActivity {
int images[] = {R.drawable.star2, R.drawable.star1, R.drawable.star3, R.drawable.star1};
GridView grid;
CustomAdapter customAdapter;
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView= (WebView) findViewById(R.id.webView);
grid = (GridView) findViewById(R.id.grid);
customAdapter = new CustomAdapter(getApplicationContext(), images);
grid.setAdapter(customAdapter);
}
}

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