Such must be fair, that when selecting a button to change the text size?
I tried to add in a onCreate if but not work correctly, is cheek only one time to start the application. The function not remains active for next time.
Thank you
button
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/inbox_string"
android:id="#+id/button"
android:textColor="#drawable/text_button_culoare"
android:drawableLeft="#drawable/ic_view_list_white_24dp"
android:background="#android:color/transparent"
style="?android:attr/borderlessButtonStyle"
android:layout_centerHorizontal="true"
android:gravity="left|center_vertical"
android:layout_gravity="center_horizontal"
android:focusable="true"
android:enabled="true"
android:clickable="true"
android:contextClickable="true"
android:elegantTextHeight="true"
android:layout_marginTop="16dp" />
Activity:
public class Work_screen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work_screen);
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if (v.getId() == R.id.button) {
}
}
};
findViewById(R.id.button).setOnClickListener(clickListener);
final Button button = (Button) findViewById(R.id.button);
if (button.isSelected()) {
Context context = getApplicationContext();
CharSequence text = "selected";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
button.setTextSize(22);
} else {
Context context = getApplicationContext();
CharSequence text = "not";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
}
You should place some code in your onClick method, because this method is called when you click on that button. The reason why your code is just called the first time is that the ONCREATE method is just called once at the creation of your application.
Just place your if else statement in your onClick method and it should work.
And you should create your button variable as a class variable to access it in your listener even if it's not part of your onCreate method.
Related
I have the following piece of codes.I am calling a second activity from main activity.Whenever the send button is pressed i want a toast to show button is pressed and start the activity.But due to some context problems only toast is appearing.Please correct the context for intent and give some clear explaination about these contexts.
MainActivity.java
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE="com.example.iamka.androiddevelop.MESSAGE";
public void Toast1(String s){
Toast.makeText(this,s+" is called",Toast.LENGTH_SHORT).show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("My app","onCreate is called");
Toast1("onCreate");
Button btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("My app","Button is pressed");
Toast.makeText(MainActivity.this,"Button pressed",Toast.LENGTH_SHORT).show();
}
});
}
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
Log.i("intent","intent is started");
startActivity(intent);
}
}
DisplayMessageActivity.java
public class DisplayMessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView =(TextView) findViewById(R.id.textView);
Log.i("intent","displaymessage");
textView.setText(message);
}
}
activity_main.xml
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="#string/edit_message"
android:inputType="textPersonName"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toLeftOf="#+id/button"
app:layout_constraintHorizontal_chainStyle="spread" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:onClick="sendMessage"
android:text="#string/button_send"
app:layout_constraintBaseline_toBaselineOf="#+id/editText"
app:layout_constraintLeft_toRightOf="#+id/editText"
app:layout_constraintRight_toRightOf="parent" />
activity_display_message.xml
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="TextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
When i remove the onClickListener() method then the intent is working.
Since you are setting your own View.OnClickListener, you are removing the one from the XML definition. Button only support one View.OnClickListener. First, the XML will create one from the android:onclick attribute like :
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sendMessage(v);
}
});
Then you are setting yours with the Toast. The button will only keep the last one, so the Intent is never send.
Solutions :
call the sendMessage method in your listener
Like:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("My app","Button is pressed");
Toast.makeText(MainActivity.this,"Button pressed",Toast.LENGTH_SHORT).show();
sendMessage(v); //Or anywhere in that method, your call.
}
});
remove the listener to keep the one create by the android:onclick.
FYI:
Usually, a set### methods means this is not supporting multiple values, add### methods do.
Also, you can check at android- multi onClick listener in one button to implement your own multi listener button if you like. But I didn't check if there were some more up to date...
Just remove android:onClick="sendMessage" for button and try. Either you have to set click listener in xml or in class file. Both it won't work
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:text="#string/button_send"
app:layout_constraintBaseline_toBaselineOf="#+id/editText"
app:layout_constraintLeft_toRightOf="#+id/editText"
app:layout_constraintRight_toRightOf="parent" />
Change this in your Activity :
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("My app","Button is pressed");
Toast.makeText(MainActivity.this,"Button pressed",Toast.LENGTH_SHORT).show();
sendMessage();
}
});
private void sendMessage() {
// Do something in response to button
Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
Log.i("intent","intent is started");
startActivity(intent);
}
Just copy paste the following MainActivity code and it will do the work. The xmls are loaded first and then on runtime you are setting the onclick listener to the button again. so the xmls onclick has been replaced by your onclick listener in java code. Hence you get toast but the sendMessage() is never called
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE="com.example.iamka.androiddevelop.MESSAGE";
public void Toast1(String s){
Toast.makeText(this,s+" is called",Toast.LENGTH_SHORT).show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("My app","onCreate is called");
Toast1("onCreate");
Button btn=(Button)findViewById(R.id.button);
}
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
Log.i("intent","intent is started");
startActivity(intent);
}
}
Hi guys I have a problem regarding the android dialog box.What I am trying to do is set few lines of description in a dialog box which I am able to do it fine but at last i need a link called "see more" which will redirect the user to other activity.I am very new to android and these is the first of some things what I am trying to do any help will be appreciated..my code onStart()
protected void onStart()
{
super.onStart();
/*final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.main);
dialog.setTitle("About Service One");
Button button = (Button) dialog.findViewById(R.id.button12);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
protected void onStart()
{
super.onStart();
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.main);
dialog.setTitle("About Service One");
Button button = (Button) dialog.findViewById(R.id.button12);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
TextView showMore= (TextView ) dialog.findViewById(R.id.tvShowMore);
showMore.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(getApplicationContext(),ShowMoreActivity.class);
startActivity(intent);
}
});
dialog.show();
}
And add in xml showmore TextView Which set text from string.xml
i.e <string name="show_more"><u>Show More </u></string> use this for set linkable text
main.xml
...........
<TextView
android:id="#+id/tvShowMore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/show_more"
android:textAppearance="?android:attr/textAppearanceMedium"
>
</TextView>
............
Create a custom Dialod layout>> create a text view as show more >> set OnClick Listener on it as ..
You can set the click handler in xml with these attribute:
android:onClick="onClick"
android:clickable="true"
Don't forget the clickable attribute, without it, the click handler isn't called.
dailog.xml
...
<TextView
android:id="#+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show more"
android:textSize="55sp"
android:onClick="onClick"
android:clickable="true"/>
...
MyActivity.java
public class MyActivity extends Activity {
public void onClick(View v) {
// ... start show more activity here
}
}
OR... find the view using dialog and set OnClick Listener on it..
showmore = (TextView)dialog.findViewById(R.id.click);
showmore..setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// ... start show more activity here
}
});
Try this:
<TextView
android:id="#+id/yourId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show more"
android:onClick="showMore"
android:clickable="true"/>
Inside your activity, get that textView :
TextView showMoreView=(TextView)findViewById(R.id.yourId);
Add this showMoreView inside your text in the dialog and
Inside your activity, define the methode "showMore"
public void showMore(View view)
{
Intent intent =new Intent(YourCurrrentActivity.this,NextActivity.class);
startActivity(intent);
}
This will take you to NextActivity on clicking the "show more" text.
I have many buttons in my calculator app. I am testing with only one button to start, that buttons id is "one" and should change colour when I click the blue theme button. I have tried following methods:
blueTheme = (Button) findViewById(R.id.blueTheme);
blueTheme.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
one.setBackgroundColor(Color.argb(175, 144, 202, 249));
one.setBackgroundColor(Color.parseColor(/*hex code here*/));
one.setBackgroundColor(Color.BLUE);
}
});
Nothing seems to do anything. I am trying to change the colour of the button in one activity via an option in another activity. Here's actual button one:
one = (Button) findViewById(R.id.one);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.append("1");
}
});
xml code of one in activity_main.xml:
<Button android:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_weight="1"
android:background="#CCCCCC"
android:text="1"
android:textColor="#FF6600"
android:textSize="50sp"
android:layout_marginRight="1dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp" />
Idea is that there will be a option in another intent where I can change colors of calculator, but testing on one button fails, can't proceed. Thank you for your time.
The problem is the click from one activity cant get through to the other activity unless you pass it over.
In the activity with the blue theme button
blueTheme.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//NOTE: Where I've put MainActivity that should actually be the name
// of whatever activity this code is nested in
Intent intent = new Intent(MainActivity.this, OtherActivity.class); //use your real class name
intent.putExtra(OtherActivity.EXTRA_COLOR, Color.BLUE);
startActivity(intent);
}
});
In your OtherActivity.class
public class OtherActivity extends Activity {
public static String EXTRA_COLOR = "EXTRA_COLOR";
public void onCreate(...) {
View one = (Button) findViewById(R.id.one);
//NOTE: if you add singleTop to this activity in the manifest
// you might need to do this on onNewIntent
Intent intent = getIntent();
if (intent.hasExtra(EXTRA_COLOR)) {
int color = intent.getIntExtra(EXTRA_COLOR, Color.WHITE);
one.setBackgroundColor(color);
}
}
}
Use this :
// If you're in an activity:
yourButton.setBackgroundColor(getResources().getColor(R.color.red));
// OR, if you're not:
yourButton.setBackgroundColor(yourButton.getContext().getResources().getColor(R.color.red));
If you want to set background color without using a pre-defined color resource, do it like so
one.setBackgroundColor(0xFFFF0000); // Red
one.setBackgroundColor(0xFF00FF00); // Green
Here, 0xFF00FF00 is equivalent to #ff00ff00 (#aarrggbb)
I want to do a certain action on button press according to which item on spinner is selected.
This is what I've got so far:
public void submitButton (View v){
Button b1 = (Button)findViewById(R.id.submitButton);
final Spinner s1 = (Spinner)findViewById(R.id.spinner1);
final Context context = this;
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final int position = s1.getSelectedItemPosition();
switch (position){
case 0:
AlertDialog.Builder spinnerErrorBuilder = new AlertDialog.Builder(context);
spinnerErrorBuilder.setTitle("Warning");
spinnerErrorBuilder.setMessage("Please choose an item from the list");
spinnerErrorBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog spinnerError = spinnerErrorBuilder.create();
spinnerError.show();
break;
case 1:
break;
}
}
});
}
When I compile my app and click the button, the app crashes and returns to main activity. It doesn't matter which item I have selected (0 or 1) the app still crashes. Could someone tell me where I went wrong?
XML code for the button:
<Button
android:id="#+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText1"
android:layout_alignBottom="#+id/editText1"
android:layout_alignLeft="#+id/checkBox25"
android:text="#string/addMaterial"
android:onClick="onClick" />
Logcat file:
06-22 15:00:13.455: E/AndroidRuntime(23409): java.lang.IllegalStateException: Could not find a method onClick(View) in the activity class com.example.gw2legendary.Bifrost for onClick handler on view class android.widget.Button with id 'submitButton'
Simply delete this line:
android:onClick="onClick"
within your xml. Be sure to call submitButton from your onCreate without passing in a view as this is not needed.
You can either set an onclicklistener in code as you have done by
b1.setOnClickListener...
OR just have a method such as:
public void method { //This is a method so do stuff here }
And set it in your xml as follows
android:onClick="method"
In your above example changing method to submitButton would work.
Your method name is submitButton but your onClick method in xml is onClick
Change it to submitButton and your problem is solved
Xml Should be
<Button
android:id="#+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText1"
android:layout_alignBottom="#+id/editText1"
android:layout_alignLeft="#+id/checkBox25"
android:text="#string/addMaterial"
android:onClick="submitButton " />
I'm going through the tutorial HelloFormStuff. I started having problems adding the RadioGroup widget. After moving some brackets around I finally got it to work.
When I tried to add the final (2) widgets, I found if I tried to add them in main.xml below the RadioGroup they wouldn't appear in the app. I guess I could just call it finished and move on, but I took the time to enter all the code (not ctrl C, ctrl P) and damn it, the widgets should show up where I tell them to! Why can't I add widgets below the RadioGroup?
public class HelloFormStuff extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
final CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox);
checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks, depending on whether it's now checked
if (((CheckBox) v).isChecked()) {
Toast.makeText(HelloFormStuff.this, "Selected", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(HelloFormStuff.this, "Not selected", Toast.LENGTH_SHORT).show();
}
}
});
final RadioButton radio_red = (RadioButton) findViewById(R.id.radio_red);
final RadioButton radio_blue = (RadioButton) findViewById(R.id.radio_blue);
radio_red.setOnClickListener(radio_listener);
radio_blue.setOnClickListener(radio_listener);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
Toast.makeText(HelloFormStuff.this, "Beep Bop", Toast.LENGTH_SHORT).show();
}
});
final ToggleButton togglebutton = (ToggleButton) findViewById(R.id.togglebutton);
togglebutton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
if (togglebutton.isChecked()) {
Toast.makeText(HelloFormStuff.this, "Checked", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(HelloFormStuff.this, "Not checked", Toast.LENGTH_SHORT).show();
}
}
});
final RatingBar ratingbar = (RatingBar) findViewById(R.id.ratingbar);
ratingbar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
Toast.makeText(HelloFormStuff.this, "New Rating: " + rating, Toast.LENGTH_SHORT).show();
}
});
}
private OnClickListener radio_listener = new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
RadioButton rb = (RadioButton) v;
Toast.makeText(HelloFormStuff.this, rb.getText(), Toast.LENGTH_SHORT).show();
}
};
}
You can try:
Toast.makeText(HelloFormStuff.this,
((RadioButton) v).getText(),
Toast.LENGTH_SHORT).show();
if it crashes, but I don't see any problem with your code.
Here is the main.xml, everything should show up:
<CheckBox android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check it out" />
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="#+id/radio_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red" />
<RadioButton android:id="#+id/radio_blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue" />
</RadioGroup>
<ToggleButton android:id="#+id/togglebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Vibrate on"
android:textOff="Vibrate off"/>
<RatingBar android:id="#+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0"/>
I know this is REALLY old, but I just started learning Android and I ran into the same thing. Maybe this will help new-comers I found out that the reason you do not see them is because the value of layout_height for the RadioGroup should be wrap_content. The example says to make it fill_parent. But as you may know by now, the object's height will go to the bottom of the screen.
#Duy's main.xml is correct, but I just wanted to point out the exact reason.