Why doesn't the replace function for fragments work? - java

I'm trying to open a Fragment from a Activity by using the fragmentManager.replace() function, but it doesn't do anything and the Logcat tells me nothing either. I have tried using both FragmentContainerView and FrameLayout inside my activity_main.xml, and even using the activity's ConstraintLayout ID to host the Fragment, but to no avail.
Here are the codes for my Activity that should open the fragment, and the fragment code itself:
OsActivity.java
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import com.leonardomaito.autocommobile.fragments.NewOsFragment;
import autocommobile.R;
public class OsActivity extends AppCompatActivity {
NewOsFragment newOsFragment = new NewOsFragment();
private View v;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_os);
}
public void createNewOs(View view) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setReorderingAllowed(true);
fragmentTransaction.replace(R.id.osFragmentContainer, newOsFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
NewOsFragment.java
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import autocommobile.R;
public class NewOsFragment extends Fragment {
private Button btOsNext;
private Button btOsCancel;
private Fragment nextOs = new Fragment();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_new_os, container, false);
btOsNext = view.findViewById(R.id.btNextOs);
btOsCancel = view.findViewById(R.id.btCancelNewOs);
return view;
}
public void nextOsStep(View view) {
FragmentTransaction fragmentManager = getActivity()
.getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_new_os, nextOs);
}
}
ActivityOs.xml
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/activity_os"
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:layout_marginBottom="0dp"
android:background="#color/white"
tools:context="com.leonardomaito.autocommobile.activities.OsActivity">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/osFragmentContainer"
android:name="com.leonardomaito.autocommobile.fragments.NewOsFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout="#layout/fragment_new_os">
</androidx.fragment.app.FragmentContainerView>
<include
android:id="#+id/include"
layout="#layout/layout_header_search" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/glRecyclerLimit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="124dp" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerViewOs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="650dp"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/glRecyclerLimit"
app:layout_constraintVertical_bias="0.0"
tools:listitem="#layout/layout_os_item" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/btNewOs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Inserir OS"
android:onClick="createNewOs"
android:src="#drawable/custom_plus_icon"
app:backgroundTint="#color/autocom_blue"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.98"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/glRecyclerLimit"
app:layout_constraintVertical_bias="0.988"
app:rippleColor="#color/autocom_blue" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_new_os.xml
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/fragment_new_os"
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.leonardomaito.autocommobile.fragments.NewOsFragment">
<TextView
android:id="#+id/tvServiceOrder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Ordem de Serviço"
android:textColor="#color/autocom_blue"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/viewHorizontalBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<View
android:id="#+id/viewHorizontalBar"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_marginTop="32dp"
android:background="#color/autocom_blue"
app:layout_constraintBottom_toTopOf="#+id/glViewBottom"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/glViewBottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="57dp" />
<TextView
android:id="#+id/tvCliente"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="136dp"
android:text="Cliente"
android:textColor="#color/autocom_blue"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.084"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/viewHorizontalBar" />
<EditText
android:id="#+id/etClientInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#drawable/custom_border"
android:drawableStart="#drawable/custom_search_icon"
android:ems="17"
android:importantForAutofill="no"
android:inputType="text"
android:minHeight="48dp"
android:textColor="#color/autocom_blue"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.49"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvCliente" />
<TextView
android:id="#+id/tvAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="Endereço"
android:textColor="#color/autocom_blue"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.094"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etClientInput" />
<EditText
android:id="#+id/etAddressInput"
android:enabled="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:background="#drawable/custom_border"
android:ems="17"
android:inputType="text"
android:minHeight="48dp"
android:textColor="#color/autocom_blue"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.49"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvAddress"
android:importantForAutofill="no" />
<TextView
android:id="#+id/tvTelephone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:text="Telefone"
android:textColor="#color/autocom_blue"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.091"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etAddressInput" />
<EditText
android:id="#+id/etTelephoneInput"
android:enabled="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:background="#drawable/custom_border"
android:ems="17"
android:inputType="text"
android:minHeight="48dp"
android:textColor="#color/autocom_blue"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.49"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvTelephone"
android:importantForAutofill="no" />
<android.widget.Button
android:id="#+id/btNextOs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:background="#drawable/custom_button"
android:minWidth="125dp"
android:onClick="nextOsStep"
android:text="Avançar"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etTelephoneInput"
app:layout_constraintVertical_bias="0.155" />
</androidx.constraintlayout.widget.ConstraintLayout>
Shouldn't the replace function be opening my fragment on top of my activity? Is there something I'm doing wrong? The majority of other posts adressing this situation seemed to use .add instead of .replace, and were fixed by doing so.

Related

How to handle click event from a Fragment, produces error Nullpointer exception, trying to invoke *setOnClickListener* from a null object reference

Please, i have been trying to handle click event from my project and haven't actually found a place to actually put the button.setOnClickListener(handler); statement. The truth is that i have a ViewPager inside another layout.xml file which i inflated from within a Fragment. But i have two other layout files, which i used to provide content for the ViewPager itself, which i did and it worked properly. But when i tried to handle onClick event from within any of my classes, it produces a NullPointer Exception. This is my first project on Android and i can't seem to find any solution even on StackOverflow, because none of the answers really solved my problem. I will really appreciate if someone could read through this long code and provide an answer to my problem. Thanks in advance. It's a Calculator App
Code below
*In MainActivity.java*
package com.projects.calculatorapp2;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedState){
super.onCreate(savedState);
getSupportFragmentManager()
.beginTransaction()
.add(android.R.id.content, new ViewPagerFragment())
.commit();
}
}
*In ViewPagerFragment.java*
package com.projects.calculatorapp2;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
/**
* <b><p>Description</p></b>
* A simple {#link Fragment} subclass that provides view for the main activity.
* #author Noah
* #since 2020
*
*/
public class ViewPagerFragment extends Fragment implements View.OnClickListener {
EditText input;
protected ViewPager pager;
public StringBuilder builderText = new StringBuilder();
#Override
public void onCreate(Bundle state) {
super.onCreate(state);
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.activity_main, container, false);
pager = view.findViewById(R.id.pager);
input = view.findViewById(R.id.edit);
pager.setAdapter(new ButtonAdapter(getFragmentManager()));
return (view);
}
#Override
public void onViewCreated(#NonNull View view, Bundle savedState){
Button one = view.findViewById(R.id.seven);
one.setOnClickListener(this);
}
#Override
public void onClick(View view) {
int buttonId = view.getId();
switch (buttonId) {
case R.id.one: {
builderText.append(1);
Log.d(getClass().getSimpleName(), String.valueOf(builderText));
input.setText(builderText);
}
}
}
private class ButtonAdapter extends FragmentPagerAdapter {
// default constructor chained to superclass
ButtonAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return 2; //number of pages
}
#Override
public Fragment getItem(int position) {
// Log.d(getClass().getSimpleName(), String.valueOf(position));
return (ButtonSet.newInstance(position));
}
}
}
*In ButtonSet.java*
package com.projects.calculatorapp2;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ButtonSet extends Fragment {
public static final String POS_KEY = "position";
static ButtonSet newInstance(int pos){
ButtonSet bs = new ButtonSet();
Bundle bundle = new Bundle();
bundle.putInt(POS_KEY, pos);
bs.setArguments(bundle);
return (bs);
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container,
Bundle state) {
//noinspection ConstantConditions
if(getArguments().getInt(POS_KEY) == 0)
return inflater.inflate(R.layout.content_1,
container, false);
return inflater.inflate(R.layout.content_2,
container, false);
}
}
*In activity_main.xml* (Main layout file when app is lauched)
<?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:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical">
<EditText
android:id="#id/edit"
android:textIsSelectable="true"
android:layout_width="match_parent"
android:layout_height="200dp"
android:inputType="text"
android:textSize="40sp"
android:background="#android:color/transparent"
tools:ignore="Autofill,LabelFor"
android:hint="#string/ediText_hint"
android:gravity="center_vertical|end"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_marginEnd="110dp"
android:layout_marginRight="70dp"
android:background="#color/black"
android:clickable="false" />
<Button
android:id="#id/c_button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="3dp"
android:layout_marginLeft="3dp"
android:textColor="#color/white"
android:textSize="20sp"
android:text="#string/delete"
android:background="#color/dark_red"/>
</RelativeLayout>
<android.support.v4.view.ViewPager
android:id="#id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
*In content_1.xml* (This is first content of ViewPager)
<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.projects.calculatorapp2.ViewPagerFragment">
<!-- First button representing 7 -->
<Button
android:id="#id/seven"
style="#android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="#string/seven"
android:textSize="50sp"
android:typeface="serif"
app:layout_constraintWidth_percent="25"
app:layout_constraintEnd_toStartOf="#+id/eight"
app:layout_constraintRight_toLeftOf="#id/eight"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="#id/four"
app:layout_constraintTop_toTopOf="parent" />
<!-- Second button representing 8 -->
<Button
android:id="#id/eight"
style="#android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toLeftOf="#id/nine"
app:layout_constraintBottom_toTopOf="#id/five"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="#string/eight"/>
<!-- Third button representing 9 -->
<Button
android:id="#id/nine"
style="#android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toLeftOf="#id/divide"
app:layout_constraintBottom_toTopOf="#id/six"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="#string/nine"/>
<!-- Fourth button representing division sign (/) -->
<Button
android:id="#id/divide"
style="#android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="#id/times"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="#string/divide" />
<!-- Fifth button representing 4 -->
<Button
android:id="#id/four"
style="#android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toBottomOf="#id/seven"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="#id/one"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="#string/four" />
<!-- Sixth button representing 5 -->
<Button
android:id="#id/five"
style="#android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toBottomOf="#id/eight"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toEndOf="#+id/four"
app:layout_constraintBottom_toTopOf="#id/two"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="#string/five" />
<!-- Seventh button representing 6 -->
<Button
android:id="#id/six"
style="#android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toBottomOf="#+id/nine"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toEndOf="#+id/five"
app:layout_constraintBottom_toTopOf="#id/three"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="#string/six" />
<!-- Eight button representing multiplication sign (x) -->
<Button
android:id="#id/times"
style="#android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toBottomOf="#+id/divide"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toEndOf="#+id/six"
app:layout_constraintBottom_toTopOf="#id/minus"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="normal"
android:text="#string/times" />
<!-- Eighth button representing 1 -->
<Button
android:id="#+id/one"
style="#android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toBottomOf="#+id/four"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toTopOf="#id/decimal"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="#string/one" />
<!-- Ninth button representing 2 -->
<Button
android:id="#id/two"
style="#android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toBottomOf="#+id/five"
app:layout_constraintStart_toEndOf="#+id/one"
app:layout_constraintBottom_toTopOf="#id/zero"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="#string/two" />
<!-- Tenth button representing 3 -->
<Button
android:id="#id/three"
style="#android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toBottomOf="#+id/times"
app:layout_constraintStart_toEndOf="#+id/two"
app:layout_constraintBottom_toTopOf="#id/equals"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="#string/three" />
<!-- Eleventh button representing subtration sign ( - ) -->
<Button
android:id="#id/minus"
style="#android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toBottomOf="#+id/times"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toEndOf="#+id/three"
app:layout_constraintBottom_toTopOf="#id/add"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="#string/minus" />
<!-- Twelfth button representing decimal point ( . ) -->
<Button
android:id="#id/decimal"
style="#android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toBottomOf="#+id/one"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="#string/decimal" />
<!-- Thirteenth button representing zero ( 0 )-->
<Button
android:id="#id/zero"
style="#android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toBottomOf="#+id/two"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toEndOf="#+id/decimal"
app:layout_constraintBottom_toBottomOf="parent"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="#string/zero" />
<!-- Fourteenth button representing equals sign ( = ) -->
<Button
android:id="#id/equals"
style="#android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toBottomOf="#+id/three"
app:layout_constraintStart_toEndOf="#+id/zero"
app:layout_constraintBottom_toBottomOf="parent"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="#string/equals" />
<!-- Fifteenth button representing addition sign ( + ) -->
<Button
android:id="#id/add"
style="#android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintTop_toBottomOf="#+id/minus"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:ignore="ButtonStyle"
android:textSize="50sp"
android:typeface="serif"
android:text="#string/add" />
</android.support.constraint.ConstraintLayout>
*In content_2.xml* (Second content of ViewPager)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Button
android:id="#id/sin"
style="#android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="#string/sin"
android:textSize="50sp"
android:typeface="serif"
app:layout_constraintBottom_toTopOf="#+id/log"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintWidth_percent="0.33"
/>
<Button
android:id="#id/cos"
style="#android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="#string/cos"
android:textSize="50sp"
android:typeface="serif"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="#+id/sin"
app:layout_constraintEnd_toStartOf="#+id/tan"
app:layout_constraintBottom_toTopOf="#+id/log10"
app:layout_constraintWidth_percent="0.33"
/>
<Button
android:id="#id/tan"
style="#android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="#string/tan"
android:textSize="50sp"
android:typeface="serif"
app:layout_constraintBottom_toTopOf="#+id/factorial"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintWidth_percent="0.33"/>
<Button
android:id="#id/log"
style="#android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="#string/log"
android:textSize="50sp"
android:typeface="serif"
app:layout_constraintBottom_toTopOf="#+id/pi"
app:layout_constraintTop_toBottomOf="#id/sin"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintWidth_percent="0.33"
/>
<Button
android:id="#id/log10"
style="#android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="#string/log10"
android:textSize="50sp"
android:typeface="serif"
app:layout_constraintTop_toBottomOf="#id/cos"
app:layout_constraintStart_toEndOf="#+id/log"
app:layout_constraintEnd_toStartOf="#+id/tan"
app:layout_constraintBottom_toTopOf="#+id/exp"
app:layout_constraintWidth_percent="0.33"
/>
<Button
android:id="#id/factorial"
style="#android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="#string/factorial"
android:textSize="50sp"
android:typeface="serif"
app:layout_constraintTop_toBottomOf="#id/tan"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintWidth_percent="0.33"
app:layout_constraintBottom_toTopOf="#+id/pow"/>
<Button
android:id="#id/pi"
style="#android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="#string/pi"
android:textSize="50sp"
android:typeface="serif"
app:layout_constraintTop_toBottomOf="#id/log"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintWidth_percent="0.33"
app:layout_constraintBottom_toBottomOf="parent"
/>
<Button
android:id="#id/exp"
style="#android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="#string/exp"
android:textSize="50sp"
android:typeface="serif"
app:layout_constraintTop_toBottomOf="#id/log10"
app:layout_constraintStart_toEndOf="#+id/pi"
app:layout_constraintEnd_toStartOf="#+id/tan"
app:layout_constraintWidth_percent="0.33"
app:layout_constraintBottom_toBottomOf="parent"
/>
<Button
android:id="#id/pow"
style="#android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="#string/pow"
android:textSize="50sp"
android:typeface="serif"
app:layout_constraintTop_toBottomOf="#id/factorial"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintWidth_percent="0.33"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>
Log message
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.projects.calculatorapp2.ViewPagerFragment.onViewCreated(ViewPagerFragment.java:57)
Override the onViewCreated() in your fragment and you can set your click listeners in that method.
Also I see an issue in your ids. It should be like
android:id="#+id/seven"
Notice the + sign
Detailed
Make your fragment having the button implement View.OnClickListener
Then override the onViewCreated method. Inside it do something like this for every button:
yourButton = view.findViewById(R.id.seven);
yourButton.setOnClickListener(this);
Do the same for all buttons
Edit:
Override the onViewCreated method in your ButtonSet fragment and also put a position check like this:
//inside onViewCreated
if(position == 0) {
//init buttons from content 1 xml layout, and set listeners
} else {
//init buttons from content 2 layout
}

How control media player in android across all activities?

Hello friends i am creating a mp3 player which have two activities how can i control media from both activities like play pause and progress bar here is
the first activity have list of the song when user click any songs it work fine and below this song listview i create progress and bar and media controll button like play pause and next and back its include spreate layoute ad in below list when user click on this layout it redirect another activity and which have also same feature like play pause and and progress bar how can i controll this media from both activities?
my code!
<?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:background="#1e1e2c"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/song_list"/>
<include
android:layout_below="#+id/song_title"
layout="#layout/mediacontroller"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<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:background="#1e1e2c"
android:layout_height="match_parent">
<ImageView
android:layout_width="180dp"
android:layout_height="180dp"
android:id="#+id/rotate"
android:layout_centerInParent="true"
android:src="#drawable/musiccirlce"/>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
<ImageButton
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:clickable="true"
android:focusable="true"
android:id="#+id/repeat"
android:visibility="visible"
android:scaleType="centerCrop"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/baseline_repeat_white_48dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.023"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.811" />
<ImageButton
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:clickable="true"
android:focusable="true"
android:id="#+id/repeatenable"
android:visibility="gone"
android:scaleType="centerCrop"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/repeatenable"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.023"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.811" />
<ImageButton
android:id="#+id/shuffle"
android:layout_width="20dp"
android:layout_height="24dp"
android:visibility="visible"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:scaleType="centerCrop"
android:src="#drawable/baseline_shuffle_white_48dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.976"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.811" />
<ImageButton
android:layout_width="25dp"
android:id="#+id/shufflenable"
android:layout_height="25dp"
android:visibility="gone"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:scaleType="centerCrop"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/shuffulenabled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.976"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.811" />
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="99dp"
android:layout_alignParentBottom="true">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="80dp"
android:clickable="false"
android:background="#color/backgroundColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="55dp"
android:layout_height="55dp"
android:padding="15dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="16dp"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:scaleType="centerCrop"
android:src="#mipmap/outline_thumb_up_alt_black_48"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/songtime"
app:layout_constraintVertical_bias="0.56"
android:layout_marginRight="8dp"
android:layout_marginLeft="16dp" />
<ImageButton
android:id="#+id/imageButton2new"
android:layout_width="55dp"
android:layout_height="55dp"
android:padding="15dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="16dp"
android:visibility="gone"
android:clickable="true"
android:focusable="true"
android:scaleType="centerCrop"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#mipmap/baseline_thumb_up_alt_black_48"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/songtime"
app:layout_constraintVertical_bias="0.56"
android:layout_marginRight="8dp"
android:layout_marginLeft="16dp" />
<ImageButton
android:id="#+id/button"
android:layout_width="55dp"
android:layout_height="55dp"
android:padding="15dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="8dp"
android:scaleType="centerCrop"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#mipmap/baseline_thumb_down_alt_black_18"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/endTime"
app:layout_constraintVertical_bias="0.56"
android:layout_marginRight="16dp"
android:layout_marginLeft="8dp" />
<ImageButton
android:id="#+id/buttontwo"
android:layout_width="55dp"
android:layout_height="55dp"
android:padding="15dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="8dp"
android:visibility="gone"
android:scaleType="centerCrop"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#mipmap/baseline_thumb_down_alt_black_18"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/endTime"
app:layout_constraintVertical_bias="0.56"
android:layout_marginRight="16dp"
android:layout_marginLeft="8dp" />
<TextView
android:id="#+id/songtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:fontFamily="sans-serif"
android:text="1:05"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/endTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:fontFamily="sans-serif"
android:text="3:06"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<LinearLayout
android:id="#+id/linearLayout5"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<ImageButton
android:id="#+id/play_button_main"
android:layout_width="55dp"
android:layout_height="55dp"
android:scaleType="centerCrop"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/play_button" />
<ImageButton
android:id="#+id/pause_button_main"
android:layout_width="55dp"
android:visibility="gone"
android:scaleType="centerCrop"
android:layout_height="55dp"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/pause_button" />
</LinearLayout>
<ImageButton
android:layout_width="55dp"
android:id="#+id/pervious"
android:layout_height="55dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:padding="15dp"
android:clickable="true"
android:focusable="true"
android:scaleType="fitCenter"
android:src="#drawable/backword_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/linearLayout5"
app:layout_constraintHorizontal_bias="0.754"
app:layout_constraintStart_toEndOf="#+id/imageButton2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<ImageButton
android:id="#+id/next"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:padding="15dp"
android:scaleType="fitCenter"
android:src="#drawable/forword_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/button"
app:layout_constraintHorizontal_bias="0.25"
app:layout_constraintStart_toEndOf="#+id/linearLayout5"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
/>
</android.support.constraint.ConstraintLayout>
</RelativeLayout>
<SeekBar
android:id="#+id/seekBar3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/relativeLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/relativeLayout"
app:layout_constraintVertical_bias="0.75" />
</android.support.constraint.ConstraintLayout>
</RelativeLayout>
package music.player.musicplayer;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class MainActivity extends AppCompatActivity {
private ArrayList<Song> songList;
private ListView songView;
LinearLayout nextactivity;
SeekBar seekBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
songView = findViewById(R.id.song_list);
songList = new ArrayList<Song>();
seekBar=findViewById(R.id.seekBar2);
getSongList();
Collections.sort(songList, new Comparator<Song>(){
public int compare(Song a, Song b){
return a.getTitle().compareTo(b.getTitle());
}
});
SongAdapter songAdt = new SongAdapter(this, songList);
songView.setAdapter(songAdt);
nextactivity=findViewById(R.id.mediacontroller);
nextactivity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
});
songView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
//song info method retrive
public void getSongList() {
//retrieve song info
ContentResolver musicResolver = getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
if (musicCursor != null && musicCursor.moveToFirst()) {
//get columns
int titleColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
int artistColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
int dataColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DATA);
//add songs to list
do {
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
String thisPath = musicCursor.getString(dataColumn);
songList.add(new Song(thisId, thisTitle, thisArtist, thisPath));
}
while (musicCursor.moveToNext());
}
}
}
package music.player.musicplayer;
public class Song {
private long id;
private String title;
private String artist;
private String getpath;
public long getId() {
return id;
}
public String getTitle() {
return title;
}
public String getArtist() {
return artist;
}
public String getGetpath() {
return getpath;
}
public Song(long id, String title, String artist, String getpath ) {
this.id = id;
this.title = title;
this.artist = artist;
this.getpath = getpath;
}
}
package music.player.musicplayer;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
class SongAdapter extends BaseAdapter{
private ArrayList<Song> songs;
private LayoutInflater songInf;
public SongAdapter(Context c, ArrayList<Song> theSongs){
songs=theSongs;
songInf=LayoutInflater.from(c);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return songs.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout songLay = (RelativeLayout)songInf.inflate(R.layout.song, parent, false);
//get title and artist views
TextView songView = (TextView)songLay.findViewById(R.id.song_title);
TextView artistView = (TextView)songLay.findViewById(R.id.song_artist);
//get song using position
Song currSong = songs.get(position);
//get title and artist strings
songView.setText(currSong.getTitle());
artistView.setText(currSong.getArtist());
//set position as tag
songLay.setTag(position);
return songLay;
}
}
Here is my activities picture you get idea what i actually i want
actity two:

Text view doesn't show after button click - Android

I have a problem with Text View and Button in Android Studio. I would like to update my text view when i clik a button but it doesn't work.
This is my code:
activity_distance_to_run.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"
tools:layout_editor_absoluteY="25dp">
<TextView
android:id="#+id/Distancerun2"
android:layout_width="126dp"
android:layout_height="50dp"
android:layout_marginTop="50dp"
android:text="Distance to ....."
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/distancetorun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:text="run"
app:layout_constraintBaseline_toBaselineOf="#+id/distancetoride"
app:layout_constraintEnd_toStartOf="#+id/distancetoride"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/distancetoride"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="9dp"
android:layout_marginRight="9dp"
android:layout_marginTop="32dp"
android:text="ride a bicycle"
app:layout_constraintEnd_toStartOf="#+id/distancetoswim"
app:layout_constraintStart_toEndOf="#+id/distancetorun"
app:layout_constraintTop_toBottomOf="#+id/Distancerun2" />
<Button
android:id="#+id/distancetoswim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="swim"
app:layout_constraintBaseline_toBaselineOf="#+id/distancetoride"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/distancetoride" />
<Button
android:id="#+id/yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="32dp"
android:layout_marginStart="32dp"
android:text="I did it !!"
app:layout_constraintBaseline_toBaselineOf="#+id/no"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="116dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:text="I don't want to do it !!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView1" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="23dp"
android:layout_marginEnd="25dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginStart="25dp"
android:layout_marginTop="221dp"
android:autoText="true"
android:clickable="true"
android:enabled="false"
android:freezesText="false"
android:text="Elo ziomek"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="#+id/no"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
distancetorun.java
package pl.agcode.sqliteexample;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class distance_to_run extends Activity {
Button distancetorun;
TextView txtView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_distance_to_run);
distancetorun = (Button) findViewById(R.id.distancetorun);
txtView = (TextView) findViewById(R.id.textView1);
distancetorun.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtView.setText("NewText");
}
});
}
}
Thanks for help I will try to repair it but don't know how.
This is very important to repair this for me. :)
Looks all good except textview1 color.
Add below line for the textview1 in layout xml
<TextView
android:id="#+id/textView1"
android:textColor="#android:color/black"
After launching the app
After clicking RUN button

Navigation Drawer not navigating

I have create a navigation drawer, i don't want to use fragments but instead activities. I have added this code which was working in many tutorials that i have watched.
public boolean onNavigationItemSelected( MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
switch (id) {
case R.id.Daily: {
Intent h= new Intent(goals.this, Daily.class );
startActivity(h);
break;
}
}
//close navigation drawer
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Whenever i click on the page i would like to navigate to. The drawer closes and nothing else happens. I am just a beginner so i don't know how to proceed. I have watched about 10 tutorials and this is what they say work but it doesn't and i am not sure why. The onNavigationItemSelected method has a grey line under it, saying that the method has no been used.
Resources:
My XML for the goals activity:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mitchell.tfit.goals">
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#414E68"
app:itemIconTint="#FFFFFF"
app:headerLayout="#layout/header_profile"
app:itemTextColor="#android:color/background_light"
app:menu="#menu/drawermenu">
</android.support.design.widget.NavigationView>
<android.support.constraint.ConstraintLayout 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="#drawable/background_daily"
tools:context="com.example.mitchell.tfit.Daily">
<ScrollView
android:id="#+id/scrollView2"
android:layout_width="0dp"
android:layout_height="0dp"
android:scrollbars="none"
app:layout_constraintBottom_toTopOf="#+id/constraintLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.constraint.ConstraintLayout
android:id="#+id/layoutSteps"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginEnd="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:background="#drawable/background_panels"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/lblSteps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="21dp"
android:layout_marginTop="5dp"
android:text="Steps"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="bold"
android:typeface="serif"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="112dp"
android:layout_height="44dp"
android:layout_above="#+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="50dp"
android:text="0"
android:textAlignment="center"
android:textColor="#6E8528"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.97"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_above="#+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="50dp"
android:text="Current Goal:"
android:textAlignment="center"
android:textColor="#6E8528"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.029"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:id="#+id/layoutExercise"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:background="#drawable/background_panels"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_above="#+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="50dp"
android:text="Current Goal:"
android:textAlignment="center"
android:textColor="#6E8528"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.046"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="#+id/lblExercise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="21dp"
android:layout_marginTop="5dp"
android:text="Weight"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="bold"
android:typeface="serif"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:id="#+id/layoutMeals"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginEnd="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="10dp"
android:background="#drawable/background_panels"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="#+id/editText"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginBottom="10dp"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:background="#drawable/rounded_edittext"
android:ems="10"
android:hint="Weight Goal"
android:inputType="numberDecimal"
android:paddingLeft="10dp"
android:textColor="#android:color/background_light"
android:textColorHint="#android:color/background_light"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="#+id/button2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText2" />
<EditText
android:id="#+id/editText2"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:background="#drawable/rounded_edittext"
android:ems="10"
android:hint="Step Goal"
android:inputType="number"
android:paddingLeft="10dp"
android:textColor="#android:color/background_light"
android:textColorHint="#android:color/background_light"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="#+id/editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/lblMeals" />
<TextView
android:id="#+id/lblMeals"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="21dp"
android:layout_marginTop="5dp"
android:text="Edit Goals"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="bold"
android:typeface="serif"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_marginBottom="300dp"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:background="#drawable/button_start"
android:text="Save"
android:textColor="#android:color/background_light"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText" />
</android.support.constraint.ConstraintLayout>
</LinearLayout>
</ScrollView>
<android.support.constraint.ConstraintLayout
android:id="#+id/constraintLayout"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginBottom="5dp"
android:layout_marginEnd="5dp"
android:layout_marginStart="5dp"
android:background="#drawable/background_camera_nav"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/scrollView2">
<ImageButton
android:id="#+id/imageButton"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginBottom="5dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="5dp"
android:adjustViewBounds="false"
android:background="#android:color/transparent"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/gallery_icon" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="90dp"
android:layout_height="40dp"
android:layout_marginBottom="5dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="5dp"
android:adjustViewBounds="false"
android:background="#drawable/background_nav_button"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="#drawable/camera_icon1" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
Java for the Daily activity
package com.example.mitchell.tfit;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toolbar;
public class Daily extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_daily);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public boolean onNavigationItemSelected( MenuItem item) {
// Handle navigation view item clicks here.
switch (item.getItemId()) {
case R.id.Daily: {
break;
}
}
//close navigation drawer
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mToggle.onOptionsItemSelected(item)){
return (true);
}
return super.onOptionsItemSelected(item);
}
}
XML for my drawermenu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/Daily"
android:title="Daily Tracker"
android:icon = "#drawable/daily_icon"/>
<item
android:id="#+id/Goals"
android:title="Goals"
android:icon = "#drawable/goals_icon"/>
<item
android:id="#+id/Settings"
android:title="Settings"
android:icon = "#drawable/settings_icon" />
<item
android:id="#+id/Logout"
android:title="Logout"
android:icon = "#drawable/logout_icon" />
Goals activity java:
package com.example.mitchell.tfit;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
public class goals extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_goals);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Your Activity needs to implement NavigationView.OnNavigationItemSelectedListener and you have to set the listener for your NavigationDrawer. Something like this should work:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
// rest of code
}
}
EDIT:
The NavigationViewer in the XML of your GoalsActivity should look like this:
<android.support.design.widget.NavigationView
android:id="navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#414E68"
app:itemIconTint="#FFFFFF"
app:headerLayout="#layout/header_profile"
app:itemTextColor="#android:color/background_light"
app:menu="#menu/drawermenu">
Furthermore, add this to your GoalsActivity.java, inside the onCreate() method:
NavigationView navigationView = findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);

App shutting down

So I want to implement a ProgressBar and set it on 100% but somehow it keeps crashing in the Emulator.
Heres the Code(MainActivity.java):
package com.example.schwarzerritter.remotec;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
public class MainScreen extends AppCompatActivity {
ProgressBar fuelBar;
public Button lDoor;
public void lDoorPageOpen() {
lDoor = (Button) findViewById(R.id.lDoor);//weist unseren Button lDoor in Java zu
lDoor.setOnClickListener(new View.OnClickListener() {//
#Override
public void onClick(View v) {
Intent lDoorPage = new Intent(MainScreen.this, lDoorPageOpen.class);//deklarieren eines intents
startActivity(lDoorPage);//starte intent lDoorPage
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
fuelBar.setProgress(100);
lDoorPageOpen();
fuelBar =(ProgressBar)findViewById(R.id.fuelB);
}
}
and this is the .xml file:
<?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.example.schwarzerritter.remotec.MainScreen"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RemoteC"
android:id="#+id/remoteC"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/lDoor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:text="LOCK"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="#+id/remoteC"
android:layout_marginTop="103dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="#+id/acB"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/fuelB"
android:layout_width="94dp"
android:layout_height="50dp"
android:fontFamily="monospace"
android:text="FUEL"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="#+id/locB"
android:layout_marginEnd="41dp"
app:layout_constraintRight_toRightOf="#+id/locB"
android:layout_marginTop="70dp"
app:layout_constraintTop_toBottomOf="#+id/remoteC"
android:layout_marginBottom="65dp"
android:layout_marginRight="41dp" />
<Button
android:id="#+id/acB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:text="AC"
tools:layout_constraintTop_creator="1"
android:layout_marginStart="48dp"
android:layout_marginTop="19dp"
app:layout_constraintTop_toBottomOf="#+id/lDoor"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="48dp" />
<Button
android:id="#+id/locB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:text="LOCATION"
app:layout_constraintBottom_toBottomOf="#+id/fuelB"
app:layout_constraintTop_toTopOf="#+id/acB"
app:layout_constraintVertical_bias="0.0"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintTop_creator="1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.859" />
<Button
android:id="#+id/engB"
android:layout_width="0dp"
android:layout_height="49dp"
android:fontFamily="monospace"
android:text="ENGINE START"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="35dp"
app:layout_constraintTop_toBottomOf="#+id/locB"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp" />
<ProgressBar
android:id="#+id/fuelB"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="370dp"
android:layout_height="42dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="310dp" />
</android.support.constraint.ConstraintLayout>
First Initialize Progressbar then use setProgress(100).
fuelBar =(ProgressBar)findViewById(R.id.fuelB);
lDoorPageOpen();
fuelBar.setProgress(100);
Check the order of your code, Progress bar must initialized before set progress.

Categories