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 Toolbar: Get Set Elements on the Toolbar

26.09.2018 (👁203)

Android Toolbar

 

View Accept inputs from Toolbar

 

How can you take view elements like an input field from the toolbar?

View elements are easily found via the findViewById (input_textbox_example).

    public void btn_Clicked(View view) {
       
//------------< btn_Clicked() >------------
        //< get_Toolbar_Edit >
       
EditText tbx=findViewById(R.id.input_textbox);
        String sText=tbx.getText().toString();
       
//</ get_Toolbar_Edit >

        //< output >
       
TextView lbl=findViewById(R.id.lblTarget);
        lbl.setText(sText);
       
//</ output >
        //------------</ btn_Clicked() >------------
   
}

 

This allows you to take settings from the toolbar into the Acitivty form

 

 

Example: Java code

package com.codedocu.demo06_toolbar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toolbar;

public class MainActivity extends AppCompatActivity {

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

   
public void btn_Clicked(View view) {
       
//------------< btn_Clicked() >------------
        //< get_Toolbar_Edit >
       
EditText tbx=findViewById(R.id.input_textbox);
        String sText=tbx.getText().toString();
       
//</ get_Toolbar_Edit >

        //< output >
       
TextView lbl=findViewById(R.id.lblTarget);
        lbl.setText(sText);
       
//</ output >
        //------------</ btn_Clicked() >------------
   
}

   
public void btnToolbar_onClick(View view) {
       
//------------< btnToolbar_onClick() >------------
        //* show or hide toolbar
        //< get toolbar >
       
Toolbar toolbar = findViewById(R.id.toolbar2);
       
//</ get toolbar >

        //< show/hide >
       
if(toolbar.getVisibility()==View.VISIBLE)
        {
            toolbar.setVisibility(View.
INVISIBLE);
        }
       
else
       
{
            toolbar.setVisibility(View.
VISIBLE);
        }
       
//</ show/hide >
        //------------</ btnToolbar_onClick() >------------
   
}
}

 

 

 

 

Activity_Main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
tools:context=".MainActivity">


    <
Toolbar
       
android:id="@+id/toolbar2"
       
android:layout_width="368dp"
       
android:layout_height="wrap_content"
       
android:layout_marginStart="8dp"
       
android:layout_marginTop="8dp"
       
android:layout_marginEnd="8dp"
       
android:background="?attr/colorPrimary"
       
android:clickable="true"
       
android:elevation="9dp"
       
android:minHeight="?attr/actionBarSize"
       
android:theme="?attr/actionBarTheme"
       
android:visibility="visible"
        
app:layout_constraintEnd_toEndOf="parent"
       
app:layout_constraintStart_toStartOf="parent"
       
app:layout_constraintTop_toTopOf="parent">


        <
EditText
           
android:id="@+id/input_textbox"
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:ems="10"
           
android:inputType="textPersonName"
           
android:text="Name"
           
tools:layout_editor_absoluteX="76dp"
           
tools:layout_editor_absoluteY="124dp" />

        <
Button
           
android:id="@+id/button"
           
android:layout_width="81dp"
           
android:layout_height="55dp"
           
android:layout_marginStart="16dp"
           
android:onClick="btn_Clicked"
           
android:text="Test"
           
app:layout_constraintStart_toStartOf="parent"
           
tools:layout_editor_absoluteY="448dp" />

    </
Toolbar>

    <
TextView
       
android:id="@+id/lblTarget"
       
android:layout_width="120dp"
       
android:layout_height="101dp"
       
android:layout_marginStart="84dp"
       
android:layout_marginTop="84dp"

       
android:text="empty start"
       
app:layout_constraintStart_toStartOf="parent"
       
app:layout_constraintTop_toTopOf="parent" />

    <
ImageButton
        
android:id="@+id/imageButton2"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_marginTop="76dp"
       
android:layout_marginEnd="8dp"
       
app:layout_constraintEnd_toEndOf="parent"
        
app:layout_constraintTop_toTopOf="@+id/toolbar2"
       
app:srcCompat="@android:drawable/ic_menu_manage"
       
android:onClick="btnToolbar_onClick"
       
/>

</
android.support.constraint.ConstraintLayout>