How to display several paragraphs of text in android studio - android

In the app I'm building, I'm hoping to have several different guides / tutorials that will each contain several paragraphs and hopefully pictures in them. Right now the only thing I would know to do is to have all of the different texts written out long form in my strings resource file. I would then need to have separate layouts and fragments for each tutorial.
Is there an easier way? Can I separate my strings resource file at least so that I don't have that one file completely bogged down? Could I maybe import the text from a separate file?

Yes, you can. you need to set text programmatically. You need only one layout for all of these same type information page.
Let's say you have
<TextView
android:id="#+id/text_view_id"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/hello" />
You can get that text view from java activity like below and set the text you want..
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView helloTextView = (TextView) findViewById(R.id.text_view_id);
helloTextView.setText(R.string.user_greeting);
}
}
you can do this setText process by adding a switch, if or any conditional checking process like below
switch(expression) {
case value :
helloTextView.setText(R.string.firstPageText);
break;
case value :
helloTextView.setText(R.string.secondPageText);
break; // optional
// You can have any number of case statements.
helloTextView.setText(R.string.defaultText);
// Statements
}
PS: I think you can use different text style while you creating resource text. You can follow this https://www.myandroidsolutions.com/2017/09/29/android-html-textview/#.W9pu1mgzaUk for it.

Related

Is there an XML equivalent to HTML classes?

Sorry if this is a really obvious question, but I've been trying to find something like it in a number of places, and I'm not sure if it just doesn't exist or if I'm using the wrong language to describe what I need.
If, for example, I have a number of TextViews in different parts of my activity, and I want all of them to open the same activity when clicked on, how would I do this without specifying each of their IDs individually in an if statement in the Java? In HTML I would use a class attribute to group them together, but I can't find a similar feature in XML.
Thanks for your help!
If your intention is to prevent code repetition and tedious code repetition, then one way is to use ButterKnife. Something link below would work:
#OnClick({R.id.view_id1, R.id.view_id2, R.id.view_id3})
public void onClick(View view) {
// TODO: Handle click
}
Or, as the other answer suggests, you can have android:onClick attribute on each of those views which points to same method in Java code.
Write your xml code of EditText as follows
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Fourth EditText"
android:onClick="editTextClick"/>
And in Java file write method
public void editTextClick(View v) {
// does something very interesting
Log.d("MainActivity","EditText pressed");
}
I hope this will help you,
Happy Coding

How to define a custom BindingMethod for a TextView?

I am starting to work with the new data binding API. I want to bind on a TextView a custom property where I change the text and background at once. As far I understood the api there is a #BindingMethod annotation for this, but the documentation is there a little weak.
I want something like this:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Important"
tools:background="#drawable/bg_badge_important"
tools:textColor="#fff"
android:id="#+id/badge"
custom:badge="#{item.badge}"/>
Where this item.badge is a string like important or notice. For i18n I cannot use this string directly but this will help me to choose the right text and the right background image.
Can you give me a small example or a reference how to use this attribute? I just found one answer on Stack Overflow, but this does not answer my question.
Just create a public static method on your codebase and annotate with #BindingAdapter.
Here is an example for fonts:
https://plus.google.com/+LisaWrayZeitouni/posts/LTr5tX5M9mb
Edit by rekire I ended in using this code:
#BindingAdapter({"bind:badge"})
public static void setBadge(TextView textView, String value) {
switch(value) {
case "email":
textView.setText(R.string.badge_email);
textView.setBackgroundResource(R.drawable.bg_badge_email);
break;
// other cases
}
}

how to exchange data between activity and layout xml in android

I need to exchange data between activity and it's layout xml in Android. But I do not find a way to do this in Android. For example, views and controller in mvc pattern always has a way to exchange data between. So I am wondering is there any way to exchange data between them to should I refresh my mind and realize there is no such way?
use below code to get value from layout in your activity
String value;
EditText editText= (EditText) findViewById(R.id.editText);
value=editText.getText();
code in xml example:
<ImageView
android:id="#+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
in java class (like onCreate()):
ImageView image = (ImageView) findViewById(R.id.image_view);
Then you can do what you want with image
I believe you're unsure exactly what you're asking. If you want to exchange information, such as ID or text entered into a textfield then any good android tutorial should be-able to demonstrate this. Considering your last comment I think you're talking about GET and POST based technologies which can be done usingREST andSOAP, or both if you want.
This questions answer has a good implementation and definition of what both of these webservices are.
P.S. If this is what you're looking for then upvote that answer.
As some additional info, the "view" (XML Layout file) gets set by your activity initially on your activity's onCreate method. Right after you call it's parent method (super.onCreate()).
To maintain scope throughout the activity I tend to declare all the layout widgets that I need to the activity to interact with outside of any methods and within the class.
TextView textWelcomeMessage;
public void MyActivity(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// before calling setContentView() we have the option to change
// window features ex requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_my_activity);
// Now to set the textview
textWelcomeMessage = (TextView) findViewById(R.id.textWelcomeMessage);
// Set some data
textWelcomeMessage.setText("Hello, welcome to my activity");
}
It's not exactly like traditional php style mvc since static typing changes thing up a bit and we have to worry about scope. But the core principles can still apply as far as data abstraction and separation go. Hope this helps =)

How to have several tabs from the same layout, correctly handling edittext?

I've got three tabs, all from a layout xml file,say list.xml. In list.xml I've defined a EditText, something like this:
<EditText
android:id="#+id/filterText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/filterClear"
android:hint="#string/filterHint"
android:text="#string/filter">
</EditText>
However, it seems that if doing a orientation change and having typed something into one of the three EditTexts (all I guess with the same ID), the text doesn't restore to the same state. Sometimes (depending on which tab I've typed into) all fields are empty or all fields are filled.
What is the proper way to handle this?
I ended up having three separate layout files list1.xml, list2.xml, list3.xml, that only differed in the IDs. So list1.xml would have #+id/filterText1, list2.xml would have #+id/filterText2 and so on.
And then in the ListActivity.java file I used to have this as part of my onCreate:
setContentView(R.layout.list);
And several references to this throughout the activity:
EditText filter = (EditText)findViewById(R.id.filterText);
...
Instead I have now got this in my onCreate:
if(START_TAB1.equals(startMode)) { // Checks which tab is being started
setContentView(R.layout.list1); // Use appropriate layout
filterTextId = R.id.filterText1; // Save correct ID in a class variable
}
// And then the same "else if..." for all other tabs
And this when referencing the field itself throughout the activity:
EditText filter = (EditText)findViewById(filterTextId); // Use common ID

include tag in xml file of Android Programming

I have a question regarding the tag. Actually I am new to the Android Programming and I want to use the Concept of Reusability in my Application at several places. I get to know that it is possible by the tag but I don't know how to use that. I have refered some of it's examples from the net but didn't found them quite satisfactory.
Can anybody please make me understand it with a Clear and appearant example!
Thanks
john
Let's say on an activity you have several buttons, all almost doing similar stuff onClick. Now you can use an onClick method, but since you cannot pass parameters in the onClick attribute, you need to put it somewhere else, which is where tag comes in handy.
In your layout you might have:
<Button android:id="#+id/btn1"
android:tag="paramValue1"
android:onClick="myOnClick"/>
<Button android:id="#+id/btn2"
android:tag="paramValue2"
android:onClick="myOnClick"/>
Then you can use one central custom onClickListener (especially if you want to reuse amonst multiple activities) or like in my case just a method in my activity for your buttons that handle the actions for it.
public void myOnClick(View v) {
String param = (String) v.getTag();
....
}
This is especially useful for generic actions, and also if you want to reuse code (i.e. same button listener) amongst multiple classes/activities.
This way you don't rely on a switch/case and checking your button (view) id; staying more independent from your activity itself.

Categories

Resources