BottomNavigationVIew: Glitch that overwrites other layout - java

I am using BottomNavigationView and I added ListView to my layout. After adding this ListView, the previous screen wont disappear and this new screen overwrites the previous screen. Does someone have an idea why this happens.
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import java.util.ArrayList;
public class favaoritesFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_favorites,container,false);
String[] line =getResources().getStringArray(R.array.JR_East);
ListView listView = (ListView)view.findViewById(R.id.listview);
ArrayAdapter<String>adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,line);
listView.setAdapter(adapter);
return view;
}
}
The issue.
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listview"/>
</RelativeLayout>

Add Background in your parent View of all fragment,
android:background="#android:color/white"

Related

buttons in fragment(android studio)

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;

Drag fragment up and down

I have the main activity which is composed by two fragments one called Canvas and the other is the Palette. In the main activity, the palette only appears on 25% of the screen and I want to make it possible so that the user can drag it up and down to choose colors from it. I have no idea how to do it.
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"
android:weightSum="2">
<FrameLayout
android:id="#+id/canvasFragment"
android:name="com.example.FragmentOne"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.5" />
<FrameLayout
android:id="#+id/paletteFragment"
android:name="com.example.FragmentTwo"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5" />
</LinearLayout>
mainActivity.java:
package com.example.paint;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**Canvas**/
// Create new fragment and transaction
Fragment canvasFragment = new CanvasFragment();
//canvasFragment.getView().setBackgroundColor(this.cor);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.canvasFragment, canvasFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
/**Palette**/
// Create new fragment and transaction
Fragment paletteFragment = new PaletteFragment();
FragmentTransaction transaction2 = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction2.replace(R.id.paletteFragment, paletteFragment);
transaction2.addToBackStack(null);
// Commit the transaction
transaction2.commit();
setContentView(R.layout.activity_main);
}
}
PaletteFragment.java:
package com.example.paint;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
public class PaletteFragment extends BottomSheetDialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_palette, container, false);
}
}
Thanks for any help given.
You can use BottomSheetDialogFragment to achieve required results. This is a dialog fragment in Android Material Library.
You have to have to extend your Palette fragment with BottomSheetDialogFragment and simply use following lines to show it in Activity:
PaletteFragment paletteFragment = new PaletteFragment();
paletteFragment.show(getSupportFragmentManager(), "TAGTEXT");
In your PaletteFragment, add listener for palette to slide using BottomSheetBehavior.BottomSheetCallback(). On this callback you can update palette height on slide.

How do I change the textview of a Fragment from WITHIN the fragment itself?

I have a MainActivity that creates 3 Fragments. Once I got into the first Fragment, I wish to change the Textview I have in the layout of THIS Fragment, from within this Fragment. I cannot see the 'findViewbyId' method popping up in the IDE when I try to access the TextView this way. Can someone please help me out? Here is the code for the Fragment which I want to carry out the operation in called 'FragmentOne'. The code is for 'FragmentOne.jav'
package com.iotaconcepts.aurum;
import android.app.Activity;
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.TextView;
import android.widget.Toast;
import com.iotaconcepts.aurum.R;
import java.io.*;
import java.util.*;
public class OneFrangment extends Fragment
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
Fragment fc=getTargetFragment();
TextView tv=(TextView)fc.findViewById(R.id.textview);//NOT WORKING
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(getActivity().getAssets().open("symp.txt")));
String parse1;
while((parse1=br.readLine())!=null)
{
Toast.makeText(getActivity(), parse1, Toast.LENGTH_LONG).show();
tv.setText(parse1 + "\n");
}
}
catch(Exception e)
{
tv.append("wtf\n");
Toast.makeText(getActivity(),"SORRY!", Toast.LENGTH_LONG).show();
}
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_one, container, false);
}
}
XML CODE:
<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="info.androidhive.materialtabs.fragments.OneFragment">
android:w
<TextView
android:id="#+id/textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hello"
/>
You need to assign the inflater.inflate method to a View before you can find views from it.
View v = inflater.inflate(R.layout.fragment_one, container, false);
TextView tv=(TextView)v.findViewById(R.id.textview);
...
return v;

Buttons not visible in gridview

I am new to android programming, and I am trying to get a button into an vertical scrolling grid. The buttons are not functional or visible. When I enable "Show layout bounds" in developer options, You see the gridview allocates space for the buttons::
My code is as follows:
layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/gridView1"
android:scrollbars="vertical"
android:numColumns="4"
/>
</LinearLayout>
Fragment:
import android.app.ActionBar;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.fairphone.datatransparency.R;
import java.util.Iterator;
/**
* Created by koen on 11/25/15.
*/
public class NewTimeLineFragment extends Fragment{
public static final String ARG_OBJECT = "object";
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// The last two arguments ensure LayoutParams are inflated
// properly.
View rootView = inflater.inflate(
R.layout.new_time_line_layout, container, false);
Bundle args = getArguments();
ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT);
Button[] buttons = new Button[6];
View.OnClickListener oclBtn = new View.OnClickListener()
{
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "GORGABAL", 5).show();
}
};
for(int i = 0; i < buttons.length; i++){
buttons[i] = new Button(getActivity());
buttons[i].setBackgroundColor(Color.BLACK);
buttons[i].setVisibility(View.VISIBLE);
buttons[i].setOnClickListener(oclBtn);
buttons[i].setLayoutParams(lp);
buttons[i].setText("GORGABAL");
}
GridView gridview;
gridview = (GridView) rootView.findViewById(R.id.gridView1);
ArrayAdapter<Button> adapter = new ArrayAdapter<Button>(this.getActivity(), android.R.layout.simple_list_item_1, buttons);
gridview.setAdapter(adapter);
gridview.setVisibility(View.VISIBLE);
Log.d("Debug", "new_time_line fragment created");
return rootView;
}
}

The Original Fragment won't go to back of stack, it stays visible

I am having some issues with FragmentTransactions. I have sucessfuly called on a fragment from the onClick method but the original mainFragment won't "disappear" or go to back of stack. I have followed the Android Development tutorial on Fragment Transcation. I think the issue is in my Survey.java class or my Details.java class. Any help is appreciated.
Update: (Added Photos below)
Before: http://imm.io/q9vt
After Clicking "Survey Button": http://imm.io/q9wf
See code for all below:
Code from Main.java
package mycompany.nst;
import mycompany.nst.R;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
Fragment _surveyFrag = new Survey();
public void focusFragment(Fragment mainFragment) {
FragmentManager FragMgr = getFragmentManager();
FragmentTransaction transaction = FragMgr.beginTransaction();
try {
transaction.replace(R.id.mainFragment, _surveyFrag);
transaction.addToBackStack(null);
transaction.commit();
} catch(Exception e){};
transaction = null;
FragMgr = null;
}
public void survey_onClick(View view) {
Log.i("onClick", "SURVEY BEGIN");
focusFragment(_surveyFrag);
}
}
Code from main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/hsdarker"
android:baselineAligned="false"
android:orientation="horizontal" >
<fragment
android:id="#+id/listFragment"
android:name="mycompany.nst.ListFragment"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
class="mycompany.nst.ListFragment" >
<!-- Preview: layout=#layout/list -->
</fragment>
<fragment
android:id="#+id/mainFragment"
android:name="mycompany.nst.Details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/listFragment"
class="mycompany.nst.Details" >
<!-- Preview: layout=#layout/details -->
</fragment>
</RelativeLayout>
Code from Details.java <-- The original fragment
package mycompany.nst;
import mycompany.nst.R;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Details extends Fragment {
/** Called when the activity is first created. */
#Override
public View onCreateView(LayoutInflater LIdetails, ViewGroup VGdetails, Bundle SaveInstancedState) {
Log.i("DetailsFragment", "onCreateView");
// Inflate the layout for this fragment
return LIdetails.inflate(R.layout.details, VGdetails, false);
}
}
Code from Details.java <-- The new fragment
package mycompany.nst;
import mycompany.nst.R;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Survey extends Fragment {
/** Called when the activity is first created. */
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.i("SurveyFragment", "onCreateView");
// Inflate the layout for this fragment
return inflater.inflate(R.layout.survey, container, false);
}
}
You can't replace or remove a fragment that's defined in XML- it becomes a non-removable part of the layout. However, if you define the fragments in code, you can replace and remove them at will.

Categories