Fragment Activity Display Back Button - java

I search to know how to display back button in a class which inherit from FragmentActivity,
public class GoogleMapsActivity extends FragmentActivity
I know there is the method onBackPressed() which can be override but i don't know how to display back button
#Override
public void onBackPressed() {
}
I tried this method : https://stackoverflow.com/a/43976262/7079226 but it doesn't work,
Thanks for your answers

Try this :-
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity)getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
and for back button to work.. use this :-
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
super.onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}

I have same problem with you. I already use ((AppCompatActivity)getApplicationContext()).setSupportActionBar(toolbar); but always getting force close.
Then my solution is create my own toolbar in the layout
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/overflow_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
app:srcCompat="?attr/homeAsUpIndicator" />
<TextView
android:id="#+id/textToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/overflow_back"
android:paddingLeft="32dp"
android:textSize="18sp"
android:textStyle="bold"
android:layout_toRightOf="#+id/overflow_back"
android:text="Toolbar Title"
android:textColor="#android:color/white" />
</RelativeLayout> </android.support.v7.widget.Toolbar>
overflow_back for backbutton, textToolbar for title.
In your activity:
imageBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});

Related

Showing created data from Activity in RecyclerView in a child fragment of TabLayout

I have a nav fragment section called GroupbookFragment which uses a TabLayout with 3 Tabs.
(GroupbookKrippeFragment, GroupbookHafenFragment & GroupbookKindergartenFragment)
Each Tab has a CardView with a RecyclerView in it. The goal is to have a global "Add Child" Button on the GroupbookFragment level, which forward you to another activity with a form where you can create a new child. After filling data & clicking a save button, you'll come back to the GroupbookFragmentview and the new created data from the form will show up in the correct CardView within one of the three corresponding fragments.
So far I successfully implemented all functionalities when the methods and "Add child" button are already IN the corresponding tab child fragment (e.g. GroupbookKrippeFragment). The problem here is, whenever I change the tab from the tabLayout, the "Add Child" button is moving away aswell. So after moving all methods & the button into the parent fragment GroupbookFragment I can navigate through all pages and fill the form & save, but the recyclerView won't update and show the new created data in the child's recyclerView anymore?!
Thanks in advance for any help! Here is the code view:
GroupbookFragment.java
public class GroupbookFragment extends Fragment {
// initializing elements
RecyclerView recyclerView;
ChildListAdapter childListAdapter;
List<Child> children = new ArrayList<>();
RoomDb database;
FloatingActionButton addChildBtn;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_groupbook, container, false);
// inflate the layout for GB Krippe fragment
View viewKrippe = inflater.inflate(R.layout.fragment_groupbook_krippe, container, false);
// link recycler view to correct element
recyclerView = viewKrippe.findViewById(R.id.recycler_view_krippe);
// Tab layout initialization
TabLayout tabLayout = view.findViewById(R.id.tabLayout);
ViewPager2 viewPager = view.findViewById(R.id.viewPager_groupBook);
GroupbookAdapter adapterGroupBook = new GroupbookAdapter(getChildFragmentManager(), getLifecycle());
viewPager.setAdapter(adapterGroupBook);
// set 3 Tab titles
tabLayout.addTab(tabLayout.newTab().setText("Krippe"));
tabLayout.addTab(tabLayout.newTab().setText("Hafen"));
tabLayout.addTab(tabLayout.newTab().setText("Kindergarten"));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
#Override
public void onPageSelected(int position) {
tabLayout.selectTab(tabLayout.getTabAt(position));
}
});
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// add Child code
database = RoomDb.getInstance(getContext());
children = database.mainDAO().getAll();
updateRecycler(children);
// link var to app element & set on click route
addChildBtn = view.findViewById(R.id.child_add_btn);
addChildBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(GroupbookFragment.this.getContext(), AddChildActivity.class);
startActivityForResult(intent, 101);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
if (resultCode == Activity.RESULT_OK) {
Child new_child = (Child) data.getSerializableExtra("child");
database.mainDAO().insert(new_child);
children.clear();
children.addAll(database.mainDAO().getAll());
childListAdapter.notifyDataSetChanged();
}
}
}
void updateRecycler(List<Child> children) {
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(GroupbookFragment.this.getContext()));
childListAdapter = new ChildListAdapter(GroupbookFragment.this.getContext(), children, childClickListener);
recyclerView.setAdapter(childListAdapter);
}
private final ChildClickListener childClickListener = new ChildClickListener() {
#Override
public void onClick(Child child) {
}
#Override
public void onLongClick(Child child, CardView cardView) {
}
};
}
GroupbookFragment.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".Fragments.HomeFragment">
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tabIndicatorAnimationMode="elastic" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#id/tabLayout">
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/viewPager_groupBook"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<!-- Add floating button to add child-->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/child_add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_margin="#dimen/margin_cardView"
android:backgroundTint="#color/green"
android:src="#drawable/ic_add_child"
app:tint="#color/white" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
GroupbookKrippeFragment.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"
tools:context=".Fragments.Groupbook.GroupbookKrippeFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="#+id/groupBook_krippe_1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="#dimen/margin_cardView"
app:cardCornerRadius="#dimen/cornerRadius_Card"
app:cardElevation="#dimen/elevation_default"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="#id/groupBook_krippe_2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/creme_500"
android:layout_weight="6"
android:padding="#dimen/padding_default">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline_group_1_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.1" />
<ImageView
android:id="#+id/ic_groupBook_krippe_1"
android:layout_width="#dimen/iconSize_groupBook_header"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="#drawable/ic_groupbook_krippe_1_logo"
app:tint="#color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="#id/guideline_group_1_start"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
<TextView
android:id="#+id/title_groupBook_krippe_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_default"
android:text="Gruppe 1"
android:textAllCaps="true"
android:textSize="#dimen/textSize_groupBook_title"
android:textColor="#color/white"
android:fontFamily="#font/candy_beans"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#id/guideline_group_1_start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/desc_groupBook_krippe_1"/>
<TextView
android:id="#+id/desc_groupBook_krippe_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_default"
android:text="Gesamt: 12 | Anwesend: 10"
android:textSize="#dimen/textSize_groupBook_subTitle"
android:textColor="#color/white"
android:fontFamily="#font/candy_beans"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#id/guideline_group_1_start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/title_groupBook_krippe_1"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="#dimen/padding_default"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:text="ID"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Vorname"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Nachname"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Geburtstag"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Geburtsort"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Konfession"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Nationalität"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
</TableRow>
</TableLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view_krippe"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>

Android Popup Menu fill parent

I try set my popup menu in way to fill hole item on grid. Currently it look like on attached first picture and the next one is effect which I would like to have.
My code:
private void showPopupMenu(View view) {
// inflate menu
ContextThemeWrapper ctw = new ContextThemeWrapper(context, R.style.PopupMenu);
PopupMenu popup = new PopupMenu(ctw, view);
Menu menu = popup.getMenu();
menu.add(Menu.NONE, 1, Menu.NONE, "Remove");
menu.add(Menu.NONE, 2, Menu.NONE, "Block");
popup.setOnMenuItemClickListener(new MyMenuItemClickListener());
popup.show();
}
Could you please point me to right direction to achieve effect from project?
Demo App for your requirment by using PopupWindow. Preview
You can add list in it or customize it according to your needs.
MainActivity
public class MainActivity extends Activity {
boolean isClicked = true;
PopupWindow popUpWindow;
RelativeLayout relative;
ImageView btnClickHere;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
relative = (RelativeLayout) findViewById(R.id.relative);
popUpWindow = new PopupWindow(this);
popUpWindow.setContentView(getLayoutInflater().inflate(R.layout.popup_design, null));
popUpWindow.getContentView().findViewById(R.id.textViewa).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "PopItemClicked", Toast.LENGTH_LONG).show();
}
});
btnClickHere = (ImageView) findViewById(R.id.imageView);
btnClickHere.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (isClicked) {
isClicked = false;
popUpWindow.setHeight(relative.getHeight());
popUpWindow.setWidth(relative.getWidth());
popUpWindow.showAsDropDown(relative, 0, -relative.getHeight());
} else {
isClicked = true;
popUpWindow.dismiss();
}
}
});
}
}
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"
tools:context="com.example.sohailzahid.testapp.MainActivity">
<RelativeLayout
android:id="#+id/relative"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#color/colorAccent"
tools:layout_editor_absoluteX="150dp"
tools:layout_editor_absoluteY="150dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:src="#android:drawable/arrow_down_float" />
</RelativeLayout>
</RelativeLayout>
popup_design.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F93567">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textViewa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Block"
android:textColor="#ffffff"
android:textSize="20dp" />
<TextView
android:id="#+id/textVsiewa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Add to friends"
android:textColor="#ffffff"
android:textSize="20dp" />
<TextView
android:id="#+id/textViesw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Remove"
android:textColor="#ffffff"
android:textSize="20dp" />
</LinearLayout>
<ImageView
android:id="#+id/imageView"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:src="#android:drawable/arrow_down_float" />
</RelativeLayout>
Try this
popup.getWindow().getAttributes().height = ViewGroup.LayoutParams.MATCH_PARENT;
popup.getWindow().getAttributes().width = ViewGroup.LayoutParams.MATCH_PARENT;

how to connect button with next screen on android studio

I am building this application on android studio and i am trying to link my button with next activity which is login screen. before i had it working with register screen but then i messed up with code now it just doesn't work when i run the application and click on register button my app crashes and shuts down and login button doesn't even do anything.
below is the code for main page activity and login page activity
first i will paste frontpage activity code where the button is, then its java class, then i will paste loginpage activity and then its java class.
can someone pls advice me how to call the login activity from the login button on the front page.
thank you so much in advance
frontpage activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Login">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:background="#drawable/bg3"
android:gravity="center|top">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Easy Booking"
android:id="#+id/textView"
android:textSize="33dp"
android:gravity="center"
android:textColor="#0c0c0c"
/>
<Button
android:layout_width="98dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Login"
android:id="#+id/btLogin"
android:onClick="bLogin"
android:background="#null"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="108dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Register"
android:id="#+id/btRegister"
android:onClick="bRegister"
android:background="#null"
android:layout_gravity="center_horizontal" />
</LinearLayout>
now its java class
public class Frontpage extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frontpage);
//OnclickButtonListener();
}
public void bLogin(View view) {
}
public void onButtonClick(View v){
if (v.getId() == R.id.btRegister) {
Intent i = new Intent(new Intent(Frontpage.this, Register.class));
startActivity(i);
}
}
/**
public void OnclickButtonListener(){
button = (Button)findViewById(R.id.bRegister);
button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("/Users/umairfarooq/AndroidStudioProjects/Easybooking/app/src/main/res/layo ut/activity_register");
startActivity(intent);
}
}
);
} /**#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_activity_login, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
*/}
below is the login activity
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="#635b5b"
android:orientation="vertical"
android:layout_width="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login Form"
android:textAppearance="?android:textAppearanceLarge"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
/>
<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:hint="Email"
android:id="#+id/etUsername"
android:layout_gravity="center_horizontal"
android:layout_marginTop="70dp"
/>
<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:hint="Password"
android:id="#+id/etPassword"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:inputType="textPassword"
/>
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Login"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:onClick="userLogin"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register Now"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:onClick="userReg"
/>
</LinearLayout>
and now login java class
package com.example.umairfarooq.easybooking;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
public class Login extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void buttonOnClick (View v){
}
}
The problem is that your activities don't contain the methods that you've set for your buttons' android:onClick attribute.
For the layout that the Frontpage activity is using, you can either change btRegister button's android:onClick attribute to android:onClick="onButtonClick" or create a public void bRegister(View v){...} method in that activity.
For the Login activity, the layout has two buttons with their android:onClick attribute set to userReg and userLogin, you can either create those methods in the activity or change both of those attributes values to buttonOnClick.
when i run the application and click on register button my app crashes
and shuts down
It is because this button:
<Button
android:layout_width="108dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Register"
android:id="#+id/btRegister"
android:onClick="bRegister"
android:background="#null"
android:layout_gravity="center_horizontal" />
in your frontpage activity needs a method with signature public void bRegister(View view) in your FrontPage.java class. As you do not have this method, it crashes.
login button doesn't even do anything
The reason is, this button
<Button
android:layout_width="98dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Login"
android:id="#+id/btLogin"
android:onClick="bLogin"
android:background="#null"
android:layout_gravity="center_horizontal" />
in your frontpage activity needs a method called public void bLogin(View view) in your Frontpage.java class. Though the method is present, you do not have any code in it, hence it does not do anything.
You need to add proper code in bLogin method so that your login button starts functioning, and even before that add a bRegister method so that your register button starts working.
Hope this helps you.

java.lang.IllegalArgumentException: Target must not be null

When I am passing image url to Picasso then it shows me error "Target must not be null". When I fetch image from url using Picasso then it shows me error. Even my id assigned to image is correct. At last I replaced the image url with static image, though I receive error as same.
Here is my code:
public class MainScreen extends AppCompatActivity {
private ImageView user_profile_pic,img2;
private ImageView like_button,dislike_button,location_button,refresh;
private ArrayList<String> al;
private ArrayAdapter<String> arrayAdapter;
private int i;
private SwipeFlingAdapterView flingContainer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
//Setting
user_profile_pic=(ImageView)findViewById(R.id.profile_image);
img2=(ImageView)findViewById(R.id.img2);
//Picasso.with(getApplicationContext()).load("https://pbs.twimg.com/profile_images/596282530652753921/bPf8NmOs.jpg").into(user_profile_pic);
//Picasso.with(getApplicationContext()).load(android.R.drawable.btn_star).into(img);
Picasso.with(getApplicationContext()).load("https://pbs.twimg.com/profile_images/596282530652753921/bPf8NmOs.jpg")
.placeholder(android.R.drawable.btn_star)
.error(android.R.drawable.btn_star)
.into(img2);
flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);
al = new ArrayList<>();
al.add("php");
al.add("c");
al.add("python");
al.add("java");
al.add("html");
al.add("c++");
al.add("css");
al.add("javascript");
arrayAdapter = new ArrayAdapter<>(this, R.layout.custom_user_details, R.id.helloText, al );
flingContainer.setAdapter(arrayAdapter);
flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
#Override
public void removeFirstObjectInAdapter() {
Log.d("LIST", "removed object!");
al.remove(0);
arrayAdapter.notifyDataSetChanged();
}
#Override
public void onLeftCardExit(Object dataObject) {
Toast.makeText(getApplicationContext(),"Left !",Toast.LENGTH_SHORT).show();
}
#Override
public void onRightCardExit(Object dataObject) {
Toast.makeText(getApplicationContext(),"Right !",Toast.LENGTH_SHORT).show();
}
#Override
public void onAdapterAboutToEmpty(int itemsInAdapter) {
al.add("XML ".concat(String.valueOf(i)));
arrayAdapter.notifyDataSetChanged();
Log.d("LIST", "notified");
i++;
}
#Override
public void onScroll(float scrollProgressPercent) {
try
{
View view = flingContainer.getSelectedView();
view.findViewById(R.id.item_swipe_right_indicator).setAlpha(scrollProgressPercent < 0 ? -scrollProgressPercent : 0);
view.findViewById(R.id.item_swipe_left_indicator).setAlpha(scrollProgressPercent > 0 ? scrollProgressPercent : 0);
}
catch (NullPointerException e) {
Log.e("tag", "NullPointerException" + e);
}
}
});
//flingContainer.getTopCardListener().selectRight(); To remove view from right side while pressing button
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_screen, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And this is what my xml is:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="80">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/img2"
android:src="#android:drawable/btn_star"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:padding="8dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vimal, "
android:id="#+id/helloText"
android:textColor="#android:color/black"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="23"
android:textColor="#android:color/black"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:src="#android:drawable/btn_dialog"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="25sp"
android:layout_marginLeft="5dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:src="#android:drawable/btn_dialog"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="25sp"
android:layout_marginLeft="5dp"
android:layout_gravity="center"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<View
android:id="#+id/item_swipe_left_indicator"
android:alpha="0"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="10dp"
android:background="#A5F" />
<View
android:id="#+id/item_swipe_right_indicator"
android:alpha="0"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="10dp"
android:layout_gravity="right"
android:background="#5AF" />
</FrameLayout>
img2 is null, and you're passing that into the into function.
Add a null check after img2=(ImageView)findViewById(R.id.img2); to check when it stops being null or whether it stays null while you're testing, or just to confirm that it is null.
Check your layout ID's and make sure they're right for ALL the configurations, and make sure you have #+id/img2 specified .
are you sure load the correct layout ? because you have R.id.profile_image in Java file, but i didn't found that id in your xml file.
You can't download image from https using Picasso library.
you can use this library.
try to change it as..
Picasso.with(MainScreen.this).load("https://pbs.twimg.com/profile_images/596282530652753921/bPf8NmOs.jpg")
.placeholder(android.R.drawable.btn_star)
.error(android.R.drawable.btn_star)
.into(img2);
also make sure that you are using the activity_main_screen.xml layout and check the id if img2 there.

Fragment replacing returns NullPointerException

There are many topics on this subject, but I couldn't find any solution that helped my case.
IDEA BEHIND:
What I have is a ViewPager, for swipping between 3 fragments. That works. But in one of the mentioned fragments, I do some functionalities - which then I check in another one of those before mentioned Fragments. Everything works.
Then, when I do the check, I open an AlertDialog, telling the user that his configuration is succesfull. By button click, I want that app transfers the user to one different Fragment (not one of the ViewPager fragments).
CODE SNIPPET - FragmentTwo:
public void displayAlertSuccess(Context ctx) {
new AlertDialog.Builder(ctx)
.setTitle("ACCESS GRANTED!")
.setMessage("Congratulations!")
.setPositiveButton("Great!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with
replaceFragment();
}
})
.setNegativeButton("Format my Card", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mainActivity.setNFC_ACTION("FORMAT");
}
})
.setIcon(android.R.drawable.ic_dialog_info)
.show();
}
private void replaceFragment() {
Fragment frag = new CardDesfire1();
getFragmentManager().beginTransaction().replace(R.id.content_frame, frag).commit();
}
CODE SNIPPET - XML Layouts:
I. activity_holder.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
II. activity_access.xml
<!-- activity_screen_slide.xml -->
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
III. fragment_access_slide2.xml (fragment that has the Dialog)
<!-- fragment_screen_slide_page.xml -->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0080ff">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/imageButton"
android:src="#drawable/img_access_slide2_2"/>
</ScrollView>
IV. activity_desfire1.xml (Fragment that I want to launch onClick)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#drawable/bg_des2">
<Button
android:layout_width="fill_parent"
android:layout_height="25dp"
android:text="You have discovered..."
android:id="#+id/btn_des1_discovered"
android:background="#3A5FCD"
android:typeface="monospace"
android:textColor="#ffffffff"
android:enabled="false"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/btn_loy"
android:text="Loyalty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttons_shape"
android:shadowColor="#000000"
android:textColor="#FFFFFF"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_above="#+id/btn_pay"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textAllCaps="false"
android:visibility="invisible"/>
<Button
android:id="#+id/btn_pay"
android:text="µPay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttons_shape"
android:shadowColor="#000000"
android:textColor="#FFFFFF"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:textAllCaps="false"
android:layout_alignParentBottom="true"
android:layout_marginBottom="70dp"
android:layout_centerHorizontal="true"
android:visibility="invisible"/>
<Button
android:id="#+id/btn_acc"
android:text="Access"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttons_shape"
android:shadowColor="#000000"
android:textColor="#FFFFFF"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:textAllCaps="false"
android:layout_above="#+id/btn_pay"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:visibility="invisible"/>
<ImageView
android:layout_width="wrap_content"
android:visibility="invisible"
android:layout_height="wrap_content"
android:id="#+id/img_card_des1"
android:layout_below="#+id/btn_des1_discovered"
android:src="#drawable/img_card_des1"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:visibility="invisible"
android:textSize="18dp"
android:gravity="center"
android:text="#string/des1_desc"
android:id="#+id/tv_des1_desc"
android:textColor="#ffffffff"
android:textStyle="italic"
android:background="#ff8db6cd"
android:layout_below="#+id/img_card_des1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="40dp" />
</RelativeLayout>
LINE OF ERROR:
getFragmentManager().beginTransaction().replace(R.id.content_frame, frag).commit();
Error Description from LogCat:
java.lang.NullPointerException
Error occurs when:
My error occurs when the user clicks the button "Great" in AlertDialog, which should then invoke the Fragment replacement.
I have tried:
I have tried to fix it with the getChildFragment , but then I get different kind of error: IllegalStateException : Activity has been destroyed.
Any help is kindly appreciated.
Have you declared default constructor in your CardDesfire1 fragment. If not then declare it
public CardDesfire1(){}
Make your parent Activity extend FragmentActivity
class ParentActivity extends FragmentActivity
The problem occurs because you call getFragmentManager() in a Fragment, you should delegate displaying the dialog to the activity.
Make activity implement an interface, and have the fragment call this interface method when it's time to show the dialog.
Example: You define a new inner interface in FragmentTwo(this code goes in FragmentTwo):
public interface OnReplaceFragmentListener {
void onReplaceFragment();
}
You link your activity to the fragment(this code goes in FragmentTwo):
private OnReplaceFragmentListener mCallback;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnReplaceFragmentListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnReplaceFragmentListener");
}
}
private void replaceFragment() {
mCallback.onReplaceFragment();
}
Then make activity implement OnReplaceFragmentListener.
And(this code goes into FragmentTwo's hosting activity):
#Override
void onReplaceFragment() {
Fragment frag = new CardDesfire1();
getFragmentManager().beginTransaction().replace(R.id.content_frame, frag).commit();
}

Categories

Resources