I have a 5 linear layouts, each containing 10 buttons, which gives rise to a 5 by 10 array of buttons. I would like the user to select 5 buttons, and each button contains a certain point value. On the next page, I would like the sum of the point values of these 5 buttons to appear in a textview.
Here is what I have tried so far, using a small sample of my code.
On the xml file: (this is a 2 by 3 sample)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button11"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="50" />
<Button
android:id="#+id/button12"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="50" />
<Button
android:id="#+id/button13"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="75" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button21"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="00" />
<Button
android:id="#+id/button22"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="25" />
<Button
android:id="#+id/button23"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="75" />
</LinearLayout>
I am not sure what to do on the java file, but I was considering giving each button id a value (which is currently represented by the name of the button) and adding up all the values, which will then be displayed on the next page.
You can just convert the text on the button to an integer:
int value = 0;
try {
value = Integer.parseInt(button.getText().toString());
}
catch(NumberFormatException nfe) {
}
Say that xml file is called activity_main.xml. You need to have an Activity class, let's call it MainActivity.java
MainActivity.java
public class MainActivity extends Activity implements OnClickListener{
private totalVal = 0;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button12 = (Button) findViewById(R.id.button12);
button12.setOnClickListener(this);
// ... Do the same for the rest of the buttons
}
#Override
public void onClick(View v){
switch(v.getId(){
case R.id.button12:
int textVal = Integer.parseInt(v.getText().toString());
totalVal = totalVal + textVal;
// do whatever else you want to when the button is clicked
break;
// ... Do the same for the rest of the buttons
}
You would also need a button that says you are done, and implement it in a similar way
Related
I am making an app where it takes two types of users. "Viewers" and "Contractors". I made two radio buttons for each option. I want to know three things:
How to activate a button when a radio button is selected. How to inactivate a button when there is no radio button selected. Lastly, how to make both of the radio buttons send you to a unique activity depending of the option chosen. For example, I pick "Contractor" then press the button to continue, it'll send me to a unique layout that connects to that radio button.
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/b"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_background"
tools:context="com.devteam.abire.abire.b">
<android.support.v7.widget.CardView
app:cardElevation="15dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="300dp"
android:layout_height="345dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
app:cardElevation="20dp"
android:layout_width="320dp"
android:layout_height="320dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:background="#141526"
android:layout_width="match_parent"
android:layout_height="50dp"/>
<ImageView
android:id="#+id/abire_app_icon_v2"
android:layout_marginTop="21dp"
android:elevation="45dp"
android:layout_centerHorizontal="true"
android:background="#drawable/abire_logo_v1"
android:layout_width="55dp"
android:layout_height="55dp" />
<TextView
android:layout_marginStart="20dp"
android:id="#+id/register_as_text"
android:layout_marginTop="10dp"
android:text="Register As A..."
android:textColor="#141526"
android:layout_below="#+id/abire_app_icon_v2"
android:textSize="28sp"
android:textAlignment="textStart"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RadioButton
android:layout_marginStart="20dp"
android:textSize="22sp"
android:textColor="#141526"
android:id="#+id/viewer_radioBtn"
android:text="Viewer"
android:layout_marginTop="18dp"
android:layout_below="#+id/register_as_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RadioButton
android:layout_marginStart="20dp"
android:textSize="22sp"
android:textColor="#141526"
android:id="#+id/contractor_radioBtn"
android:text="Contractor"
android:layout_marginTop="18dp"
android:layout_below="#+id/viewer_radioBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/continueBtn"
android:textSize="18sp"
android:text="CONTINUE"
android:textColor="#fff"
android:layout_marginTop="25dp"
android:layout_centerHorizontal="true"
android:layout_below="#+id/contractor_radioBtn"
android:background="#drawable/ripple_maroon"
android:layout_width="250dp"
android:layout_height="38dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
Here is my Java:
package com.devteam.abire.abire;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class b extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.b);
}
}
If you want to make only one of the buttons clickable at a time, you should put radiobuttons inside radiogroup
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:layout_marginStart="20dp"
android:textSize="22sp"
android:textColor="#141526"
android:id="#+id/viewer_radioBtn"
android:onCLick="onRadioButtonClicked"
android:text="Viewer"
android:layout_marginTop="18dp"
android:layout_below="#+id/register_as_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RadioButton
android:layout_marginStart="20dp"
android:textSize="22sp"
android:textColor="#141526"
android:id="#+id/contractor_radioBtn"
android:onCLick="onRadioButtonClicked"
android:text="Contractor"
android:layout_marginTop="18dp"
android:layout_below="#+id/viewer_radioBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RadioGroup>
<Button
android:id="#+id/continueBtn"
android:textSize="18sp"
android:text="CONTINUE"
android:textColor="#fff"
android:layout_marginTop="25dp"
android:layout_centerHorizontal="true"
android:layout_below="#+id/contractor_radioBtn"
android:onCLick="onButtonClicked"
android:background="#drawable/ripple_maroon"
android:layout_width="250dp"
android:layout_height="38dp" />
package com.devteam.abire.abire;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class b extends AppCompatActivity {
private String mClickedRadioButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.b);
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.viewer_radioBtn":
if (checked)
sClickedRadioButton = "viewer";
break;
case R.id.contractor_radioBtn"
if (checked)
sClickedRadioButton; = "contractor";
break;
}
}
public void onButtonClicked(View v){
if(sClickedRadioButton == null){
return;
}else if(sClickedRadioButton.equals("viewer")){
//Do something when view is checked
}else if(sClickedRadioButton.equals("contractor"){
// Do something when contractor is checked
}
}
Firstly, it is good to use radiogroup instead of radio button if you have multipe buttons.
For "How to activate a button when a radio button is selected. How to inactivate a button when there is no radio button selected.", create references for both buttons and radio buttons in activity. Then, if first radio button is checked, disable button by :
button.setEnabled(false);
For "Lastly, how to make both of the radio buttons send you to a unique activity depending of the option chosen.", while using radiogroup use :
public void onCheckedChanged(RadioGroup arg0, int arg1) {
radioButton = (RadioButton) findViewById(radioGroup.getCheckedRadioButtonId(););
if (radioButton.isChecked()) {
text=radioButton.getText().toString();
if (text.equals("radiobtn1Option")) {
//TODO : start new activity
Intent intent = new Intent(this, YourNextSCreen.class);
startActivity(intent);
} if (text.equals("radiobtn2Option")) {
//TODO
} else {
//TODO
}
}
}
});
For example. There is picture on the top of the screen below that there are some empty boxes and below the boxes there are some buttons. Every button has a character for text("a","c","t"). You click on a button and the button's text appear in the box. You can click them in the order you want to but the answer is "cat" so when you put the characters in the correct order then you got a toast.
I tried to do it with TextViews and Buttons. I can make the button disappear when i click on it and a textview appear in the same time. But every textview has a fix place on the screen, so i need to put every character in every box invisible and when i click on the "c" character it appear in the first box and the other "c" characters stay invisible. But if i click on the "a" first, then it appears in the second box because there is too much variation to do all. I'm not good at explaining but if anyone has an idea how to do that easier please response!
Here is my code:
public class MainActivity extends ActionBarActivity implements OnClickListener{
Button b1;
Button b2;
Button b3;
TextView tg1;
TextView tg2;
TextView tg3;
TextView to1;
TextView to2;
TextView to3;
TextView tl1;
TextView tl2;
TextView tl3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.bg);
b1.setOnClickListener(this);
b2 = (Button)findViewById(R.id.bo);
b2.setOnClickListener(this);
b3 = (Button)findViewById(R.id.bl);
b3.setOnClickListener(this);
tg1 = (TextView)findViewById(R.id.tg1);
tg2 = (TextView)findViewById(R.id.tg2);
tg3 = (TextView)findViewById(R.id.tg3);
to1 = (TextView)findViewById(R.id.to1);
to2 = (TextView)findViewById(R.id.to2);
to3 = (TextView)findViewById(R.id.to3);
tl1 = (TextView)findViewById(R.id.tl1);
tl2 = (TextView)findViewById(R.id.tl2);
tl3 = (TextView)findViewById(R.id.tl3);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.bg:
b1.setVisibility(View.INVISIBLE);
tg1.setVisibility(View.VISIBLE);
tg2.setVisibility(View.INVISIBLE);
tg3.setVisibility(View.INVISIBLE);
break;
case R.id.bo:
b2.setVisibility(View.INVISIBLE);
to2.setVisibility(View.VISIBLE);
to1.setVisibility(View.INVISIBLE);
to3.setVisibility(View.INVISIBLE);
break;
case R.id.bl:
b3.setVisibility(View.INVISIBLE);
tl3.setVisibility(View.VISIBLE);
tl2.setVisibility(View.INVISIBLE);
tl1.setVisibility(View.INVISIBLE);
}
}
}
<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: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="hu.szada.gombokelso.MainActivity"
android:orientation="horizontal">
<TextView
android:id="#+id/tl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="invisible"
android:text="l"/>
<Button
android:id="#+id/bo"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="86dp"
android:onClick="onClick"
android:text="o" />
<Button
android:id="#+id/bl"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignBaseline="#+id/bg"
android:layout_alignBottom="#+id/bg"
android:layout_alignParentLeft="true"
android:layout_marginLeft="36dp"
android:onClick="onClick"
android:text="l" />
<Button
android:id="#+id/bg"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginRight="14dp"
android:layout_toLeftOf="#+id/bo"
android:onClick="onClick"
android:text="g" />
<TextView
android:id="#+id/tg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tl"
android:layout_alignBottom="#+id/tl"
android:layout_alignLeft="#+id/tl"
android:layout_weight="1"
android:visibility="invisible"
android:text="g" />
<TextView
android:id="#+id/to1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tl"
android:layout_alignBottom="#+id/tl"
android:layout_alignLeft="#+id/tl"
android:layout_weight="1"
android:visibility="invisible"
android:text="o" />
/// Second
<TextView
android:id="#+id/to2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tl1"
android:layout_alignBottom="#+id/tl1"
android:layout_marginLeft="19dp"
android:layout_toRightOf="#+id/tl1"
android:layout_weight="1"
android:visibility="invisible"
android:text="o" />
<TextView
android:id="#+id/tg2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/to2"
android:layout_alignBottom="#+id/to2"
android:layout_alignLeft="#+id/to2"
android:layout_weight="1"
android:visibility="invisible"
android:text="g" />
<TextView
android:id="#+id/tl2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/to2"
android:layout_alignBottom="#+id/to2"
android:layout_alignRight="#+id/to2"
android:layout_weight="1"
android:visibility="invisible"
android:text="l" />
/// Third
<TextView
android:id="#+id/tg3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/to2"
android:layout_alignBottom="#+id/to2"
android:layout_alignRight="#+id/bl"
android:layout_weight="1"
android:visibility="invisible"
android:text="g" />
<TextView
android:id="#+id/tl3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tg3"
android:layout_alignBottom="#+id/tg3"
android:layout_alignLeft="#+id/tg3"
android:layout_weight="1"
android:visibility="invisible"
android:text="l" />
<TextView
android:id="#+id/to3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tg3"
android:layout_alignBottom="#+id/tg3"
android:layout_toRightOf="#+id/tl3"
android:layout_weight="1"
android:visibility="invisible"
android:text="o" />
You might want to try a slightly different approach.
If I understand you correctly, you want to "type" a word out using given lettered buttons. Like one of those hangman style games.
Why not append the text views on the fly.
Something like
#Override
public void onClick(View v) {
//Grab the surrounding layout for the textviews
GridView answerGrid = (GridView)getViewById(R.id.answerGrid);
//Get the text that was on the button
Button b = (Button)v;
String btnText = b.getText().toString();
//Make a text view with text
TextView txt = new TextView();
text.setText(btnText);
//Append to text view container
answerGrid.addView(txt);
//Invisible button
b.setVisibility(View.INVISIBLE);
}
Haven't tested to see if this is perfect, but its a start.
=====
I've looked at your xml
Why not use GridViews?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
....>
<GridView android:id="#+id/answerGrid"
....>
<!-- Put nothing here. This is for answers -->
</GridView>
<GridView android:id="#+id/lettersGrid"
android:layout_below="answerGrid"
....>
<!-- Buttons in here -->
</GridView>
</RelativeLayout>
This way you can customise the number of rows/columns based on the length of the word you're playing with. And GridView will automatically give you a neat layout and spacing.
Have a look at the GridView doc and get it customised the way you want it.
See my edits above for the Java code.
I am trying to change visibility of Relative Layout in my code on Button click, But i am facing a weird behavior.
Whenever i click on my Button say A first time, it shows the Relative layout say RA, and after clicking it(Second time) again it hides RA. The problem i am facing here is when i click on A second time it also hides its parent layout, And then on clicking it again it shows RA and its parent layout, After that it hides and shows both layouts on click event.
How this is happening here,
Here is my Code
this is my XML
<RelativeLayout
android:id="#+id/mainscreenrelativeLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_margin="3dp"
android:background="#ffffff" >
<Button
android:id="#+id/imagebuttonSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:background="#drawable/setting_icon_hover"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/RL_mainpage_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/mainscreenrelativeLayout2"
android:layout_alignParentRight="true"
android:visibility="invisible"
>
<Button
android:id="#+id/button_settings_monthlyissue"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/monthly_issue_select"/>
<Button
android:id="#+id/button_settings_changepassword"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button_settings_monthlyissue"
android:background="#drawable/password_select" />
<Button
android:id="#+id/button_settings_logout"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button_settings_changepassword"
android:background="#drawable/logout_select" />
</RelativeLayout>
And this is my CODE in Activity Class:
Button _settings;
RelativeLayout __settingslayout;
boolean isSettingsClicked = false;
........
........
_settings = (Button) findViewById(R.id.imagebuttonSettings);
_settingslayout = (RelativeLayout) findViewById(R.id.RL_mainpage_settings);
_settings.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (isSettingsClicked == false) {
_settings.setBackgroundResource(R.drawable.setting_icon);
_settingslayout.setVisibility(View.VISIBLE);
isSettingsClicked = true;
} else if (isSettingsClicked == true) {
_settingslayout.setVisibility(View.INVISIBLE);
_settings
.setBackgroundResource(R.drawable.setting_icon_hover);
isSettingsClicked = false;
}
}
});
The problem is , RelativeLayout with id android:id="#+id/mainscreenrelativeLayout2" is also hiding on click event, as you can see i haven't used this id in my Activity.
I am new to java and android so please forgive me if i am asking to simple question.
I have an application which requires user input in two EditTexts. Those inputs are multipied and result is displayed in TextView. I would like to use "clear entries" button which would clear the content of user entries and displayed result. Is there any way to do it?
Here is an application code.
package c.example.rectangle;
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
EditText l;
EditText w;
TextView a;
Button b;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l = (EditText) findViewById(R.id.length);
w = (EditText) findViewById(R.id.width);
a = (TextView) findViewById(R.id.lblarea);
b = (Button) findViewById(R.id.calculate);
b.setOnClickListener(this);
}
public void onClick(View v) {
calculateRectangle(l.getText().toString(), w.getText().toString());
}
private void calculateRectangle(String clength, String cwidth){
double area = Double.parseDouble(clength)*Double.parseDouble(cwidth);
a.setText(String.valueOf(area));
}}
And here is my XML Code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#8B4513"
android:orientation="vertical" >
<TextView
android:id="#+id/label1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:background="#2F4F4F"
android:gravity="center"
android:text="#string/rect"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#8B4513"
android:orientation="horizontal" >
<TextView
android:id="#+id/label2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="50dp"
android:background="#2F4F4F"
android:gravity="center"
android:text="#string/cm"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/length"
android:layout_width="110dp"
android:layout_height="21dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="50dp"
android:background="#2F4F4F"
android:ems="10"
android:gravity="center"
android:hint="#string/help"
android:inputType="text" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#8B4513"
android:orientation="horizontal" >
<TextView
android:id="#+id/label3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#2F4F4F"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:text="#string/breadth"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/width"
android:layout_width="110dp"
android:layout_height="21dp"
android:layout_marginLeft="33dp"
android:layout_marginTop="20dp"
android:background="#2F4F4F"
android:inputType="text"
android:hint="#string/help"
android:ems="10"
android:gravity="center"
>
<requestFocus />
</EditText>
</LinearLayout>
<Button
android:id="#+id/calculate"
android:layout_width="fill_parent"
android:layout_marginLeft="100dip"
android:layout_marginRight="100dip"
android:layout_height="wrap_content"
android:text="#string/calculate"
android:layout_marginTop="20dp" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#8B4513"
android:orientation="horizontal" >
<TextView
android:id="#+id/label4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:background="#2F4F4F"
android:text="#string/area"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/lblarea"
android:layout_width="110dp"
android:layout_height="21dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="20dp"
android:background="#2F4F4F"
android:gravity="center"/>
</LinearLayout>
<Button
android:id="#+id/clear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="100dip"
android:layout_marginRight="100dip"
android:layout_marginTop="20dp"
android:text="#string/clear" />
</LinearLayout>
I would be very appreciate for the answer.
Why not just set both editTexts to empty when you no longer need the data in them to be displayed?
EditText.setText("");
Same thing with the TextView;
TextView.setText("");
If you are wanting to iterate through them, you can put them in a list then use a for loop to set the text to ""
List<EditText> myList = new List<EditText>();
myList.add(editText1);
Then in your clear method
for (int x = 0; x < myList.size(); x++
{
myList.get(x).setText("");
}
Why nobody seems to use the really useful android:onClick ?
<Button
...
android:text="#string/calculate"
android:onClick="calculate" />
<Button
...
android:text="#string/clear"
android:onClick="clearForm" />
With the following activity :
class MyActivity extends Activity
{
...
/**
* Calculate
* android:onClick="calculate"
*/
public void calculate(View view)
{
// Handle click on your 'Calculate' button
}
/**
* Clear form
* android:onClick="clearForm"
*/
public void clearForm(View view)
{
int[] ids = new int[]{R.id.length, R.id.width};
for(int id : ids)
{
((EditText) this.findViewById(id).)setText("");
}
}
}
This way, you do not have to care about ids and your code will be more clean than clean.
Ids should not be overused! They are great on views that can be 'changed' by the user to handle those changes (and to enjoy the onSaveInstanceState() natural behavior) but that's it!
IMO.
If you want to create a clear button, do the following.
Create a Button in your xml:
<Button
android:id="#+id/clear_button"
... you own layout prefs ...
/>
Create a listener for the button in your code:
OnClickListener clearButtonListener = new OnClickListener(){
#Override
public void onClick(View view) {
((EditText)findViewById(R.id.id_for_text_box_a)).setText("");
//...do this for all your edit texts that you want to clear
}
};
Connect the listener to the button
Button clearButton = (Button) findViewById(R.id.clear_button);
clearButton.setOnClickListener(clearButtonListener);
Alternatively, instead of finding the edit texts by id in the listener, they could be instance variables that get initialized in onCreate or wherever. I would also recommend not using one letter variable names.
Personally, I would set OnClickListeners instead of the onClick attribute in the XML. Although using the XML onClick attribute may amount to fewer lines of code, it unfortunately creates a very tight coupling of layout and functionality. I prefer to have XML for layout, and Java for functionality. Additionally, being forced to use ids amounts to requiring XML elements to have variable names, which makes for more readable layout code (what is this button? what is this checkbox for?). Another problem I see with using the onClick attribute is that it forces your methods called to be public, which doesn't really make sense for many of these methods. I prefer to understand what an Activity does functionally through reading the Java, and I would rather not have unreferenced public methods floating around in my Activities.
I have three buttons in my Activity with three separate onClickListeners set like I have done plenty times before. But one of the listeners does not react to a click event and I have no clue as to why. Here is the code segment:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_reminder_2);
//References to layout resources
edit2Back = (Button) findViewById(R.id.edit2Back);
edit2Next = (Button) findViewById(R.id.edit2Next);
edit2ChangeGPS = (Button) findViewById(R.id.edit2ChangeGPS);
//Assigning listeners to Buttons
edit2Back.setOnClickListener(listenerBack);
edit2Next.setOnClickListener(listenerNext);
edit2ChangeGPS.setOnClickListener(listenerChange);
}
final OnClickListener listenerNext = new OnClickListener() {
public void onClick(View v) {
Log.v("edit2Next","Click!");
db.open();
String sName = edit2ReminderName.getText().toString();
String sNote = edit2ReminderText.getText().toString();
int sRadius = Integer.parseInt(edit2Radius.getText().toString());
String sUnits = (String) edit2SpinnerUnits.getSelectedItem();
int sChecked = 0;
if (edit2Check.isChecked()) {
sChecked = 1;
}
db.insertReminder(sName, sNote, lat, lon, sRadius, sUnits, sChecked);
db.close();
Intent intent = new Intent(context, Reminders.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
};
All my other listener I wrote in the same fashion and they work perfectly fine, but this one does not. I looked all over the code but could not find the reason. The listener does not start at all, not even the Log.v instruction runs. Thanks for your advice!
EDIT:
This is part of the XML code where i define my Buttons:
<LinearLayout
android:id="#+id/edit2ControlLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/azure"
android:padding="15dp"
android:orientation="horizontal" >
<Button
android:id="#+id/edit2Back"
android:layout_height="wrap_content"
android:layout_width="0.0dip"
android:text="Back"
android:background="#drawable/round_button_violet"
android:textColor="#color/azure"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_weight="1.0"
android:layout_marginRight="5dp" />
<Button
android:id="#+id/edit2Next"
android:layout_height="wrap_content"
android:layout_width="0.0dip"
android:text="Next"
android:background="#drawable/round_button_violet"
android:textColor="#color/azure"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_weight="1.0"
android:enabled="false" />
</LinearLayout>
Please remove android:enabled="false" in your next button code in-order to work next button.
<Button
android:id="#+id/edit2Next"
android:layout_height="wrap_content"
android:layout_width="0.0dip"
android:text="Next"
android:background="#drawable/round_button_violet"
android:textColor="#color/azure"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_weight="1.0"
android:enabled="false" />