-1

I am extremely new to Android Studio, and as a learning exercise, I am trying to create an application without using any xml files other than manifest. I want to make an activity, layout, toolbar, and drawer menu completely within Java. I know it is not how things are normally done, and I am likely causing myself a lot of unnecessary stress, but, as I said, this is a learning exercise. Everything seems to execute, but the toolbar does not display. Also, getSupportActionBar().getHeight() returns zero.

Any help is appreciated!

Thanks, John

Here is my Activity.java

package com.example.john.myblankapplication;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.design.widget.Snackbar;

import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by John on 7/3/2016.
 */

public class MyActivity extends AppCompatActivity {
    private RelativeLayout myRelativeLayout;
    private LinearLayout myLinearLayout;
    private Toolbar myToolbar;
    private TextView myTextView;
    private ActionBar myActionBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Create the main layout programmatically
        createRelativeLayout();
        createLinearLayout();

        // I have tried it with LinearLayout and RelativeLayout
        setContentView(myLinearLayout);

        // Create the toolbar layout programmatically
        createToolbarLayout();

        //myLinearLayout.addView(myToolbar); // that turned the whole screen red

        setSupportActionBar(myToolbar);
        myActionBar = getSupportActionBar();
        myActionBar.setDisplayHomeAsUpEnabled(true); // new test - didn't help

        // Create the text programmatically
        myLinearLayout.addView(createTextView());

        Snackbar.make(myTextView, "ActionBarHeight="+myActionBar.getHeight(),
                Snackbar.LENGTH_LONG)
                .setAction("Action", null).show(); // displays 0
    }

    private void createRelativeLayout() {
        myRelativeLayout = new RelativeLayout(this);

        // Specifies the layout properties
        RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT
        );
        myRelativeLayout.setLayoutParams(relativeParams);
    }

    private void createLinearLayout() {
        myLinearLayout = new LinearLayout(this);

        // Specifies the layout properties
        LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT
        );
        myLinearLayout.setLayoutParams(linearParams);
    }

    private Toolbar createToolbarLayout() {
        myToolbar = new Toolbar(this);
        Toolbar.LayoutParams toolBarParams = new Toolbar.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                R.attr.actionBarSize, Gravity.TOP
        );
        myToolbar.setTitle("My Toolbar");
        myToolbar.setLayoutParams(toolBarParams);
        myToolbar.setBackgroundColor(Color.RED);
        myToolbar.setVisibility(View.VISIBLE);
        return myToolbar;
    }

    private TextView createTextView() {
        myTextView = new TextView(this);

        // Set initial layout parameters
        RelativeLayout.LayoutParams textViewParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT
        );

        // Set alignment parameters
        //textViewParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        textViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

        myTextView.setText("Here is some text");

        myTextView.setLayoutParams(textViewParams);

        return myTextView;
    }

}
John Ureke
  • 19
  • 5
  • Well, for starters, the type of `LayoutParams` a `View` should have depends on its parent `View`. E.g., your `Toolbar` should have `LinearLayout.LayoutParams` set on it. Next, `R.attr.actionBarSize` is not the actual height value. Have a look at [this post](http://stackoverflow.com/questions/12301510/how-to-get-the-actionbar-height). – Mike M. Jul 04 '16 at 02:27
  • Works great. Thanks. – John Ureke Jul 05 '16 at 23:03

2 Answers2

0

I would recommend making the toolbar a custom view and just set the dimensions in the layout screen. You can declare the custom view as a toolbar if you import the toolbar package. Then you can use all the toolbar commands built in to add a title and items.

Hope this helps!

Kelton_O
  • 158
  • 14
0

You also need to add toolbar to linear layout.

     // Create the toolbar layout programmatically

myLinearLayout.addView(// Create the toolbar layout programmatically
        createToolbarLayout(););
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154