kcc__
kcc__

Reputation: 1648

Programmatic Toolbar

In my Android assignment we have to create a simple App using only Java and not design view. I want to add icons and text to the toolbar. I can get it to work using design view but I cannot get it to work using Java.

Please advice me if my idea is right. For example to add a button to the toolbar, I create a

android.widget.Button button = new android.widget.Button()

and then add the button to the Toolbar.

My onCreate() function is as follows:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    relative_layout = new RelativeLayout(this);
    relative_layout.setBackgroundColor(Color.WHITE);
    Toolbar tool_bar = new Toolbar(this);
    tool_bar.setTitle("Hello");
    tool_bar.setBackgroundColor(Color.RED);  // actual one I read from R.color.xyz
    tool_bar.setTitleTextColor(Color.WHITE);
    tool_bar.setId(1);

    // tool_bar.setNavigationIcon(R.drawable.ic_launcher_foreground);

    tool_bar.setPopupTheme(R.style.Theme_AppCompat_Dialog);
    this.setVisible(true);
    this.setSupportActionBar(tool_bar);

    RelativeLayout.LayoutParams toolbar_layout = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    toolbar_layout.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    toolbar_layout.addRule(RelativeLayout.CENTER_HORIZONTAL);

    Button button = new Button(this);
    button.setBackgroundColor(Color.GREEN);
    button.setText("Button");
    button.setId(2);

    RelativeLayout.LayoutParams button_layout = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    button_layout.addRule(RelativeLayout.CENTER_HORIZONTAL);
    button_layout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    // button_layout.setMargins(400, 0, 0, 0);

    tool_bar.addView(button, button_layout);

    relative_layout.addView(tool_bar, toolbar_layout);
    this.setContentView(relative_layout);

}

1) I would like to confirm is this is correct way of doing this?

2) I cannot move the button to the right of the screen as shown in the picture, although I used

RelativeLayout.ALIGN_PARENT_RIGHT

How can we set the alignment to the right?

enter image description here

Upvotes: 0

Views: 64

Answers (2)

Abhishek kumar
Abhishek kumar

Reputation: 4445

Your Toolbar will look like this after adding this code :

enter image description here

Use this code to crate your toolbar activity.xml Code :

  <android.support.v7.widget.Toolbar
    android:id="@+id/toolbarAdjustScan"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:background="@color/colorPrimary"
    android:elevation="6dp"
    android:minHeight="56dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/titleToolbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="15dp"
            android:ellipsize="end"
            android:layout_weight="1"
            android:maxLines="1"
            android:textColor="@color/white"
            android:textSize="18dp" />

          <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:padding="10dp"
            android:id="@+id/searchAdjustBtn"
            android:layout_marginLeft="15dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="SEARCH   "
            android:textColor="@color/white"
            android:textSize="13dp" />
    </LinearLayout>

</android.support.v7.widget.Toolbar>

Activity.java Code :

Toolbar toolbarTop;

Call setToolbar() inside from your onCreate()

 private void setToolbar() {
    toolbarTop = (Toolbar) findViewById(R.id.toolbarAdjustScan);
    setSupportActionBar(toolbarTop);
    TextView search = (TextView)toolbarTop.findViewById(R.id.searchAdjustBtn);
    TextView titleToolbar = (TextView)toolbarTop.findViewById(R.id.titleToolbar);
    titleToolbar.setText("TakeInventory");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    getSupportActionBar().setElevation(0);

    search.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), TakeInventoryResult.class);
           // Bundle args = new Bundle();
           //  args.putSerializable("ARRAYLIST",(Serializable)inList);
           // intent.putExtra("BUNDLE",args);
            startActivity(intent);
            finish();
        }
    });

}

Upvotes: 1

iamgopal
iamgopal

Reputation: 644

The toolbar is just a ViewGroup. So just as you add views to any ViewGroup programmatically, do the same treatment for the toolbar.

Button bt = new Button(this);
bt.setText("Button");
LinearLayout.LayoutParams params = new 
LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
LayoutParams.FILL_PARENT);
params.gravity = Gravity.RIGHT;
button.setLayoutParams(params);
toolbar.addView(bt);

Happy to help :)

Upvotes: 1

Related Questions