How to select ToggleButton with the same id on different layout - android

I have the following hierarchy:
Do you have any idea how to interact with the second ToggleButton - The one that exists under the second LinearLayout(8)?
Please note that the 2 ToggleButtons have the same id/class. The only difference is the text on the buttons.
Thank you for your help.

You can assign a different tag to each ToggleButton so that you can use findViewWithTag()
to access the buttons separately.

It is best to fetch the enclosing element first:
LinearLayout enclosingLayout = (LinearLayout) view.findViewById(R.id.enclosungElement1);
Then find the child view also by using findViewById:
ToggleButton button = (ToggleButton) enclosingLayout.findViewById(R.id.yourToggleButton);

If i follow it correct..
You can use SetTag for the views and find later with fiViewByTag(Tag of the view).
OR
Just find the parent linear layout like findViewById(LinearLayout(8)).
and get the chield as parent.findViewById(toggle Button.)

Related

Adding tags and listeners to the views of a multiple times inflated XML

I am actually developing an android application which needs to inflates many times the same XML layout.
This layout contains 2 buttons, some textViews and a progressBar which I'll need to update later. I would like to add onClick listeners to the buttons and to set custom tags with setTag() to all of these elements, so I will be able to know which button has been clicked and to modify the right textView (or progressBar).
I inflate the XML with this code :
LinearLayout countersList = (LinearLayout)findViewById(R.id.countersLayout);
View child = getLayoutInflater().inflate(R.layout.counter, null);
countersList.addView(child);
How can I access to the right view to set the tag and to add listeners? Is there a better way to do what I want to do ?
Thank you very much !
As far as how to tag or add onClick listeners to your views: You can add an ID to the views you want to tag and find them using findViewById. For example,
LinearLayout countersList = (LinearLayout)findViewById(R.id.countersLayout);
ViewGroup child = (ViewGroup) getLayoutInflater().inflate(R.layout.counter, null);
child.findViewById(R.id.myButton).setTag("myTagForTheButton");
countersList.addView(child);
On the second question, I'm not sure what your UI looks like, but many repeated views might call for using a ListView.
There is no problem of setting tags and listener
LinearLayout countersList = (LinearLayout)findViewById(R.id.countersLayout);
View child = getLayoutInflater().inflate(R.layout.counter, null);
child.setTag("YourString");
// Similarly
view.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
}
});
countersList.addView(child);
If you want it for some of its child you can also do it using findViewById
you can user view.setTag(key, tag) to many tags.
ex:
view.setTag("right_view", rightView);

two views with same id

How does android use R.id.id_name to find views after inflating the XML?
1.Suppose I have two XML's with one button each with same id.
2.I have inflated them and converted them into views
3.In R.id class only one int will be created for both the buttons.
How does android differentiate these buttons with same id Using same Resource name(R.id.id_name) .
The ID is not a unique reference.
However, in practice, you differentiate by using the parent view.
If we consider that 'this' is an Activity, set up with a layout that contains your buttons, then:
Button button = (Button) this.findViewById( R.id.id_name );
will return the first one it finds in the layout (I think - not sure the actual behaviour is defined).
However what you might do is call findViewById on some parent view that contains only one such instance with that ID.
LinearLayout okParent = (LinearLayout) this.findViewById( R.id.okLayout );
LinearLayout cancelParent = (LinearLayout) this.findViewById( R.id.cancelLayout );
Button okButton = (Button) okParent.findViewById( R.id.id_name );
Button cancelButton = (Button) cancelParent.findViewById( R.id.id_name );
Conceptually, this is a sort of path based lookup. You should be careful to design your layouts such that this is possible.
Android takes the easy road: IDs are not application-wide unique, but layout-wide unique.
The value of R.id.foo is the same across two different layouts holding a control with foo id. Since they are not competing for uniqueness, it doesn't matter.
It knows which View should it use, because it looks for View with this specific id in a XML file that is currently set as content view (or is inflated).

Dynamically add layout on button click and get text from the layout?

So I have a layout that gets added to the main layout every time the user presses a button. I got that working fine, but that layout happens to consist of several EditTexts. What would be the best way to get the text from the EditText? I only have the id of the layout itself, not the EditTexts inside the layout.
I thought of just adding EditTexts dynamically one by one, but is there a more efficient way of doing it? I'd much rather just inflate an xml layout every the button is clicked.
I assume you're adding new views by inflating them and then adding them to the main view similar to below.
LinearLayout newView = (LinearLayout)this.getLayoutInflater().inflate(R.layout.my_layout, null);
LinearLayout mainView = (LinearLayout)this.findViewById(R.id.mainLayout);
mainView.addView(newView);
You can use findViewById() on the newView to access each EditText as required.
EditText editText = (EditText) newView.findViewById(R.id.myButton);
String text = editText.getText().toString();
Since it's a fixed layout you inflate from a particular XML file, you can also use getChildAt(int index) to find a particular view of the ViewGroup.

Set the XML file for Layout design according IPhone screen widgets

I want this type of layout how i can do this
all the titles should be clickable
please help me
I used Button and TextView but it seems very dull
Inflate a LinearLayout with a TextView and an image (the right arrow). That LinearLayout must have clickable="true" and onClick="methodYouLike"
To identify what is being clicked you can add a tag to each, with an id, like android:tag="1", android:tag="2" ...
On the Activity onClick receives a View, só you can get that view, an obtain the tag, and do what you need.
If you are not content with the standard UI you can always write your own custom widgets. See here for some more information.
Aso if you need not only 4 items but many, you can use ListView You can provide a layout resource id and it will be atomatically inflated into each row. Datasource could be as DB so array and so on.

How to access Button inside "include" layout

Refer to the doc: http://android-developers.blogspot.com/2009/02/android-layout-tricks-2-reusing-layouts.html
I have a button inside the included layout, how can I access the button? I don't know the id! How can I define the OnClickListener...?
Please help...
The id you have with the include tag is assigned to the root View of the included layout.
First get a reference to that View using findViewByid. Then you can call findViewById on that specific View to get a reference to a View inside the layout. So:
View myLayout = findViewById( R.id.cell1 ); // root View id from that link
View myView = myLayout.findViewById( R.id.someinnerview ); // id of a view contained in the included file
All you need is the id of the parent view:
In this example, the LinearLayout is the parent of the TextView I want from the included View.
I can now do this:
View view = findViewById(R.id.include_title_layout);
TextView titleTV = (TextView) view.findViewById(R.id.newsTitleTextView);
titleTV.setText("Whatever");
You do know the id of the include. You can get that complete element, and then get one of its children starting from there.
Actually include tags include all the elements in your root layout. So you can always access them using findViewById on your Activity.
Suppose you have a alayout.xml in which there is a include layout. Now in one of your activity A,inside onCreate you declared setContentView(R.layout.alayout) Now inside your include layout you might have a button with id myBtn. You can access that inside onCreate in the same way you could access it if it were in main layout: findViewById(R.id.myBtn)
i think your mean is NavigationView. you can access to inner layout by this way:
NavigationView ngv= (NavigationView) findViewById(R.id.navigation_id);
View innerview = ngv.getHeaderView(0);
TextView user_view= (TextView)innerview.findViewById(R.id.nav_name); //any you need
user_view.setText(user);
works for me in java
<include
android:id="#+id/layout_infoProfile"
layout="#layout/user_info_doctor_profile_layout"
/>
preferences = mContext.getSharedPreferences(MyConstant.MY_SHARED_PREF,Context.MODE_PRIVATE);
View view = mView.findViewById(R.id.layout_infoProfile);
((TextView)view.findViewById(R.id.tv_name)).setText(preferences.getString(MyConstant.ROLE,MyConstant.NO_VALUE));

Categories

Resources