Android ImageButton does not display image - java

I am new to java and I am developing a simple mediaplayer: I have problems with two image buttons: when i run the app the relative images are not displayed. This is my code:
JAVA:
package lukes.mediaplayer;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.app.Activity;
public class alpha extends Activity implements View.OnClickListener {
SeekBar seekBar_alpha;
ImageButton btPlay_alpha;
ImageButton btStop_alpha;
MediaPlayer mp;
Handler seekHandler = new Handler();
ImageView display;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alpha);
display = (ImageView) findViewById(R.id.imagetop);
display.setOnClickListener(this);
getInit();
seekUpdation();
}
public void getInit() {
seekBar_alpha = (SeekBar) findViewById(R.id.seekBar_alpha);
btPlay_alpha = (ImageButton) findViewById(R.id.btPlay_alpha);
btStop_alpha = (ImageButton) findViewById(R.id.btStop_alpha);
btPlay_alpha.setOnClickListener(this);
btStop_alpha.setOnClickListener(this);
mp = MediaPlayer.create(this, R.raw.alpha_audio);
seekBar_alpha.setMax(mp.getDuration());
}
Runnable run = new Runnable() {
#Override
public void run() {
seekUpdation();
}
};
public void seekUpdation() {
seekBar_alpha.setProgress(mp.getCurrentPosition());
seekHandler.postDelayed(run, 1000);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.imagetop:
Intent intent = new Intent (alpha.this, alpha_img.class);
startActivity(intent);
case R.id.btPlay_alpha:
mp.start();
break;
case R.id.btStop_alpha:
mp.pause();
}
}
#Override
protected void onPause(){
super.onPause();
if (mp!=null && mp.isPlaying()){
mp.pause();
}
}
}
The "btPlay_alpha" and "btStop_alpha" image buttons does not display their image. Can you help me to fix that? Thank you!
Hey guys, I add the XML code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
android:fitsSystemWindows="true"
tools:context=".alpha">
<android.support.design.widget.AppBarLayout
android:id="#+id/main.appbar"
android:layout_width="match_parent"
android:layout_height="350dp"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/main.collapsing"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="#+id/main.toolbar"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<ImageButton
android:id="#+id/imagetop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="#drawable/alpha"
app:layout_collapseMode="parallax" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed"
android:lineSpacingExtra="8dp"
android:padding="16dp"
android:text="#string/alpha_title"
android:textColor="#color/colorPrimary"
android:textSize="24sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<ImageButton
android:id="#+id/btStop_alpha"
android:layout_width="60dp"
android:layout_height="55dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:backgroundTint="#color/lightRed"
android:elevation="6dp"
android:padding="16dp"
app:srcCompat="#android:drawable/ic_media_pause" />
<ImageButton
android:id="#+id/btPlay_alpha"
android:layout_width="60dp"
android:layout_height="55dp"
android:layout_alignParentTop="true"
android:layout_gravity="center"
android:layout_toEndOf="#+id/btStop_alpha"
android:layout_toRightOf="#+id/btStop_alpha"
android:backgroundTint="#color/lightRed"
android:elevation="6dp"
app:srcCompat="#android:drawable/ic_media_play" />
<SeekBar
android:id="#+id/seekBar_alpha"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/btPlay_alpha"
android:layout_toRightOf="#+id/btPlay_alpha"
android:max="100" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="serif"
android:lineSpacingExtra="8dp"
android:padding="16dp"
android:text="#string/alpha_text"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
Thank you for your support!

I see youre extending Activity Instead of AppCompatActivity. In your xml, you are setting attribute srcCompat; make sure As of Android Support Library 23.3.0, support vector drawables can only be loaded via app:srcCompat .
you need to add vectorDrawables.useSupportLibrary = true to your build.gradle file
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}

Related

Radio button as tabs highlight when selected (Android Studio)

I am trying to create radio buttons that allows the users to select one of the three options. Once selected, the option should have a border, while the others do not.
Here is an image of what I am trying to create:
I'm not sure how to go about this. Any suggestions are greatly appreciated.
You could create a group of objects (which are the options) and programmatically ensure that only one of them can be selected.
selection could be presented as you wish it would be, I'd give a code example that imitates approximately the image you've attached. here's the structure:
XML file: CardViews with base of invisible FrameLayout (will be functioning as stroke), above the base is the cards' content
Java Class: setChecked(MaterialCardView selected) method that selects the chosen card (making it's 'stroke' visible) and checkedCard() that returns the selected card
so here's the code:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="#color/parentBackground"
tools:context=".MainActivity">
<com.google.android.material.card.MaterialCardView
android:id="#+id/public_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardBackgroundColor="#color/cardBackground"
app:cardCornerRadius="5dp">
<FrameLayout
android:id="#+id/public_stroke"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/selectedCardStroke"
android:visibility="visible" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#color/cardBackground">
<com.github.abdularis.civ.CircleImageView
android:id="#+id/card1Icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerVertical="true"
android:layout_margin="20dp"
android:src="#mipmap/ic_public" />
<TextView
android:id="#+id/card1Label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/card1Icon"
android:text="Public Profile"
android:textAllCaps="false"
android:textColor="#color/cardLabelColor"
android:textSize="35sp" />
<TextView
android:id="#+id/card1Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/card1Label"
android:layout_alignStart="#id/card1Label"
android:text="Everyone can see your account. \nIncluding non followers."
android:textAllCaps="false"
android:textColor="#color/cardTextColor"
android:textSize="15sp" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="#+id/friends_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/public_card"
android:layout_margin="10dp"
app:cardBackgroundColor="#color/cardBackground"
app:cardCornerRadius="5dp">
<FrameLayout
android:id="#+id/friends_stroke"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/selectedCardStroke"
android:visibility="invisible" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#color/cardBackground">
<com.github.abdularis.civ.CircleImageView
android:id="#+id/card2Icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerVertical="true"
android:layout_margin="20dp"
android:src="#mipmap/ic_friends" />
<TextView
android:id="#+id/card2Label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/card2Icon"
android:text="Friends Only"
android:textAllCaps="false"
android:textColor="#color/cardLabelColor"
android:textSize="35sp" />
<TextView
android:id="#+id/card2Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/card2Label"
android:layout_alignStart="#id/card2Label"
android:text="Connections nly. Only your friends \nand friends of friends can see your account."
android:textAllCaps="false"
android:textColor="#color/cardTextColor"
android:textSize="15sp" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="#+id/private_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/friends_card"
android:layout_margin="10dp"
app:cardBackgroundColor="#color/cardBackground"
app:cardCornerRadius="5dp">
<FrameLayout
android:id="#+id/private_stroke"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/selectedCardStroke"
android:visibility="invisible" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#color/cardBackground">
<com.github.abdularis.civ.CircleImageView
android:id="#+id/card3Icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerVertical="true"
android:layout_margin="20dp"
android:src="#mipmap/ic_private" />
<TextView
android:id="#+id/card3Label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/card3Icon"
android:text="Private"
android:textAllCaps="false"
android:textColor="#color/cardLabelColor"
android:textSize="35sp" />
<TextView
android:id="#+id/card3Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/card3Label"
android:layout_alignStart="#id/card3Label"
android:text="Only you can see your account."
android:textAllCaps="false"
android:textColor="#color/cardTextColor"
android:textSize="15sp" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
</RelativeLayout>
MainActivity.java:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import com.google.android.material.card.MaterialCardView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
public MaterialCardView cPublic, cFriends, cPrivate;
public FrameLayout sPublic, sFriends, sPrivate;
public ArrayList<MaterialCardView> cards;
public ArrayList<FrameLayout> strokes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cards = new ArrayList<>();
strokes = new ArrayList<>();
cPublic = findViewById(R.id.public_card);
cFriends = findViewById(R.id.friends_card);
cPrivate = findViewById(R.id.private_card);
cards.add(cPublic);
cards.add(cFriends);
cards.add(cPrivate);
sPublic = findViewById(R.id.public_stroke);
sFriends = findViewById(R.id.friends_stroke);
sPrivate = findViewById(R.id.private_stroke);
strokes.add(sPublic);
strokes.add(sFriends);
strokes.add(sPrivate);
cPublic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setChecked(cPublic);
}
});
cFriends.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setChecked(cFriends);
}
});
cPrivate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setChecked(cPrivate);
}
});
}
public void setChecked(MaterialCardView selected) {
int index = cards.indexOf(selected);
FrameLayout stroke = strokes.get(index);
stroke.setVisibility(View.VISIBLE);
for (FrameLayout s : strokes) {
if (!s.equals(stroke)) {
s.setVisibility(View.INVISIBLE);
}
}
}
public MaterialCardView checkedCard() {
int index = 0;
for (FrameLayout s : strokes) {
if (s.getVisibility() == View.VISIBLE) {
index = strokes.indexOf(s);
}
}
return cards.get(index);
}
}
Hope it could help you (:

JSON text not autosizing in textview (uniformly in horizontal and vertical direction)

I'm trying to fetch text from a JSON file uploaded in my Firebase database, but when the text lines are fetched, they aren't auto-sized (wrt x and y-axis), although I used autoSizeTextType = uniform along with added parameters. Blank spaces are still present on the right side (as one can see from the snapshots). I've used 3rd party Gradle dependencies, but, they also don't work out well. So, pls help me out and tell me solution based on my code.
Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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=".CourseDetail">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing"
android:layout_width="match_parent"
android:layout_height="350dp"
android:fitsSystemWindows="true"
app:contentScrim="#262628"
app:expandedTitleTextAppearance="#android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="#+id/img_course"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#null"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:layout_collapseMode="parallax"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:title="Course Name"
app:titleTextColor="#ffffff" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="#+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="1dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:padding="12dp"
android:text="DESCRIPTION"
android:textColor="#262628"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="1dp"
app:cardUseCompatPadding="true">
<TextView
android:id="#+id/course_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:autoSizeTextType="uniform"
android:fitsSystemWindows="true"
android:lineSpacingMultiplier="1.5"
android:text="Description "
android:textColor="#android:color/black"
android:autoSizeMinTextSize="15sp"
android:autoSizeMaxTextSize="20sp"
android:autoSizeStepGranularity="1sp"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="1dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:padding="12dp"
android:text="DEGREE INFROMATION"
android:textColor="#262628"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="1dp"
app:cardUseCompatPadding="true">
<TextView
android:id="#+id/degree_information"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:lineSpacingMultiplier="1.5"
android:padding="12dp"
android:text="Description"
android:textColor="#android:color/black"
android:textSize="14sp" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="1dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:padding="12dp"
android:text="JOB SCOPE"
android:textColor="#262628"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="1dp"
app:cardUseCompatPadding="true">
<TextView
android:id="#+id/job_scope"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:lineSpacingMultiplier="1.5"
android:padding="12dp"
android:text="Description"
android:textColor="#android:color/black"
android:textSize="14sp" />
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
Activity.java
package com.example.shubhojit.careersafter10th;
import android.support.annotation.NonNull;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.shubhojit.careersafter10th.Model.Courses_After10th;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;
public class CourseDetail extends AppCompatActivity {
TextView course_description,deg_info,job_scope;
ImageView courseImage;
CollapsingToolbarLayout collapsingToolbarLayout;
String courseId="";
FirebaseDatabase database;
DatabaseReference coursedetail;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course_detail);
database = FirebaseDatabase.getInstance();
coursedetail = database.getReference("Courses_After10th");
course_description = (TextView)findViewById(R.id.course_description);
deg_info = (TextView)findViewById(R.id.degree_information);
job_scope = (TextView)findViewById(R.id.job_scope);
courseImage = (ImageView)findViewById(R.id.img_course);
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing);
collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppbar);
collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapsedAppbar);
if(getIntent() != null)
courseId = getIntent().getStringExtra("CourseId");
if(!courseId.isEmpty())
{
getDetailCourse(courseId);
}
}
private void getDetailCourse(String courseId) {
coursedetail.child(courseId).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
Courses_After10th course = dataSnapshot.getValue(Courses_After10th.class);
Picasso.with(getBaseContext()).load(course.getImage()).into(courseImage);
collapsingToolbarLayout.setTitle(course.getName());
deg_info.setText(course.getDegree_Information());
job_scope.setText(course.getJob_Scope());
course_description.setSingleLine(false);
course_description.setText(course.getDescription());
course_description.setEllipsize(TextUtils.TruncateAt.END);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
TextView does not provide Justify kinda alignment available in Word kinda tools.
To Justify text, instead use WebView with HTML content. Convert your text into HTML tags with proper alignment and styling. And then load the content in WebView as HTML.

Managing onClickListeners in more then one xml file

I created an app that adds forms dynamically when a user clicks the add butting and deletes it when he clicks the cancel button. I have two cancel buttons, one in the Field.xml file and the other in the activity_school_search_setup file.
They both have the same ID but the one in the Field.xml file does not delete the field. It appears as though the onclicklistener does not function for the delete button.
Main Java class file
import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.view.View;
import android.widget.Toast;
public class SchoolSearchSetup extends AppCompatActivity implements View.OnClickListener {
private EditText searchSchoolID;
private Button searchSchoolButtonID;
private ListView listOfSchoolsID;
private Button openNewschoolID;
private LinearLayout schoolSetupLayout;
private Button addNewClass;
private EditText classNameEditText;
private Button deleteButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_school_search_setup);
findAllViewsID();
initializeListenners();
}
public void findAllViewsID(){
classNameEditText = findViewById(R.id.classNameText);
addNewClass = findViewById(R.id.addNewClassButton);
schoolSetupLayout = findViewById(R.id.schoolSetupLayout);
searchSchoolID = findViewById(R.id.searchSchoolID);
searchSchoolButtonID = findViewById(R.id.searchSchoolButtonID);
listOfSchoolsID = findViewById(R.id.listOfSchoolsID);
openNewschoolID = findViewById(R.id.openNewschoolID);
deleteButton = findViewById(R.id.delete_button);
}
public void initializeListenners(){
openNewschoolID.setOnClickListener(SchoolSearchSetup.this);
addNewClass.setOnClickListener(SchoolSearchSetup.this);
deleteButton.setOnClickListener(SchoolSearchSetup.this);
}
public void addNewClass(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.field, null);
schoolSetupLayout.addView(rowView, schoolSetupLayout.getChildCount() -1);
}
#Override
public void onClick(View view) {
switch(view.getId()){
case R.id.openNewschoolID:
displaySchoolSetUpForms();
break;
case R.id.addNewClassButton:
addNewClass();
break;
case R.id.delete_button:
schoolSetupLayout.removeView((View) view.getParent());
}
}
private void displaySchoolSetUpForms() {
schoolSetupLayout.setVisibility(View.VISIBLE);
}
}
Here is the main XML activity file
activity.school_search_setup.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
tools:context="com.example.demeainc.demea.MainActivity"
android:orientation="vertical"
android:background="#color/backgroundColor">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<ImageView
android:id="#+id/backArrowClassView"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/ic_arrow_class"
android:layout_marginLeft="5dp"/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="50dp"
android:text="Search your school."
android:gravity="center"
android:textSize="25dp" />
<android.support.v7.widget.CardView
android:foreground="?attr/selectableItemBackground"
android:clickable="true"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp">
<EditText
android:id="#+id/searchSchoolID"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableLeft="#drawable/ic_search_black_24dp"
android:hint="Search"/>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp">
<Button
android:id="#+id/searchSchoolButtonID"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_marginLeft="60dp"
android:text="Search"
android:textColor="#ffff"
android:background="#color/colorPrimary"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp">
<android.support.v7.widget.CardView
android:layout_width="300dp"
android:layout_height="wrap_content">
<ListView
android:id="#+id/listOfSchoolsID"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#android:color/transparent"
android:cacheColorHint="#color/ligtherDarkGrey"
android:divider="#CCCCCC"
android:dividerHeight="2dp"
android:paddingLeft="2dp" >
</ListView>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<Button
android:id="#+id/openNewschoolID"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_marginLeft="60dp"
android:background="#color/colorPrimary"
android:text="Open New School"
android:textColor="#ffff"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/schoolSetupLayout"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<TextView
android:id="#+id/schoolSetupText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="School Setup"
android:textSize="20dp"
android:layout_marginLeft="50dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/classNameText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:hint="Class name, e.g Grade one" />
<Button
android:id="#+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#android:drawable/ic_delete"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:id="#+id/addNewClassButton"
android:layout_marginTop="15dp"
android:layout_marginLeft="50dp"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Add new class"
android:textColor="#ffff"
android:background="#color/colorPrimary"/>
<Button
android:id="#+id/nextButton"
android:layout_marginTop="25dp"
android:layout_marginLeft="8dp"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Next"
android:textColor="#ffff"
android:background="#color/green"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Here is the field.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/schoolSetupLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/classNameText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:hint="Class name, e.g Grade one" />
<Button
android:id="#+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#android:drawable/ic_delete"/>
</LinearLayout>
</LinearLayout>
You either should add a clickListener on
View rowView = inflater.inflate(R.layout.field, null);
Or add android:onClick within an element of field.xml if you want some click event to happen on it.
initializeListenners(); is only called once, during the initial layout, not on dynamic view additions.
Implement OnClickListener for Field.xml button after you inflate that layout.
Here is example:
public void addNewClass(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.field, null);
final Button btnDelete = rowView.findViewById(R.id.delete_button);
btnDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Your stuff
}
});
schoolSetupLayout.addView(rowView, schoolSetupLayout.getChildCount() -1);
}
Hope that help :)

Dynamically generated delete button does not respond to an OnclickListener(Android JAVA)

I created an app that dynamically adds an edittext field to the view by clicking an Add button and deletes it by clicking a delete button. I created a separate xml file called field.xml that is called up when a new edittext field is added to the view. But the delete button does not remove this edittext field when clicked. What am I missing in the code I have tried most options but to no avail.
Here is the main java file
SchoolSearchSetup.java
import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.view.View;
import android.widget.Toast;
public class SchoolSearchSetup extends AppCompatActivity implements View.OnClickListener {
private EditText searchSchoolID;
private Button searchSchoolButtonID;
private ListView listOfSchoolsID;
private Button openNewschoolID;
private LinearLayout schoolSetupLayout;
private Button addNewClass;
private EditText classNameEditText;
private Button deleteButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_school_search_setup);
findAllViewsID();
initializeListenners();
}
public void findAllViewsID(){
classNameEditText = findViewById(R.id.classNameText);
addNewClass = findViewById(R.id.addNewClassButton);
schoolSetupLayout = findViewById(R.id.schoolSetupLayout);
searchSchoolID = findViewById(R.id.searchSchoolID);
searchSchoolButtonID = findViewById(R.id.searchSchoolButtonID);
listOfSchoolsID = findViewById(R.id.listOfSchoolsID);
openNewschoolID = findViewById(R.id.openNewschoolID);
deleteButton = findViewById(R.id.delete_button);
}
public void initializeListenners(){
openNewschoolID.setOnClickListener(SchoolSearchSetup.this);
addNewClass.setOnClickListener(SchoolSearchSetup.this);
deleteButton.setOnClickListener(SchoolSearchSetup.this);
}
public void addNewClass(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.field, null);
schoolSetupLayout.addView(rowView, schoolSetupLayout.getChildCount() -1);
}
#Override
public void onClick(View view) {
switch(view.getId()){
case R.id.openNewschoolID:
displaySchoolSetUpForms();
break;
case R.id.addNewClassButton:
addNewClass();
break;
case R.id.delete_button:
schoolSetupLayout.removeView((View) view.getParent());
}
}
private void displaySchoolSetUpForms() {
schoolSetupLayout.setVisibility(View.VISIBLE);
}
}
The main XML file
activity.school_search_setup.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
tools:context="com.example.demeainc.demea.MainActivity"
android:orientation="vertical"
android:background="#color/backgroundColor">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<ImageView
android:id="#+id/backArrowClassView"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/ic_arrow_class"
android:layout_marginLeft="5dp"/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="50dp"
android:text="Search your school."
android:gravity="center"
android:textSize="25dp" />
<android.support.v7.widget.CardView
android:foreground="?attr/selectableItemBackground"
android:clickable="true"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp">
<EditText
android:id="#+id/searchSchoolID"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableLeft="#drawable/ic_search_black_24dp"
android:hint="Search"/>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp">
<Button
android:id="#+id/searchSchoolButtonID"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_marginLeft="60dp"
android:text="Search"
android:textColor="#ffff"
android:background="#color/colorPrimary"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp">
<android.support.v7.widget.CardView
android:layout_width="300dp"
android:layout_height="wrap_content">
<ListView
android:id="#+id/listOfSchoolsID"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#android:color/transparent"
android:cacheColorHint="#color/ligtherDarkGrey"
android:divider="#CCCCCC"
android:dividerHeight="2dp"
android:paddingLeft="2dp" >
</ListView>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<Button
android:id="#+id/openNewschoolID"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_marginLeft="60dp"
android:background="#color/colorPrimary"
android:text="Open New School"
android:textColor="#ffff"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/schoolSetupLayout"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<TextView
android:id="#+id/schoolSetupText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="School Setup"
android:textSize="20dp"
android:layout_marginLeft="50dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/classNameText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:hint="Class name, e.g Grade one" />
<Button
android:id="#+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#android:drawable/ic_delete"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:id="#+id/addNewClassButton"
android:layout_marginTop="15dp"
android:layout_marginLeft="50dp"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Add new class"
android:textColor="#ffff"
android:background="#color/colorPrimary"/>
<Button
android:id="#+id/nextButton"
android:layout_marginTop="25dp"
android:layout_marginLeft="8dp"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Next"
android:textColor="#ffff"
android:background="#color/green"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Here is the Field.xml file for the edit text to be added. Both the field.xml and the activity.school_search_setup.xml share the same IDs.
Field.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/schoolSetupLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/classNameText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:hint="Class name, e.g Grade one" />
<Button
android:id="#+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#android:drawable/ic_delete"/>
</LinearLayout>
</LinearLayout>
Have you tried using
setVisibility(View.GONE);
Update:
Try this code block:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setId(i);
final int id_ = btn.getId();
btn.setText("button " + id_);
btn.setBackgroundColor(Color.rgb(80, 80, 90));
linear.addView(btn, params);
btn1 = ((Button) findViewById(id_));
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(view.getContext(),
"Button clicked index = " + id_, Toast.LENGTH_SHORT)
.show();
}
});

button BACK to a collapsingToolbar - Android

It happens that from my main activity I call an activity in that activity that I call tale with collapsingToolbar, well everything works fine only that I can not give BACK because the collapsinToolbar does not generate a button back.
Java code of the main activity
package com.herprogramacion.alquileres;
import android.database.Cursor;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.herprogramacion.alquileres.provider.Contrato.Alquileres;
public class ActividadListaAlquileres extends AppCompatActivity implements AdaptadorAlquileres.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor> {
private RecyclerView listaUI;
private LinearLayoutManager linearLayoutManager;
private AdaptadorAlquileres adaptador;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.actividad_lista_alquileres);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Filtro...", Snackbar.LENGTH_LONG)
.setAction("Acción", null).show();
}
});
// Preparar lista
listaUI = (RecyclerView) findViewById(R.id.lista);
listaUI.setHasFixedSize(true);
linearLayoutManager = new LinearLayoutManager(this);
listaUI.setLayoutManager(linearLayoutManager);
adaptador = new AdaptadorAlquileres(this, this);
listaUI.setAdapter(adaptador);
// Iniciar loader
getSupportLoaderManager().restartLoader(1, null, this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_actividad_lista_alquileres, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(AdaptadorAlquileres.ViewHolder holder, String idAlquiler) {
// Snackbar.make(findViewById(android.R.id.content), ":id = " + idAlquiler,
// Snackbar.LENGTH_LONG).show();
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(this, Alquileres.URI_CONTENIDO, null, null, null, null);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
if (adaptador != null) {
adaptador.swapCursor(data);
}
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
}
}
Java code of the activity where I generate the collapsingToolbar
package com.herprogramacion.alquileres;
import android.content.Intent;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
public class biografia2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_biografia2);
CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle("Titulo ");
}
}
Xml code of my main activity
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context="com.herprogramacion.alquileres.ActividadListaAlquileres">
<android.support.design.widget.AppBarLayout
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"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/contenido_actividad_lista_alquileres" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/icono_filtro" />
</android.support.design.widget.CoordinatorLayout>
Xml code of the activity where I generate the collapsingToolbar
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="#dimen/detail_backdrop_height"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp" >
<ImageView
android:id="#+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax"
android:src="#drawable/jack"
android:alpha="0.5"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="170dp"
android:layout_gravity="center">
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="24dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/card_margin">
<LinearLayout
style="#style/Widget.CardContent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Info"
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/lorem_ipsum" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/card_margin"
android:layout_marginLeft="#dimen/card_margin"
android:layout_marginRight="#dimen/card_margin">
<LinearLayout
style="#style/Widget.CardContent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Friends"
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/lorem_ipsum" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/card_margin"
android:layout_marginLeft="#dimen/card_margin"
android:layout_marginRight="#dimen/card_margin">
<LinearLayout
style="#style/Widget.CardContent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Related"
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/lorem_ipsum" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_anchor="#id/appbar"
app:layout_anchorGravity="bottom|right|end"
android:src="#drawable/ic_discuss"
android:layout_margin="#dimen/fab_margin"
android:clickable="true"/>
</android.support.design.widget.CoordinatorLayout>
You can add back navigation icon in the xml for Toolbar component using app:navigationIcon attribute.
<android.support.v7.widget.Toolbar
android:id="#+id/materialup.toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:layout_scrollFlags="scroll"
app:navigationIcon="?homeAsUpIndicator" />
You should be able to show the default back button using setDisplayHomeAsUpEnabled:
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Categories