Hey I have a problem creating an animation inside of a fragment. I wanted to create a fadeIn animation with a "onClick", but when I click it, it crashes, and I don't understand why. I tried to create the Animation in a seperate XML file, but right now I it in the Fragment class.
Fragment XML file:
<?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"
android:background="#fff"
tools:context=".SecondFragment">
<ImageButton
android:id="#+id/button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#fff"
android:contentDescription="Betätigungsknopf"
android:onClick="onRotateButtonClicked"
android:src="#drawable/ic_adjust_150"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:background="#drawable/my_border"
android:text="Klicke um Suche zu starten."
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_down_50"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView7" />
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment JAVA class:
package com.example.appsplashscreen;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
/**
* A simple {#link Fragment} subclass.
*/
public class SecondFragment extends Fragment {
private ImageView i1;
public SecondFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_second, container, false);
}
public void onRotateButtonClicked(View view) {
// Tween Animation using Java code
i1 = (ImageView) getView().findViewById(R.id.imageView4);
Animation rotateAnimation = new RotateAnimation(
0,
360,
Animation.RELATIVE_TO_SELF,
0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
rotateAnimation.setDuration(500);
i1.startAnimation(rotateAnimation);
}
public void onStartSearchButtonClicked(View view) {
}
}
Errormessage:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.appsplashscreen, PID: 25131
java.lang.IllegalStateException: Could not find method onRotateButtonClicked(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatImageButton with id 'button'
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:436)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:393)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
If used Fragment then try below code.
issue in your onRotateButtonClicked
public class SecondFragment extends Fragment implements OnClickListener{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_second, container, false);
ImageButton button = (ImageButton) v.findViewById(R.id.button);
button.setOnClickListener(this);
return v;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button :
// your button click
i1 = (ImageView) getView().findViewById(R.id.imageView4);
Animation rotateAnimation = new RotateAnimation(
0,
360,
Animation.RELATIVE_TO_SELF,
0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
rotateAnimation.setDuration(500);
i1.startAnimation(rotateAnimation);
break;
}
}
}
its more better that just inflate layout in
onCreateView()
and you should implement your logic in
onViewCreated()
View rootView;
Animation rotateAnimation;
Button btn;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootview= inflater.inflate(R.layout.fragment_second, container, false);
return rootView;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setupViews();
}
private void setupViews(){
i1 = rootView.findViewById(R.id.imageView4);
btn=rootView.findViewByIdR.id.youtButton)
rotateAnimation = new RotateAnimation(
0,
360,
Animation.RELATIVE_TO_SELF,
0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
rotateAnimation.setDuration(500);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onRotateButtonClicked();
}
});
}
public void onRotateButtonClicked() { i1.startAnimation(rotateAnimation)}
}
Related
I was trying to do a simple log out process in Fragment. But before that, I take this error: Could not find method log_out(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton with id 'AppCompatButton'
And this is my ProfileFragment.java
package com.example.yemektarifi;
import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.firebase.auth.FirebaseAuth;
public class ProfileFragment extends Fragment {
FirebaseAuth firebaseAuth;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_profile, container, false);
}
public void log_out(View view){
firebaseAuth.signOut();
Intent intent = new Intent(ProfileFragment.this.getActivity(), LoginScreen.class);
startActivity(intent);
}
}
And this is fragment_profile.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=".ProfileFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="#+id/textView13"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="Profile Fragment"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/AppCompatButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="207dp"
android:background="#drawable/custom_button"
android:gravity="center"
android:onClick="log_out"
android:text="#string/log_out"
android:textColor="#CADCDA"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
I solved this problem using ViewGroup.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final ViewGroup viewGroup =(ViewGroup)inflater.inflate(R.layout.fragment_add,container,false);
firebaseAuth = firebaseAuth.getInstance();
button = viewGroup.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
firebaseAuth.signOut();
startActivity(new Intent(ProfileFragment.this.getContext(), LoginScreen.class));
}
});
return viewGroup;
}
I'm fairly new to android studio so I will try to explain as best as I can.
I've made a menu using fragments, so my activity_home is a fragmented activity. Inside the fragmented activity I've created a button that, upon clicking, should open a new activity.
The problem is that I don't know how to implement the onClickListener inside the fragmented activity.
Every tutorial I went trough does it from the beginning.
This is my main activity:
package com.example.relja.diplomskirad;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
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.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new home(), "Home");
adapter.addFragment(new profil(), "Profil");
adapter.addFragment(new mapa(), "Mapa");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
This is my main xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
This is home.java where i want to put the onclick listener:
package com.example.relja.diplomskirad;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class home extends Fragment {
public home() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_home, container, false);
}
}
And this is home xml where the button is:
<?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=".home">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="80dp"
android:orientation="vertical">
<LinearLayout
android:id="#+id/glavnaStranica1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:id="#+id/logoSlika"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="#drawable/logo2"
android:layout_gravity="center"/>
</LinearLayout>
<LinearLayout
android:id="#+id/glavnaStranica2"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginStart="30dp">
<EditText
android:id="#+id/search"
android:layout_width="250dp"
android:layout_height="38dp"
android:hint="#string/search"
android:background="#drawable/ivica"
android:drawableLeft="#drawable/search"
android:drawablePadding="10dp"
android:inputType="text"
/>
<Button
android:id="#+id/dugme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/dugme"
/>
</LinearLayout>
<Button
android:id="#+id/dugmeLogin"
android:layout_width="70dp"
android:layout_height="40dp"
android:text="#string/dugmeLogin"
android:layout_gravity="end"
android:layout_marginRight="10dp"
android:onClick="loginLogin"
style="?android:attr/borderlessButtonStyle"/>
</LinearLayout>
</RelativeLayout>
You can try this.. this will help
public class home extends Fragment {
private Button btn_dumge_login;
public home() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_home, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
btn_dumge_login = view.findViewById(R.id.dumgeLogin)
btn_dumge_login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), TestActivity.class) ;
getActivity().startActivity(intent);
}
});
}
}
View view = inflater.inflate(R.layout.your_layout_file, container, false);
view.findViewById(R.id.button_id).setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
getContext().startActivity(new Intent(getContext(), YourNewActivity.class));
});
return view;
you can use this code in your fragment 'home' in onCreateView() method
You should select first the view you want to be navigated from, for example, the Button with id dugmeLogin
then your code should be something like that
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_home, container, false);
Button loginButton = view.findViewById(R.id.dugmeLogin);
loginButton.setOnClickListener(v -> {
// You code goes here
});
return view;
}
this is in your home.java
Heyy all! I'm trying to learn fragments while coding, and I'm attempting to switch one fragment with another. Whenever I click the next button, the current fragment is replaced with a blank one and I have no idea why!
MainActivity.Java:
package com.alexoladele.testingshit;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import layout.WelcomeScreenFragment;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Makes sure that the root_layout view is not null before doing anything
if (findViewById(R.id.root_layout) != null) {
// Makes sure that there's no saved instance before proceeding
if (savedInstanceState == null) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction manager = fm.beginTransaction();
manager
.add(R.id.root_layout, WelcomeScreenFragment.newInstance(), "welcomeScreen")
.commit();
}
}
}
}
activity_main.xml:
<?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:id="#+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.alexoladele.testingshit.MainActivity">
</FrameLayout>
WelcomeScreenFragment.java:
package layout;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.alexoladele.testingshit.MainScreenFragment;
import com.alexoladele.testingshit.R;
// Declares WelcomeScreenFragment as subclass of fragment
public class WelcomeScreenFragment extends Fragment {
private static final String TAG = "WelcomeScreenFragment";
private Button startBtn;
// +++++++++++++++++++++ OVERRIDE - METHODS ++++++++++++++++++++++++++
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
// Attaches the Fragment
#Override
public void onAttach(Context context) {
super.onAttach(context);
}
// Creates Fragment view for use
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_welcome_screen, container, false);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
startBtn = (Button) view.findViewById(R.id.start_screen_btn);
startBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction
.replace(R.id.root_layout, MainScreenFragment.newInstance())
.addToBackStack(null)
.commit();
}
});
}
// +++++++++++++++++++++ User METHODS ++++++++++++++++++++++++++
// Method to create new instances of Fragment
public static WelcomeScreenFragment newInstance() {
return new WelcomeScreenFragment();
}
}
fragment_welcome_screen.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/welcome_screen_root"
tools:context="layout.WelcomeScreenFragment">
<TextView
android:id="#+id/start_screen_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="176dp"
android:fontFamily="monospace"
android:text="#string/welcome_message"
android:textAlignment="center"
android:textSize="13sp"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1"
app:layout_constraintHorizontal_bias="0.6" />
<android.support.v7.widget.AppCompatButton
android:id="#+id/start_screen_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/welcome_button"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="59dp"
app:layout_constraintTop_toBottomOf="#+id/start_screen_msg"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.5"
android:layout_below="#+id/start_screen_msg"
android:layout_centerHorizontal="true" />
</RelativeLayout>
MainScreenFragment.java:
package com.alexoladele.testingshit;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainScreenFragment extends Fragment {
#Override
public void onAttach(Context context) {
super.onAttach(context);
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
public static MainScreenFragment newInstance() {
return new MainScreenFragment();
}
}
fragment_main_screen.xml:
<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:id="#+id/main_screen_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.alexoladele.testingshit.MainScreenFragment">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="163dp"
android:text="#string/main_screen_tempmsg"
android:textAlignment="center"
android:visibility="visible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintTop_creator="1" />
</RelativeLayout>
you did not inflate your fragment_main_screen xml !
In your MainScreenFragment.java do that
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main_screen, container, false);
return view;
}
Inflate layout for MainScreenFragment also like you did it for WelcomeScreenFragment.
while creating fragment, we need to attach fragment xml, same as we are attaching in activity, but the method is different.
Add below code in MainScreenFragment.java:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_main_screen, container, false);
// do your code here.
return view;
}
I want to implement onclick in my fragment to change fragment.
Here is this function:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
super.onViewCreated(view, savedInstanceState);
imageView9 = (ImageView)view.findViewById(R.id.imageView3);
imageView9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Create new fragment and transaction
Fragment newFragment = new FragEditProfile();
// consider using Java coding conventions (upper first char class names!!!)
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.content_frame_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
});
It's 'clicking' cause I hear the typical button sound. But i'm getting this error when clicks:
ssl=0xaf840c00 cert_verify_callback x509_store_ctx=0xa17d2280 arg=0x0
ssl=0xaf840c00 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_RSA
Here is my main_activity layout:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000" >
<android.support.v7.widget.Toolbar
android:id="#+id/toolbarInner"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout android:id="#+id/content_frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#212"
android:layout_below="#+id/toolbar_app_bar_layout">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
<android.support.design.widget.AppBarLayout
android:id="#+id/toolbar_app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/top_points_bar" />
</LinearLayout>
<android.support.design.widget.TabLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar"
app:tabMode="scrollable"
app:paddingStart="16dp"
app:tabPaddingStart="16dp"
app:tabPaddingEnd="16dp"
app:tabMinWidth="96dp"
app:tabGravity="center" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<android.support.design.widget.NavigationView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="#color/md_brown_100"
app:menu="#menu/menu_drawer"
android:textColor="#color/okurwa"
app:theme="#style/MyTabStyle"
app:headerLayout="#layout/nav_drawer_header"
app:itemTextAppearance="#style/MyTabTextStyle"
android:clipToPadding="false">
<TextView
android:id="#+id/nav_drawer_wallet_id"
android:layout_width="177dp"
android:layout_height="wrap_content"
android:textSize="15sp"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginBottom="8dp"/>
<TextView
android:id="#+id/nav_drawer_total_credits"
android:layout_width="182dp"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_marginTop="200dp"
android:layout_marginLeft="16dp" />
<TextView
android:id="#+id/nav_drawer_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="22sp"
android:layout_marginTop="150dp"/>
<!--<FrameLayout-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_gravity="bottom"-->
<!--android:background="#color/md_white_1000"-->
<!--android:elevation="4dp"-->
<!--android:layout_marginBottom="-96dp">-->
<!--<Button android:id="#+id/navigation_button_footer"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:text=""-->
<!--android:textSize="13sp"-->
<!--android:textColor="#color/md_grey_800"-->
<!--android:lines="3"-->
<!--android:gravity="center"-->
<!--style="#style/Widget.AppCompat.ActionButton"-->
<!--android:paddingTop="20dp"-->
<!--android:paddingLeft="20dp"-->
<!--android:paddingRight="20dp"-->
<!--android:paddingBottom="20dp"/>-->
<!--</FrameLayout>-->
</android.support.design.widget.NavigationView>
<RelativeLayout
android:id="#+id/lay_connection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/actmain_margintop"
android:background="#3c3c3c"
android:visibility="gone" >
<TextView
android:id="#+id/text_error"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="#string/error_no_internet"
android:textColor="#color/md_white_1000"
android:textSize="#dimen/twentyfive" />
<RelativeLayout
android:id="#+id/lay_dialog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" >
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/progressBar1"
android:layout_centerHorizontal="true"
android:text="#string/error_no_internet"
android:textColor="#color/md_white_1000"
android:textSize="#dimen/twentyfive" />
</RelativeLayout>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
Please help me guys! I see this error first time. I don't know why I'm getting this error and why it's not working. Thanks Stack's Community!
EDIT
This is full code of this fragment:
/**
* Created by otsma on 12.12.2016.
*/
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import com.commonutility.PreferenceConnector;
import com.justfashion.R;
import java.util.Random;
public class OneFragment extends Fragment {
final Random rnd = new Random();
public OneFragment() {
// Required empty public constructor
}
private static TextView creditWallet;
private String[] myString;
private static final Random rgenerator = new Random();
private TextView firstName, textView13,textView14,textView15,textView16,textView17,textView18;
private Animation animShake, wbijam, load1, load2, load3, load4, load5, load6, wbijam1, wbijam2, wbijam3, wbijam4, wbijam5;
private ImageView img, imageView9,imageView19,imageView4,imageView5,imageView6,imageView7,imageView8;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public static void onUpdateView(Context aiContext) {
// TODO Auto-generated method stub
if (aiContext != null && creditWallet != null)
creditWallet.setText(PreferenceConnector.readInteger(aiContext, PreferenceConnector.WALLETPOINTS, 0) + "");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
imageView9 = (ImageView) view.findViewById(R.id.imageView3);
imageView9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Create new fragment and transaction
Fragment newFragment = new FragEditProfile();
// consider using Java coding conventions (upper first char class names!!!)
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.content_frame, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
});
img = (ImageView)view.findViewById(R.id.imgRandom);
imageView4 = (ImageView)view.findViewById(R.id.imageView4);
imageView5 = (ImageView)view.findViewById(R.id.imageView5);
imageView6 = (ImageView)view.findViewById(R.id.imageView6);
imageView7 = (ImageView)view.findViewById(R.id.imageView7);
imageView8 = (ImageView)view.findViewById(R.id.imageView8);
firstName = (TextView)view.findViewById(R.id.imie);
textView13 = (TextView)view.findViewById(R.id.textView13);
textView14 = (TextView)view.findViewById(R.id.textView15);
textView15 = (TextView)view.findViewById(R.id.textView16);
textView16 = (TextView)view.findViewById(R.id.textView17);
textView17 = (TextView)view.findViewById(R.id.textView18);
textView18 = (TextView)view.findViewById(R.id.textView19);
final Animation wbijam = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam);
textView13.startAnimation(wbijam);
final Animation wbijam1 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam);
textView14.startAnimation(wbijam1);
final Animation wbijam2 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam);
textView15.startAnimation(wbijam2);
final Animation wbijam3 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam);
textView16.startAnimation(wbijam3);
final Animation wbijam4 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam);
textView17.startAnimation(wbijam4);
final Animation wbijam5 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam);
textView18.startAnimation(wbijam5);
final Animation animShake = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.shak);
img.startAnimation(animShake);
final Animation load2 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load2);
imageView4.startAnimation(load2);
final Animation load3 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load3);
imageView5.startAnimation(load3);
final Animation load4 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load4);
imageView6.startAnimation(load4);
final Animation load5 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load5);
imageView7.startAnimation(load5);
final Animation load6 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load6);
imageView8.startAnimation(load6);
firstName.setText("Hey,"+" " +PreferenceConnector.readString(getActivity().getApplicationContext(), PreferenceConnector.FIRST_NAME, ""));
// Inflate the layout for this fragment
final ImageView img = (ImageView) view.findViewById(R.id.imgRandom);
// return view;
// ^^^^ error remove it
Resources res = getResources();
myString = res.getStringArray(R.array.myArray);
String q = myString[rgenerator.nextInt(myString.length)];
TextView tv = (TextView) view.findViewById(R.id.tv);
tv.setText(q);
// I have 3 images named img_0 to img_2, so...
final String str = "img_" + rnd.nextInt(9);
img.setImageDrawable
(
getResources().getDrawable(getResourceID(str, "drawable",
getActivity().getApplicationContext())
));
return view;
// ^^^ move it here
}
// I have 3 images named img_0 to img_2, so...
protected final static int getResourceID
(final String resName, final String resType, final Context ctx) {
final int ResourceID =
ctx.getResources().getIdentifier(resName, resType,
ctx.getApplicationInfo().packageName);
if (ResourceID == 0) {
throw new IllegalArgumentException
(
"No resource string found with name " + resName
);
} else {
return ResourceID;
}
}
}
remove super.onViewCreated(view, savedInstanceState);
return your view
please return the view
retun view;
if the problem still persists
To Make Sure whether the problem is with Onclick or not, comment
Everything inside the onclick method and put a Toast inside it. so, i
think we could get a clear picture about the problem..
try this !!
replace the following line:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
to this:
android.app.FragmentTransaction transaction = getActivity().getFragmentManager().beginTransaction();
Also don't forget to return view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
super.onViewCreated(view, savedInstanceState);
imageView9 = (ImageView)view.findViewById(R.id.imageView3);
imageView9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Create new fragment and transaction
Fragment newFragment = new FragEditProfile();
// consider using Java coding conventions (upper first char class names!!!)
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.content_frame_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
});
return view;
}
your fragment and fragment manager should be of same type.so either import
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
OR
import android.app.Fragment;
import android.app.FragmentTransaction;
java file
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
imageView9 = (ImageView) view.findViewById(R.id.imageView3);
imageView9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Create new fragment and transaction
Fragment newFragment = new FragEditProfile();
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, newFragment)
.addToBackStack(null)
.commit();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.content_frame, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
});
// your other contents.....
return view;
}
You better implement Interface for click event like this
public class fragment_test extends Fragment implements View.OnClickListener {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onClick(View v) {
switch (v.getItemId()) {
case R.id.action_zoom_in:
// .....
break;
}
return true;
}
}
}
In my app, I have three tabs. In each tab, I have a few controls. When I tried to implement the onClick method for my buttons, I found the buttons not responding to hand gestures, more specifically, the clicks.
Here is the class for the tab:
package com.telkitty.myPetProject
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.provider.Contacts;
import android.provider.ContactsContract;
public class ContactPage extends Fragment {
private static final int CONTACT_PICKER_RESULT = 1001;
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Activity activity = getActivity();
if (activity != null)
{
addListenerOnButton();
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.contact_page, container, false);
return view;
}
public void onListItemClick(ListView l, View v, int position, long id) {
Activity activity = getActivity();
}
public void addListenerOnButton()
{
Button add = (Button) getView().findViewById(R.id.add_button);
add.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}});
}
}
Here is the xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/edit_button"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dp"
android:text="#string/edit_contact"/>
<Button
android:id="#+id/add_button"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:text="#string/add_contact"/>
<ListView
android:id="#+id/contact_list"
android:layout_width="320dp"
android:layout_height="380dp"
android:paddingTop="50dp" >
</ListView>
</RelativeLayout>
What have I done wrong?
Define your views in the onCreateView(..) of your Fragment and register the listeners as followed:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.contact_page, container, false);
Button lEditButton = (Button) view.findViewById(R.id.edit_button);
Button lAddButton = (Button) view.findViewById(R.id.add_button);
ListView lContactList = (ListView) view.findViewById(R.id.contact_list);
lEditButton.setOnClickListener(this);
lAddButton.setOnClickListener(this);
lContactList.setOnItemClickListener(this);
return view;
}
Implement the onClickListener and onItemClickListener interface:
public class ContactPage extends Fragment implements onClickListener, onItemClickListener {
By using these interfaces you will be forced to override the onClick(View v) and onItemClick(..) where you can handle your actions for the buttons and list.
Ok, it just happens my buttons were actually beneath the ListView, so although they looked as if they were there, everytime I clicked on them, the ListView was actually selected.
I added the marked line, now the button is working as it should.
public void addListenerOnButton()
{
Button add = (Button) getView().findViewById(R.id.add_button);
add.bringToFront(); // <--- this line
add.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}});
}