Refreshing Layout with EditText and Buttons - android

I have made a form and connected it to a firebase database. It has an EditText in which you write something(e.x a suggestion) and then you press the button to submit it in the firebase database. But how can I refresh the whole xml file so I can write a new suggestion. I have tried this at first
setContentView(R.layout.activity_suggestions);
But after that, the xml file was refreshed but I couldn't submit a new suggestion.
and then I tried this which works but its really annoying because if I press the back button it gets me back to the same page
Intent reportBugIntent = new Intent(ReportBug.this, ReportBug.class);
startActivity(reportBugIntent);
Is the any other way I can do that?

You just need to clear editText content on submit button click.
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Save content to firebase database
// clear the editext
editText.setText("");// where edit text is your suggestion edit text
}
}

There are! How about refreshing Activity: Reload activity in Android
The current code is useless because it will just return you to the current page, instead, you may wanna finish the current Activity by finish() method then doing such thing.
So:
finish();
startActivity(getIntent());
Or:
// Refresh main activity upon close of dialog box
Intent refresh = new Intent(this, clsMainUIActivity.class);
startActivity(refresh);
this.finish(); //
As mentioned on the above link.

Related

Using the same activity for different buttons but different action using xml resource

I am doing a project in Android Studio. I will have 2/3 activities at most. On main activity I will have some buttons. There will be a second activity. So for each button that second activity will pop up and show some more buttons or imageButton but they will be different. The buttons on the second activity will also be meant for different actions but all of it will be on a third activity. The third activity will have some images and a text view. So image view and text view will show different data from the "XML resource" for different buttons. Any ideas how I can do it?I am using Android Studio.
Can it be done using base Adapter?
Also when all the data are stored on sql database, what different thing do I need to do?
Thank You!
When you go from main activity to the second activity, you must be using intent.
You can add extra info in your intent to to modify accordingly using putExtra()
Then in your second activity, you can simply retrieve the extra info and make changes accordingly.
in main activity,
//your first button
Button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent newintent= new Intent(MainActivity.this, SecondActivity.class);
newintent.putExtra("details", "load first set of images");
startActivity(newintent);
}
});
//your second button
Button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent newintent= new Intent(MainActivity.this, SecondActivity.class);
newintent.putExtra("details", "load second set of images");
startActivity(newintent);
}
});
in second activity,
String extrainfo=getIntent().getStringExtra("details");
if(extrainfo.equals("load first set of images"))
{
//make your changes accordingly
}
else if(extrainfo.equals("load second set of images"))
{
//make your changes accordingly
}
else
{
//make according changes
}
This is how you can achieve it.
Hope this helps!

How to pass the value of a pressed button

I was wondering how to pass the value of a button to another activity. I have a screen with 7 buttons, and each button shows different data from the SQLitedatabase. So I was wondering how I can implement that in the activity so that I can select different data from the database, dependable which button was clicked in the previous activity. I know I have to use Bundles and Intents, but I cannot find how I can implement that so that the Activity knows which button was clicked and which data to select. Thanks!
How to pass the value of a pressed button
Buttons don't really have a "value". You could use the button's ID, though:
public void onClick(View v) {
startActivity(new Intent(this, YourOtherActivity.class)
.putExtra(YourOtherActivity.EXTRA_SOME_KEY, v.getId()));
}
This will pass the widget ID of the Button that was clicked in an extra (named YourOtherActivity.EXTRA_SOME_KEY) to YourOtherActivity. YourOtherActivity, in onCreate() or elsewhere, could call getIntent().getIntExtra(EXTRA_SOME_KEY, -1)) to retrieve the widget ID of the button that was clicked, then use a switch statement or some such to route your behavior as needed.
Set it with
Intent myIntent = new Intent(this, ACTIVITY.class);
myIntent.putExtra("buttonValue", BUTTON VALUE);
startActivity(myIntent);
Get it with
getIntent().getStringExtra("buttonValue")
Button Text
String buttonText = button.getText().toString()

Change Value of TextView, ImageView, ImageButton when i press button in other Activity

I am developing one simple app but i have problem.
The problem I have is that two activities, Activity1, have 26 button A-Z and Activity2 have textView,ImageView. I don't know how to set the button with default value.
Question
How can I change the value of textView, ImageView, and button in Activity2 whenever I press any button in Activity1?
Question
if i click buttonA how can i change texView display from "TextView" to "A" and imageView to other Image can u guys help me?
In simple way you can pass the value within intent like
Intent Activity2Intent = new Intent(this, Activity2.class);
Activity2Intent.putExtra("BUTTON_TEXT", yourText);
Activity2Intent.putExtra("IMAGE_VIEW", yourText);
startActivity(Activity2Intent);
Else if the data is needed later you can store that in SqliteDatabase
UPDATE
You should also need to extract the values passed from the bundle on Activity2.
You can use SharedPreferences or Intent for passing the value from Activity A and retrive the value in next Activity .
In Activity A:::
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Do something and save the value and pass it using intent
// pass the button name or use boolean is_state= true;
}
});
In Activity B:::
By using getIntent() retrive the button name and change theTextViewandImageView` as per your requirement

Android - Save text and launching an Activity with the same button

I'm currently working in an Activity which saves text from three EditViews and constructs a SQL query. After that, the query is given to another activity to search and display the results.
Right now I've got two buttons, one to save the query, inside an onClickListener and another button to start the second activity:
searchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//First the data from the editviews is saved
EditText searchName = (EditText) findViewById(com.pedro.books.R.id.search_name);
search_name = searchName.getText().toString();
searchName.setText("");
EditText searchAut = (EditText) findViewById(com.pedro.books.R.id.search_aut);
search_aut = searchAut.getText().toString();
searchAut.setText("");
EditText searchYea = (EditText) findViewById(com.pedro.books.R.id.search_yea);
search_yea = searchYea.getText().toString();
searchYea.setText("");
//here i construct the query
});
And with an onClick in the xml code, I start the activity with another button:
public void ReadResults(View view){
//The query is given to the ReadActivity to display the results
Intent intent = new Intent(this, ReadActivity.class);
intent.putExtra(ReadActivity.EXTRA_QUERY, query);
startActivity(intent);
}
I've tried with the same button for both without changing anything and it obviously doesn't work, and I've also tried to start the activity inside the onClickListener, but I got this error: "The constructor Intent(new View.OnClickListener(){}, Class) is undefined"
Is there a way to start the activity inside the onClickListener or to stop the second activity to start until the query is saved?
Thanks in advance!!
if you are putting the intent in the onClick of a button you cannot use this you need to use YourActivity.this to properly get context.
this in your button is your OnClickListener like the error says
Where/How do you call your ReadResults method? On a side note - does it need to be public? If you call it inside the clickListener handler then that's wrong.
1) Extract your code out of the clickListener handler and have it into a private method within your activity class.
2) Your clickListener should only call that private method.
3) You could have your 3 editboxes as memebers of your activity and instantiate them onCreate() instead of getting them each time you click the button, it's expensive parsing the UI if not necessary.

how to move data between 2 activities

i have a first screen in my app that the user enter his name in an editText.Then, when the user presses the button "ok",the app is going to a new activity.I would like to get the text from the first activity and move it to the second.For example,if the user fills the edittext with the name "kostas",when he goes to the second activity,to appear a textView writing "Hello kostas"..
i have tried to use putExtra, but i m thinking that i m doing it in a wrong way.In the first class i m using this
Button ok = (Button) findViewById(R.id.ok);
ok.setOnClickListener(new View.OnClickListener() {
public void onClick (View view) {
Intent newActivity = new Intent(view.getContext(),home.class);
newActivity.putExtra("NAME", name);
startActivity(newActivity);
}
});
in order to move the name into my next activity "home".but then i dont know how to get it there...
and then in my new "home" activity i m using this:
Bundle extras = getIntent().getExtras();
String Name = extras.getString("NAME");
First, in your onClick Handler I would extract the text from the editText box and place the text into the intent.
Second, to debug the problem I would enable LogCat viewing
Third, I would log the actual values being passed (in Act 1) and extracted (
in Act 2) using a call such as:
Log.d(TAG,name);
Hope that helps,
JAL

Categories

Resources