Material Design Toolbar to save content

1

I want to generate the following toolbar following the guidelines of Material Design

on the left that shows the X , the title and on the right a button Save

Starting with the template that is autogenerated by Android-Studio, I have the following: Layout.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="com.webserveis.app.servermonitortools.AddServerActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

</android.support.constraint.ConstraintLayout>

Activity.java

public class MainActivity extends AppCompatActivity {

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


        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        toolbar.setTitle(getTitle());

    }
}

AndroidMainifest.xml

    <activity
        android:name=".ItemDetailActivity"
        android:label="@string/title_item_detail"
        android:theme="@style/AppTheme.NoActionBar">
    </activity>

I need to insert the icon X with the action close and go the previous activity, add the text button Save

    
asked by Webserveis 06.02.2018 в 15:37
source

1 answer

3

To change the default icon of toolbar

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    if (getSupportActionBar() != null) {
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close_white_24dp);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }
    toolbar.setTitle(getTitle());

The icon is the same Android-studio, it should only change the color to white

Menu creation

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/action_save"
        android:title="@string/menu_save"
        myapp:showAsAction="ifRoom" />
</menu>

Loading the menu

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_addserver, menu);
    return true;
}

Detect action in the menu

To go back you can use NavUtils.navigateUpFromSameTask(this); as indicated by the official documentation must specify the father in AndroidManifest.xml in activity android:name="android.support.PARENT_ACTIVITY" and android:parentActivityName

<activity
    android:name="com.example.myfirstapp.DisplayMessageActivity"
    android:label="@string/title_activity_display_message"
    android:parentActivityName="com.example.myfirstapp.MainActivity" >
    <!-- Parent activity meta-data to support 4.0 and lower -->
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.myfirstapp.MainActivity" />
</activity>
  

By nature when going back to the activity father recreates himself again   activity, to avoid that, it must be indicated that there can only be one   instance of parent activity android:launchMode="singleTop"

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        case R.id.action_save:
            Toast.makeText(this, "Save action", Toast.LENGTH_SHORT).show();
            return true;
    }
    return super.onOptionsItemSelected(item);
}
    
answered by 06.02.2018 / 16:29
source