Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Android: View items by name Tag

26.09.2018 (👁212)


 

 

How do you find display elements such as buttons in an Android App by name?

 

Solution:

For this you use View.findViewById ("name_of_Element")

In Android, there is the assignment of ID and tag to a view element at runtime.

 

However, if you look for a dynamically created element such as text entry EditView or a button at runtime, then you usually can not do anything with the current ID number.

That's why you often search for the name like "Button_Raimund" or "Input_2018_10_01".

For this one uses getViewbyTag

You can only apply findViewWithTag to views. That's why you first look for the layout of the page

final LinearLayout root_Panel= findViewById(R.id.root_Layout);

 

Then at runtime elements are created which one allocates a day. 

like here buttons

Button button1=new Button(this);
button1.setText(
"1: YELLOW");
button1.setTag(
"Button1");  //*=name

 

 

Later, you can search the view element and evaluate or modify it like an object at runtime.

Button btn = root_Panel.findViewWithTag("Button2");
String sCaption=btn.getText().toString();

 

 

 

example

Button 2 searches for button 1 and evaluates the headline.

 

package com.codedocu.myapplication;

import android.app.AlertDialog;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity  {
   
//----------------------< Activity >----------------------

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        create_Buttons();
    }


   
public void create_Buttons()
    {
        
//------------< create_Buttons() >------------
        //< get_Layout >
       
final LinearLayout root_Panel= findViewById(R.id.root_Layout);
       
//< get_Layout >

        //--< Element1 >--
       
Button button1=new Button(this);
        button1.setText(
"1: YELLOW");
        button1.setTag(
"Button1");  //*=name
       
button1.setBackgroundColor(Color.YELLOW);
        button1.setOnClickListener(
on_Button_clicked);

       
//< add >
       
root_Panel.addView(button1);
       
//</ add >
        //--</ Element1 >--

        //--< Element2 >--
        //*Inline-Event
       
Button button2=new Button(this);
        button2.setText(
"2: RED");
        button2.setTag(
"Button2");
        button2.setBackgroundColor(Color.
RED);
        button2.setOnClickListener(
new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
                Button btn =
root_Panel.findViewWithTag("Button1");
                String sCaption=btn.getText().toString();
                show_Messagebox(
"2->1 found =" + sCaption  , "inline-Event Button2");
            }
        });

       
//< add >
       
root_Panel.addView(button2);
       
//</ add >
        //--</ Element1 >--


        //------------</ create_Buttons() >------------
   
}


   
//===================< region: events >===================
   
View.OnClickListener on_Button_clicked = new View.OnClickListener() {
       
public void onClick(View v) {
           
final LinearLayout root_Panel= findViewById(R.id.root_Layout);
            Button btn = root_Panel.findViewWithTag(
"Button2");
            String sCaption=btn.getText().toString();
            show_Messagebox(
"1->2 found button2:=" + sCaption  , "onButton1_click");
        }
    };
   
//===================</ region: events >===================


    //============< region: system >==============
   
public void show_Messagebox(CharSequence  Message_Text, CharSequence Title) {
       
//--------------< fp_Messagebox() >----------------
        //< Message erstellen >
       
AlertDialog.Builder msg = new AlertDialog.Builder(this);
        msg.setTitle(Title);
        msg.setMessage(Message_Text);
        msg.setCancelable(
true);
       
//</ Message erstellen >

        //< anzeigen >
       
AlertDialog msgDialog = msg.create();
        msgDialog.show();
       
//</ anzeigen >
        //--------------</ fp_Messagebox() >----------------
   
}
   
//=============</ region: system >=============
    //===================</ Activity >======================
}

 

 

 

 

Activity_Main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
android:id="@+id/root_Layout"
   
android:layout_height="match_parent"
   
android:layout_width="match_parent"
   
android:orientation="vertical"
   
>

</
LinearLayout>