How to Bind data to layout objects in Android - android

I want to create an activity that show informations about a deal, i know how to use JSONParser to retrieve date (like texts and images) in Android but i don't know how to attach these data to the layout tha i've created which is a complicated layout

You need to access TextViews ImageViews etc. which you created in your Layout.
To do so you need to do something like this.
For example if this is your layout object.
<TextView android:id="#+id/my_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dynamic content gonna get me."/>
You can access it in your calling activity like this.
TextView textView = (TextView) findViewById(R.id.my_textview);
t.setText("Whatever Dynamic data comes from your source");
Also for ImageView you need to do pretty similar stuff.
Assuming that you have your ImageView declared in your layout.
ImageView img = (ImageView) findViewById(R.id.my_imageview);
img.setImageResource(R.drawable.random_image);

Just adding the answer here as I got confused initially after reading the question.
You can attach custom data to Android views using the setTag method. You can store any Object as a tag. To retrieve data use view.getTag and cast it to your stored data type.

There seems to be a nice binding framework for Android:
http://code.google.com/p/android-binding/wiki/ContactManagerDemo
http://www.codeproject.com/Articles/145203/Android-Binding-Introduction
Going to try it now :)

Related

Best practice reusing components with MVVM

I was wondering if someone found some clever solution to reuse components (multiple views) when working with MVVM.
By component I mean a set of views that end up being reused in an app.
For instance, an empty state formed of an ImageView and a TextView, and let's also add some sort of ClickListener for the text, for the sake of the example.
Now, what I want to do is to reuse this view in multiple .xml files BUT providing different values for the text, the image, and bind the listener to action in the Fragment's ViewModel.
What I've been doing is create a CustomEmptyState that would extend a LinearLayout or some kind of Layout and add Custom Attributes to it.
So, in the end, I would use my custom view like this:
<com.whatever.customViews.CutomEmptyState
app:image="#drawable/someImage"
app:text="#string/empty_text"
app:onTextClicked="#{viewModel.onEmptyStateClicked()}" />
My question would be, is there a different approach to this? A better one? What I dislike about this is writing the custom attributes with <declare-styleable> and all because then I have to keep track of 3 files:
The .xml layout of the base view
The .java/.kt of the view with the boilerplate code to handle the attributes
The <declare-styleable> with all the attributes
Is there any way to combine 2 and 3?
Say, you have to display some text value which you are sure will be databinded.
Then, if you databind the value, then there is a way, but not an elegant way.
declare a variable in the custom view like: private var status = ""
then write a setter function:
fun setStatus(status: String) {
this.status = status
//refresh your views based on value or set this to the text view
}
and then databind like this:
app:status="#{viewModel.status}"
so that you don't need to declare the stylable anymore
<com.whatever.customViews.CutomEmptyState
app:image="#drawable/someImage"
app:text="#string/empty_text"
app:onTextClicked="#{viewModel::onEmptyStateClicked}" />
public void onEmptyStateClicked(View view){
your code
}

Querying root and indirect sibling views in Android ListView

I have a ListView to show a list of articles. Each ListView element is a LinearLayout. On each article, there is a TextView button to edit the title (another TextView). However, the button and the title are not under a direct parent (and actually not at the same level).
A sample structure is shown below:
<LinearLayout>
<LinearLayout
android:id="#+id/title_zone">
<TextView
android:id="#+id/title">
<ImageView
android:id="#+id/icon_popularity">
</LinearLayout><!--end of title_zone-->
<LinearLayout
android:id="#+id/content_zone">
...
</LinearLayout><!--end of content_zone-->
<LinearLayout
android:id="#+id/button_zone">
<LinearLayout
android:id="#+id/author_buttons">
<TextView
android:id="#+id/edit_title_button"
android:onClick="editTitle">
...
</LinearLayout><!--end of author_buttons-->
</LinearLayout><!--end of button_zone-->
</LinearLayout>
I write a SimpleAdapter to apply data to views, so the root LinearLayout will have a tag of the article ID. When editTitle() is called, it needs to find its parent's parent's parent to the root. And after new title is entered, a message will send to server containing new title and the article ID. Also, the title text will be changed visually, which means I need to find the title TextView based on the root.
The problem is that this querying root process is tightly coupled to the UI structure. If I changed the structure in XML, I need to pay attention to change the querying code in Java. (The querying title view is relatively easy, if the root is obtained.)
Is there a more maintainable way to implement my purpose?
OK. A solution that works for my situation is as follows:
In the customized simple adapter for this ListView, set the article ID as the edit_title_button's tag. And set the article ID composed string as the root LinearLayout's tag, e.g., "a_123" if the article ID is 123.
So, when editTitle(View v) is called, we can get the story ID by v.getTag(). Of course, we can also get the ListView by its ID (e.g., ListView list=findViewById(R.id.my_list_view);). Then, we can simply get the root LinearLayout by list.findViewWithTag("a_123").
Since the root LinearLayout is obtained without knowing the UI structure, following codes are easy to maintain.

Better to inflate or instantiate controls in Android?

I'm wondering if anyone can shed some insight as to the best practice for dynamically creating controls (inflate vs instantiate).
Inflate:
TextView styledText = (TextView)inflater.inflate(R.layout.styledTextView);
Instantiate:
TextView styledText = new TextView(mContext);
styledText.setTextAppearance(R.style.StyledTextStyle);
The object being created can either contain attributes in the inflated XML file, or be contained in a Style definition which is added to the instantiated object afterwards. (Assume that this styling includes width, background, text color, etc).
Haven't been able to run any time/memory tests of each method, was wondering if anyone knew which was quickest/most efficient.
LayoutInflator has a slight overhead because it has to parse xml in order to build the object. It also temporarily takes more memory for the same reason. Other than that, it builds the View object in the same manner that you would anyway. It may be something to worry about if you call it hundreds of times a second for some reason. 99.9% of the time though you'll never know the difference.
Also to note, any method that accepts an xml resource like "setTextAppearance" will have the same xml parsing overhead. The only difference in the examples you provided is it's not parsing the TextView xml, but it would still have to parse the style attributes.
Though this post asks about controls specifically, I think it's relevant to note that .. for working with a layout you want to dynamically create/add, I found in using the new (aka instantiate) approach , I was not able to get a reference to an inner ImageButton element that was defined in the xml file for which I instantiate the layout object reference.
When I use the inflate approach, the ImageButton was present upon reference.
So in my case:
Works :)
LayoutInflater inflater = LayoutInflater.from(getActivity());
CardView myCardView = (CardView) inflater.inflate(R.layout.my_cardview, null);
ImageView icon = (ImageView) myCardView.findViewById(R.id.iconId);
~~~~~~~~~~~~
Don't Work :( .. variable icon is null in this case
CardView myCardView = new CardView(getActivity());
ImageView icon = (ImageView) myCardView.findViewById(R.id.iconId);

What's the "Android way" to dynamically create Views with special styles/properties?

As mentioned in topic, I have some Views, e.g. a TableRow with always the same background used as topic, or a special TableRow containing a TextView with some special styles/properties. These Views are set dynamically, so it's problematic to use a XML for this. As I read it's not possible to set styles programmatically too. So what's the best way to solve that?
Possibility 1:
I use and instance derived Views, like this:
public class TopicTableRow extends TableRow {
public TopicTableRow(Context context) {
super(context);
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
setBackgroundColor(Color.parseColor("#777777"));
setClickable(false);
}
}
Possibility 2:
I could create a valid xml template with a special layout I never use in the application, containing the needed Views which have already all assigned styles. Afterward I access the needed Views by R.id....
But this method seems to be very dilettante to me.
I don't think that those 2 possibilities are the "real" Android way to do this, so how is this usually done?
If you want to set specific styles for groups of elements, you can use the themes and styles concepts in android.
You can read up on them here: http://developer.android.com/guide/topics/ui/themes.html
It is not possible though to change the style attribute of a view programatically.
Therefore the android way is probably to create the Views you need in XML and use a LayoutInflater to get create an 'java' version of the xml view. This allows you to reuse the component and fill it with apropriate data for as many rows as you would like.
Button view = (Button) LayoutInflater.from(this).inflate(R.layout.textViewFromWeb, null);
I hope this will be of use to you!

Android TextView and null pointer exception

Why is
TextView test = (TextView) findViewById(R.id.testTextView);
test.getText();
generating a null pointer exception? The id is correct, testTextView is correctly declared in my XML layout file.
The only reason for findViewById to return null if you are passing a valid id is that you are either setting the wrong content view (with setContentView) or not setting a content view at all.
I think you might have written setContentView(..) after defining the TextView. Reverse these, and it should work.
Change:
TextView test = (TextView) findViewById(R.id.testTextView);
.
.
setContetView(..)
To:
setContetView(..)
.
.
TextView test = (TextView) findViewById(R.id.testTextView);
You probably haven't called setContentView. You can only use findViewById to get elements of views that have already been inflated.
You could also use a layoutinflater to inflate the view, but that's probably not what you want.
Are you sure the TextView is set on the right XML?
For example if you're creating a Dialog that loads a custom XML, to get an element from that xml you have to mention it in dialog.findViewById(R.id.testTextView);
Like Falmarri said, the view has to be inflated.
I understand you solved it by creating a new project, but still thought to mention it for future users.
It can also be that you defined the activity in two files. For example layout and layout-v21 and some information like id is missing on one of them. So check all the activity's layouts
In my case, the layout was not finished inflating. Solved by adding a small delay before trying to access the TextView.
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
#Override
public void run() {
TextView test = (TextView) findViewById(R.id.testTextView);
test.getText();
}
}, 100);
I struggled with this for a while and what I realized was, that, if you have more than one layout file version like:
"activity_one.xml" in "layout" folder and one in "layout - small" folder
Which I used for multiple phone layout support, the problem was that one of the TextViews was in both, with the exact same ID and everything, however the difference was that one was higher up in the hierarchy of views in the layout.
When I changed them to be in the same spot it worked.
(I know this is already answered, but maybe this helps someone out there. Very rare though.)

Categories

Resources