When i clicking on button i need change activity and text in TextView which belong this activity, i try do it like this:
in MyActivity:
public void onClick(View v) {
switch (v.getId()) {
case R.id.MondayButton:
NameDiscipline = (TextView) findViewById(R.id.Discipline8);
NameDiscipline.setText("Some Text");
Intent intent = new Intent(this, SheduleForTheDayActivity.class);
startActivity(intent);
break;
default:
break;
}
in OtherActivity.xml:
<TextView
android:id="#+id/Discipline8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
and when i click on MondayButton my app crashing. Without lines
NameDiscipline = (TextView) findViewById(R.id.Discipline8);
NameDiscipline.setText("Some Text");
code work correctly.
What you want is a BroadcastReceiver in the Activity class with Discipline8. Have the onReceive method change the text. Then, in MyActivity, call
LocalBroadcastManager.getInstance(this)
.sendBroadcast(new IntentFilter("some string to use as action"));
Related
I want to create a button in a fragment for switching to another activity. I did everything but the button just does not respond. An animation appears that it is being pressed, but the method itself does not work. For clarity, I made a toast, it also does not work. How to make the button work?
ActionsFragment.java
public class ActionsFragment extends Fragment {
public ActionsFragment(){
}
Button Button_Add;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_actions, container, false);
Button_Add = (Button) v.findViewById(R.id.button_Add);
Button_Add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast toast = Toast.makeText(getContext(), "Button", Toast.LENGTH_SHORT);
toast.show();
switch (v.getId()){
case R.id.button_Add:
Intent intent = new Intent(ActionsFragment.this.getActivity(),Action_AddActivity.class);
startActivity(intent);
break;
default:
break;
}
}
});
return v;
}
}
My xml file
<Button
android:id="#+id/button_Add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="25dp"
android:layout_marginRight="25dp"
android:layout_marginBottom="25dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="#+id/divider3"
app:layout_constraintEnd_toEndOf="parent" />
you need to call startActivity(intent) from activity context. so either pass the context to the fragment when you create it, or use
getActivity().startActivity(intent);
The main problem that I see is that you are switching a View, by that manner you will be not able to pass to other activity. It's simple, just clear the code.
Try this:
button_Add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ActionsFragment.this.getActivity(),Action_AddActivity.class);
getActivity().startActivity(intent);
}
});
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 need to link the button with the page (not the main page) like when I click on the button to go to the page for example (location page)?
private void setupLocationPageButton() {
Button LocationPageButton = (Button) findViewById(R.id.btnLocationPage);
LocationPageButton.setOnClickListener(new View.OnClickListener()
If your goal is to open another activity this is what you are going to want to do.
In your xml file you are going to want something like this
...
<Button
android:id="#+id/activity_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonClicked"
android:text="#string/text_of_button" />
...
Then in your activity you want
public void onButtonClicked(View view) {
Intent intent = new Intent(this, OtherActivity.class);
startActivity(intent);
}
what i think you trying to do: is when you click on button it's change the MainActivity
Button LocationPageButton = (Button) findViewById(R.id.btnLocationPage);
LocationPageButton.setOnClickListener(new View.OnClickListener({
public void onClick(View _view) {
Intent i = new Intent(MainActivity.this,TheActivityTheclassNameYouWannGoTo.class);
startActivity(i);
}
}));
but first you have to creat activity and class inherit Activity like MainActivity
initialize the class in AndroidMainfest.xml
i hope this help you
You can use fragment, each fragment contains a specific page, see this for Android fragment.
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 found this code very usefull to get from one activity to the other but the problem is, that I don't see where the destination is mentioned. I would really appreciate it if some one pointed out how to change the destination.
Here is the code:
Button getTaxi = (Button) findViewById(R.id.GetTaxi);
getTaxi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
final Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
The respective part in the xml:
<Button
android:id="#+id/GetTaxi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="GetTaxi" >
</Button>
Thank you very much in advance!
Actually, the code that executes on the Button's click states that your Activity (which is a sub-activity here by the way) has done its job and now finishes with the result code RESULT_OK. It means that there was another Activity that has started this actual Activity for some kind of result. So, when you'll click the Button your Activity will finish. To start some other Activity on the Button's click you should create an Intent, specifying explicitly the Activity you want to start, or just the action you want to perform over some data, letting the Android resolve the final Activity for you. Then you should call startActivity(), passing in the Intent you've created. Hope this helps.
You can use something like this:
Button getTaxi = (Button) findViewById(R.id.GetTaxi);
getTaxi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
Intent intent = new Intent();
intent.setClass(this, GetTaxiActivity.class);
startActivity(intent);
//call finish() if the current activity is something like a loading page
}
});
The code piece above which you mentioned is a sub-activity which is called using
StartActivityForResult();