Dialog box content doesnot change according to that row - java

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();
}
});

Related

App crashes at the second start, wont keep the key in memory

public String SavedPassword;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log_in);
Button btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Random rnd = new Random();
int n = 1000 + rnd.nextInt(9000);
String str = "";
str = Integer.toString(n);
SavedPassword = str;
String pass = str.toString();
TextView tv1 = (TextView) findViewById(R.id.tv1);
tv1.setText(pass);
Button btn2 = (Button) findViewById(R.id.btn2);
}
});
Button btn2 = (Button) findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String pass = SavedPassword;
TextView tv2 = (TextView) findViewById(R.id.tv2);
EditText edt = (EditText) findViewById(R.id.edt);
String passc = edt.getText().toString();
if (pass.equals(passc)) {
Intent intent = new Intent(getApplicationContext(), Photos.class);
startActivity(intent);
} else {
tv2.setText("TRY AGAIN!");
}
}
});
}
So, I want the first button btn1 to make a key, and store it, after that I want the button to be disabled , button 2 (btn2) should be the LogIn one, and it should compare if the key is equal with the password you enter in EditText, if so, should send you to Photos.class, does that the first time when you run the app, (without the disable part because I dont know how to implement it yet) but the second time I open the app and put the "key" as password it will crash the app, I doesnt store the key in memory...
How can I fix this without using a database?
Here is a very basic guide you can follow: https://www.journaldev.com/9412/android-shared-preferences-example-tutorial
I added some code below that should help you I think. You'll have to double check if the syntax is all good, I didn't run it anywhere.
Basically I have it checking shared preferences first, to see if you have any passwords saved. If you don't, I disable the second button. If you do, I enable the second button. Also SavedPassword gets set to the value.
public String SavedPassword;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log_in);
Button btn1 = (Button) findViewById(R.id.btn1);
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
SavedPassword = pref.getString("stored_password", null); // getting String
if(SavedPassword == null) {
btn2.setEnabled(false);
} else {
bt2.setEnabled(true);
}
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Random rnd = new Random();
int n = 1000 + rnd.nextInt(9000);
String str = "";
str = Integer.toString(n);
SavedPassword = str;
editor.putString("stored_password", SavedPassword).commit(); // Storing string
String pass = str.toString();
TextView tv1 = (TextView) findViewById(R.id.tv1);
tv1.setText(pass);
Button btn2 = (Button) findViewById(R.id.btn2);
}
});
Button btn2 = (Button) findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String pass = SavedPassword;
TextView tv2 = (TextView) findViewById(R.id.tv2);
EditText edt = (EditText) findViewById(R.id.edt);
String passc = edt.getText().toString();
if (pass.equals(passc)) {
Intent intent = new Intent(getApplicationContext(), Photos.class);
startActivity(intent);
} else {
tv2.setText("TRY AGAIN!");
}
}
});
}

Create ImageButton in seperate activity when Save button is clicked?

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.

How to change a text on the click of a button in android?

I need to set text of a button by taking value from edit text ,when button is clicked it has to change its text to the text specified in Edittext.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn=(Button)findViewById(R.id.button);
assert btn != null;
btn.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v)
{
TextView txt=(TextView)findViewById(R.id.editText);
btn.setText((CharSequence) txt);
}
}
);
}
you just need define EditText variable instead of TextView and call getText method of editText and then toString method like this:
final Button btn=(Button)findViewById(R.id.button);
assert btn != null;
btn.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v)
{
EditText txt = (EditText)findViewById(R.id.editText);
btn.setText(txt.getText().toString());
}
}
);
Declare your views globally
Button btn;
EditText txt;
set your id in onCreate method
btn=(Button)findViewById(R.id.button);
txt=(EditText)findViewById(R.id.editText);
keep your onclicklistener as follow
btn.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v)
{
final String newText=txt.getText().toString().trim();
if(newText.length()>0){
btn.setText(newText);
}
}
}
);
final Button btn = (Button)findViewById(R.id.button);
if (btn != null) {
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText et = (EditText)findViewById(R.id.editText);
if (et != null){
Button btn = (Button)view;
btn.setText(et.getText());
}
}
});
}

How do I remove an imageButton after it is clicked?

View.OnClickListener imgButtonHandler3 = new View.OnClickListener() {
public void onClick(View v) {
ImageButton btn = (ImageButton) findViewById(R.id.imageButton2);
btn.setVisibility(View.GONE);
}
};
I want the imageButton to disappear or delete after the image view is clicked. I am making this app on Android Studios and when it runs and I click the imageButton the view does not become hidden.
Thank you to all who are willing to help!!!
You are creating the onClick handler but maybe you are not setting it:
ImageButton btn = (ImageButton) findViewById(R.id.imageButton2);
View.OnClickListener imgButtonHandler3 = new View.OnClickListener() {
public void onClick(View v) {
btn.setVisibility(View.GONE);
}
};
btn.setOnClickListener(imgButtonHandler3 ); // this sets the handler
We can disable buttons in android. Try the below code.
View.OnClickListener imgButtonHandler3 = new View.OnClickListener() {
public void onClick(View v) {
ImageButton btn = (ImageButton) findViewById(R.id.imageButton2);
btn.setEnabled(false);
}
};
Use btn.setVisibility(View.INVISIBLE) instead of View.Gone
View.OnClickListener imgButtonHandler3 = new View.OnClickListener() {
public void onClick(View v) {
ImageButton btn = (ImageButton) findViewById(R.id.imageButton2);
btn.setVisibility(View.INVISIBLE);
}
};

Issues with stopping MediaPlayer

i have a one layout and 3 buttons and textView , when i cick a button it change the textView , button3 i have a song to play and textView show lyrics, and my problem here is how to make the MediaPlayer stopping when click any other button
here is my code i used :
public class Langue extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.langue);
final String St1 ="My topic1"
Button But1 = (Button)findViewById(R.id.button1);
But1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St1);
// code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
}
});
final String St2="My Topic2"
Button But2 = (Button)findViewById(R.id.button2);
But2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St2);
// code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
}
});
final String St3="Lyrics of song iles.mp3"
Button But3 = (Button)findViewById(R.id.button3);
But3.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St3);
//code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
//play song
iles= MediaPlayer.create(Langue.this, R.raw.iles);
iles.start();
}
});
public class Langue extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.langue);
final MediaPlayer iles= MediaPlayer.create(Langue.this, R.raw.iles);//creating iles here
final String St1 ="My topic1"
Button But1 = (Button)findViewById(R.id.button1);
But1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St1);
// code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
iles.stop(); // in here the song will stop
}
});
final String St2="My Topic2"
Button But2 = (Button)findViewById(R.id.button2);
But2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St2);
// code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
iles.stop(); // in here the song will stop
}
});
final String St3="Lyrics of song iles.mp3"
Button But3 = (Button)findViewById(R.id.button3);
But3.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St3);
//code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
//play song
iles.start();
}
});
I believe this will do., in the above code i created a iles as MediaPlayer object and used it to stop the song...

Categories

Resources