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;
}
Related
I'm working on my app, and came to a huge problem. I need to implement buttons in my fragments and can't find a normal solution that works. this is the code:
package com.example.konjicdiscover10;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class SettingsFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_settings, container, false);
}
}
and XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
android:paddingTop="60dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="14dp"
android:background="#color/white">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/button"
android:layout_width="350sp"
android:layout_height="50sp"
android:layout_centerHorizontal="true"
android:textSize="11sp"
android:layout_marginVertical="20dp"
android:text="dark mode(in preparation)" />
<Button
android:id="#+id/button2"
android:layout_width="350sp"
android:layout_height="50sp"
android:layout_centerHorizontal="true"
android:layout_marginVertical="80dp"
android:text="Languages(in preparation)"
android:textSize="11sp"/>
</RelativeLayout>
</ScrollView>
basically what I'm looking for is that a buttons can lead to a new fragments or activities where content will be.
you need to add declare a View :
package com.example.konjicdiscover10;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
View view;
public class SettingsFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_settings, container, false);
Button myButton = view.findViewById(R.id.button);
myButton..setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//write what you want the button to do
}
});
return view;
}
}
add View :
View view;
And set the :
view = inflater.inflate(R.layout.fragment_settings, container, false);
and when you link your xml code with java you will use the 'view' like that :
myButton = view.findViewById(R.id.button);
finally you return the view
return view;
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;
}
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)}
}
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
I am trying to add buttons within a Fragment of PageView.
Please find attached the code that I am using for this application:
The specific code for the xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/banner_name"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:background="#drawable/button_banner"
android:gravity="center_vertical"
android:paddingLeft="10dip"
android:text="#string/equations"
android:textStyle="bold"
android:textColor="#ffffff" />
</LinearLayout>
<ScrollView
android:id="#+id/ScrollViewEquations"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/Button"
android:layout_width="fill_parent"
android:layout_height="25dip"
android:background="#drawable/custom_button"
android:gravity="center_vertical"
android:paddingLeft="25dip"
android:textColor="#516CE2"
android:text="#string/button" />
</LinearLayout>
</ScrollView>
</LinearLayout
this is the associated fragment code
package com.example.app;
import com.example.app![enter image description here][1].R;
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;
public class EquationsMain extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.equations, container, false);
return view;
}
}
and this is the main code:
package com.example.app;
import com.example.app.R;
import com.google.ads.AdRequest;
import com.google.ads.AdView;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Window;
import android.view.WindowManager;
public class Main extends FragmentActivity {
private MyAdapter mAdapter;
private ViewPager mPager;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
mAdapter = new MyAdapter(getSupportFragmentManager());
AdView adView = (AdView)this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return 3;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new EquationsMain();
case 1:
return new MaterialsMain();
case 2:
return new AboutMain();
default:
return null;
}
}
}
}