Log Informations In A Textview
Solution 1:
There's a pretty clean approach, which is to use DataBinding
in android. I will go through a short example of how your use case might end up looking.
First, to use DataBinding
you must enable it from the build.gradle of your app:
android {
dataBinding {
enabled = true
}
}
Second thing would be to create a model that would contain your log message, this is also a clean way to store the message instead of simply appending to a string. In my case, I'll call it Log
:
publicclassLogextendsBaseObservable{
//This will be the message you wanna printprivateString message;
@BindablepublicStringgetMessage() {
return message;
}
publicvoidsetMessage(String message) {
this.message = message;
//This would automatically update any binded views with this model whenever the message changesnotifyPropertyChanged(BR.message);
}
}
Third thing would be to update your layout file which contains the TextView
you're using. Basically, we'll create a Log
variable in the layout, and tell the textView
to read the message from it and display it as its text. In my example, activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?><layoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"><data><variablename="log"type="com.riad.crypto.databinding.Log" /></data><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:id="@+id/text_view"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="top"android:textSize="18sp"android:text="@={log.message}" /><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/button_view"android:gravity="bottom"/></LinearLayout>
Now simply, wherever you wanna display the logs, create a Logger
object and bind it with the textView
. In my example MainActivity
:
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
finalLoglogger=newLog();
ActivityMainBindingbinding= DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.setLog(logger); //This is where we bind the layout with the objectButtonbutton= (Button) findViewById(R.id.button_view);
logger.setMessage("1st log statement"); //You'll notice that the texview displays this message first
button.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
logger.setMessage("2nd log message"); //After button click, the texview would automatically change text to this. All you did was set the message
}
});
}
So now you could change the textView
on every button click, but ofcourse you could do it wherever you want. And in case you need the Log
object in different classes you could improve this by creating a Singleton
or something, but this was just for the demo.
Hope this helps! Goodluck
Post a Comment for "Log Informations In A Textview"