Saving several EditText after program closes - java

I'm trying to make a Character sheet for a DnD like game. The point is to take user input and store it for future viewing and changing. Maybe I'm looking for a solution that doesn't exists but I want the EditTexts to stay the way they are if the user closes the app or just goes back to the title screen. Following is part of the xml file for the user input, the java file is basically blank
**<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/scroll_s">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/name"
android:inputType="textPersonName"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/player"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#id/player"
android:inputType="textPersonName"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/pts_total"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/unspent_pts"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#id/pts_total"
android:layout_weight="1"
android:inputType="number"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#id/unspent_pts"
android:layout_weight="1"
android:inputType="number"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/ht"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/wt"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/age"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#id/height"
android:inputType="number"
android:layout_weight="1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#id/weightt"
android:inputType="number"
android:layout_weight="1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#id/age"
android:inputType="number"
android:layout_weight="1"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/size_mod"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/appearance"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/appearance"
android:inputType="text"/>
</LinearLayout>
</ScrollView>**
Java file
package com.example.mapuchii.gurps;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Character_activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_character_redo);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Read values from the "savedInstanceState"-object and put them in your textview
}
#Override
protected void onSaveInstanceState(Bundle outState) {
// Save the values you need from your textview into "outState"-object
super.onSaveInstanceState(outState);
}
}

Here is one example:
public class Character_activity extends AppCompatActivity {
private EditText playerEditText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_character_redo);
playerEditText = (EditText) findViewById(R.id.player);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
playerEditText.setText(savedInstanceState.getString("SavedPlayer", "");
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString("SavedPlayer", playerEditText.getText().toString());
super.onSaveInstanceState(outState);
}
}
This will save your text over an orientation change. If you want to save it when your process is killed then you need to look at SharedPreferences instead of a Bundle.
http://developer.android.com/training/basics/data-storage/shared-preferences.html

Related

Can't make an activity actually launch [duplicate]

Im trying to call a second activity from my Main Activity, using this code
Intent goToOrder = new Intent(ListOfOrdersActivity.this, OrderSummaryActivity.class);
startActivity(goToOrder);
Being my OrderSummaryActivity:
public class OrderSummaryActivity extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.order_summary);
}
}
But the layout wont show on this activity, just a blank page.
This is the xml of the 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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/name"
android:textSize="20sp"
android:textStyle="bold"
android:layout_margin="10dp"
/>
<TextView
android:id="#+id/order_resume_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/quantity"
android:textSize="20sp"
android:textStyle="bold"
android:layout_margin="10dp"/>
<TextView
android:id="#+id/order_resume_quantity"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/whipped_cream"
android:textSize="20sp"
android:textStyle="bold"
android:layout_margin="10dp"/>
<TextView
android:id="#+id/order_resume_whipped_cream"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/chocolate"
android:textSize="20sp"
android:textStyle="bold"
android:layout_margin="10dp"/>
<TextView
android:id="#+id/order_resume_chocolate"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/order_summary_total"
android:textSize="20sp"
android:textStyle="bold"
android:layout_margin="10dp"/>
<TextView
android:id="#+id/order_resume_total"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Any hint?
try overriding
public void onCreate(Bundle savedInstanceState)
instead of
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState)
Try changing your OrderSummaryActivity.class to :
public class OrderSummaryActivity extends ActionBarActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.order_summary);
}
}

Can hide ProgressBar but cannot show it

I'm using the default ProgressBar widget, for indeterminate progress, in a Linear Layout with Style: #android:style/Widget.DeviceDefault.Light.ProgressBar.Small
If I start the activity with the ProgressBar VISIBLE and never make it INVISIBLE or GONE it shows ok. But if I put progressBar.setVisibility(progressBar.GONE); or progressBar.setVisibility(progressBar.INVISIBLE); anywhere in my code, the space for the ProgressBar is on the UI, but I cant see it.
I thought it might be a leaky parallel thread and removed all multi-threading from my app, but it still won't appear if INVISIBLE or GONE is anywhere in my code.
It works fine on other Activities.
Layout XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="260dp">
<ScrollView
android:id="#+id/myTeamsScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content_frame"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:orientation="horizontal">
<LinearLayout
android:layout_width="32dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:paddingTop="32dp">
<ImageView
android:id="#+id/menu1ImageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:contentDescription="#string/userNavigationMenuButtonContent"
android:onClick="openDrawer"
app:srcCompat="#drawable/menu_icon" />
</LinearLayout>
</LinearLayout>
<EditText
android:id="#+id/enterNewTeamEditText"
android:layout_width="340dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_gravity="center_horizontal"
android:contentDescription="#string/enterTeamNameEditTextcontent"
android:ems="10"
android:hint="#string/teamNameHint"
android:inputType="textEmailAddress"
android:textColor="#color/colorPrimary" />
<ProgressBar
android:id="#+id/myTeamsProgressBar"
style="#android:style/Widget.DeviceDefault.Light.ProgressBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:alpha="1"
android:foregroundTint="#FFFFCC"
android:progressTint="#FFFFCC"
android:visibility="visible" />
<TextView
android:id="#+id/deckTypeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:contentDescription="#string/remotePokerlabelcontent"
android:enabled="false"
android:text="#string/newTeamDeckType"
android:textAlignment="center"
android:textColor="#color/colorPrimary"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/remotePokerTextView" />
<Spinner
android:id="#+id/newTeamDeckTypeSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="8dp"
android:background="#drawable/spinner"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/enterNewTeamEditText" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:paddingTop="16dp">
<Button
android:id="#+id/createTeamButton"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:layout_gravity="center_horizontal"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:background="#color/colorButton"
android:contentDescription="#string/createTeamButtoncontent"
android:fontFamily="#string/fontfamily"
android:onClick="createTeam"
android:text="Create Team"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:paddingTop="16dp">
<Button
android:id="#+id/receivedJoinRequestsButton"
android:layout_width="147dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:background="#color/colorButton"
android:contentDescription="#string/createTeamButtoncontent"
android:fontFamily="#string/fontfamily"
android:onClick="showJoinRequests"
android:text="#string/myTeamsJoinRequestsButton"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp"
android:layout_weight="1" />
<Switch
android:id="#+id/teamTypesSwitch"
android:layout_width="143dp"
android:layout_height="wrap_content"
android:checked="false"
android:fontFamily="#string/fontfamily"
android:text="#string/switchMemberTeams"
android:textColor="#color/colorPrimary"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:orientation="vertical">
<ListView
android:id="#+id/myTeamsListView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="32dp"
android:layout_weight="1"
android:alwaysDrawnWithCache="true"
android:background="#drawable/spinner"
android:contentDescription="#string/myTeamsListViewcontent"
android:isScrollContainer="true"
android:scrollbars="vertical"
android:scrollingCache="true"
android:smoothScrollbar="true"></ListView>
</LinearLayout>
</LinearLayout>
</ScrollView>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:onClick="onSwitchChange"
app:headerLayout= "#layout/nav_header_main"
app:menu="#xml/drawer_view" />
</androidx.drawerlayout.widget.DrawerLayout>
In the Activity onCreate I call populateMemberTeams(); I added a CountDownTimer to check if the progressBar is on the screen and it is...Before the 2 seconds, there is a empty space, but I can't see the ProgressBar
When the timer expires the UI adjusts when the ProgressBar changes to GONE.
private void populateMemberTeams() {
ListView myTeamsListView = findViewById(R.id.myTeamsListView);
ProgressBar progressBar = findViewById(R.id.myTeamsProgressBar);
progressBar.setVisibility(progressBar.VISIBLE);
mAuth = FirebaseAuth.getInstance();
String userId = mAuth.getCurrentUser().getUid();
callTeamManagement.populateMemberTeams(TAG, passedActivity, context, userId);
new CountDownTimer(2000, 1000) {
public void onFinish() {
progressBar.setVisibility(progressBar.GONE);
}
public void onTick(long millisUntilFinished) {
// millisUntilFinished The amount of time until finished.
}
}.start();
}
I have tried:
Changing the ProgressBar style in case it was a colour issue;
INVISIBLE instead of GONE;
Putting the ProgressBar inside its own LinearLayout
progressBar.setVisibility(progressBar.VISIBLE);
progressBar.setVisibility(View.VISIBLE);
progressBar.setVisibility(ProgressBar.VISIBLE);
You should use runOnUiThread.
Helper method for running part of a method on the UI thread.
runOnUiThread(new Runnable() {
#Override
public void run() {
progressBar.setVisibility(View.VISIBLE);
//progressBar.setVisibility(View.INVISIBLE);
//progressBar.setVisibility(View.GONE);
}
});

Design Edit Text Code Editor with line number not scrollable

I am trying to design a code editor with the help of Android Edit Text. I am using a text view parallel to it which is showing line number. It is working fine but the problem is after going down more then 30 lines it text view remains same but edit text becomes scroll able I can make the text view scroll able but it will not going to scroll with respect to edit text. The main problem is of scrolling. If anyone can help me to correct this or some other way to use similar kind of simple activity to code with line numbers, So that further I will be able to save that text in a file.
Below is the Activity and xml code I have tried.
MainActivity.java
package com.rk.codeareadesignworkingedittextline;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText ed=(EditText)findViewById(R.id.editText);
final TextView tv=(TextView)findViewById(R.id.textView);
ed.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
int lines=ed.getLineCount();
String lineText="";
for (int i=1;i<=lines;i++){
lineText=lineText+i+"\n";
tv.setText(lineText);
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
}
Below is the xml file
<?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=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/textView"
android:layout_width="51dp"
android:layout_height="587dp"
android:background="#eeeeee"
android:gravity="center_horizontal"
android:padding="12dp"
android:text="1"
android:textSize="18dp" />
<EditText
android:id="#+id/editText"
android:layout_width="350dp"
android:layout_height="595dp"
android:ems="10"
android:gravity="left|top"
android:inputType="textMultiLine"
android:text=""
android:textSize="18dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Put your EditText in a ScrollView. And let the EditText and TextView expand as much as it can with wrap_content.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView"
android:layout_width="51dp"
android:layout_height="wrap_content"
android:background="#eeeeee"
android:gravity="center_horizontal"
android:padding="12dp"
android:text="1"
android:textSize="18dp" />
<EditText
android:id="#+id/editText"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="left|top"
android:inputType="textMultiLine"
android:text=""
android:textSize="18dp" />
</LinearLayout>
</ScrollView>
Try this
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/textView"
android:layout_width="51dp"
android:layout_height="match_parent"
android:background="#eeeeee"
android:gravity="center_horizontal"
android:padding="12dp"
android:text="1"
android:textSize="18dp" />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:textAlignment="textStart"
android:inputType="textMultiLine"
android:text=""
android:textSize="18dp" />
</LinearLayout>
</ScrollView>

My Button is not working to get next activity by clicking on them

I implemented Bottom navigation bar in my project. When I declare and initialize buttons and then set onClickListener for them. The onclicklistener is not working fine without any error in logcat. I think its logical mistake that I'm unable to understand please see the code and guide me. (If you think the question is not according to community standards then I'm sorry in advance because I'm a beginner in java)
I searched a lot of questions related to this but nothing works for me.
Here is my main activity:
public class HomeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
//I added this if statement to keep the selected fragment when rotating the device
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
}
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.nav_home:
selectedFragment = new HomeFragment();
break;
case R.id.nav_notifications:
selectedFragment = new NotificationFragment();
break;
case R.id.nav_search:
selectedFragment = new SearchFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
}
};
}
Here is my First Fragment named HomeFragment:
public class HomeFragment extends Fragment implements View.OnClickListener {
Button btnFertilizers;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
btnFertilizers = (Button)v.findViewById(R.id.btnFertilizers);
return inflater.inflate(R.layout.fragment_home, container, false);
}
#Override
public void onClick(View view) {
btnFertilizers.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(),Fertilizers.class);
getActivity().startActivity(intent);
}
});
}
Logcat shows zero error.
I believe that here is the problem area. This is because the click function is not working properly for Fertilizers.class:
#Override
public void onClick(View view) {
btnFertilizers.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(),Fertilizers.class);
getActivity().startActivity(intent);
}
});
}
Here is my FragmentHome.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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_green_light">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="230dp"
android:orientation="vertical"
android:gravity="center"
android:background="#drawable/agriculture_home">
<ImageView
android:layout_width="110dp"
android:layout_height="110dp"
android:src="#drawable/e_agriculture_logo"
android:layout_gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E-Agriculture"
android:textColor="#ffffff"
android:layout_gravity="center"
android:textStyle="bold"
android:textSize="29sp"
android:layout_marginTop="10dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="215dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="21dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="0dp"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/fertilizers"
android:layout_width="144dp"
android:layout_height="113dp"
android:layout_centerInParent="true"
android:src="#drawable/fertilizers_png" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/fertilizers"
android:text="Fertilizers"
android:textSize="20sp"
android:gravity="center"
android:textColor="#color/colorBlack"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="0dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btnFertilizers"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/seeds"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="#drawable/seed" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/seeds"
android:text="Seeds"
android:gravity="center"
android:textColor="#color/colorBlack"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btn_seeds"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="21dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="0dp"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/deseases"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="#drawable/deseases" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/deseases"
android:text="Diseases"
android:textSize="20sp"
android:gravity="center"
android:textColor="#color/colorBlack"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btn_deseases"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageViewPesticides"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="#drawable/pesticides" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageViewPesticides"
android:text="Pesticides"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:gravity="center"
android:textColor="#color/colorBlack"
android:layout_marginBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btnPesticides"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="21dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="0dp"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageViewWeatherForecast"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="#drawable/weatherforecast" />
<TextView
android:gravity="center"
android:textColor="#color/colorBlack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageViewWeatherForecast"
android:text="Weather Forecast"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btnWeatherForecast"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageViewArticles"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="#drawable/video" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageViewArticles"
android:text="Videos"
android:textSize="20sp"
android:gravity="center"
android:textColor="#color/colorBlack"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btnArticles"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="21dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="0dp"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageViewFeedback"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="#drawable/feedback" />
<TextView
android:gravity="center"
android:textColor="#color/colorBlack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageViewFeedback"
android:text="Feedback"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btnFeedback"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageViewLocation"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="#drawable/location_icon" />
<TextView
android:gravity="center"
android:textColor="#color/colorBlack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageViewLocation"
android:text="Location"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
tools:ignore="NotSibling" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btnLocation"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Here is my MainActivity.xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/bottom_navigation"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="#menu/bottom_navigation"
app:itemIconTint="#color/colorWhite"
app:itemTextColor="#color/colorWhite"
android:background="#color/colorGreen"/>
</RelativeLayout>
How I should setOnClickListener to access my wanted activity?
Move this code into onCreateView.
btnFertilizers.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(),Fertilizers.class);
getActivity().startActivity(intent);
}
});
remove this
implements View.OnClickListener
remove this
#Override
public void onClick(View view) { }
You are calling another onClick lisnter inside the onclick method so it will no work.You should have to change the code like this:
#Override
public void onClick(View view) {
if (view.getId() == R.id.btnFertilizers) {
Intent intent = new Intent(getActivity(),Fertilizers.class);
getActivity().startActivity(intent);
}
Return v in your onCreateViewMethod .
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
btnFertilizers = (Button)v.findViewById(R.id.btnFertilizers);
return v;
}

Android fragment oncreateview is called but view is not inflated and there are no errors

So I've done this a fair amount of times but I'm truly stumped this time. My fragment is called from my activity with no problems and although the onCreateView() is called, nothing at all shows up, still no errors. I've spent a couple hours looking for similar questions here with no luck.
Here is my swapFragment() method that calls my fragment:
private void swapFragment() {
GenresFragment genresFragment = new GenresFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.flgenre, genresFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
Here is my GenresFragment.java:
public class GenresFragment extends Fragment {
// TODO: Customize parameter argument names
private static final String ARG_COLUMN_COUNT = "column-count";
// TODO: Customize parameters
private int mColumnCount = 1;
private OnListFragmentInteractionListener mListener;
private DatabaseReference mPostReference;
String userID = FirebaseAuth.getInstance().getCurrentUser().getUid();
private ArrayList<String> genreList = new ArrayList<>();
private MyGenreRecyclerViewAdapter myGenreRecyclerViewAdapter;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public GenresFragment() {
}
// TODO: Customize parameter initialization
#SuppressWarnings("unused")
public static GenresFragment newInstance(int columnCount) {
GenresFragment fragment = new GenresFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
}
}
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_mini_add, container, false);
if (rootView == null){
Log.e("TEST1", "Is Null");
}else {
Log.e("TEST2", "Not Null");
}
return rootView;
}
public void failure(){
Toast.makeText(getActivity(),"Something Went Wrong",Toast.LENGTH_LONG).show();
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnListFragmentInteractionListener) {
mListener = (OnListFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnListFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnListFragmentInteractionListener {
// TODO: Update argument type and name
void onListFragmentInteraction(String item);
}
}
And my fragment_mini_add.xml:
<FrameLayout 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="#80000000"
tools:context="com.nocrat.fanti.ProfileActivity"
android:id="#+id/flgenre">
<!-- TODO: Update blank fragment layout -->
<RelativeLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="#ffffff"
android:layout_gravity="center">
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="14dp"
android:text="#string/add_new_project"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="24sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/rLayout1"
android:layout_below="#id/textView4">
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="#string/project_name"
android:gravity="center_vertical"
/>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center_vertical"
android:hint="#string/group_name"
android:inputType="textPersonName"
android:layout_below="#id/editText"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:padding="10dp"
android:layout_below="#id/rLayout1">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/create"
android:textColor="#FFD700"
android:textSize="18sp" />
<Space
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cancel"
android:textColor="#FFD700"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
</FrameLayout>
Here is my onClickListener that calls swapFragment():
addGenre.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
swapFragment();
}
});
Here is my activity xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:fitsSystemWindows="false"
android:background="#ffffff"
android:orientation="vertical"
android:id="#+id/flgenre">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:elevation="40dp"
android:background="#ffffff">
<include
layout="#layout/app_bar_profile"
android:layout_height="190dp"
android:layout_width="match_parent"
android:id="#+id/app1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_margin="20dp"
android:elevation="20dp"
android:background="#ffffff"
android:padding="10dp">
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView3"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
app:srcCompat="#drawable/icons8musicalnotes64" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Genres"
android:layout_gravity="center"
android:gravity="center_vertical"
android:padding="5dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/rounded_genres"
android:gravity="center"
android:clickable="true"
android:id="#+id/addGenres">
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#drawable/ic_add_white_48px" />
<TextView
android:id="#+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add"
android:layout_gravity="center"
android:gravity="center_vertical"
android:padding="5dp"
android:textColor="#ffffff"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_margin="20dp"
android:elevation="20dp"
android:background="#ffffff"
android:padding="10dp">
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView5"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
app:srcCompat="#drawable/icons8resume64" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:focusableInTouchMode="true">
<TextView
android:id="#+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center_vertical"
android:padding="5dp"
android:text="Bio" />
<EditText
android:id="#+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="false"
android:enabled="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="Tell us about yourself..."
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="#+id/textView12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="edit"
android:textColor="#FF00FF"
android:layout_gravity="right"
android:gravity="right"
android:padding="5dp"/>
<TextView
android:id="#+id/textView13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="save"
android:textColor="#FF00FF"
android:layout_gravity="right"
android:gravity="right"
android:padding="5dp"
android:visibility="gone"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_margin="20dp"
android:elevation="20dp"
android:background="#ffffff"
android:padding="10dp">
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView6"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
app:srcCompat="#drawable/icons8trophy64" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Average Rank"
android:layout_gravity="center"
android:gravity="center_vertical"
android:padding="5dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_margin="20dp"
android:elevation="20dp"
android:background="#ffffff"
android:padding="10dp">
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView7"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
app:srcCompat="#drawable/icons8meeting64" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Previous Collaborations"
android:layout_gravity="center"
android:gravity="center_vertical"
android:padding="5dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
I finally figured out the answer, all I had to do was change my root element(linear layout) to frame layout and it worked. Hope this helps someone. Thanks to everyone for their suggestions.
Try to modify you swapFragment() with below code by replacing replace() with add()
private void swapFragment() {
GenresFragment genresFragment = new GenresFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.flgenre, genresFragment);
fragmentTransaction.commit();
}
Other people already asked for the activity xml, but for what I could see right now, it looks like you are inflating the fragment's layout in a FrameLayout that is contained in it's own layout.
Meaning, the container you are inflating into should not be in the fragment's layout, but in the activity's layout.
Unless I understood it wrong, you should not have the with id/flgenre in fragment_mini_add.xml but in Activity_main.xml (or whatever the layout of the activity is)
Edit: in both activity and the fragment_mini_add.xml there is a layout with the same id you are trying to inflate into. I'm not sure if this causes the problem, but I imagine this could be the cause.
You should remove android:id="#+id/flgenre" in the first xml tag from your activity xml and fragment_mini_add.xml.
replace tools:context="com.nocrat.fanti.ProfileActivity" with tools:context="com.nocrat.fanti.GenresFragment" in fragment_mini_add.xml.
then, add this code to your activity xml to contain the fragment
<FrameLayout
android:id="#+id/flgenre"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Categories