Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android update layout from another class

Tags:

java

android

I have two classes so far..

MainActivity.java

public class MainActivity extends AppCompatActivity {

private PageBuilder pb = new PageBuilder();

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

    pb.mainMenu();
} }

PageBuilder.java

public class PageBuilder extends MainActivity {

public void mainMenu() {
    LinearLayout ll = (LinearLayout) findViewById(R.id.empty);
    LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    Button btnOne = new Button(getApplicationContext());
    btnOne.setLayoutParams(btnParams);
    btnOne.setText("One");
    ll.addView(btnOne);

    Button btnTwo = new Button(getApplicationContext());
    btnTwo.setLayoutParams(btnParams);
    btnTwo.setText("Two");
    ll.addView(btnTwo);

    Button btnThree = new Button(getApplicationContext());
    btnThree.setLayoutParams(btnParams);
    btnThree.setText("Three");
    ll.addView(btnThree);
} }

I think the issue is that i'm trying to findViewById from another class? It works when I have the method in the same class as it's called.

I have tried public class PageBuilder extends Activity && AppCompatActivity. The method is called but it doesn't like LinearLayout ll = (LinearLayout) findViewById(R.id.empty); (empty.xml does exist and is in the layout dir).

The only real reason I have these methods in another class is for cleanliness, but I would have thought this was a pretty basic request.

like image 714
TomFirth Avatar asked Sep 13 '26 21:09

TomFirth


1 Answers

In you MainActivity

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    PageBuilder obj = new PageBuilder(this);
    obj.mainMenu();
}}

In PageBuilder.java

public class PageBuilder {

Context mContext;
PageBuilder(Context mContext){
    this.mContext = mContext;
}

public void mainMenu() {
    LinearLayout ll = new LinearLayout(mContext);
    LinearLayout.LayoutParams llP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    ll.setLayoutParams(llP);
    ll.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    Button btnOne = new Button(mContext.getApplicationContext());
    btnOne.setLayoutParams(btnParams);
    btnOne.setText("One");
    ll.addView(btnOne);

    Button btnTwo = new Button(mContext.getApplicationContext());
    btnTwo.setLayoutParams(btnParams);
    btnTwo.setText("Two");
    ll.addView(btnTwo);

    Button btnThree = new Button(mContext.getApplicationContext());
    btnThree.setLayoutParams(btnParams);
    btnThree.setText("Three");
    ll.addView(btnThree);
    Activity activity = (Activity)mContext;
    activity.setContentView(ll);
}}
like image 64
Rana Shahzaib Avatar answered Sep 16 '26 10:09

Rana Shahzaib



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!