Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setContentView and findViewById

Tags:

android

After reading some related posts I already know that I have to explicitly call to setContenView before trying to find by id an element into an Activity-View-Hierarchy. I've created an Activity with an inflated view and all works fine. The problem is when I try to set another view to this activity by using the next xml and code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:background="#B12A6D"
tools:context=".MainActivity"
android:orientation="vertical"
>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="top"
    android:background="@drawable/bg_purple_500"
    android:layout_margin="0dp"
    >

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/GridLayout1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="50"
        android:columnCount="4"
        android:rowCount="4"
        android:orientation="horizontal"
        tools:context=".GridXMLActivity"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        >

         <ImageView
            android:id="@+id/my_bnt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon_new_game"
            android:contentDescription="@string/text"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginBottom="10dp"
            />
</GridLayout>

</LinearLayout>

</RelativeLayout>

code:

public class MainActivity extends ActionBarActivity {

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

    setContentView(R.layout.activity_main);

}


....

protected void method_triggered_from_UI_thread() {

  // Inflate other layout...
  View view = View.inflate(this, R.layout.my_layout, null) ;
  setContentView(view);
  ImageView btn = (ImageView) findViewById(R.id.my_bnt);
  // btn is null...

}

I've tried other slight variations:

View mainView = findViewById(android.R.id.content);
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.my_layout, (ViewGroup) mainView, false)  ;
setContentView(view);
ImageView btn = (ImageView) view.findViewById(R.id.my_bnt);

And I always get btn == null. What am I doing wrong?

Thx

like image 510
acimutal Avatar asked Oct 08 '15 05:10

acimutal


People also ask

What is setContentView () used for?

SetContentView is used to fill the window with the UI provided from layout file incase of setContentView(R. layout. somae_file). Here layoutfile is inflated to view and added to the Activity context(Window).

What is findViewById used for?

FindViewById<T>(Int32)Finds a view that was identified by the id attribute from the XML layout resource.

What is the different between setContentView () function and startActivity () function?

So, with respect to time, setContentView alone is faster, since it doesn't start a new activity. Hence, your app will show the new screen faster... On the other hand, if you call startActivity, this activity is put on the stack, so you can go back by pressing the back button.

Why do we need to call setContentView () in onCreate ()?

As onCreate() of an Activity is called only once, this is the point where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById to programmatically interact with widgets in the UI, calling managedQuery(android.


1 Answers

You could try LayoutInflater.

View inflatedView = LayoutoutInflater.from(context).inflate(R.layout, null);
setContentView(inflatedView);

Happy codings

like image 102
ralphgabb Avatar answered Sep 16 '22 11:09

ralphgabb