0

I am working on an app which works well in landscape layout, but now I'm trying to get it to work in portrait layout as well. The problem is, setText does not update the text in the TextView which I am trying to display. The same fragment is called in landscape layout and works great.

MainActivity.java

//If landscape layout 
if(mTwoPane) {
    switch(index) {
        case 0:
        case 3:
            Bundle arguments = new Bundle();
            TextFragment tf = new TextFragment();
            if (index == 0) {
                arguments.putString("typ", "info");
            } else {
                arguments.putString("typ", "kontakt");
            }
            tf.setArguments(arguments);
            ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.details, tf);
            ft.commit();
            break;
        case 2:
           ..
           ..
}
//If portrait layout
else{
    Context context = view.getContext();
    switch(index) {
        case 0:
        case 3:
            Intent intent = new Intent(context, TextActivity.class);
            if(index == 0){
                intent.putExtra("typ", "info");
            }else{
                intent.putExtra("typ", "kontakt");
            }
            context.startActivity(intent);
            break;
        default:
            break;
    }
}

TextActivity.java

public class TextActivity extends AppCompatActivity {

    FragmentTransaction ft;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.portrait_details_layout);

        if (savedInstanceState == null) {
            Bundle arguments = new Bundle();
            arguments.putString("typ", getIntent().getStringExtra("typ"));

            TextFragment fragment = new TextFragment();
            fragment.setArguments(arguments);

            ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.detailss, fragment);
            ft.commit();
        }
    }
}

TextFragment.java

public class TextFragment extends Fragment {

    private static TextView textview;
    private String typ;
    private static final String TAG = TextFragment.class.getName();

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        if(getArguments().containsKey("typ")){
            typ = getArguments().getString("typ");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.text_fragment, container, false);

        textview = (TextView) view.findViewById(R.id.textView1);
        setText();

        Log.d(TAG, "typ = " + typ +"\ntextview.getText = " + textview.getText().toString());
        return view;
    }

    private void setText(){
        if(typ == "kontakt") {
            textview.setText("Kontakt..");
        }else if(typ == "info"){
            textview.setText("Info..");
        }
    }
}

text_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textAppearance="?android:textAppearanceLarge"
    android:autoLink="phone|email"/>

portrait_details_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.app.MainActivity"
    android:baselineAligned="false">

    <FrameLayout
        android:id="@+id/detailss"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?android:attr/detailsElementBackground" />

</LinearLayout>

In MainActivity.java, if mTwoPane = true, I directly create an instance of the fragment (and display it to the right of a ListView I have), but if it's in portrait mode I want it to be displayed alone, without the ListView, so therefore I call an Activity which is linked to another layout (portrait_details_layout.xml).

In TextFragment.java I print out a message to Logcat in onCreateView. If I run the app and try to view the fragment in landscape mode, the message prints:

index == 0:

com.example.app.TextFragment: typ = info
                              textview.getText = Info..

index == 3:

com.example.app.TextFragment: typ = kontakt
                              textview.getText = Kontakt..

Which is exactly what I want. Now, if I do the same in portrait mode:

index == 0:

com.example.app.TextFragment: typ = info
                             textview.getText = 

index == 3:

com.example.app.TextFragment: typ = kontakt
                              textview.getText = 

It just shows a blank screen. However, if I hard code the text in text_fragment.xml, it shows that text, and textview.getText will also return that text.

What am I missing here?

joakim.g
  • 71
  • 1
  • 6

1 Answers1

1

I think the problem is the way you compare Strings in your custom setText() in TextFragment class. You should use .equals() method and not == operator.

For example here or here you can read more about it.

Community
  • 1
  • 1
paulina_glab
  • 2,467
  • 2
  • 16
  • 25
  • Thank you so much! I had completely ruled out problems with the variable "typ", because the Logcat message showed it was given the expected value :) – joakim.g Oct 16 '16 at 13:27