Add EditText views dynamically to Horizontal Scroll View - android

In my app, the user inputs a number (in this case, the number of reactants in a chemical equation, so the number of starting materials) and as a result a number of boxes, the amount matching that of the number given by the user, are produced/spawned/created below after pressing a button.
How do I go about doing this? I thought a for-loop would come into play, so I started it off.
Here's the Java class:
public class EquationBalancer extends Activity {
final EditText reactantNumberField = (EditText) findViewById(R.id.reactantsNumber);
final int reactantNom = Integer.parseInt(reactantNumberField.getText().toString());
HorizontalScrollView reactants = (HorizontalScrollView) findViewById(R.id.reactants);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.balancelayout);
Button setReactantsNo = (Button) findViewById(R.id.reactantsNumberOk);
setReactantsNo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
for (int i = 1; i < reactantNom; i++) {
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.balancelayout, menu);
return true;
}
}
and the XML layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/reactantsHowMany"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:text="#string/reactantsHowMany"
android:textSize="18dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:id="#+id/reactantsNumber"
android:paddingTop="5dp"
android:layout_width="65dp"
android:layout_height="wrap_content"
android:layout_below="#id/reactantsHowMany"
android:layout_marginLeft="60dp"
android:inputType="number"
android:hint="e.g. 4" />
<Button
android:id="#+id/reactantsNumberOk"
android:paddingTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/reactantsHowMany"
android:layout_alignParentRight="true"
android:layout_marginRight="60dp"
android:layout_alignBottom="#id/reactantsNumber"
android:text="Set" />
<HorizontalScrollView
android:id="#+id/reactants"
android:paddingTop="5dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#id/reactantsNumber" >
</HorizontalScrollView>
</RelativeLayout>

for (int i = 1; i < reactantNom; i++) {
EditText editText=new EditText(this);
editText.setText("some text if you want else remove this line");
reactants.addView(editText);
}
and if you want to get data from these edittext some where else in your code then maintain an array of edittext and save these edittext objects in that array..,.
Try this..,.

Related

How to get value from dynamically created edit text in Android?

How to get value from dynamically created EditText in Android?
This is my dynamic view (XML file)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/et_waypoint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/background"
android:hint="Way Points"
android:padding="10dp"
android:textSize="16sp" />
<Button
android:id="#+id/btn_waypoint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Find" />
</LinearLayout>
And I am using LayoutInflater because I want perfect Designed layout which is not accomplish by me via setLayoutParams so this is the reason I am using LayoutInflater.
And here is my code:
LayoutInflater l = getLayoutInflater();
final LinearLayout mainActivity = (LinearLayout) findViewById(R.id.dynamic);
final View view = l.inflate(R.layout.dynamic_layout_waypoints, null);
buttonWayPoints = (Button) view.findViewById(R.id.btn_waypoint);
editTextWayPoints = (EditText) view.findViewById(R.id.et_waypoint);
mainActivity.addView(editTextWayPoints);
I am new to Android. Most of people suggest this solution but it's not worked for me, the issue is when I implement this code my ADD NEW EDIT TEXT BUTTON not respond me.
This is how I am trying to get inserted value but it returns value of only last created edit text:
public Cursor getPerson(String Query) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(Query, null);
return c;
}
sb is StringBuilder sb;, w is String w;
String queryWayPoints = "select * from route_table where trip_id = " + t;
Cursor s = myDb.getPerson(queryWayPoints);
int count3 = s.getCount();
for (int j = 0; j < count3; j++) {
s.moveToNext();
w = s.getString(s.getColumnIndex("waypoints"));
// se.append(w + "." + "00");
}
trip_name.setText(n);
trip_date.setText(d);
sb.append(w);
}
trip_route.setText(sb.toString());
Although, I didn't get what exactly your question is, still I'll try to help.
What exactly this code does is, on clicking the button on top an new layout(one that you wanted) will add to the activity screen at runtime, i.e an edittext with a button, and when you click on that button, A toast with the value of respective edittext will be shown. Hence solving the problem of getting the value of dynamically added edittext
MainActivity
public class MainActivity extends AppCompatActivity {
Button generateET;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout myLinearLay = (LinearLayout) findViewById(R.id.dynamic);
generateET = (Button) findViewById(R.id.generateBtn);
generateET.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LayoutInflater l = getLayoutInflater();
final View viewToAdd = l.inflate(R.layout.to_add, null);
Button buttonWayPoints = (Button) viewToAdd.findViewById(R.id.btn_waypoint);
final EditText editTextWayPoints = (EditText) viewToAdd.findViewById(R.id.et_waypoint);
buttonWayPoints.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (editTextWayPoints.getText().toString() != null) {
Toast.makeText(MainActivity.this, editTextWayPoints.getText().toString(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "No text found", Toast.LENGTH_SHORT).show();
}
}
});
myLinearLay.addView(viewToAdd);
}
});
}
}
activity_main.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="com.techmahindra.stackques.MainActivity">
<Button
android:id="#+id/generateBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="add editText To layout" />
<ScrollView
android:layout_below="#+id/generateBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:id="#+id/dynamic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
></LinearLayout>
</ScrollView>
</RelativeLayout>
to_add.xml(layout which will be inflated at runtime)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/et_waypoint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Way Points"
android:padding="10dp"
android:textSize="16sp" />
<Button
android:id="#+id/btn_waypoint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Find" />
</LinearLayout>
There are some problems with your code. You're trying to add editTextWayPoints to mainActivity, but editTextWayPoints already has a parent, which is view. I think you should be adding view to mainActivity (mainActivity.addView(view)). Finally, to access the value of editTextWayPoints, you simply do editTextWayPoints.getText().toString();

Android getting particular element from arra using edittext

I have array in xml and I am trying to get a particular element from array using its number from user input but my app is crashing when click on button.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
String [] tabela;
TextView textView;
TextView textView2;
TextView textView3;
Button next, prev;
int index;
SharedPreferences sPref;
SharedPreferences.Editor editor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sPref = getSharedPreferences("daniel.myapplication", Context.MODE_PRIVATE);
editor = sPref.edit();
// Importing the string array from Valuses folder
tabela = getResources().getStringArray(R.array.array);
// initialization of textview
textView =(TextView)findViewById(R.id.textView);
textView2 =(TextView)findViewById(R.id.textView2);
textView3 =(TextView)findViewById(R.id.textView3);
//Initialization of buttons
next =(Button)findViewById(R.id.button);
prev =(Button)findViewById(R.id.button2);
//OnClickListener for buttons
next.setOnClickListener(this);
prev.setOnClickListener(this);
// Setting values for variable and textviews
index = sPref.getInt("key", 0);
textView.setText(tabela[index]);
textView2.setText(String.valueOf(index+1));
textView3.setText("/"+String.valueOf(tabela.length));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onPause(){
super.onPause();
editor.putInt("key",index);
editor.commit();
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button2:
index++;
if (index==tabela.length){
index=0;
textView.setText(tabela[index]);
textView2.setText(String.valueOf(index+1));
}else {
textView.setText(tabela[index]);
textView2.setText(String.valueOf(index + 1));
}
break;
case R.id.button:
index--;
if (index ==-1){
index = tabela.length -1;
textView.setText(tabela[index]);
textView2.setText(String.valueOf(index+1));
}else {
textView.setText(tabela[index]);
textView2.setText(String.valueOf(index + 1));
}
break;
}
}
public void random(View view) {
Random losuj = new Random();
int i = losuj.nextInt(tabela.length);
textView.setText(tabela[i]);
textView2.setText(String.valueOf(i + 1));
}
public void press(View view) {
EditText editText = (EditText) findViewById(R.id.editText);
int b = Integer.parseInt(editText.getText().toString());
// b = (int) Array.get(tabela, b);
textView.setText(tabela[b]);
textView2.setText(String.valueOf(b + 1));
}
}
This is my XML:
<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">
<TextView android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="84dp"
android:textSize="25sp"
android:id="#+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/previous"
android:id="#+id/button"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/next"
android:id="#+id/button2"
android:layout_alignBottom="#+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/xx"
android:id="#+id/textView2"
android:textSize="20sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/yy"
android:id="#+id/textView3"
android:textSize="20sp"
/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rand"
android:id="#+id/button3"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="random"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button4"
android:layout_below="#+id/button3"
android:layout_centerHorizontal="true"
android:layout_marginTop="78dp"
android:onClick="press"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_alignTop="#+id/button4"
android:layout_toRightOf="#+id/button4"
android:onClick="press"/>
int[] myArray = getResources().getIntArray(R.array.my_xml_int_array);
EditText editText = (EditText) findViewById(R.id.editText);
int b = Integer.valueOf(editText.getText().toString());
// Check the number is within range
if (b >= 0 && b < (myArray.length - 1)) {
textView.setText(String.valueOf(myArray[b]));
}
You should probably also make sure you set the input type for your EditText as number in your layout xml.
<EditText
android:id="#+id/editText"
...
android:inputType="number"/>
Additional:
As per my comments under your question "remove onClick from the EditText".
Your final EditText definition should look something like this:
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button4"
android:layout_toRightOf="#+id/button4"
android:inputType="number"/>
The most important thing is removing android:onClick="press" from the EditText.
When you run it, enter your number in the edit text and then press button4 (which you have labeled "New Button". The required string is then correctly shown in the TextView.
I have tested your code, copy/pasting it exactly as it is, and only changed the `EditText' as described, and created a dummy string-array in xml for testing. It works fine. If you are still having problems, provide some more description of what is happening.
In order to get an array from resource, do this somewhere within your onCreate():
ArrayList<String> yourArray;
yourArray = getResources().getStringArray(R.array.your_array_name_in_xml);
After that, you could just call yourArray.get(index) to get its content.
Given that, you could do this in order to get what you want:
int index = Integer.parseInt(yourEditText.getText().toString());
String textFromArray = yourArray.get(index);
yourTextView.setText(textFromArray);

How to set EditText values to the RangeSeekbar when edittext values based moving the rangeseekbar?

I am facing one issue while using customRangeSeekBar in android when rangeseekbar movies based on Edittext values my RangeSeekbar values Startingvalue startingvalue 0 and endingvalue 30
My requirement is suppose i will take two edittexts
firstedittext value is 10 and second edittextvalue is 20 based on this values RangeSeekbar will move
I wrote xml like:
<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"
android:background="#ffffff" >
<!-- <RelativeLayout
android:id="#+id/relative1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" >
<WebView
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout> -->
<RelativeLayout
android:id="#+id/relative2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/relative1"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentLeft="true"
>
<TextView
android:id="#+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:text="Start"
android:paddingBottom="5dp"
android:textColor="#0072ff"
android:textStyle="bold" />
<EditText
android:id="#+id/edt_starttime"
android:layout_width="50dp"
android:layout_height="30dp"
android:background="#0072ff"
android:inputType="number"
android:imeOptions="actionDone"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentRight="true"
>
<TextView
android:id="#+id/end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingBottom="5dp"
android:text="End"
android:textColor="#0072ff"
android:textStyle="bold"/>
<EditText
android:id="#+id/edt_endtime"
android:layout_width="50dp"
android:layout_height="30dp"
android:background="#0072ff"
android:inputType="number"
android:imeOptions="actionDone"/>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/relative2"
android:orientation="horizontal"
android:layout_margin="10dp"
android:id="#+id/layout">
</LinearLayout>
</RelativeLayout>
I used custom RangeSeekbar class like
RangeSeekBar.java
this class is developed based on this link
and finally my activity class is
MainActivity2.java
public class MainActivity2 extends Activity
{
//declaring widgets and variables...............
WebView webView1;
TextView start,end;
EditText edt_starttime,edt_endtime;
String url,edt_starttime1;
View handler1;
int minValue1;
#Override
//onCreate() starts............
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//getting refrences of widgets.......
webView1=(WebView)findViewById(R.id.webView1);
start=(TextView)findViewById(R.id.start);
end=(TextView)findViewById(R.id.end);
edt_starttime=(EditText)findViewById(R.id.edt_starttime);
edt_endtime=(EditText)findViewById(R.id.edt_endtime);
edt_starttime = (EditText) findViewById(R.id.edt_starttime);
// edittext listenr starts...........
// editettxt start listener ends.........
//editetxt end listener starts..............
edt_endtime.setOnEditorActionListener(new OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
// TODO Auto-generated method stub
if (actionId == EditorInfo.IME_ACTION_DONE)
{
Log.e("Ram san","jhgkfdjgh");
String edt_endtime1=edt_endtime.getText().toString();
if(edt_endtime1.length()!=0)
{
minValue1=Integer.parseInt(edt_endtime1);
Log.e("", ""+minValue1);
}
return true; // consume.
}
// pass on to other listeners.
return false;
}
});
//edittext listeners ends.................
// create RangeSeekBar as Integer range between 20 and 75
RangeSeekBar<Integer> seekBar = new RangeSeekBar<Integer>(0, 30, MainActivity2.this);
seekBar.setLeft(25);
seekBar.setOnRangeSeekBarChangeListener(new OnRangeSeekBarChangeListener<Integer>()
{
#Override
public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, final Integer minValue, Integer maxValue)
{
edt_starttime.setText(""+minValue);
edt_endtime.setText(""+maxValue);
Log.e("values ", ""+ minValue + ", MAX=" + maxValue);
}
});
// add RangeSeekBar to pre-defined layout
ViewGroup layout = (ViewGroup) findViewById(R.id.layout);
// layout.setP
layout.addView(seekBar);
}
//onCreate() ends.................
}
How can I set EditText values to the RangeSeekbar when edittext values are based on moving the rangeseekbar?

Problems with Radio Button Group

Am trying to use radio button group widget to select gender in my application. Now here is the thing. The user will select a gender and on clicking the button the user will be guided to the respective activity. I wrote the code . The second problem is that my program does nothing though am launching new activities in the if part. I debugged my program to see if the if condition is satisfied seems to me it does. Here is the xml and the .java file
public class Gender extends Activity{
private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
private Button btnDisplay;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gender_asking);
addListenerOnButton();
}
public void addListenerOnButton() {
radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
btnDisplay = (Button) findViewById(R.id.gender_button);
btnDisplay.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//****************************
int selectedId = radioSexGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioSexButton = (RadioButton) findViewById(selectedId);
if("Iam a Guy!".equals(radioSexButton.getText()))
{
Intent male_activity = new Intent(Gender.this, Meter.class);
startActivity(male_activity);
}
if("Iam a Girl!".equals(radioSexButton.getText()))
{
Intent male_activity = new Intent(Gender.this, For_Girl.class);
startActivity(male_activity);
}
//********************************
}
});
}
}
and the associated xml is:
<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:background="#drawable/hyu_bg"
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=".KissingMeter" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:text="Tell Us Your Gender!"
android:textSize="30dp" />
<RadioGroup
android:id="#+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/gender_button"
android:layout_below="#+id/textView1"
android:layout_marginTop="30dp" >
<RadioButton
android:id="#+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/radioFemale"
android:layout_alignLeft="#+id/radioFemale"
android:layout_marginBottom="34dp"
android:checked="true"
android:text="Iam a Guy!" />
<RadioButton
android:id="#+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/gender_button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="44dp"
android:text="Iam a Girl!" />
</RadioGroup>
<Button
android:id="#+id/gender_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioSex"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="Lol" />
</RelativeLayout>
First of all, don't rely on the text of a View in this situation. If you decide to change it later then it will create problems. Most likely, you won't change the id of it so just use that. Change it to something like this
btnDisplay.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
int selectedId = radioSexGroup.getCheckedRadioButtonId(); / get the id
switch (selectedId) // switch on the button selected
{
case R.id.radioMale:
Intent male_activity = new Intent(Gender.this, Meter.class);
startActivity(male_activity);
break;
case R.id.radioFemale:
Intent male_activity = new Intent(Gender.this, For_Girl.class);
startActivity(male_activity);
break;
default:
Toast.makeText(v.getContext(), "No gender selected",
Toast.LENGTH_SHORT).show();
break;
}
//********************************
}
});

Hide a EditText & make it visible by clicking a menu

I have a layout having the contact deatils of the phone. When i click the option menu i need make an edittext visible in that screen. I have done it. But there is problem facing that the edit text height is occupied in the screen when its made invisible. How can i remove the space occupied by edit text, while its invisible in the screen(layout).. My code is given below
My xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:paddingLeft="10dp"
android:paddingRight="10dp">
<ListView android:id="#id/android:list" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1"
android:drawSelectorOnTop="false">
</ListView>
<TextView android:id="#id/android:empty" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:text="No Entries available">
</TextView>
<TableRow android:id="#+id/TableRow001"
android:layout_width="wrap_content" android:background="#C0C0C0"
android:layout_height="wrap_content">
<EditText android:id="#+id/NumberEditText01"
android:layout_width="wrap_content"
android:paddingLeft="20dip"
android:layout_height="wrap_content">
</EditText>
<Button android:layout_width="wrap_content" android:id="#+id/callNow01"
android:layout_height="wrap_content"
android:text="Call now"
>
</Button>
</TableRow>
</LinearLayout>
and class:
public class ListContacts extends ListActivity {
TableRow tableRow;
EditText phoneNumber;
Button callNow;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Associate the xml with the activity
setContentView(R.layout.activitylist);
tableRow = (TableRow) findViewById(R.id.TableRow001);
tableRow.setVisibility(View.INVISIBLE);
phoneNumber = (EditText) findViewById(R.id.NumberEditText01);
phoneNumber.setVisibility(View.INVISIBLE);
phoneNumber.setKeyListener(DialerKeyListener.getInstance());
callNow = (Button) findViewById(R.id.callNow01);
callNow.setVisibility(View.INVISIBLE);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case FIRST:
tableRow.setVisibility(View.VISIBLE);
phoneNumber.setVisibility(View.VISIBLE);
callNow.setVisibility(View.VISIBLE);
break;
}
}
}
Try phoneNumber.setVisibility(View.GONE);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.waist2height); {
final EditText edit = (EditText)findViewById(R.id.editText);
final RadioButton rb1 = (RadioButton) findViewById(R.id.radioCM);
final RadioButton rb2 = (RadioButton) findViewById(R.id.radioFT);
if(rb1.isChecked()){
edit.setVisibility(View.VISIBLE);
}
else if(rb2.isChecked()){
edit.setVisibility(View.INVISIBLE);
}
}

Categories

Resources