Change to Activity from Fragment - java

I'm new to Android programming. I have the question, that how i can intent from fragment to an activity.
I was trying to code it, but i failed :( ('l_places' is the layout of Fragment Places, and 'p_tr_fr' is a clickable TableRow)
Here's my code of the Fragment:
package com.fragment.toactivity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Places extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.l_places, container, false);//l_places is the layout
return rootView;
}
public void goToAttract(View v)
{
if (v.getId()== R.id.p_tr_fr) {
Intent intent = new Intent(getActivity(), Listact.class);
startActivity(intent);
}
}
public View onCreateView(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
};
}
and here is my XML:
<?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:background="#ffd4d4d4">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollView2" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="#ffffffff">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_gravity="bottom"
style="#android:style/Widget.AutoCompleteTextView"
android:layout_margin="0dp"
android:id="#+id/p_tr_fr"
android:onClick="onClick">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/imageView2"
android:src="#drawable/m_pf"
android:scaleType="fitCenter"
android:layout_marginLeft="6dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/s_shop"
android:id="#+id/textView3"
android:layout_column="1"
android:layout_gravity="center_vertical"
android:textStyle="bold"
android:textSize="18dp"
android:textColor="#ff000000"
android:layout_marginLeft="6dp" />
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>
When I click on the TableRow, the app stops. Please help!
(sorry for my english)

do like this,
Intent myIntent = new Intent(getActivity(), BookmarkActivity.class);
getActivity().startActivity(myIntent);

#NAP code should work fine. If app stops - give us the stack trace?
Possible reason is that your activity is not declared in AndroidManifest.
you should have something like
<activity android:name="yourpackage.Listact" />
between <application> tag

Related

I want to add stopwatch in a fragment but the app crashes on clicking the image buttons. how to resolve?

I wanted to add Stopwatch in a fragment with bottom navigation activity. I got no error at all but the app crashes on onclicking the image buttons I have created for start pause and reset. Please Help!
package com.example.chatapp;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Chronometer;
import android.widget.ImageButton;
public class StopwatchFragment extends Fragment {
private boolean running;
private long pauseoffset;
private Chronometer chronometer;
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable
Bundle savedInstanceState ){
View view = inflater.inflate(R.layout.fragment_stopwatch, null);
chronometer = view.findViewById(R.id.cm);
return view;
}
public void startcm (View view){
if (!running){
chronometer.setBase(SystemClock.elapsedRealtime() - pauseoffset);
chronometer.start();
running = true;
}
}
public void pausecm (View view){
if (running){
chronometer.stop();
pauseoffset = SystemClock.elapsedRealtime() - chronometer.getBase();
running = false;
}
}
public void resetcm (View view){
chronometer.setBase(SystemClock.elapsedRealtime());
pauseoffset = 0;
}
}
This is my java code of that fragment. Android Studio shows no error in this.
But here in my xml code of that fragment. In front of onclick it shows
cannot resolve symbol name "startcm"
cannot resolve symbol name "pausecm"
cannot resolve symbol name "restartcm"
<?xml version="1.0" encoding="utf-8"?>
<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=".StopwatchFragment">
<TextView
android:id="#+id/text_stopwatch"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title_stopwatch"
android:fontFamily="#font/bold"
android:textColor="#color/black"
android:textStyle="bold"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:gravity="center_horizontal" />
<Chronometer
android:id="#+id/cm"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="68dp"
android:fontFamily="#font/bold"
android:textSize="60sp"
android:textColor="#color/colorPrimary"
android:textAlignment="center"/>
<ImageButton
android:id="#+id/start"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_play_arrow_black_24dp"
android:background="#drawable/circular_button"
android:tint="#fff"
android:layout_marginTop="175dp"
android:layout_marginLeft="50dp"
android:onClick="startcm"/>
<TextView
android:id="#+id/text_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/start"
android:layout_marginTop="225dp"
android:layout_marginLeft="60dp"
android:textColor="#color/black"
android:fontFamily="#font/regular"
/>
<ImageButton
android:id="#+id/pause"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_pause_black_24dp"
android:background="#drawable/circular_button"
android:tint="#fff"
android:layout_marginTop="175dp"
android:layout_marginLeft="150dp"
android:onClick="pausecm"/>
<TextView
android:id="#+id/text_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pause"
android:layout_marginTop="225dp"
android:layout_marginLeft="157dp"
android:textColor="#color/black"
android:fontFamily="#font/regular"
/>
<ImageButton
android:id="#+id/reset"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/restart"
android:background="#drawable/circular_button"
android:tint="#fff"
android:layout_marginTop="175dp"
android:layout_marginLeft="250dp"
android:onClick="resetcm"/>
<TextView
android:id="#+id/text_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/reset"
android:layout_marginTop="225dp"
android:layout_marginLeft="258dp"
android:textColor="#color/black"
android:fontFamily="#font/regular"
/>
</RelativeLayout>
Please Help!!!
The xml onClick tag looks for your functions in your Activity, not in your fragment. Probably 3 solutions:
Move these functions to your Activity
Add view.findViewById(R.id.cmstart).setOnClickListener() to your fragment, and remove the xml tag.
You might can add a reference to your fragment in the xml tag, but I don't know how by heart.

EditText autoscroll doesn't work after showing dialog

I'm developing an app for users to share their books. To achieve this, I'm getting different data about the book from the user. The problem is, horizontal autoscrolling of all EditTexts are working until Dialog has been shown and dismissed. Once a dialog has been dismissed on the fragment, horizontal autoscrolling of the EditText's on this layout won't work.
I added:
android:focusable="true"
android:focusableInTouchMode="true"
to parent layout of the edittext but doesn't work.
Also to make sure that autoscrolling is enabled I've added:
android:scrollHorizontally="true"
But none of above helped.
Here is XML code of the fragment layout.
<?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:background="#color/white"
android:orientation="vertical"
android:paddingStart="#dimen/dp25"
android:paddingEnd="#dimen/dp25"
tools:context=".UI.Fragments.SharePostFragments.Fragment1.OverViewFragment">
<LinearLayout
android:id="#+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<EditText
android:id="#+id/edit_text_name_of_book"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:fontFamily="#font/segoe_ui_semi_bold"
android:hint="#string/name_of_book"
android:inputType="text"
android:maxLength="50"
android:maxLines="1"
android:scrollHorizontally="true"
android:textAlignment="textStart"
android:textColor="#color/colorPrimaryDark"
android:textColorHint="#color/colorPrimaryDark"
android:textSize="#dimen/font22" />
<TextView
android:id="#+id/text_view_number_of_characters"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="#dimen/dp10"
android:fontFamily="#font/segoe_ui_light"
android:text="#string/_0_50"
android:textColor="#color/colorAccent"
android:textSize="#dimen/font16"
tools:ignore="RtlSymmetry" />
<EditText
android:id="#+id/edit_text_name_of_author"
android:layout_width="match_parent"
android:layout_height="#dimen/textBoxHeight"
android:layout_marginTop="#dimen/dp30"
android:background="#drawable/round_text_box_gray"
android:fontFamily="#font/segoe_ui_regular"
android:hint="#string/name_of_writer"
android:inputType="text"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:textColor="#color/colorPrimaryDark"
android:textColorHint="#color/colorPrimaryDark"
android:textSize="#dimen/font16" />
<RelativeLayout
android:id="#+id/constraint_layout_1"
android:layout_width="match_parent"
android:layout_height="#dimen/textBoxHeight"
android:layout_marginTop="#dimen/dp20"
android:background="#drawable/round_text_box_border_gray"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp"
android:fontFamily="#font/segoe_ui_regular"
android:paddingStart="#dimen/dp20"
android:paddingEnd="#dimen/dp20"
android:text="#string/price_of_book"
android:textColor="#color/colorPrimaryDark"
android:textSize="#dimen/font16" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:paddingStart="#dimen/dp20"
android:paddingEnd="#dimen/dp20"
android:text="#string/azn_sign"
android:textColor="#color/colorPrimaryDark"
android:textSize="#dimen/font16" />
<EditText
android:id="#+id/edit_text_price_of_book"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="#+id/textView2"
android:background="#color/white"
android:clickable="false"
android:hint="#string/_0_0"
android:inputType="text|numberDecimal"
android:maxLength="6"
android:singleLine="true"
android:textColorHint="#android:color/black" />
</RelativeLayout>
<LinearLayout
android:id="#+id/constraint_layout_conditions_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dp20"
android:background="#drawable/round_text_box_border_gray"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/linear_layout_1"
android:layout_width="match_parent"
android:layout_height="#dimen/textBoxHeight"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="9"
android:gravity="center_vertical"
android:paddingStart="#dimen/dp20"
android:paddingEnd="#dimen/dp20"
android:text="#string/book_condition_placeholder"
android:textColor="#color/colorPrimaryDark" />
<ImageButton
android:id="#+id/image_button_conditions"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginEnd="#dimen/dp10"
android:layout_weight="1"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/ic_spinner" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear_layout_conditions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:id="#+id/text_view_new"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="#dimen/dp20"
android:paddingTop="#dimen/dp10"
android:paddingEnd="#dimen/dp20"
android:paddingBottom="#dimen/dp10"
android:text="#string/_new"
android:textColor="#color/dark_gray_text_color" />
<TextView
android:id="#+id/text_view_normal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="#dimen/dp20"
android:paddingTop="#dimen/dp10"
android:paddingEnd="#dimen/dp20"
android:paddingBottom="#dimen/dp10"
android:text="#string/normal"
android:textColor="#color/dark_gray_text_color" />
<TextView
android:id="#+id/text_view_old"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="#dimen/dp20"
android:paddingTop="#dimen/dp10"
android:paddingEnd="#dimen/dp20"
android:paddingBottom="#dimen/dp10"
android:text="#string/old"
android:textColor="#color/dark_gray_text_color" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/constraint_layout_languages_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dp20"
android:background="#drawable/round_text_box_border_gray"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/linear_layout_2"
android:layout_width="match_parent"
android:layout_height="#dimen/textBoxHeight"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="9"
android:gravity="center_vertical"
android:paddingStart="#dimen/dp20"
android:paddingEnd="#dimen/dp20"
android:text="#string/language"
android:textColor="#color/colorPrimaryDark" />
<ImageButton
android:id="#+id/image_button_languages"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginEnd="#dimen/dp10"
android:layout_weight="1"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/ic_spinner" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view_languages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="#dimen/dp20"
android:paddingEnd="#dimen/dp20"
android:paddingBottom="#dimen/dp10"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Fragment Java code:
package org.kitapp.UI.Fragments.SharePostFragments.Fragment1;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import org.kitapp.R;
import org.kitapp.UI.Dialogs.ConditionDialog.ConditionOfBookDialog;
import org.kitapp.UI.Dialogs.LanguageDialog.LanguageDialog;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
public class OverViewFragment extends Fragment implements OverViewContractor.View {
private final String TAG = OverViewFragment.class.getSimpleName();
private Context mContext;
private OverViewCallback mListener;
private OverViewPresenter mPresenter;
#BindView(R.id.constraint_layout_conditions_root)
LinearLayout conditionRoot;
#BindView(R.id.constraint_layout_languages_root)
LinearLayout languagesRoot;
#BindView(R.id.linear_layout_conditions)
LinearLayout conditions;
#BindView(R.id.root_layout)
LinearLayout rootLayout;
#BindView(R.id.text_view_number_of_characters)
TextView numberOfChars;
#BindView(R.id.edit_text_name_of_book)
EditText nameOfBook;
public interface OverViewCallback {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_over_view, container, false);
ButterKnife.bind(this, view);
new OverViewPresenter(this);
//rootLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
return view;
}
////// CONTRACTOR METHODS //////
#Override
public void setPresenter(OverViewPresenter presenter) {
this.mPresenter = presenter;
}
////// LISTENERS //////
#Override
#OnClick(R.id.image_button_conditions)
public void showConds() {
ConditionOfBookDialog conditionOfBookDialog = new ConditionOfBookDialog(mContext);
conditionOfBookDialog.show();
}
#Override
#OnClick(R.id.image_button_languages)
public void showLangs() {
LanguageDialog languageDialog = new LanguageDialog(mContext);
languageDialog.show();
}
#Override
#SuppressLint("SetTextI18n")
#OnTextChanged(R.id.edit_text_name_of_book)
public void onNameOfBookChange() {
int len = nameOfBook.getText().toString().trim().length();
numberOfChars.setText(len + "/50");
if (len > 50) {
nameOfBook.setText(nameOfBook.getText().toString().substring(0, 50));
nameOfBook.setSelection(nameOfBook.getText().length());
}
}
////// FRAGMENT METHODS //////
#Override
public void onAttach(Context context) {
super.onAttach(context);
this.mContext = context;
if (context instanceof OverViewCallback) {
mListener = (OverViewCallback) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OverViewCallback");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
}
UI Design and Dialog
In the image above, there are EditTexts Name Of Book and Name of Author. Before showing a dialog, both of them work like a charm. When writing something, the cursor automatically scrolls to the end. Once Dialog has been shown and dismissed, none of them works properly, and the cursor stays at the end of the EditText but still the characters are being written continues in the hidden part.
In this UI it cannot be seen because of the issue, but I've written.
Roses are red, violets are blue, Stackoverflow I love u
But only Roses are red, violets are blue, Stac can be seen.
Weird buggy layout UI image
Please, help me to solve this problem. Thanks in advance.
As a solution, I don't know the underlying reason, but the problem solved once I removed Butterknife library from my project.

Hide/Show TextView when touching button inside a Fragment

So I am here asking for your kind help.
I am trying to make a TextView visible and gone by touching a single button.
It did work inside a MainActivityJava but when doing the same inside a Fragment Activity, I am facing many troubles.
It seems that the setOnClickListener does not match with fragment but I do not know how to deal with it...
package com.androidbegin.locumatix;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class Tab1Fragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tab1, container, false);
return view;
}
Button button = getActivity().findViewById(R.id.buttonTaureauBrute);
TextView textView = getActivity().findViewById(R.id.textView3);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
textView.setVisibility(View.GONE);
textView.setVisibility(View.VISIBLE);
}
};
}
<?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"
android:background="#drawable/wallpaper_selection">
<ScrollView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="#+id/imageButtonTaureauSon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
android:scaleType="fitXY"
app:srcCompat="#drawable/prononciation_icone" />
<Button
android:id="#+id/buttonTaureauBrute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="5"
android:text="Prendre le taureau par les cornes"
android:textAlignment="center"
android:onClick="onClick"/>
</LinearLayout>
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_weight="1"
android:gravity="center"
android:visibility="gone"
android:text="Affronter une difficulté avec détermination"
android:textAlignment="center"
android:textColor="#color/colorAccent" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
app:srcCompat="#drawable/taureau" />
<Button
android:id="#+id/button10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Exemples :"
android:textAlignment="center" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="NATURE :\nverbe"
android:textAlignment="center"
android:textColor="#color/colorAccent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="REGISTRE :\nstandard"
android:textAlignment="center"
android:textColor="#color/colorAccent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="NIVEAU :\nB1"
android:textAlignment="center"
android:textColor="#color/colorAccent" />
</LinearLayout>
<Button
android:id="#+id/button14"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="EXPLICATIONS :" />
<Button
android:id="#+id/button11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="USAGES :" />
<Button
android:id="#+id/button13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lexique :" />
<Button
android:id="#+id/button12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Traductions :" />
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
Try this code
public class Tab1Fragment extends Fragment {
Button button;
TextView textView;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tab1, container, false);
button = getActivity().findViewById(R.id.buttonTaureauBrute);
textView = getActivity().findViewById(R.id.textView3);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if( view.getVisibility() == View.GONE ) {
textView.setVisibility(View.VISIBLE);
} else {
textView.setVisibility(View.GONE);
}
}
};
return view;
}
}

Update a TextView in a ViewPager from anohter Fragment

I have created a Tabbed Activity with 3 fragments. How can I update the text of a TextView in a Fragment from the MainActivity Java File?
TabStatus.java:
package com.sanderjochems.bluetoothdata;
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 TabStatus extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_status, container, false);
}
}
fragment_status.xml
<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:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sanderjochems.bluetoothdata.MainActivity"
tools:layout_editor_absoluteY="81dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="24dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title_bluetooth"
android:textAllCaps="false"
android:textColor="#color/colorPrimaryText"
android:textSize="24sp"
android:textStyle="bold" />
<Space
android:layout_width="match_parent"
android:layout_height="8dp" />
<TextView
android:id="#+id/tbMacAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/text_mac_address" />
<TextView
android:id="#+id/tbState"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/text_state" />
</LinearLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
I want to update the TextView tbState. How can I do that?
I tried to use a LayoutInflater, but that didnt work.
I used this piece of code for that:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layoutStatus = inflater.inflate(R.layout.fragment_status, null);
tvCurrentState = (TextView) layoutStatus.findViewById(R.id.tbState);
Regards,
Sander Jochems
Follow the documentation for communicating between fragments.
use event bus and fire the event when you perform an action in your fragment and catch that event in another fragment and update the desired textview.
follow this very simple library implementation and use
https://github.com/greenrobot/EventBus

Unable to Load Fragment from Main Activity

I am trying to load a frame on the Onclicklistener of a Button in the main activity. In my view I have written all the code correctly of main activity and fragment that requires to attach a fragment to activity. But something seems off. Appreciate any thoughts or suggestions.
I have used this link as a reference.
Below are the code details.
MainActivity.java
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends FragmentActivity {
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final android.app.ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Button btnLoad = (Button) findViewById(R.id.Button01);
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
AboutFragment mAboutFragment = new AboutFragment();
fragmentTransaction.add(R.id.fragment_container, mAboutFragment,"About");
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.commit();
}
};
btnLoad.setOnClickListener(listener);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_about"
android:minHeight="92dp"
android:layout_marginTop="40dp"
android:layout_marginLeft="40dp"
android:textSize="22sp"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_report"
android:id="#+id/Button02"
android:minHeight="92dp"
android:textSize="22sp"
android:layout_marginTop="40dp"
android:layout_marginLeft="80dp"
android:layout_toRightOf="#+id/Button01"></Button>
</RelativeLayout>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
AboutFragement.jave
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class AboutFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.about_fragment_main, container, false);
return view;
}
}
about_fragement_main.xml
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="vertical" >
<TextView android:id="#+id/about_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:paddingTop="30px"
android:text="header text"
android:textStyle="bold|italic"/>
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="text here" />
<TextView android:id="#+id/about_textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:text="zariya mission"
android:textStyle="bold|italic"/>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingTop="10px"
android:text="text here" />
<TextView android:id="#+id/about_textview3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:text="zariya vision"
android:textStyle="bold|italic"/>
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingTop="10px"
android:text="text here" />
<TextView android:id="#+id/about_textview4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:text="zariya values"
android:textStyle="bold|italic"/>
<TextView
android:id="#+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingTop="10px"
android:text="sometezt." />
</LinearLayout>
The error is in the main XML file.
You root is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
But then the first child have the height fill_parent this makes the first child take all the space of the LinearLayout so when you try to put your fragment in the container, it wont appear in the screen.
Instead try this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
....
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
Try to get used to use match_parent instead of fill_parent :)

Categories

Resources