Android displaying pop-up message or animation on button click - android

I am really new to android programming, and I'm stuck with a really small problem in my app. The (very simple) app is almost ready, its just that there is a button and when it is clicked, I want the user to get a response in the form of either a message or some animation so that the user can feel that the app has registered the button click. I've been looking up and trying stackoverflow and other tutorials but to no avail. I just want some kind of a response when the button is clicked, so that the user doesn't feel confused about whether the app is working or not. Any help will be greatly appreciated!! Here's the code below:-
The XML file:-
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MyActivity"
android:background="#d1000000">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:id="#+id/Hinglishbutton"
android:background="#cfa16cff"
android:hapticFeedbackEnabled="true"
android:onClick="buttonHandler"
android:clickable="true"
android:textColor="#a5fafbf9"
tools:ignore="HardcodedText"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="71dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Welcome to Hinglish :)"
android:id="#+id/textView4"
android:textColor="#a5fafbf9"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hinglish lets you add Hindi words to your predictive dictionary thus enabling a richer typing experience!"
android:id="#+id/textView"
android:textColor="#a5fafbf9"
android:textIsSelectable="false"
android:textSize="14sp"
android:typeface="sans"
android:layout_marginTop="80dp"
android:layout_below="#+id/textView4"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click the button once, minimize Hinglish and go grab a coffee. You will now be able to type hindi words without autocorrect converting them to junk english!"
android:id="#+id/textView2"
android:textColor="#a5fafbf9"
android:textIsSelectable="false"
android:textSize="14sp"
android:typeface="sans"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Hosting this app costs money! To pitch in, contact the developer at spandan.madan#gmail.com :) :)"
android:id="#+id/textView5"
android:textColor="#a5fafbf9"
android:layout_below="#+id/textView2"
android:layout_alignRight="#+id/textView2"
android:layout_alignEnd="#+id/textView2"
android:layout_marginTop="43dp" />
</RelativeLayout>
The myActivity.java file:-
package com.example.spandanmadan1.hinglish;
import android.content.res.AssetManager;
import android.provider.UserDictionary;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MyActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Button bsubmit = (Button) findViewById(R.id.Hinglishbutton);
bsubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Thank you for using Hinglish", Toast.LENGTH_LONG).show();
InputStream fis = getResources().openRawResource(R.raw.hindislang);
BufferedReader bfr = null;
try {
bfr = new BufferedReader(new InputStreamReader(fis));
String line = null;
while ((line = bfr.readLine()) != null) {
UserDictionary.Words.addWord(getApplicationContext(), line, 1, "", null);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_my, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

I created a new project, used your xml and in the buttonHandler function I gave the toast as follows:
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MyActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
public void buttonHandler(View view) {
Toast.makeText(getApplicationContext(), "Thank you for using Hinglish", Toast.LENGTH_LONG).show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_my, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
This works. The Toast : "Thank you for using Hinglish" was displayed on screen.

You can use a Toast:
Toast.makeText(getApplicationContext(),"<Your message to the User>",Toast.LENGTH_SHORT).show();
The message will be shown when you click the button.

Related

Popup Dialog with Android ActionBar when ActionBar Menu Item is Clicked

I'm building an app where I need an information activity with another Android Actionbar to popup when the info ActionBar Item is clicked on.
Here is the screenshot of the activity_main_menu.xml:
activity_main_menu.xml code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/menu_item_info"
android:icon="#drawable/info_outline"
android:title="#string/information"
android:titleCondensed="#string/info"
app:showAsAction="ifRoom" />
<item
android:id="#+id/menu_item_share"
android:title="#string/share"
app:showAsAction="ifRoom"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
</menu>
When the "menu_item_info" is clicked, here is what I want to happen:
Desired Result
activity_main.xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
android:orientation="horizontal"
tools:context=".MainActivity">
<!-- <clip xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clipOrientation="vertical"
android:drawable="#drawable/bitcoin"
android:gravity="top" /> -->
<TextView
android:id="#+id/tvTotal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:padding="10dp"
android:text="Text"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold"
app:fontFamily="sans-serif"
app:lineHeight="60dp" />
<ImageView
android:id="#+id/ivBitcoin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:contentDescription="You own text!"
android:padding="10dp"
app:srcCompat="#drawable/bitcoin" />
</LinearLayout>
MainActivity.java code:
package com.shikhar_mainalee.iownallbitcoin;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.ShareActionProvider;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONObject;
import java.util.Collection;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private TextView tvTotal;
private ImageView ivBitcoin;
private ShareActionProvider mShareActionProvider;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvTotal = findViewById(R.id.tvTotal);
ivBitcoin = findViewById(R.id.ivBitcoin);
Main.ivBitcoin = ivBitcoin;
tvTotal.setText("You Own\n21,000,000 / 21,000,000\nBTC!");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.activity_main_menu, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
ShareActionProvider shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
shareActionProvider.setShareIntent(createShareIntent());
new ShareActionProvider(this).setShareIntent(null);
// Return true to display menu
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_info:
// What code should go here?
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
}
What is the best way to go about this? Any help is appreciated! Thank you!
If you want to go to another activity here is what you should do:
Create another activity
Create an Intent to go to that activity
Intent myIntent = new Intent(this, SecondActivity.class);
you can call that Intent from anywhere in your main activity. In your case:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_info:
startActivity(myIntent); // switch to second activity
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
to change the second activity action bar menu you should create a new Menu and like your main activity Inflate it in onCreateOptionsMenu
getMenuInflater().inflate(R.menu.activity_second_menu, menu);
try this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
this is a example of a template from android studio.

ArrayAdapter<Button> displays buttons and object reference behind it in ListView

this is my first "project" with android and I'm trying to make a single-activity app that displays a list of buttons using a listview. For some reason my ArrayAdapter displays the button correctly but behind it, it displays the object reference. So when I debug it using my phone, I see a button and right behind it its object reference.
My activity looks like this:
package com.example.madelenko.showcase;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ArrayList<Button> projectList =
new ArrayList<Button>();
Button button1 = new Button(this);
Button button2 = new Button(this);
button1.setText("Spotify Streamer");
button2.setText("Scores App");
projectList.add(button1);
projectList.add(button2);
ArrayAdapter<Button> adapter =
new ArrayAdapter<Button>(this,R.layout.element_listview_layout,
R.id.list_item_string,projectList);
ListView listView = (ListView)findViewById(R.id.buttonListView);
listView.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
My button element layout looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/list_item_string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:paddingLeft="8dp"
android:textSize="18sp"
android:textStyle="bold"
android:text=""/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/projectButton"
android:text="Hello world!"
android:layout_alignTop="#+id/list_item_string"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Finally, my listview looks like this:
<FrameLayout 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="com.example.madelenko.showcase.MainActivityFragment"
tools:showIn="#layout/activity_main">
<ListView
android:layout_width="wrap_content"
android:layout_height="401dp"
android:id="#+id/buttonListView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="My nanodegree apps!"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal|top" />
</FrameLayout>
Please, be patient with this newbie!
ArrayAdapter<Button> is not a recommended pattern. The adapter is supposed to be adapting model objects, whether those are trivial (e.g., ArrayAdapter<String>) or more complex (e.g., ArrayAdapter<Invoice>).
So, in this case:
You are creating an ArrayList<Button>, but those buttons will never be shown on the screen
You are inflating a layout for the rows that contains a TextView and a Button; those Button widgets will be the ones that are shown
You are telling ArrayAdapter to bind the String representation of the Button (from a call to toString() from the ArrayList to the list_item_string TextView in the row, when you create your ArrayAdapter<Button> instance
Also note that it is rather unusual to see buttons in ListView rows, since the user can click on the ListView rows themselves.
If you want to have a ListView with two rows for your two strings, you can do something like this:
public class ListViewDemo extends ListActivity {
private static final String[] items={"Spotify Streamer", "Scores App"};
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
items));
}
}

button hovering not working in android

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- <TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="24dp"
android:text="#string/name" /> -->
<EditText
android:id="#+id/etname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:ems="10" android:inputType="text"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/etname"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp"
android:text="Login" />
</RelativeLayout>
I have a button, I want when I "hover" over the button it shows a hello message with a toast.
I have a button in the layout. I tried to fetch it by findViewById in the FirstActivity. Then I tried using button.setOnHoverListener but it isn't working.
package com.example.datapass;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class FirstActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form);
final EditText name ;
Button loginButton ;
//final Context context = this;
/** Called when the activity is first created. */
//name= (EditText) findViewById(R.id.etname) ;
final TextView tv = (TextView) (findViewById(R.id.textView1) );
loginButton = (Button) findViewById(R.id.button1);
loginButton.setOnHoverListener(new View.OnHoverListener() {
#Override
public boolean onHover(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.d("hover", "Bring yor cursor over the button");
if(event.getAction()==MotionEvent.ACTION_HOVER_ENTER)
{
//tv.setText("hi");
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();
}
return false;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Can anyone explain what's wrong?
It would be worth changing your onHover method to be like below and break once it carries out an individual action:
#Override
public boolean onHover(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_HOVER_ENTER:
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
I would also advise changing this line as this is not standard format/syntax:
final TextView tv = (TextView) (findViewById(R.id.textView1) );
To be this instead:
final TextView tv = (TextView) findViewById(R.id.textView1);
However I think your actual issue is more relating to the support on mobile devices of hovering as none of your code actually seems like it would break this code section. The majority of devices can only recognise hovers whilst using a stylus and not actually just with a finger. Only some of the higher end devices can actually recognise a hover action with just a finger. This might be something to note and may be advisable to use a swipe or a click instead of a hover to display the Toast.

I created an Android application with two activities, but cannot switch back from the second activity to the first

I created an Android application with two activities, but somehow the buttons on the second activity do not seem to work properly. The button on the first activity (a map activity and a button to add infromat which takes users to the second activity with an information entry screen) works fine, neither button on the second activity seems to have any effect... I can't even get an OnClick Toast message to work.
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback , LocationListener {
public void AddMapInfo(View view) {
Intent intent = new Intent(this, InformationInputActivity.class);
LatLng providedLocation = current;
Bundle args = new Bundle();
args.putParcelable("provided_location", providedLocation);
intent.putExtra("bundle", args);
startActivityForResult(intent, 1);
}
public void onActivityResult (int requestCode, int resultCode, Intent data){
Toast.makeText(this, "back with cheese!", Toast.LENGTH_LONG).show();
if (requestCode != 1){
Toast.makeText(this, "Oops... This shouldn't happen", Toast.LENGTH_LONG).show();
}
else{
setContentView(R.layout.activity_maps);
InformationPoint informationPoint = (InformationPoint) data.getSerializableExtra("information_point");
informationPoint.display(mymap);
}
}
}
The second activity brings on the layout for the screen, and it sets the proper options for the EditText field, so I know it gets control... but that's about it. There is no Toast message from any of the methods (I still get the Toasts when the location update info is received on the first activity instead), and neither the radio button or submit buttons seem to work, I'm also not getting the result and control back in the first activity. (Previously, I used to be able to get a Toast on ever on checked option on the radio button, but now that doesn't seem to work either). Trying to send the activity result aborts the application (since I added finish() to the second activity).
/* java */
package com.example.m.googlemapappfirst;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.maps.model.LatLng;
import org.w3c.dom.Text;
public class InformationInputActivity extends ActionBarActivity {
EditText editText;
String content = "";
LatLng providedLocation;
public InfoType infoType = InfoType.NO_INFO;
InformationPoint informationPoint;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information_input);
Intent intent = getIntent();
Bundle bundle = getIntent().getParcelableExtra("bundle");
providedLocation = bundle.getParcelable("provided_location");
String message = String.valueOf(providedLocation);
editText = (EditText)findViewById(R.id.description);
editText.setHorizontallyScrolling(false);
editText.setMaxLines(5);
Toast.makeText(this, "enter cheese info", Toast.LENGTH_LONG);
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
Toast.makeText(this,"you selected your cheese", Toast.LENGTH_LONG);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_information_input, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void AddPictureHandler(View view){
Toast.makeText(this, "i like to take pictures of cheese", Toast.LENGTH_LONG).show();
}
public void SubmitHandler(View view){
content = editText.getText().toString();
Toast.makeText(this, "add cheese info", Toast.LENGTH_LONG).show();
if(infoType == InfoType.NO_INFO){
Toast.makeText(this, "Please select an Information type", Toast.LENGTH_LONG).show();
}
else if(content.equals("")){
Toast.makeText(this, "Please write a description", Toast.LENGTH_LONG).show();
}
informationPoint = new InformationPoint(providedLocation, infoType, content);
Intent returnIntent = new Intent(this, MapsActivity.class);
returnIntent.putExtra("information_point", informationPoint);
this.setResult(1, returnIntent);
finish(); /* added after original post, this makes app crash consistently */
}
}
Here's the xml layout file for the second activity:
<LinearLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.m.googlemapappfirst.InformationInputActivity">
<TextView android:text="#string/instructions"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="#+id/radio_swiss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/swiss"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="#+id/radio_american"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/american"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="#+id/radio_gruyere"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/gruyere"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="#+id/radio_cheddar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cheddar"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
<TextView android:text="#string/instructions2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/description"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_weight="1"
android:hint="#string/type_here"
android:inputType="text"
android:imeOptions="actionDone"
android:maxLines ="4"
android:maxLength ="2000"
android:scrollHorizontally="false"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="horizontal">
<Button
android:id="#+id/take_picture_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Picture"
android:onClick="AddPictureHandler" />
<Button
android:id="#+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Report Information"
android:onClick="SubmitHandler" />
</LinearLayout>
I didn't see any problem but missing invocation of show() after toast creation, just like this:
Toast.makeText(this, "Oops... This shouldn't happen", Toast.LENGTH_LONG).show();

I need to create three buttons and each button shows different text

I need to create three buttons in Android Studio and each button shows different text. When the user clicks on first button it shows "Welcome" at the right side of the button. When the user click on the second button the "Welcome" message will disappear, and in the same place a"Hello" message will appear. Third button is the same with difference message"Bye". Here my code and thanks in advance.
XML Code:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.android.Recipes">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="R1"
android:id="#+id/click_btn"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/response"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/click_btn"
android:layout_toEndOf="#+id/click_btn" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="R2"
android:id="#+id/button15"
android:layout_below="#+id/click_btn"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="34dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="R3"
android:id="#+id/button16"
android:layout_centerVertical="true"
android:layout_alignRight="#+id/button15"
android:layout_alignEnd="#+id/button15" />
Java Code:
package com.example.android;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import static com.example.android.R.*;
public class Recipes extends ActionBarActivity implements View.OnClickListener {
TextView resp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_recipes);
resp = (TextView)this.findViewById(id.response);
Button b = (Button)this.findViewById(id.click_btn);
b.setOnClickListener(this);
resp.setText("Welcome ");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_recipes, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View v) {
resp.setText("Welcome ");
}
}
Set setOnClickListener for all Button's in same way as currently doing for click_btn Button .
To change text of TextView clicked use switch-case inside onClick method like:
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.click_btn: /** on click_btn button click */
resp.setText("Welcome ");
break;
case R.id.button15: /** on button15 button click */
resp.setText("Hello ");
break;
case R.id.button16: /** on button16 button click */
resp.setText("Bye ");
break;
}
}

Categories

Resources