I'm making a View by inflating an xml layout:
LayoutInflater inflater = (LayoutInflater) getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(com.example.R.layout.aaa, null);
in aaa.xml I have a RelativeLayout that itself includes a TextView and a Button. now how can I access to that TextView on view object?
First of all the TextView you are trying to reach must have an id (e.g. #android:id="#+id/yourTxtId"), and then from your rootView, in your case the View you are actually inflating all you have to do is:
View view=inflater.inflate(com.example.R.layout.aaa, null);
TextView txt = (TextView)view.findViewById(R.id.yourTxtId);
//And here you have the reference...
Regards!
Related
I have a dynamic adapter that fills up a listview with textviews with values read from a JSON file, here is an example ( https://imgur.com/a/w9CHzxX ). Now I need to gather the user input from those fields and use it in a later part of my application, but my question is. when i generate textviews like that they all have the same ID right? so how can i specificly gather the user input of lets say the 2nd textview?
Adapter
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.layout_question_textfield, null, true);
---> holder.editText = (AutoCompleteTextView) convertView.findViewById(R.id.edt);
---> holder.editTitel = (Button) convertView.findViewById(R.id.btn);
convertView.setTag(holder);
}
else {...}
holder.editText.setHint(HintArrayList.get(position).getEditTextValue());
ID is an attribute of a view that scope of this attribute is limited by the inflated view group that includes this view.
for example if we have this xml file (item_view.xml):
<LinearLayout
...>
<TextView
...
android:id="#+id/textView_itemVew"/>
</LinearLayout>
and then we inflate this file several times (maybe in an adapter class) as below:
View item1 = inflater.inflate(R.layout.item_view, null,true);
View item2 = inflater.inflate(R.layout.item_view, null,true);
View item3 = inflater.inflate(R.layout.item_view, null,true);
now we can access the TextView view by it's ID but we should use the view group of this TextView to find it.
//Id's are the same but view groups no!
TextView editText_of_item1 = item1.findViewById(R.id.editText_itemVew);.
TextView editText_of_item2 = item2.findViewById(R.id.editText_itemVew);
TextView editText_of_item3 = item3.findViewById(R.id.editText_itemVew);
.
.
.
in your case you can add each EditText to a list to access it after whole list inflated ... like this:
List<EditText> editTextList = new ArrayList<>();
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View itemView = inflater.inflate(R.layout.item_view, null,true);
EditText itemEditText = (EditText) itemView.findViewById(R.id.editText);
editTextList.add(itemEditText);
return itemView;
};
there is useful example in javaTpoint website for you.
I am trying to implement a Newsfeed-type layout with multiple feed items.
The newsfeed item would have a certain layout when collapsed, and this layout would be replaced by an 'exploded' version, when the item is clicked.
I accomplished this by using a ListView of custom items. The custom item XML layout file has a ViewStub which is what I used to change the layout back and forth.
Now, though, I wanted to 'migrate' the layout over to RecyclerView, and to also follow a ViewHolder design pattern.
The latter is what I have tried first, and I'm running into all sorts of problems.
My approach has been as follows:
Get reference to collapsed layout (events_list_item_content) and expanded layout (events_list_item_selected_content);
Get reference to a simple layout resource file to be set as the ViewStub layout (view_stub_layout).
Get ViewStub reference, set its layout (view_stub_layout) inflate, and add the collapsed layout view to this layout (when first creating the feed, all of its items are going to be collapsed).
(After initialisation, when an item is clicked) Remove previous view (layout) from the ViewStubLayout, add the other type of layout.
Here is my custom adapter class:
public class FeedRecyclerAdapter extends BaseAdapter {
public class ViewHolder {
View inflatedViewStub1;
ViewStub viewStub;
LinearLayout viewStubLayout;
LinearLayout listItemContent, listItemContentSelected;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final FeedItem item = feedItems.get(position);
final ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.events_list_item_content_new_container, parent, false);
View view = null;
view = inflater.inflate(R.layout.events_list_item_content, null);
viewHolder.listItemContent = (LinearLayout) view.findViewById(R.id.events_list_item_content);
view = inflater.inflate(R.layout.events_list_item_selected_content, null);
viewHolder.listItemContentSelected = (LinearLayout) view.findViewById(R.id.events_list_item_content_selected);
view = inflater.inflate(R.layout.view_stub_layout, null);
viewHolder.viewStubLayout = (LinearLayout) view.findViewById(R.id.view_stub_layout);
viewHolder.viewStub = (ViewStub) convertView.findViewById(R.id.list_item_feed);
(viewHolder.viewStubLayout).addView(viewHolder.listItemContent);
viewHolder.viewStub.setLayoutResource(R.layout.view_stub_layout);
viewHolder.inflatedViewStub1 = viewHolder.viewStub.inflate();
convertView.setTag(viewHolder);
} else viewHolder = (ViewHolder) convertView.getTag();
if (item.getExploded()) {
viewHolder.viewStubLayout.removeAllViews();
viewHolder.viewStubLayout.addView(viewHolder.listItemContentSelected);
} else {
viewHolder.viewStubLayout.removeAllViews();
viewHolder.viewStubLayout.addView(viewHolder.listItemContent);
}
return convertView;
}
However, when testing, the page where the Newsfeed is supposed to appear is blank.
ViewStub stub = (ViewStub) findViewById(R.id.layout_stub);
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
stub.setLayoutResource(layoutId);
stub.inflate(); // inflate 1st layout
ll.removeAllViews(); // remove previous view, add 2nd layout
ll.addView(LayoutInflater.from(context).inflate(secondLayoutId, ll, false));
Android ViewStub change layouts programatically
I have his following code:
public class MySimpleArrayAdapter extends ArrayAdapter {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.todo_list, parent, false);
TextView titleTextView = (TextView) rowView
.findViewById(R.id.todo_row_title);
TextView dateTextView = (TextView) rowView
.findViewById(R.id.todo_row_date);
ImageView imageView = (ImageView) rowView
.findViewById(R.id.todo_row_image);
titleTextView.setText(mTitles[position]);
..
}
and this content:
mTitles = [עברית, english, עברית]
meaning some strings in hebrew, and some in english
I run this on my physical device.
However in the UI is see only dates strings. How can I enable hebrew viewing?
Other apps on my mobile shows hebrew.
You will need an internationalization library to handle language conversions.
Gettext-Commons is a popular one, there are many others.
https://code.google.com/p/gettext-commons/
Here is an example that demonstrates how easy it is to use the Gettext Commons:
I18n i18n = I18nFactory.getI18n(getClass());
System.out.println(i18n.tr("This text will be translated"));
i'm having two layout files. The first layout file containing some data in textview and the another layout containing one empty textview. Now, i want to pass the first layout's textview data to new layout textview when i'm using button in first layout. How can i use this? Thanks in advance.
Basically you can do like this, in button click,get the view of second layout
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.secondlayout, parent, false);
and use below statement to get id of second textview
TextView text2=(TextView)row.findViewById(R.id.SecondTextView)
then get the text in the first textview using id of first one into String:
String s=text1.getText().toString();
then set this String to 2nd textview.
text2.setText(s);
If, textview2 is the textview from second layout. and the
//to get data from text view of first layout use the following code
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.layout1, null);
TextView tv1 = (TextView) vi.findViewById(R.id.textview1);
// to set of first textview in second textview
textview2.setText(tv1.getText().toString
i came across this
http://www.brighthub.com/mobile/google-android/articles/48845.aspx
i understand i can create a view. But i want to define a layout xml and put that into my view. for example in layout i will have a few textviews, i will set value to them dynamically and add them to view and display. is this possible if yes how ? if no whats the alternative ?
Look at LayoutInflater
Sample:
LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout dialerLayout = (LinearLayout) layoutInflater.inflate(R.layout.phone_dialer, null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
dialerLayout.setLayoutParams(params);
LinearLayout tabDialer = (LinearLayout) findViewById(R.id.tabDialer);
tabDialer.addView(dialerLayout);