TextView text = (TextView) vi.findViewById(R.id.text);
text.setText(text_value);
This string edits the android:text value.
But if I want to edit the andoid:onClick value, what should I do?
For example:
// from:
android:onClick("action(1)");
// to:
android:onClick("action(2)");
Thanks a lot :)
PS: sorry for any error, I'm Italian :(
you cannot edit the android:onClick programmatically in android try setOnClickListener
text.setOnClickListener(this);
#Override
public void onClick(View v) {
}
Call setOnClickListener(), providing an implementation of View.OnClickListener as argument.
Nitpick: Calling setText() doesn't "edit" the XML attribute value. It just changes the same TextView internal value that is influenced by the attribute at construction time. The same goes for onClick.
Create the method in the activity class.
res/layout/main.xml
<FrameLayout
....>
<TextView android:id="#+id/text1"
..../>
<Button android:id="#+id/btn1"
android:onClick="actionTwo"
.../>
</FrameLayout>
class MyActivity extends Activity {
public void onCreate(Bundle b) {
setContentView(R.layout.main);
}
public void actionOne(View view) {
// the action here
}
public void actionTwo(View view) {
// the action here
TextView textView = (TextView) findById(R.id.text1);
textView.setText("Uhuuu funcionou!!!");
}
}
Related
Can u help me understand why when I want set text in TextView, I got NullPointerException? I know, because my TextView is null but how I can get TextView again?
What is my logic:
Start Application
Click button
Go to nullPoint and get data from firebase, when the proccessing is finished back to MainActivity and update text in TextView.
This is my example code:
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnAddNewWord = findViewById(R.id.button);
btnAddNewWord.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
nullPoint np = new nullPoint();
np.takeCount();
}
});
}
public void setTextView(int count){
TextView tv = findViewById(R.id.textView);
tv.setText("count = " + count);
}
}
nullPoint
public class nullPoint {
//the class gets asynchronous data from the Firebase database and does not know when the process will end
public void takeCount(){
//det data
//.
//.
// finish
//send data to MainActivity and update textView text
MainActivity ma = new MainActivity();
ma.setTextView(5);
}
}
XML
<?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:orientation="vertical"
tools:context=".MainActivity"
android:layout_gravity="center">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_gravity="center"/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="center"/>
</LinearLayout>
I guess the nullPoint class is not inside the MainActivity. As a result, inside your nullPoint class there is no reference for the TextView in which you are trying to set a data.
In your case I would like to suggest you to implement a listener like the following.
public interface FirebaseResponseListener {
void onFirebaseResponseReceived(int count);
}
Now from your MainActivity, you need to implement the listener as follows.
public class MainActivity extends AppCompatActivity implements FirebaseResponseListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnAddNewWord = findViewById(R.id.button);
btnAddNewWord.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
nullPoint np = new nullPoint();
np.listener = this; // Initialize the listener here
np.takeCount();
}
});
}
public void setTextView(int count){
TextView tv = findViewById(R.id.textView);
tv.setText("count = " + count);
}
#Override
void onFirebaseResponseReceived(int count) {
setTextView(count);
}
}
And you have to add a listener in your nullPoint class as well.
public class nullPoint {
public FirebaseResponseListener listener;
//the class gets asynchronous data from the Firebase database and does not know when the process will end
public void takeCount(){
//det data
//.
//.
// finish
//send data to MainActivity and update textView text
listener.onFirebaseResponseReceived(5);
}
}
Hope that solves your problem.
So, I want a button like this in my app:
I don't know how to add code for button in the android-studio, and I only have the scanner code. I don't know how to put 2 function in the same project. What should I do? Can someone help me?
It sounds like you need to add a function to handle the OnClick event. The Android documentation has a nice example on how to set and OnClick listener:
<Button
android:id="#+id/button_id"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/self_destruct" />
public class MyActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout_id);
final Button button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
// This should be where you add your button logic.
}
});
}
}
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 still porting a J2ME app to Android and now my problem is with the GUI.
For what i've seen, Android's Activities are great, but my J2ME is filled with the classic:
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
Display display = getDisplay();
if (alert == null)
display.setCurrent(nextDisplayable);
else
display.setCurrent(alert, nextDisplayable);
}
I just can't make every Displayable to an Activity, so i thought about replacing them with View. I tried that but it just doesn't works, the app doesn't changes the screen.
Update:
Thanks for answering, but placed al vews inside FrameLayout and still nothing. This is the core of my test code, so you can check it out:
public class TestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView t = (TextView)findViewById(R.id.text); // Shows "Hi"
showDialog(); // it just shows a dialog asking if the user wants to change screen
}
showDialog() {
// in OnClick()... i do the following, and here is where it fails, i tried so far:
TestView testv= new MarcoLoco(MFActivity.this);
setContentView(testv);
testv.invalidate();
testv.requestFocus();
testv.showMeSomething();
}
public class TestView extends View{
private Context context;
TextView tv;
public TestView(Context context) {
super(context);
this.context=context;
}
public void showMeSomething() {
tv = (TextView)findViewById(R.id.tessto); // it should show "Bye"
}
}
After the OnClick the "Hi" dissapears from the screen but nothing appears, no "Bye".
Why, oh, why!?
I don't have any expirience with Java ME, but this might help you
Put your Views inside a FrameLayout, then you can use
mViewA.setVisibility(View.VISIBLE);
mViewB.setVisibility(View.GONE);
mViewA.requestFocus();
to switch between diffrent views
EDIT:
Here is a short example program that toggles between 2 textviews:
public class test extends Activity {
boolean showTV1 = true;
OnClickListener ocl = new OnClickListener() {
#Override
public void onClick(View v) {
showTV1= !showTV1;
if (showTV1){
tv1.setVisibility(View.VISIBLE);
tv2.setVisibility(View.GONE);
tv1.requestFocus();
} else {
tv2.setVisibility(View.VISIBLE);
tv1.setVisibility(View.GONE);
tv2.requestFocus();
}
}
};
private TextView tv1, tv2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1 = (TextView) findViewById(R.id.TextView01);
tv2 = (TextView) findViewById(R.id.TextView02);
tv1.setOnClickListener(ocl);
tv2.setOnClickListener(ocl);
}
}
and main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frameLayout01" android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:id="#+id/TextView01" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#0f0"
android:text="Hi" />
<TextView android:id="#+id/TextView02" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#f00"
android:text="Bye" android:visibility="gone"/>
</FrameLayout>
I am not sure I fully understand your questions, but try looking at ViewAnimator and ViewFlipper - perhaps these can help
PS. just out of curiosity... have you tried any of automatic converters?
Why are you porting from J2ME to Android on your own, instead of using a conversion tool ?
Quicker, no need to learn and excel with Android, no need to debug and maintain 2 code bases... Give it a try at www.UpOnTek.com. Thanks.