Rahul Waghmare
Rahul Waghmare

Reputation: 68

How to replace text of textView in fragment

I want to change a text of TextView on clicking a button. I have 2 buttons in my fragment and both have an onClick method for same TextView. Here is in detail what's happen in my fragment.

I set a default text to TextView means 1st text. When 1st button clicked the 2nd text will appear on the TextView. When 2nd button clicked again 1st text will appear on the TextView.

Here is my Fragment code

@Override
public void onCreate(Bundle savedInstanceState) {
    Log.d("Rahul", "On Ganpati 1 fragment On Create ");
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    Log.d("Rahul", "On Ganpati 1 fragment On Create View ");
    // Inflate the layout for this fragment

    final TextView textView = (TextView)getView().findViewById(R.id.g1TextView);
    textView.setText(getString(R.string.Ganpati_1));

    Button marathi = (Button)getView().findViewById(R.id.g1Marathi);
    marathi.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            textView.setText(getString(R.string.Ganpati_1));
        }
    });

    Button english = (Button)getView().findViewById(R.id.g1English);
    english.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            textView.setText(getString(R.string.GanpatiE));
        }
    });
    return inflater.inflate(R.layout.fragment_ganpati_1, container, false);
}

Here is the XML file

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.lenovo.shopping.Shankar">

    <Button
        android:layout_width="168dp"
        android:layout_height="50dp"
        android:text="Left"
        android:id="@+id/g1Marathi"
        android:textAlignment="center"
        android:layout_gravity="left|top" />

    <Button
        android:layout_width="168dp"
        android:layout_height="50dp"
        android:text="Right"
        android:id="@+id/g1English"
        android:textAlignment="center"
        android:layout_gravity="right|top" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:paddingBottom="50dp"
        android:id="@+id/scrollView"
        android:layout_gravity="end|fill_vertical">

        <TextView
            android:id="@+id/g1TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/TextView" />

    </ScrollView>

</FrameLayout>

Upvotes: 2

Views: 4170

Answers (1)

Danh DC
Danh DC

Reputation: 1246

When use Fragment, you need to override onCreateView and return View like this :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_ganpati_1, null);
    Log.d("Rahul", "On Ganpati 1 fragment On Create View ");
    // Inflate the layout for this fragment

    final TextView textView = (TextView)view.findViewById(R.id.g1TextView);
    textView.setText(getString(R.string.Ganpati_1));

    Button marathi = (Button)view.findViewById(R.id.g1Marathi);

    marathi.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        textView.setText(getString(R.string.Ganpati_1));
        }
    });

    Button english = (Button)view.findViewById(R.id.g1English);
    english.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        textView.setText(getString(R.string.GanpatiE));
        }
    });
    return view;
}

Upvotes: 7

Related Questions