How to get a view from the xml file using LayoutInflater in a java class does not extend the activity

advertisements

I have a java class where I am displaying a dialog using an XML layout file. I want to set the text/contents of the layout dynamically.

To achieve this I am writing a method like this :

private void setContentMessage(String theMessage)
{
    View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_content, null, false);

    TextView titleMessage = (TextView) contentView.findViewById(R.id.title_message);
    titleMessage.setText(theMessage);
}

So here inside the inflate method I am using null because I don't know what to use.

Generally, we use ViewGroup object as the second argument in inflate method but I don't know how to create a ViewGroup inside a java class not extending Activity.

The function that I have written above is making not change inside the dialog layout. So please tell how can I inflate a layout in a java class.

AmitSmartDialog.java

package com.amitupadhyay.touchme.utility;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.amitupadhyay.touchme.R;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.Holder;
import com.orhanobut.dialogplus.OnCancelListener;
import com.orhanobut.dialogplus.OnClickListener;
import com.orhanobut.dialogplus.OnDismissListener;
import com.orhanobut.dialogplus.OnItemClickListener;
import com.orhanobut.dialogplus.ViewHolder;

/**
 * Created by aupadhyay on 12/9/16.
 */

public class AmitSmartDialog {

    Context context;

    public AmitSmartDialog(Context context) {
        this.context = context;
    }

    public void showDialog(int holderId, int gravity, boolean showHeader, boolean showFooter, boolean expanded, String message) {

        setContentMessage(message);
        Holder holder;
        holder = new ViewHolder(R.layout.dialog_content);

        OnClickListener clickListener = new OnClickListener() {
            @Override
            public void onClick(DialogPlus dialog, View view) {
                switch (view.getId()) {
                    case R.id.like_it_button:
                        Toast.makeText(context, "We're glad that you like it", Toast.LENGTH_LONG).show();
                        break;
                    case R.id.love_it_button:
                        Toast.makeText(context, "We're glad that you love it", Toast.LENGTH_LONG).show();
                        break;
                }
                dialog.dismiss();
            }
        };

        OnItemClickListener itemClickListener = new OnItemClickListener() {
            @Override
            public void onItemClick(DialogPlus dialog, Object item, View view, int position) {

            }
        };

        OnDismissListener dismissListener = new OnDismissListener() {
            @Override
            public void onDismiss(DialogPlus dialog) {

            }
        };

        OnCancelListener cancelListener = new OnCancelListener() {
            @Override
            public void onCancel(DialogPlus dialog) {

            }
        };

        showCompleteDialog(holder, gravity, new BaseAdapter() {
                    @Override
                    public int getCount() {
                        return 0;
                    }
                    @Override
                    public Object getItem(int i) {
                        return null;
                    }
                    @Override
                    public long getItemId(int i) {
                        return 0;
                    }
                    @Override
                    public View getView(int i, View view, ViewGroup viewGroup) {
                        return null;
                    }
                }, clickListener, itemClickListener, dismissListener, cancelListener,
                expanded);

    }

    private void showCompleteDialog(Holder holder, int gravity, BaseAdapter adapter,
                                    OnClickListener clickListener, OnItemClickListener itemClickListener,
                                    OnDismissListener dismissListener, OnCancelListener cancelListener,
                                    boolean expanded) {
        final DialogPlus dialog = DialogPlus.newDialog(context)
                .setContentHolder(holder)
                .setHeader(R.layout.dialog_header)
                .setCancelable(false)
                .setGravity(gravity)
                .setAdapter(adapter)
                .setOnClickListener(clickListener)
                .setOnItemClickListener(new OnItemClickListener() {
                    @Override public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                    }
                })
                .setOnDismissListener(dismissListener)
                .setExpanded(expanded)
                .setContentHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
                .setOnCancelListener(cancelListener)
                .setOverlayBackgroundResource(android.R.color.transparent)
                .create();
        dialog.show();
    }

    private void setContentMessage(String theMessage)
    {
        Toast.makeText(context, "HI BRO", Toast.LENGTH_SHORT).show();

        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_content, null, false);

        TextView titleMessage = (TextView) contentView.findViewById(R.id.title_message);
        titleMessage.setText(theMessage);
    }
}


Use this way :

LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.dialog_content, null,false);

Or

LayoutInflater inflater = LayoutInflater.from(getApplicationContext);
View contentView = inflater.inflate(R.layout.dialog_content, null,false);

UpDate :

Do some Change like this way

setContentMessage(message); pass context as parameter.

so change like this

setContentMessage(message, context);

and change this also.

private void setContentMessage(String theMessage,Context context)
    {

        View contentView = contxet.getLayoutInflater().inflate(R.layout.dialog_content, null);

        TextView titleMessage = (TextView) contentView.findViewById(R.id.title_message);
        titleMessage.setText(theMessage);
    }