How to set Id of dynamic created layout?
You create an ids.xml file and place all your required ids in it as below
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="layout1" />
<item type="id" name="layout2" />
<item type="id" name="layout3" />
</resources>
Now for your dynamically created layouts or views you can use these ids as below
new_layout1.setId(R.id.layout1);
new_view2.setId(R.id.layout2);
new_layout3.setId(R.id.layout3);
I hope it may help you.
Dynamically set id of a view
You can just use the View.setId(int)
for this. According to documentation id
need not to be unique in a tree heirarchy, you can use any (positive) Integer for the Views
you add programmatically.
Doc says
The identifier does not have to be unique in this view's hierarchy. The identifier should be a positive number.
if you want some other info attached with the View
you can tag objects to view using setTag()
, and look by findViewWithTag("obj");
see more about creating ids here https://stackoverflow.com/a/13241629/5235032
How to set id for each item in a view dynamically?
As @Rasoul Miri said, you can use View.generateViewId()
to set id for newly inserted views, and you should use a list to record them. And also you should set the id for those three items.
like these:
for the inserted view
<EditText
android:id="@+id/one_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"/>
<EditText
android:id="@+id/two_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"/>
<EditText
android:id="@+id/three_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"/>
for the MainActivity
public ArrayList<InsertedView> list;
public void addView() {
View new_view = layoutInflater.inflate(R.layout.new_layout,insertPoint, false);
insertPoint.addView(new_view);
index_num++;
InsertedView insertedView = new InsertedView(View.generateViewId(), View.generateViewId(), View.generateViewId());
new_view.findViewById(R.id.one_view).setId(insertedView.firstId);
new_view.findViewById(R.id.two_view).setId(insertedView.secondId);
new_view.findViewById(R.id.three_view).setId(insertedView.thirdId);
list.add(insertedView);
}
class InsertedView {
public int firstId;
public int secondId;
public int thirdId;
public InsertedView(int one, int two, int three) {
firstId = one;
secondId = two;
thirdId = three;
}
}
How to set Id's for list of EditText within the dynamically created TableLayout in android
I followed the below steps to set the Id's for the programmatically created EditText and to retrieve data entered in the edittext.
Step 1: Make global declaration for edittext and int variable as
int idOfEditText;
EditText t5v;
Step 2: Setup the Id for editText as
t5v = new EditText(this);
t5v.setId(Integer.valueOf(j)); //I tried with just j being declared as t5v.setId(j); but it was showing up error with the suggestion as Expected resource of type id in android studio. I have made int variable **j** to loop till list.size()
Step 3: Get the id of the row onclick over the table using
tbrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TableRow tr1=(TableRow)v;
TextView tv1= (TextView)tr1.getChildAt(0);
//Get the integer value of tv1 to make the corresponding edittext to be visible
idOfEditText = Integer.valueOf(tv1.getText().toString());
t5v = (EditText) findViewById(idOfEditText);
}
});
Step 4: The data entered in the EditText can be retrieved by
t5v = (EditText) findViewById(idOfEditText);
String value = t5v.getText().toString();
Toast.makeText(this, "You entered: "+value, Toast.LENGTH_SHORT).show();
Set Id for views dynamically created by a for each loop - android
First View.generateViewId() (API level > 17) will create an unique id for your view.
Then use setId() to set that generated id.
How to assign unique ids to dynamically created Views?
Assign id
via code (programmatically)
- Manually set
id
s usingsomeView.setId(
int);
- The
int
must be positive, but is otherwise arbitrary- it can be whatever you want (keep reading if this is frightful.) - For example, if creating and numbering several views representing items, you could use their item number.
Normally we do:
To assign id -
for(int i =0 ; i < yourIDcount ; i++){
yourView.setId(i);
}
To get id -
View.findViewById(yourView.getId());
Also,
API 17
introduced View.generateViewId()
which generates a unique ID.
Check:
how-can-i-assign-an-id-to-a-view-programmatically and android-assign-and-retrieve-ids-dynamically.
How to get Id of dynamically created buttons in android
You can always do something like this with your view group.
int count = yourLinearLayout.getChildCount();
View v = null;
for(int i=0; i<count; i++) {
v = layout.getChildAt(i);
if(v instanceof Button && v.getTag() == 1/2/3){
v.setBackgroundColor(Color.parseColor("#ff2233"));
}
}
How to set a unique id to dynamically generated views in Kotlin for Android?
You can generate unique id dynamically for your views using View.generateViewId()
function.
For you programmatically created layout or view, you can set id like this
container_overall.id = View.generateViewId()
Related Topics
Android Studio Gradle Configuration with Name 'Default' Not Found
Error: Fix the Version Conflict (Google-Services Plugin)
How to Define a Circle Shape in an Android Xml Drawable File
How to Create Edittext with Rounded Corners
Using the Recyclerview with a Database
How to Create Text File and Insert Data to That File on Android
Error Message 'Java.Net.Socketexception: Socket Failed: Eacces (Permission Denied)'
Android Set Up Volley to Use from Cache
Handle Screen Rotation Without Losing Data - Android
Why My App Is Not Showing Up on Tablets in Google Play
Show an Alert Dialog in Broadcast Receiver After a System Reboot
What Proguard Configuration Do I Need for Firebase on Android
Android Push Notification (Gcm), Is There Any Daily Limit
How to Maintain Multi Layers of Imageviews and Keep Their Aspect Ratio Based on the Largest One
How to Create a User-Defined Function in SQLite
How to Get Higher Precision of "Cpu%" Than That from Top Command