Android: Text not being applied to Card - android

So I'm trying to create an app where the user will go enter some text in two edittext fields and when a submit button is pressed, it will close the edit window and then add a new card to the list on the main activity. I've set up all my code and everything works without error. However, no cards are added and when the application starts, there's one card which has no text anywhere.
My Code:
MainActivity.java:
//The following lines of code are in onCreate() and setContentView() has been
//called
//Get the intent that launched the activity
Intent i = getIntent();
//Get the strings from the intent
String sig = i.getStringExtra("signature");
String lMessage = i.getStringExtra("long_message");
//Init mCardView
mCardView = (CardUI) findViewById(R.id.cardsview);
//Allow swipe to delete for cards
mCardView.setSwipeable(true);
//Add new card with the two extra strings from the edit text fields
mCardView.addCard(new MyCard(sig, lMessage));
//Draw the cards
mCardView.refresh();
AddText.java
//The following lines of code are in onCreate() and setContentView() has been
//called
//Find edit text's and button
EditText name = (EditText) findViewById(R.id.enter_name);
EditText message = (EditText) findViewById(R.id.enter_message);
button = (Button) findViewById(R.id.add_signature);
//Get the string from the edit text...whatever it may be.
final String signature = name.getText().toString();
final String longMessage = message.getText().toString();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Make a new intent to start main activity
Intent intent = new Intent(getApplication(), YearbookMain.class);
//Put extras into intent
intent.putExtra("signature", signature);
intent.putExtra("long_message", longMessage);
//Start intent
startActivity(intent);
}
});
Again, I'm not getting any crashes it's just that the card is not being edited. I've tried everything I can think of so I'm hoping someone can think of something else!
Thanks

Related

setText from a class for another

I am really new to android. And my English speaking is not good. So,I was going to get a username using an EditText and set it to a TextView But the problem is that the EditText is in the first class (MyActivity),and the TextView is in the second class(MyAcyivity2).
I did everything such as FindViewById and....
but when I set on a click listener :
Textview1.setText(EditText1.getText())
and open the app, by clicking on button it says: Unfortunately app has stopped.
what to do?
In MyActivity :
EditText et = (EditText) findViewById(R.id.editText1);
On button click, inside onClickListener :
void onClick(View view) {
Intent intent = new Intent(MyActivity.this, MyActivity2.class);
intent.putExtra("myString", et.getText().toString());
startActivity(intent);
}
In MyActivity2 inside onCreate()
String myString = getIntent().getStringExtra("myString");
TextView tv = findViewById(R.id.textView1);
tv.setText(myString);

How to save add button dynamically in android?

The code should add a button dynamically when the user needs it.
When an users click on addService this code takes (a name for button and a value to use in the intent for this button) from a second Activity, then adds the button dynamically in this Activity with the name and intent and the user can click it for service.
How to save the button dynamically added by the user?
Button btn = new Button(MainActivity.this);
btn.setText(buttonName);
layout.addView(btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String phone = serviceNum;
Intent e = new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null));
startActivity(e);
}
});
Include the save button in your layout, then hide it before the view loads. When the user performs the action, simply unhide the button. The layout should handle it automatically for you.

How to display inputted text in a TextView after clicking a button in Android studio?

So I am experimenting with Android Studio. I am stuck in the part where I want my "Submit" button to display the user inputted text underneath the page.
Button submit is the button.
"name" and "email" are the EditText variables a.k.a text fields
My TextView variable is outputText
So after the user enters random stuff in both text fields and click submit, I want my outputText to display both texts that were inputted in 2 separate lines
ex:
John Smith
john.smith#gmail.com
The code below is my .java code for the activity. I need to know how to initialize the new intent in a way that lets me do the 4 lines below it. Thanks.
public void buttonOnClick(View v){
Button submit = (Button) v;
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i =
new Intent(...?...);
startActivity(i);
}
});
editName = (EditText) findViewById(R.id.name);
editEmail = (EditText) findViewById(R.id.email);
textout = (TextView) findViewById(R.id.textView);
textout.setText(editName.getText()+" "+editEmail.getText());
}
I want my outputText to display both texts that were inputted in 2
separate lines
Use <br> and Html.fromHtml between text to add new line:
String strText=(String)editName.getText()+"<br>"+(String)editEmail.getText();
textout.setText(Html.fromHtml(strText));
how to initialize the new intent in a way that lets me do the 4 lines
below it...
Intent i =new Intent(CurrentActivityName.this,TargetActivityName.class);
startActivity(i);
For print in next line
textout.setText(editName.getText()+"\n"+editEmail.getText());
For intent
Intent i =new Intent(FirstActivity.this,SecondActivity.class);
startActivity(i);
Ok I was able to accomplish what I wanted without an Intent initialized. Here's the code:
public void buttonOnClick(){
Button submit = (Button) findViewById(R.id.submit);
editName = (EditText) findViewById(R.id.name);
editEmail = (EditText) findViewById(R.id.email);
textout = (TextView) findViewById(R.id.outputText);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textout.setText(editName.getText()+"\n"+editEmail.getText());
}
});
}

How to create another button in android dynamically

As the title states, I am looking to find out how to create a button dynamically when another button in another activity is pressed. This is being done with the Android SDK.
Basically, I have two activities, MainActivity and SecondaryActivity, within the SecondaryActivity you enter some information, title, text, id, so on and so forth. When you click the "Save" button it sends some, information to MainActivity(not the issue). As well as sending the information, I need to create an entirely new button within the MainActivity.
Any suggestions on how this should be accomplished?
Thanks.
Edit 1
public void CreateNewButton(View view)
{
LinearLayout lineLayout = (LinearLayout)findViewById(R.id.linear_layout);
TextView newTextView = new TextView(this);
int id = rand.nextInt(100);
int newId;
newTextView.setVisibility(View.VISIBLE);
newTextView.setId( R.id.new_button + id );
newTextView.setText("New Item");
newTextView.setTextSize(35);
newTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
intent = new Intent(getBaseContext(), SecondActivity.class);
startActivity(intent);
}
});
lineLayout.addView(newTextView);
}
This code generates the new TextView( Decided to change it up ) but now the issue I have, is newTextView.setText(); needs to get the text from the other activity
newTextView.setText(i.getData().toString());
putting this in the CreateNewButton(View view) methods causes an error since technically there is no data in the field that it is trying to grab from.
The problem at hand is I need to create the new TextView field WITH the name of the new account that has yet to be created. If that makes any sense.
I'm going to assume you want to add this button to a LinearLayout:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout1);
Button button = new Button(this);
button.setText("I'm a button!");
// add whatever other attributes you want to the button
linearLayout.addView(button);

How can I display values from EditText user input?

I have a SharedPreferences that saves EditText input from one activity and displays the String value in another activity.
When I enter an input into the EditText fields and press (a button I created) "Save" (which commits the EditText to editor), the file is stored successfully. However, the display activity (which displays the stored String values in SharedPreferences) doesn't display the values. I am guessing it is because it is the onCreate, so the screen is "created" when it runs. How can I get it to update the EditText values when the user commits (presses save) immediately?
CustomStoreEditActivity - Just storing the user inputs (EditText entries):
final Button saveButton = (Button) findViewById(R.id.saveButton);
saveButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if (saveButton.isClickable()) {
SharedPreferences prefs = getSharedPreferences(
"customtime", 0);
// prefs.registerOnSharedPreferenceChangeListener(this);
final SharedPreferences.Editor edit = prefs.edit();
EditText shopName = (EditText) findViewById(R.id.shopName);
EditText open1 = (EditText) findViewById(R.id.open1);
EditText close1 = (EditText) findViewById(R.id.close1);
EditText open2 = (EditText) findViewById(R.id.open2);
EditText close2 = (EditText) findViewById(R.id.close2);
EditText open3 = (EditText) findViewById(R.id.open3);
EditText close3 = (EditText) findViewById(R.id.close3);
EditText open4 = (EditText) findViewById(R.id.open4);
EditText close4 = (EditText) findViewById(R.id.close4);
EditText open5 = (EditText) findViewById(R.id.open5);
EditText close5 = (EditText) findViewById(R.id.close5);
EditText open6 = (EditText) findViewById(R.id.open6);
EditText close6 = (EditText) findViewById(R.id.close6);
EditText open7 = (EditText) findViewById(R.id.open7);
EditText close7 = (EditText) findViewById(R.id.close7);
EditText comments = (EditText) findViewById(R.id.comments);
edit.putString("shopName", shopName.getText().toString());
edit.putString("Monday1", open1.getText().toString());
edit.putString("Monday2", close1.getText().toString());
edit.putString("Tuesday1", open2.getText().toString());
edit.putString("Tuesday2", close2.getText().toString());
edit.putString("Wednesday1", open3.getText().toString());
edit.putString("Wednesday2", close3.getText().toString());
edit.putString("Thursday1", open4.getText().toString());
edit.putString("Thursday2", close4.getText().toString());
edit.putString("Friday1", open5.getText().toString());
edit.putString("Friday2", close5.getText().toString());
edit.putString("Saturday1", open6.getText().toString());
edit.putString("Saturday2", close6.getText().toString());
edit.putString("Sunday1", open7.getText().toString());
edit.putString("Sunday2", close7.getText().toString());
edit.putString("comments", comments.getText().toString());
edit.commit();
Intent myIntent = new Intent(getBaseContext(),
CustomStoreActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(myIntent);
Toast.makeText(getBaseContext(), "Opening Hours Saved!",
Toast.LENGTH_SHORT).show();
}
}
});
}
CustomStoreActivity - where I believe the problem lies:
private void displayPreferences(){
SharedPreferences prefs = getSharedPreferences("customtime", 0);
String shopName = prefs.getString("shopName", "Empty");
String shopTime1 = prefs.getString("Monday1", " ");
String shopTime2 = prefs.getString("Monday2", " ");
String shopComments = prefs.getString("comments", "");
TextView displayPrefs = (TextView) findViewById(R.id.displayPrefs);
displayPrefs.setText(shopName + shopTime1 + shopTime2 + shopComments);
}
Thank you for your ample time.
I think part of what you're looking for is to put your CustomStoreActivity code in onResume instead of onCreate. Whatever code you move into onResume will occur whenever the CustomStoreActivity is brought to the front (including when it is first created).
Another alternative, assuming that CustomStoreActivity is used to launch the Activity that contains the EditTexts, is to use startActivityForResult and pass the data back in the result Intent instead of using the SharedPreferences. Here is a simple example (though note that the setResult call in this example is passed null where you would want to pass in an Intent, as documented here in the Android docs).
It also seems like you're trying to use your second Activity like a dialog box with editable fields. If that's the case, yet another alternative is to actually use a variant of the Dialog class within your CustomStoreActivity class instead of creating another Activity to act like one. See the Android doc for dialogs.
I'm not sure I really understand your question but are you trying to update the TextView of a different Activity from the one you are in? In which case I don't think this can be done and you should use Intents to pass the data to the activity

Categories

Resources