protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view) {
CheckBox yes = (CheckBox) findViewById(R.id.yes);
CheckBox no = (CheckBox) findViewById(R.id.no);
EditText ageText = (EditText) findViewById(R.id.ageText);
EditText editText = (EditText) findViewById(R.id.editText);
EditText editText3 = (EditText) findViewById(R.id.editText3);
int age;
age = 0;
if (yes.isChecked()) {
editText3.setText(age + 15);
} else if (no.isChecked()) {
editText3.setText(age - 10);
}
finish();
}
});
}
This code is not giving me any output in editText3. Why this is happening? The code has no bugs but still no output!
Change
button.setOnClickListener(new)onClickListener() ...
to
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean checked = ((CheckBox) view).isChecked();
if (checkBox.isChecked()){
editText3.setText(age+15);
} else if (checkBox.isChecked()){
editText3.setText(age - 10);
}
}
});
You wrongly used setOnClickListener. Change your code this way:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean checked = ((CheckBox) view).isChecked();
if (checkBox.isChecked()) {
//check age is string or not.if not a string means change into String format
editText3.setText(age + 15);
} else if (checkBox.isChecked()) {
editText3.setText(age - 10);
}
}
});
Related
This is my code for the pizza app through updating I wanted to understand where I the data gathered from clicking a radiobutton or a checkbox using Intent will be placed here then moved to the 2nd activity? I just want to know where the Intent be placed in this code?
First Activity
public class MainActivity extends AppCompatActivity {
private Button button;
private RadioGroup g1;
private CheckBox cb1, cb2, cb3, cb4, cb5, cb6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
g1 = (RadioGroup) findViewById(R.id.G1);
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
cb4 = (CheckBox) findViewById(R.id.cb4);
cb5 = (CheckBox) findViewById(R.id.cb5);
cb6 = (CheckBox) findViewById(R.id.cb6);
g1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.rb1)
{
Toast.makeText(MainActivity.this, "Small", Toast.LENGTH_SHORT).show();
}
if (checkedId == R.id.rb2)
{
Toast.makeText(MainActivity.this, "Medium", Toast.LENGTH_SHORT).show();
}
if (checkedId == R.id.rb3)
{
Toast.makeText(MainActivity.this, "Large", Toast.LENGTH_SHORT).show();
}
}
});
cb1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
if (cb1.isChecked()){
Toast.makeText(MainActivity.this, "Pepperoni", Toast.LENGTH_SHORT).show();
}
}
});
cb2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
if (cb2.isChecked()){
Toast.makeText(MainActivity.this, "Olive", Toast.LENGTH_SHORT).show();
}
}
});
cb3.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
if (cb3.isChecked()){
Toast.makeText(MainActivity.this, "Mushroom", Toast.LENGTH_SHORT).show();
}
}
});
cb4.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
if (cb4.isChecked()){
Toast.makeText(MainActivity.this, "Extra Cheese", Toast.LENGTH_SHORT).show();
}
}
});
cb5.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
if (cb5.isChecked()){
Toast.makeText(MainActivity.this, "Chicken", Toast.LENGTH_SHORT).show();
}
}
});
cb6.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
if (cb6.isChecked()){
Toast.makeText(MainActivity.this, "Green Pepper", Toast.LENGTH_SHORT).show();
}
}
});
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
openActivity2();
}
});
}
public void openActivity2(){
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
}
}
this is my code for my radiobuttons where do I put the intent to send the data gathered from here to the textView in the second activity?
Second Activity
public class Activity2 extends AppCompatActivity {
EditText editName, editPhone, editEmail, editAddress;
TextView total;
Button buttonSubmit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
editName = (EditText) findViewById(R.id.editName);
editPhone = (EditText) findViewById(R.id.editPhone);
editEmail = (EditText) findViewById(R.id.editEmail);
editAddress = (EditText) findViewById(R.id.editAddress);
total = (TextView) findViewById(R.id.total);
buttonSubmit = (Button) findViewById(R.id.button2);
buttonSubmit.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
String name = editName.getText().toString();
String phone = editPhone.getText().toString();
String email = editEmail.getText().toString();
String address = editAddress.getText().toString();
total.setText("Name:\t" + name + "\nPhone:\t" + phone +"\nEmail:\t" + email + "\nAddress:\t" + address);
}
});
}
}
where do I place the getIntent()?
if I understood correctly you want to send the size of the pizza to the second Activity when user select Size from RadioButton for that you can put extra string on the onCheckedChanged method and start your second Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
g1 = (RadioGroup) findViewById(R.id.G1);
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
cb4 = (CheckBox) findViewById(R.id.cb4);
cb5 = (CheckBox) findViewById(R.id.cb5);
cb6 = (CheckBox) findViewById(R.id.cb6);
g1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
String size = "";
if (checkedId == R.id.rb1) {
Toast.makeText(MainActivity.this, "Small", Toast.LENGTH_SHORT).show();
size = "Small";
}
if (checkedId == R.id.rb2) {
Toast.makeText(MainActivity.this, "Medium", Toast.LENGTH_SHORT).show();
size = "Medium";
}
if (checkedId == R.id.rb3) {
Toast.makeText(MainActivity.this, "Large", Toast.LENGTH_SHORT).show();
size = "Large";
}
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("PizzaSize", size);
startActivity(intent);
}
});
and this is your Activity2:
public class Activity2 extends AppCompatActivity {
EditText editName, editPhone, editEmail, editAddress;
TextView total;
Button buttonSubmit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
editName = (EditText) findViewById(R.id.editName);
editPhone = (EditText) findViewById(R.id.editPhone);
editEmail = (EditText) findViewById(R.id.editEmail);
editAddress = (EditText) findViewById(R.id.editAddress);
total = (TextView) findViewById(R.id.total);
buttonSubmit = (Button) findViewById(R.id.button2);
// you can here get your Intent after initializing the views
String size = getIntent().getStringExtra("PizzaSize");
buttonSubmit.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
String name = editName.getText().toString();
String phone = editPhone.getText().toString();
String email = editEmail.getText().toString();
String address = editAddress.getText().toString();
total.setText("Name:\t" + name + "\nPhone:\t" + phone +"\nEmail:\t" + email + "\nAddress:\t" + address);
}
});
}
}
I have been trying to make a simple calculator on Android studio. It shows the calculations perfectly fine. but whenever I try to press equals or plus sub or minus operation when the EditText box is empty, my app always crashes.
I tried different ways but it didn't help.
Is there any way that I can do it and stop my app from crashing?
Here's my java code
package com.example.mathcalculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button button0,button1,button2,button3,button4,button5,button6,button7,button8,button9,buttonClear,buttonMul,buttonSub,buttonAdd,buttonDec,buttonEqual,buttonDiv;
float val1,val2;
EditText result;
boolean mAdditon,msubtraction,mMultiplication, mDivision;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button0 = (Button) findViewById(R.id.button0);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
button9 = (Button) findViewById(R.id.button9);
buttonAdd = (Button) findViewById(R.id.buttonAdd);
buttonMul = (Button) findViewById(R.id.buttonMul);
buttonDiv = (Button) findViewById(R.id.buttonDiv);
buttonClear = (Button) findViewById(R.id.buttonClear);
buttonEqual = (Button) findViewById(R.id.buttonEqual);
buttonDec = (Button) findViewById(R.id.buttonDec);
buttonSub = (Button) findViewById(R.id.buttonSub);
result = (EditText) findViewById(R.id.result);
button0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"0");
}
});
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"1");
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"2");
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"3");
}
});
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"4");
}
});
button5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"5");
}
});
button6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"6");
}
});
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"7");
}
});
button8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"8");
}
});
button9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+"9");
}
});
buttonDec.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText(result.getText()+".");
}
});
buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (result == null)
{
result.setText("");
}
else
{
val1 =Float.parseFloat(result.getText()+"");
mAdditon=true;
result.setText(null);
}
}
});
buttonSub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
{
val1=Float.parseFloat(result.getText()+"");
msubtraction=true;
result.setText(null);
}
}
});
buttonDiv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
{
val1=Float.parseFloat(result.getText()+"");
mDivision=true;
result.setText(null);
}
}
});
buttonMul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
{
val1=Float.parseFloat(result.getText()+"");
mMultiplication = true;
result.setText(null);
}
}
});
buttonClear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText("");
}
});
buttonEqual.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
val2 = Float.parseFloat(result.getText() + "");
if (mAdditon == true) {
result.setText(val1 + val2 + "");
mAdditon = false;
}
if (msubtraction == true) {
result.setText(val1 - val2 + "");
msubtraction = false;
}
if (mMultiplication == true) {
result.setText(val1 * val2 + "");
msubtraction = false;
}
if (mDivision == true) {
if (val2 == 0) {
result.setText("Can't Divide by Zero");
return;
} else {
result.setText(val1 / val2 + "");
msubtraction = false;
}
}
}
});
}
}
You say it crashes when the EditText box is empty, so most likely, what is happening is that when you do
Float.parseFloat(result.getText() + "");
result.getText() returns "", or you end up doing
Float.parseFloat("");
which result in java.lang.NumberFormatException: empty String
Try replacing
val1=Float.parseFloat(result.getText()+"");
with
val1 = 0;
if(!result.getText().isEmpty())
val1 = Float.parseFloat(result.getText());
NOTE:
Also, as you use it several times, you can make a method for it
private float getResult() {
int result = 0;
if(!result.getText().isEmpty())
val1=Float.parseFloat(result.getText());
return result;
}
and then replace everywhere with val1 = getResult();
as above code when get data from EditText use edit.getText().toString()
and for Parsing it always wrap it with try/catch block as EditText it is initially empty
some hints about the code
- here if (result == null) result.setText(""); result is always not null.
so for better solution always use try/catch when parsing EditText.
Tablayout is properly working with single textview which is displayed in each tab, i add calculator to one of tab named Tabfragment2, calculator xml in Tabfragment2 has no error while java code of calculator in fragment2.java i face error in findViewById all of them
public class TabFragment2 extends Fragment {
Activity referenceActivity;
Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btndiv, btnadd, btnsub, btnmul, btnzero, btndot, btnclear, btnequal ;
TextView textview;
float ValueOne, ValueTwo;
boolean Addition, Subtract, Multiplication, Division;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_fragment_2, container, false);
referenceActivity = getActivity();
btnzero = (Button) findViewById(R.id.btnzero);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
btn5 = (Button) findViewById(R.id.btn5);
btn6 = (Button) findViewById(R.id.btn6);
btn7 = (Button) findViewById(R.id.btn7);
btn8 = (Button) findViewById(R.id.btn8);
btn9 = (Button) findViewById(R.id.btn9);
btnadd = (Button) findViewById(R.id.btnadd);
btndiv = (Button) findViewById(R.id.btndiv);
btnsub = (Button) findViewById(R.id.btnsub);
btnmul = (Button) findViewById(R.id.btnmul);
btndot = (Button) findViewById(R.id.btndot);
btnclear = (Button) findViewById(R.id.btnclear);
btnequal = (Button) findViewById(R.id.btnequal);
textview = (TextView) findViewById(R.id.textview);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText(textview.getText() + "1");
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText(textview.getText() + "2");
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText(textview.getText() + "3");
}
});
btn4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText(textview.getText() + "4");
}
});
btn5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText(textview.getText() + "5");
}
});
btn6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText(textview.getText() + "6");
}
});
btn7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText(textview.getText() + "7");
}
});
btn8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText(textview.getText() + "8");
}
});
btn9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText(textview.getText() + "9");
}
});
btnzero.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText(textview.getText() + "0");
}
});
btndiv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText(textview.getText() + "/");
}
});
btnadd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (textview == null) {
textview.setText("");
} else {
ValueOne = Float.parseFloat(textview.getText() + "");
Addition = true;
textview.setText(null);
}
}
});
btnsub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (textview == null) {
textview.setText("");
} else {
ValueOne = Float.parseFloat(textview.getText() + "");
Subtract = true;
textview.setText(null);
}
}
});
btndiv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (textview == null) {
textview.setText("");
} else {
ValueOne = Float.parseFloat(textview.getText() + "");
Division=true;
textview.setText(null);
}
}
});
btnmul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (textview == null) {
textview.setText("");
} else {
ValueOne = Float.parseFloat(textview.getText() + "");
Multiplication = true;
textview.setText(null);
}
}
});
btnequal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ValueTwo = Float.parseFloat(textview.getText() + "");
if (Addition == true) {
textview.setText(ValueOne + ValueTwo + "");
Addition = false;
}
if (Subtract == true) {
textview.setText(ValueOne - ValueTwo + "");
Subtract = false;
}
if (Multiplication == true) {
textview.setText(ValueOne * ValueTwo + "");
Multiplication = false;
}
if (Division == true) {
textview.setText(ValueOne / ValueTwo + "");
Division = false;
}
}
});
btnclear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText("");
}
});
btndot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textview.setText(textview.getText() + ".");
}
});
return view;
}
}
Replace your all findViewById by below code.
btnzero = (Button) view.findViewById(R.id.btnzero);
btn1 = (Button) view.findViewById(R.id.btn1);
btn2 = (Button) view.findViewById(R.id.btn2);
btn3 = (Button) view.findViewById(R.id.btn3);
btn4 = (Button) view.findViewById(R.id.btn4);
btn5 = (Button) view.findViewById(R.id.btn5);
btn6 = (Button) view.findViewById(R.id.btn6);
btn7 = (Button) view.findViewById(R.id.btn7);
btn8 = (Button) view.findViewById(R.id.btn8);
btn9 = (Button) view.findViewById(R.id.btn9);
btnadd = (Button) view.findViewById(R.id.btnadd);
btndiv = (Button) view.findViewById(R.id.btndiv);
btnsub = (Button) view.findViewById(R.id.btnsub);
btnmul = (Button) view.findViewById(R.id.btnmul);
btndot = (Button) view.findViewById(R.id.btndot);
btnclear = (Button) view.findViewById(R.id.btnclear);
btnequal = (Button) view.findViewById(R.id.btnequal);
textview = (TextView) view.findViewById(R.id.textview);
call findViewById with your view object.
btnzero = (Button) view.findViewById(R.id.btnzero);
btn1 = (Button) view.findViewById(R.id.btn1);
btn2 = (Button) view.findViewById(R.id.btn2);
and go on.
I'm trying to do something slightly out of the ordinary. I have an Add a Profile page where a user can add multiple profiles. I want to represent these profiles as circles (ImageButtons) on the homepage.
Much like this:
So when a user saves a profile, how could I add one of these circles onto this horizontal scroll view? I would like the created circle to have a letter inside it from the first letter of the created clients name.
I think this would require me to have all 26 letters of the alphabet already created, but this is no problem.
Any suggestions on how to do this? I'm sorry if I'm not clear, I'd be happy to post any more code or clarify anything. The database I use is Firebase. I'm running the latest version of Android Studio.
Any help is greatly appreciated, cheers guys!
AddAProfile.java:
public class AddProfileActivity extends AppCompatActivity {
EditText etName;
EditText etCareer;
CheckBox cbTech;
CheckBox cbMedi;
CheckBox nfRenewableEnergy;
CheckBox nfGoogle;
CheckBox cfNovartis;
CheckBox nfTesla;
CheckBox lrsFB;
CheckBox lrsAAPL;
CheckBox lrsYHOO;
CheckBox cbEURUSD;
CheckBox lrcuUSDRUB;
CheckBox lrcoSILVER;
CheckBox lrcoGOLD;
CheckBox lrcuGBPUSD;
CheckBox lriNSDQ;
CheckBox lriSP500;
Button bSave;
DatabaseReference databaseClients;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_profile);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
databaseClients = FirebaseDatabase.getInstance().getReference("clients");
etName = (EditText) findViewById(R.id.etName);
etCareer = (EditText) findViewById(R.id.etCareer);
cbTech = (CheckBox) findViewById(R.id.cbTech);
cbMedi = (CheckBox) findViewById(R.id.cbMedi);
nfRenewableEnergy = (CheckBox) findViewById(R.id.nfRenewableEnergy);
nfGoogle = (CheckBox) findViewById(R.id.nfGoogle);
cfNovartis = (CheckBox) findViewById(R.id.cfNovartis);
nfTesla = (CheckBox) findViewById(R.id.nfTesla);
lrsFB = (CheckBox) findViewById(R.id.lrsFB);
lrsAAPL = (CheckBox) findViewById(R.id.lrsAAPL);
lrsYHOO = (CheckBox) findViewById(R.id.lrsYHOO);
cbEURUSD = (CheckBox) findViewById(R.id.cbEURUSD);
lrcuUSDRUB = (CheckBox) findViewById(R.id.lrcuUSDRUB);
lrcoSILVER = (CheckBox) findViewById(R.id.lrcoSILVER);
lrcoGOLD = (CheckBox) findViewById(R.id.lrcoGOLD);
lrcuGBPUSD = (CheckBox) findViewById(R.id.lrcuGBPUSD);
lriNSDQ = (CheckBox) findViewById(R.id.lriNSDQ);
lriSP500 = (CheckBox) findViewById(R.id.lriSP500);
bSave = (Button) findViewById(R.id.bSave);
ImageButton bSettingsBlack = (ImageButton) findViewById(R.id.ibSettingsBlack);
ImageButton bEconCalBlack = (ImageButton) findViewById(R.id.ibEconCalBlack);
ImageButton bLiveRates = (ImageButton) findViewById(R.id.ibLiveRates);
ImageButton bNewsBlack = (ImageButton) findViewById(R.id.ibNewsBlack);
bSettingsBlack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(AddProfileActivity.this, SettingsActivity.class));
}
});
bEconCalBlack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(AddProfileActivity.this, WebViewActivity.class));
}
});
bLiveRates.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(AddProfileActivity.this, sortingrates.class));
}
});
bNewsBlack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(AddProfileActivity.this, HomePageNews.class));
}
});
bSave.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
bSave();
}
});
}
private void bSave(){
String name = etName.getText().toString().trim();
String career = etCareer.getText().toString().trim();
Boolean techCB = cbTech.isChecked();
Boolean mediCB = cbMedi.isChecked();
Boolean renewableEnergyNF = nfRenewableEnergy.isChecked();
Boolean googleNF = nfGoogle.isChecked();
Boolean novartisNF = cfNovartis.isChecked();
Boolean teslaNF = nfTesla.isChecked();
Boolean fbLRS = lrsFB.isChecked();
Boolean applLRS = lrsAAPL.isChecked();
Boolean yhooLRS = lrsYHOO.isChecked();
Boolean eurusdCB = cbEURUSD.isChecked();
Boolean usdrubCU = lrcuUSDRUB.isChecked();
Boolean silverCO = lrcoSILVER.isChecked();
Boolean goldCO = lrcoGOLD.isChecked();
Boolean gbpusdCU = lrcuGBPUSD.isChecked();
Boolean nsdqI = lriNSDQ.isChecked();
Boolean sp500I = lriSP500.isChecked();
if(!TextUtils.isEmpty(name)){
String id = databaseClients.push().getKey();
Clients clients = new Clients(id, name, career, techCB, mediCB, renewableEnergyNF, googleNF,
novartisNF, teslaNF, fbLRS, applLRS, yhooLRS, eurusdCB, usdrubCU, silverCO,
goldCO, gbpusdCU, nsdqI, sp500I );
databaseClients.child(id).setValue(clients);
Toast.makeText(this, "Client added", Toast.LENGTH_LONG).show();
startActivity(new Intent(AddProfileActivity.this, AddProfileActivity.class));
}else{
Toast.makeText(this, "Please enter your name", Toast.LENGTH_LONG).show();
}
}
}
EDIT (What I've tried):
I tired with other buttons as an experiment to change the image as I couldn't figure out to straight up create it.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main17);
ImageView view = (ImageView) findViewById(R.id.imageView3);
view.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
ImageView imageView = (ImageView) view;
assert(R.id.imageView3 == imageView.getId());
Integer integer = (Integer) imageView.getTag();
integer = integer == null ? 0 : integer;
switch(integer) {
case R.drawable.circlesambergrey2:
imageView.setImageResource(R.drawable.bell);
imageView.setTag(R.drawable.bell);
break;
case R.drawable.bell:
default:
imageView.setImageResource(R.drawable.circlesambergrey2);
imageView.setTag(R.drawable.circlesambergrey2);
break;}
}
});
}
I've also thought that the user could select the image button with the correct letter from a spinner. This is what I am currently trying! Cheers.
I am giving one button in all the row,if we click that button dialog box has to appear in that one it has to display the content according to the row.
public void bindView(View view, Context context, final Cursor cursor){
int row_id = cursor.getColumnIndex("_id"); //Your row id (might need to replace)
TextView tv = (TextView) view.findViewById(R.id.text2);
final TextView tv1 = (TextView) view.findViewById(R.id.text1);
TextView tv2 = (TextView) view.findViewById(R.id.date);
CheckBox cb = (CheckBox) view.findViewById(R.id.checkbox);
int col1 = cursor.getColumnIndex("expname");
final String expname = cursor.getString(col1 );
int col2 = cursor.getColumnIndex("expcontent");
final String expcontent = cursor.getString(col2 );
int col3 = cursor.getColumnIndex("expdate");
final String expdate = cursor.getString(col3);
cb.setTag(cursor.getPosition());
cb.setChecked(mCheckStates.get(cursor.getPosition(), false));
cb.setOnCheckedChangeListener(this);
// TextView tv2 = (TextView) view.findViewById(R.id.text3);
//cursor.getColumnName(1)
tv.setText( expname);
tv1.setText( expcontent);
tv2.setText(expdate);
//tv2.setText( ""+cursor.getColumnIndex(GinfyDbAdapter.CATEGORY_COLUMN_COUNT));
// String[] from = new String[]{GinfyDbAdapter.CATEGORY_COLUMN_TITLE, GinfyDbAdapter.CATEGORY_COLUMN_CONTENT, GinfyDbAdapter.CATEGORY_COLUMN_COUNT}
ImageButton button = (ImageButton) view.findViewById(R.id.sms);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
StringBuffer sb2 = new StringBuffer();
sb2.append("Title:");
sb2.append(Html.fromHtml(expname));
sb2.append(",Content:");
sb2.append(Html.fromHtml(expcontent));
sb2.append("\n");
String strContactList1 = (sb2.toString().trim());
sendsmsdata(strContactList1);
}
});
ImageButton button1 = (ImageButton) view.findViewById(R.id.mail);
button1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
StringBuffer sb3 = new StringBuffer();
sb3.append("Title:");
sb3.append(Html.fromHtml(expname));
sb3.append(",Content:");
sb3.append(Html.fromHtml(expcontent));
sb3.append("\n");
String strContactList2 = (sb3.toString().trim());
sendmaildata(strContactList2);
}
});
ImageButton button2 = (ImageButton) view.findViewById(R.id.btnaudioprayer);
button2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
//ADD STUFF HERE you know which row is clicked. and which button
typed = expcontent;
speakOut();
}
});
ImageButton button3 = (ImageButton) view.findViewById(R.id.bBlame);
button3.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
//ADD STUFF HERE you know which row is clicked. and which button
dialog.show();
}
});
dialog=new Dialog(ExperiencesActivity.this);
dialog.setContentView(R.layout.exp1);
dialog.setCancelable(true);
dialog.setTitle("My Experiences");
textview1 = (TextView)dialog.findViewById(R.id.tv11);
textview1.setText("Title:" + expname);
textview2 = (TextView)dialog.findViewById(R.id.tv22);
textview2.setText("Content:" + expcontent);
Button button23=(Button)dialog.findViewById(R.id.btnSubmit);
button23.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
But here my problem is it will not take the respective content in dialog box,whenever we click the first item in an button,it will shows the content in all the row of button click. Dialog box content has to change according to the row.
You need to create saperate classe for your textviews. Like
private class ViewHolder {
TextView textview1;
TextView textview2;
Button button23;
}
Update your dialog like...
public void showMyDialog(){
dialog=new Dialog(ExperiencesActivity.this);
ViewHolder viewholder;
dialog.setContentView(R.layout.exp1);
dialog.setCancelable(true);
dialog.setTitle("My Experiences");
ViewHolder.textview1 = (TextView)dialog.findViewById(R.id.tv11);
ViewHolder.textview1.setText("Title:" + expname);
ViewHolder.textview2 = (TextView)dialog.findViewById(R.id.tv22);
ViewHolder.textview2.setText("Content:" + expcontent);
ViewHolder.button23=(Button)dialog.findViewById(R.id.btnSubmit);
ViewHolder.button23.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
And call showMyDialog() method onclick of button.
ImageButton button3 = (ImageButton) view.findViewById(R.id.bBlame);
button3.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
//ADD STUFF HERE you know which row is clicked. and which button
showMyDialog();
}
});