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;
}
}
Related
Basically this app is supposed to ask for your name, with an option to save, which when click, an alert dialog pops up and asks 'are you sure?', when yes is clicked, it should say 'welcome + whatever name put'. my problem is that the app keeps shutting down before it says welcome. I declared the string as userName and ran it without any function to the string, and it just said 'welcome, null'.
but when i did
userName=editText.getText().toString();
the app shut down immediately. Please HELP I'm out of ideas.
the page which calls the welcome page works fine, but the welcome.java is the file with the issue.
public class Welcome extends Activity {
String userName;
final EditText editText=(EditText)findViewById(R.id.editText);
final TextView textView=(TextView)findViewById(R.id.textView);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
final TextView textView=(TextView)findViewById(R.id.textView);
final EditText editText=(EditText)findViewById(R.id.editText);
userName=editText.getText().toString();
textView.setText(String.format("Welcome, %s.", userName));
}
}
The logcat is basically the title, giving an error to the following line:
userName=editText.getText().toString();
PS I've moved that line of code before and after onCreate and it still gave errors
My MainActivity.java is
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.view.View;
public class MainActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] menu={"User Preferences","Animation","Browser","Media","Take Picture"};
setListAdapter(new ArrayAdapter<String>(this,R.layout.activity_main,R.id.options, menu));
}
protected void onListItemClick(ListView l, View v, int position, long id){
switch(position){
case 0:
startActivity(new Intent(MainActivity.this,userPreferences.class));
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
}
}
#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 activity_main.xml is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/ic_launcher_sf"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:layout_marginTop="2px">
</ImageView>
<TextView
android:id="#+id/options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/travel"
android:textSize="25sp"></TextView>
My welcome.xml is
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:textSize="30dp" />
my userpreferences.java
public class userPreferences extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userpreferences);
Button save=(Button)findViewById(R.id.button);
save.setOnClickListener(new View.OnClickListener() {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(userPreferences.this);
#Override
public void onClick(View v) {
alertDialog.setTitle("Alert Dialog");
alertDialog.setMessage("Are you sure you want to save?");
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(userPreferences.this,Welcome.class));
}
});
alertDialog.setNegativeButton("No", null);
alertDialog.show();
}
});
}
}
and my user preferences.xml file
<?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"
android:weightSum="1">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_weight="1"
android:hint="Enter Your Name"
android:textSize="20dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:id="#+id/button"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true" />
The problem lies in both of these
final EditText editText=(EditText)findViewById(R.id.editText);
final TextView textView=(TextView)findViewById(R.id.textView);
Do not do findViewById without setContentView() called, or especially outside the methods. Make it:
private EditText editText;
private TextView textView;
And also:
inside your onCreate, you don't have to redeclare the views just do:
textView=(TextView)findViewById(R.id.textView);
editText=(EditText)findViewById(R.id.editText);
Plus: Add an EditText on your welcome.xml
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:hint="THIS IS AN EDITTEXT" />
The problem is that you have 2 different views in 2 different xml's called from one single activity. Which is why you get the nullpointerexception. so instead pass the context as an extra via an intent to the required activity and implement it in the findviewbyid method.
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 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.
I created three buttons and each button shows text when it is clicked. Now I need to add to that so the buttons show text + an image. all the texts and images will only appear if the buttons are clicked. For example, when the user clicks on first button, text1 + image1 appears. When the user clicks on second button, text2 + image2 appears in the same places of text1 and image1. Any help please? here are my codes:
XML (I couldn't post the first code part here but below is the most important parts)
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recipe 1"
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_below="#+id/click_btn"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/button15"
android:layout_alignEnd="#+id/button15" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recipe 2"
android:id="#+id/button15"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recipe 3"
android:id="#+id/button16"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView4"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/response"
android:layout_toEndOf="#+id/response" />
</RelativeLayout>
Java:
package com.example.android.ch;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import static com.example.android.R.*;
public class Recipes extends ActionBarActivity implements View.OnClickListener {
TextView resp; ImageView imageView;
#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);
Button c = (Button)this.findViewById(id.button15);
Button d = (Button)this.findViewById(id.button16);
ImageView imageView = (ImageView) findViewById(id.imageView);
b.setOnClickListener(this);
c.setOnClickListener(this);
d.setOnClickListener(this);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
}
}
#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) {
switch(v.getId()){
case R.id.click_btn: /** on click_btn button click */
resp.setText("2 medium sweet potatoes (about 1 pound total) \n1/2 teaspoon salt\n1/2 teaspoon ground cumin\n1/2 teaspoon chili powder\n1/2 teaspoon paprika\n1/4 teaspoon ground black pepper");
break;
case R.id.button15: /** on button15 button click */
resp.setText("1 large egg yolk, at room temperature\n1/2 cup extra-virgin olive oil; more for grilling\n2 teaspoons minced fresh flat-leaf parsley\n1 teaspoon minced fresh tarragon\n1 1/2 teaspoons fresh lemon juice; more to taste Kosher salt");
break;
case R.id.button16: /** on button16 button click */
resp.setText("1/2 cup butter, softened\n1 cup packed light brown sugar\n1 1/2 cups all-purpose flour\n3 cups rolled oats\n1 teaspoon ground cinnamon");
break;
}
}
}
yes you can add image or icon to a button with text.
use below code for this -
<Button
android:drawableRight="#drawable/ic_logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recipe 3"
android:id="#+id/button16"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
here i have added one more property android:drawableRight you can change it to android:drawableLeft, android:drawableTop, or android:drawableBottom and set image on button.
Use drawableLeft (or any direction) attribute to display image with text,
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_text"
android:drawableLeft="#drawable/button_icon"
... />
Above code will display image like
include the imageview and textview in a layout and keep it's visibility as invisible/gone.Then on clicking a button(for example button1) inside the onclick listener method you need to try like
imageview.setimage("your image");
textview.setText("your text");
change this code inside each buttonclick listener with various value.
I got right answer with many of you and thank you all but I meant to create three buttons and show text+image Not inside the icon. To do that I created three fragments xml files so when the user click on a button it will bring the fragment and place it.
I am trying to create a radio group with two button on and off in Android Studio. I already created the radio group with two button. I want when the user clicks on an picture will appear beneath them and if the user clicks off the picture will disappear. Thank you.
Here is my Java code:
package com.example.android.testapp;
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.ImageView;
import android.widget.RadioButton;
import android.widget.ToggleButton;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static com.example.android.testapp.R.id.imageView;
import static com.example.android.testapp.R.id.radioButton;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioButton rb = (RadioButton) findViewById(R.id.radioButton);
ImageView image = (ImageView) findViewById(R.id.imageView);
if(rb.ischecked()){ image.setVisibility(View.VISIBLE);
} else {
image.setVisibility(View.GONE);
}}
#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);
}
}
Here is my XML:
<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=".MainActivity">
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="200dp">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="On"
android:id="#+id/radioButton"
android:checked="true"
android:enabled="true"
android:clickable="true"
android:onClick="SetImage"
android:nestedScrollingEnabled="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Off"
android:id="#+id/radioButton2"
android:checked="false" />
</RadioGroup>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:src="#drawable/ic_launcher"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="90dp"
android:layout_marginStart="90dp"
android:layout_marginBottom="130dp"
/>
There is no need to have 2 radio buttons if you only want a on/off state. One is enough and when it will be checked then you can make your picture appear and if it's unchecked, you can make it disappear. How to do this, in your onCreate method, you can write:
RadioButton rb = (RadioButton) findViewById(R.id.radiobutton);
ImageView image = (ImageView) findViewById(R.id.image);
if(rb.isChecked()){
image.setVisibility(View.VISIBLE);
} else {
image.setVisibility(View.GONE);
}
Set setOnCheckedChangeListener for RadioGroup to change image visibility according to RadioButton click.
1. Add id to RadioGroup in xml :
android:id="#+id/radioGroup"
2. Add setOnCheckedChangeListener to RadioGroup in onCreate method after setContentView :
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// find which radio button is selected
if(checkedId == R.id.radioButton) {
image.setVisibility(View.VISIBLE);
} else if(checkedId == R.id.radioButton2) {
image.setVisibility(View.GONE);
}
}});