0

I am facing problem to convert layout .xml files to Java I fave tried but failed to complete. I need anyones help to complete it. Need Help! Thanks in advance.

Here is my layout .xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lay"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <CheckBox android:id="@+id/checkbox_cheese"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cheese"
        android:onClick="onCheckboxClicked"/>
</LinearLayout>

I just want to convert it to java which i have tried so far:

    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.lay);
    CheckBox box = new CheckBox(this);
    box.setId(c);
    /* from here how to convert those below lines in Java         
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cheese"
        android:onClick="onCheckboxClicked"
    */
4
  • 2
    Have you tried to refer to the official tutorials that explain how to inflate views? Commented Dec 11, 2014 at 22:54
  • No sir i just want to simply do it. Please see my edited code sir. :) Commented Dec 11, 2014 at 22:56
  • 1
    I dont mean to be rude, but this is a place to help you figure out solutions to your problems, not ask other people to do your work for you. Commented Dec 11, 2014 at 23:09
  • @Laith Alnagem: This is obviously not work - but a novice question. Believe it or not, some people are actually beginners. No need to berate them for asking a beginner's question. You could encourage by explaining how and where to search or recommending a good read / tutorial. Sometimes people just need a push in the right direction. Commented Dec 11, 2014 at 23:25

3 Answers 3

1

I am in a good mood, or I would have just pointed you to Egor's comment; the tutorials are designed to show you the efficient ways to do what you want.

LinearLayout.LayoutParams boxParams = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
box.setLayoutParams(boxParams);
box.setText(R.string.cheese);

// if you want checkbox change listener
box.setOnCheckedChangedListener(new OnCheckedChangedListener(){
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (isChecked){
            // perform logic
        }
    }
};

// if you want on click listener functionality
box.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v){
        // perform logic
    }
}

linearLayout.addView(box);

Haven't tested it, don't know if it is bug free (may have missed something or done something wrong), but I can tell you that unless you plan on only doing this functionality and no other functionality you REALLY need to read into the basics of Android.

Sign up to request clarification or add additional context in comments.

2 Comments

Nice details answer. A few thoughts: setText() accepts resources directly - no need to get the string. i.e. box.setText(R.string.cheese). More readable IMO. Also setting an OnClickListener implicitly sets the View to be clickable, making the explicit call redundant.
@zgc7009 Thanks sir but i am getting nullpointerException in here linearLayout.addView(box); In addition i remove the checkbox tag in xml file
0
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

->setLayoutParams(..)

    android:text="@string/cheese"

->setText(..)

    android:onClick="onCheckboxClicked"

->setOnClickListener(..)

2 Comments

Sir in setLayoutParams(..) what should i write for "wrap_content" and in setOnClickListener(..) what should i write inside it?
@RazilKala The fact you have to ask those questions is 100% why people are telling you that you should read the tutorials. That stuff is Android 101.
0

Do something like this:

View layout = ViewInflater.from(this).inflate(R.layout.layout, null); //your layout name

LinearLayout linearLayout = (LinearLayout) layout.findViewById(R.id.lay);
CheckBox box = (CheckBox) layout.findViewById(R.id.checkbox_cheese);

Now the box object will have all the properties assigned in the xml.

2 Comments

Actually sir, i dont to want to use findViewByID of checkbox i want to create it in java and set its attributes in java.
Very well, but you should do it this way, this is the proper Android way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.