Layout issue with Viewpages in Android - java

i work with viewpages i have 3 and i got this in every page.
i dont find the error to get this output]2
there ist a white stripe at the top and buttom. i dont find anything to fix it. here is my code:
and the story.java:
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class Story1Fragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_story1, container, false);
Button _play = (Button) rootView.findViewById(R.id.btn_play);
_play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
return rootView;
}
}
the main layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.jeravare.hypralyne.MainActivity">
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="368dp"
android:layout_height="495dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="8dp"></android.support.v4.view.ViewPager>
</android.support.constraint.ConstraintLayout>

The problem is not in the fragment layout.
You can change this:
android:layout_width="368dp"
android:layout_height="495dp"
android:layout_marginTop="8dp"
to this:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp" // you can remove this if you don't need a space at the top

Related

How use OnClickListener in Fragments

This is my code. I'm still learning android studio. I want to show a toast when the button is clicked but when I run the application and click the button it does nothing. (this is a Fragment). Does anyone know how to fix this? I tried putting "onClick" method in the xml(android:onClick="onClick") then the app crashed as soon as I clicked the button.
fragment_fragment_course.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=".fragmentCourse">
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Course"
android:textSize="25sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btnCourse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/textView2"
app:layout_constraintStart_toStartOf="#+id/textView2"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragmentCourse.java
package com.example.sma;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
public class fragmentCourse extends Fragment implements View.OnClickListener {
Button btn2;
public fragmentCourse() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View CView = inflater.inflate(R.layout.fragment_fragment_course, container, false);
btn2 = (Button)CView.findViewById(R.id.btnCourse);
final TextView txt = (TextView)CView.findViewById(R.id.textView2);
btn2.setOnClickListener(this);
return CView;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnCourse:
Toast.makeText(getActivity(), "done", Toast.LENGTH_SHORT).show();
break;
}
}
}
Override onViewCreated method and move below code to there. You can't access the views in onCreateView of fragment.
btn2 = (Button)CView.findViewById(R.id.btnCourse);
final TextView txt = (TextView)CView.findViewById(R.id.textView2);
btn2.setOnClickListener(this)

I want to add horizontally scrolled card view in recyclerview on my Homefragment.java class

Here is my MainActivity.java class
package com.example.bottomnavigationdrawer;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNavigationView=findViewById(R.id.bottomNavigationView);
NavController navController= Navigation.findNavController(this,R.id.nav_host_fragment);
NavigationUI.setupWithNavController(bottomNavigationView,navController);
}
}
Here is my activity_main.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=".MainActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:itemTextColor="#00FAF7F7"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_nav_menu" />
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here is my HomeFragment.java class:
package com.example.bottomnavigationdrawer;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.smarteist.autoimageslider.SliderView;
/**
* A simple {#link Fragment} subclass.
*/
public class HomeFragment extends Fragment {
public HomeFragment() {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
SliderView imageView = (SliderView) view.findViewById(R.id.imageSlider);
return view;
}}
Here is my fragment_home.xml:
<?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="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="3dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:paddingTop="3dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".HomeFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:gravity="center"
android:textSize="70sp"
android:textColor="#FAF8F8"
android:background="#25212E"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Home" />
</RelativeLayout>
I am new in Android, and I have searched a lot about it. But every tutorial I get they do this with main activity, model,adapter. But I have to do it on my home fragment in main activity.
Actually I don't understand your question. If you want to learn about recyclerview and card view and bottom navigation view, follow this tutorial:
https://www.androidhive.info/2016/01/android-working-with-recycler-view/
https://www.androidhive.info/2017/12/android-working-with-bottom-navigation/
It's very easy. If you want to set scroll horizontal use LinearLayoutManager like this
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView myRecyclerView= (RecyclerView) findViewById(R.id.my_recycler_view);
myRecyclerView.setLayoutManager(layoutManager);

Unable to change background color of a fragment

I am new to android and currently learning right now.
I faced a problem related to android-fragments, I write code and it did nothing! Just show the fragment color.
when I tried to remove the color of the fragment, it worked! I am unable to rectify this problem. Please help!
XML -activity_main.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=".MainActivity">
<Button
android:id="#+id/button"
android:layout_width="113dp"
android:layout_height="50dp"
android:text="Switch1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.945" />
<fragment
android:id="#+id/fragment"
android:name="com.example.fragments.Color_Purple"
android:layout_width="390dp"
android:layout_height="385dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.68"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.137" />
</androidx.constraintlayout.widget.ConstraintLayout>
XML - Fragment code
<?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_width="match_parent"
android:layout_height="match_parent"
tools:context=".Color_Purple">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C403FA" />
</FrameLayout>
JAVA-mainActivity.java
package com.example.fragments;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button b1;
Fragment f1;
Boolean flag=true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
f1=new Color_Purple();
// Toast.makeText(getApplicationContext(),"Button pressed",Toast.LENGTH_SHORT).show();
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
if(flag)
{
findViewById(R.id.fragment).setBackgroundColor(Color.GREEN);
// Toast.makeText(getApplicationContext(),"Button pressed Color GREEN",Toast.LENGTH_SHORT).show();
flag=false;
}
else
{
findViewById(R.id.fragment).setBackgroundColor(Color.RED);
//Toast.makeText(getApplicationContext(),"Button pressed Color RED",Toast.LENGTH_SHORT).show();
flag=true;
}
}
});
}
}
Java - fragment java code
package com.example.fragments;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Color_Purple extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_color__purple, container, false);
}
}
Note:
If i remove android:background from xml - fragment, this code works but not with color.
just change :
Boolean flag = true;
to
boolean flag = true;
and remove android:background="#C403FA" from your TextView :
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C403FA" //remove this line
/>

Unable to replace fragment on button click

I'm trying to learn how to implement fragments in android.
So, I created two buttons and a fragment in the activity_main.xml.
If I click on the first button, fragment_one should be inflated,
similarly if I click the second button, fragment_two should be inflated.
But the issue is it doesn't replace the fragment.
When I debug the code, performClick() method in the View.class returns false.
Also there is no error in LogCat.
I'm unable to figure out what is the issue with the code.
Here's the activity_main.xml
<?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" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/fragment1" />
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/fragment2" />
<fragment
android:id="#+id/fragment_place"
android:name="com.example.myfragmentwithbuttonapp.FragmentTwo"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Here's the fragment_one.xml
<?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"
android:background="#00ffff">
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="#string/thisisfragment1"
android:textStyle="bold" />
</LinearLayout>
Here's the fragment_two.xml
<?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"
android:background="#ffff00">
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/thisisfragment2"
android:textStyle="bold" />
</LinearLayout>
Here's the FragmentOne.java
package com.example.myfragmentwithbuttonapp;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(
R.layout.fragment_one, container, false);
}
}
Here's the FragmentTwo.java
package com.example.myfragmentwithbuttonapp;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentTwo extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(
R.layout.fragment_two, container, false);
}
}
Here's the MainActivity.java
package com.example.myfragmentwithbuttonapp;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Fragment fr;
FragmentManager fm;
FragmentTransaction fragmentTransaction;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fr = new FragmentOne();
fm = getFragmentManager();
fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fr = new FragmentTwo();
fm = getFragmentManager();
fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
}
});
}
}
I hope I have been able to explain the problem.
Any help is appreciated.
Thanks
For fragmentTransaction.replace() you need to specify a container for your fragment and the fragment itself. The container is the parent view which is going to hold your fragment. Tipically a FrameLayout.
Right now you are passing R.id.fragment_place as the container, but that id refers to a fragment and not to a fragment container.
In activity_main.xml, replace:
<fragment
android:id="#+id/fragment_place"
android:name="com.example.myfragmentwithbuttonapp.FragmentTwo"
android:layout_width="match_parent"
android:layout_height="match_parent" />
with:
<FrameLayout
android:id="#+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent" />
It works fine like this. The rest of your code is OK.

FragmentTest app is not working, my app keeps crashing

My app crash when ever I try to run my program .This is the code I am using .
MainActivity.java
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void selectFrag(View view) {
Fragment fr;
if(view == findViewById(R.id.button2)) {
fr = new FragmentTwo();
}else {
fr = new FragmentOne();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
}
}
FragmentOne.java
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(
R.layout.fragment_one, container, false);
}
}
FragmentTwo.java
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(
R.layout.fragment_one, container, false);
}
}
main_activity.xml
<?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" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Fragment No.1"
android:onClick="selectFrag" />
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"
android:text="Fragment No.2" />
<fragment
android:name="com.javacodegeeks.android.fragmentstest.FragmentOne"
android:id="#+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
fragment_one.xml
<?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"
android:background="#00ffff">
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="This is fragment No.1"
android:textStyle="bold" />
</LinearLayout>
The fragment_two.xml is also same as fragment_one.xml but in the TextView I am printing fragment no.2 instead of fragment no.1.
And in my AndroidManifest.xml file the min sdk version I am using is 11. Please guys enligthen me were I am going wrong and correct me .
I copied the code from this link
Since your package name is package name is com.example.fragmentstest, change this line:
android:name="com.javacodegeeks.android.fragmentstest.FragmentOne"
to
android:name="com.example.fragmentstest.FragmentOne"
in main_activity.xml

Categories

Resources