I have to set a message and title in onPreExcecute() but am not able to do it. ProgressDialog had the setMessage() method, but ProgressBar does not.
protected void onPreExecute() {
progressBar.
super.onPreExecute();
}
Use ProgressBar to show progress and TextView to show message:
<ProgressBar
android:id="#+id/indeterminateBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
Create your own ProgressBar and put a Textview next to it. A library from https://github.com/Q115/DelayedProgressDialog will do the trick.
Usage:
DelayedProgressDialog progressDialog = new DelayedProgressDialog();
progressDialog.show(getSupportFragmentManager(), "tag");
You can use AlertDialog by overriding onCreateDialog after extending DialogFragment. You should design an xml that contains a ProgressBar and a TextView(for the message). Then call the setView() method of the AlertDialog on the xml like below.
Your layout in xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ProgressBar
android:id="#+id/indeterminateBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/progress_bar_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="loading..."
/>
</LinearLayout>
Your DialogFragment Class
public class Dialog extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new
AlertDialog.Builder(getActivity());
//use layoutinflater to inflate xml
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.xml_containing_progressbar,null));
return builder.create();
}
}
you can now call .show on an instance of the DialogFragment Class
Dialog dialog = new Dialog();
dialog.show(getSupportFragmentManager(), "tag");
source: https://developer.android.com/guide/topics/ui/dialogs
I know it's late but better late than never...
I had the same problem so I created a custom ProgressBar.
First you make the class for the view:
public class CustomProgressBar extends RelativeLayout {
private ProgressBar progressBar;
private TextView textView;
public CustomProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.custom_progress_bar, this);
progressBar = findViewById(R.id.progressBarCustom);
textView = findViewById(R.id.pbTV);
init(attrs);
private void init(#Nullable AttributeSet attrs) {
if (attrs != null) {
TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.CustomProgressBar);
String textAttrib = ta
.getString(R.styleable.CustomProgressBar_text);
textView.setText(textAttrib);
ta.recycle();
}
}
public void setProgressBar(ProgressBar progressBar) {
this.progressBar = progressBar;
}
public String getText() {
return textView.getText().toString();
}
public void setText(String text) {
textView.setText(text);
}
public void setText(int resid) {
textView.setText(resid);
}
}
Then, in order to be able to set this class attributes from the layout, we need to make it styleable. So you need to create an xml file called "attrs.xml" within the directory "values".
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomProgressBar">
<attr name="text" format="string"/>
</declare-styleable>
</resources>
Now we create the layout for our custom view:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content">
<ProgressBar
android:id="#+id/progressBarCustom"
style="?android:attr/progressBarStyle"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerInParent="true"/>
<TextView
android:id="#+id/pbTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"/>
</RelativeLayout>
Finally, just use it wherever you want:
<com.example.mypackage.CustomProgressBar
android:id="#+id/radioGroupProgressBar"
android:layout_width="match_parent"
android:layout_height="150dp"
app:text="Loading..."
android:layout_gravity="center_horizontal"
/>
And that's it :)
Related
I create a class for custom dialog and I used PrettyDialog. I want an edittext on dialog. I use Inflater but Error: getActivity -> mHost:null. I tried create getTextDialogFragment() method, then getLayoutInflater error mHost null. What can I do fix that?
TextDialogFragment:
public class TextDialogFragment extends DialogFragment {
private EditText edtDialog;
public interface SingleChoiceListener{
void onPositiveButtonClicked();
void onNegativeButtonClicked();
}
TextDialogFragment.SingleChoiceListener mListener;
#NonNull
public PrettyDialog onCreateTextDialog(Context context) {
PrettyDialog prettyDialog = new PrettyDialog(context);
LayoutInflater inflater=getActivity().getLayoutInflater();
View view=inflater.inflate(R.layout.dialog_text,null);
prettyDialog.setContentView(view);
prettyDialog.setMessage("sdf");
prettyDialog.setIcon(R.drawable.question_icon);
prettyDialog.setCanceledOnTouchOutside(false);
prettyDialog.addButton("EVET", R.color.pdlg_color_white, R.color.pdlg_color_green, new PrettyDialogCallback() {
#Override
public void onClick() {
mListener.onPositiveButtonClicked();
prettyDialog.dismiss();
}
});
prettyDialog.addButton("HAYIR", R.color.pdlg_color_white, R.color.pdlg_color_red, new PrettyDialogCallback() {
#Override
public void onClick() {
mListener.onNegativeButtonClicked();
prettyDialog.dismiss();
}
});
edtDialog=view.findViewById(R.id.edtDialog);
prettyDialog.show();
return prettyDialog;
}
public void setListener(TextDialogFragment.SingleChoiceListener singleChoiceListener){
mListener=singleChoiceListener;
}
}
this layout that I want add to dialog. Dialog has to work edittext+prettydialog:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:layout_margin="8dp">
<EditText
android:id="#+id/edtDialog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#null"
android:focusable="true"
android:hint="Giriş yapınız"
android:gravity="center"
android:imeOptions="actionDone"
android:inputType="text"
android:textColor="#android:color/black"
android:textColorHint="#android:color/darker_gray"
android:textSize="40sp">
</EditText>
</RelativeLayout>
Try below code
View view=LayoutInflater.from(context).inflate(R.layout.dialog_text,null);
Instead of
LayoutInflater inflater=getActivity().getLayoutInflater();
View view=inflater.inflate(R.layout.dialog_text,null);
I hope this can help you!
Thank You.
I am making a custom progress bar using one small image/icon which will basically rotate. But in the background there is white patch or background is showing. I have attached the image here..
I have used the below code for this:
CustomDialog.java
public class CustomDialog extends Dialog{
public Activity c;
public CustomDialog(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
public CustomDialog(Activity a, int s) {
super(a, s);
// TODO Auto-generated constructor stub
this.c = a;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.progressbar);
setCancelable(true);
}
}
progressbar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent"
android:gravity="center"
android:orientation="vertical" >
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminate="true"
android:background="#android:color/transparent"
android:indeterminateDrawable="#drawable/my_progress_interminate" >
</ProgressBar>
</RelativeLayout>
my_progress_interminate.xml
<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/sai_icon"
android:pivotX="50%"
android:pivotY="50%" />
How can i remove the white background here.. Please help.
This is working for me. Add this to the onCreate of your dialog.
getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
The simplest way, add this line to your progress bar XML:
android:background="#android:color/transparent"
Just Do The Below Method To Get It Done
In res->values->styles add the below code
<style name="MyGravity" parent="android:Theme.Material.Dialog" ></style>
Then create yours ProgressDialog as mentioned below
ProgressDialog dialog = new ProgressDialog(ctx, R.style.MyGravity);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.setTitle("Your Title");
dialog.setMessage("Your Message");
I´m trying to implement a basic custom Alert Dialog.
It should look like this
With the following XML Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/tv_custom_dialog_event"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#drawable/actionbar_background"
android:gravity="top"
android:orientation="horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="100dp"
android:text="Delete Event ?"
android:textColor="#color/white"
android:textSize="20sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#DFDFDF" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/btn_custom_dialog_events_cancel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/ic_action_cancel" />
<ImageButton
android:id="#+id/btn_custom_dialog_events_true"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/ic_action_accept" />
</LinearLayout>
</LinearLayout>
However when I run it on my emulator it looks like this:
Does anybody know why this happens and how I can fix that?
Assuming you have a class that extends Dialog you can do the following:
First define a style in styles.xml with something like this:
<style name="CustomDialogThemeTrasparent" parent="#android:style/Theme.Dialog">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:windowBackground">#color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
Then in the constructor of your custom dialog class you set this theme:
public class MyCustomDialog extends Dialog {
public MyCustomDialog(final Context context)
{
super(context, R.style.CustomDialogThemeTrasparent);
}
To set your custom layout as view you can make a function called setTheme() and then called in the show() of your dialog, even with parameters and a nice done layout you can make a more generic class that you can use to show your custom dialogs all over your app, something like this:
//Function to set the layout when the dialog is instantiated, here we
//set the layout and if you want you can set parameter to
//show/hide controls/views and can show different types of dialogs with the same class but with a unified style
private void setTheme()
{
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = layoutInflater.inflate(R.layout.custom_dialog, null);
this.setContentView(mView);
}
then you can make function(s) to show your dialog(s), like:
public void showDeletionDialog(String pMessage)
{
this.setTheme();
this.show();
}
hope it help you
Try this approach. You can inflate your layout:
public AlertDialog displayLayoutDialog(int layout,final Context context, int theme){
AlertDialog.Builder builder = new AlertDialog.Builder(context, theme);
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(layout, null);
builder.setView(view);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog= builder.create();
dialog.show();
Button tb = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
tb.setOnClickListener(new CustomListener(dialog, context));
return dialog;
}
And your Listener where all the validation and anything else that you need happens:
public class CustomListener implements View.OnClickListener {
private final Dialog dialog;
private Context context;
public CustomListener(Dialog dialog, Context context) {
this.dialog = dialog;
this.context = context;
}
#Override
public void onClick(View v) {
Toast.makeText(context, "Custom Layout", Toast.LENGTH_LONG).show();
}
}
Hope it helps!!!
I have created a customized dialog and applied the layout xml to it. But the layout is always applied to the body of the dialog and never to the title section. All what I can do is to set the title of the dilaog programmatically using setTitle and add an icon using setFeatureDrawableResource. Please let me know how to customize the title section of a customized dialog?
Side Question: today when I accessed my stackoverflow account I found that, there are more than 200 points are deducted? any Idea why?
Java_Code:
reportDialog = new Dialog(MeetingPointFix.this);
reportDialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
reportDialog.setCancelable(false);
LayoutInflater reportAlertDialogLayoutInflater = LayoutInflater.from(getApplicationContext());
View reportAlertDialogInflatedView = reportAlertDialogLayoutInflater.inflate(R.layout.meetingpointfix_report_dialog, null);
reportDialog.setContentView(reportAlertDialogInflatedView);
int [] viewsRefsIds = {R.id.reportLocNameValue, R.id.reportLocLatValue, R.id.reportLocLngValue, R.id.reportTimeValue,
R.id.reportDateValue, R.id.reportImgTitleValue, R.id.reportImgPathValue
};
reportDialog.setTitle(REPORT_ALERT_DIALOG_TITLE);
reportDialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.reporticon01);
TextView reportDialogMSG = (TextView) reportDialog.findViewById(R.id.reportDialogMessageValue);
Button reportOkBtn = (Button) reportDialog.findViewById(R.id.reportOkBtnID);
Button reportNavigateBtn = (Button) reportDialog.findViewById(R.id.reportNavigateBtnID);
Yes , I agree that some times the default dialog title doesn'nt match the theme style of your app .
Luckily android provides you a way to update the title layout you just need to take care of these three lines of code while you are creating your dialog.
dialog.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
dialog.setContentView(R.layout.test);
dialog.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.dialog_title_test);
Make sure that the call to setContentView() occurs after requestWindowFeature() and before setFetureInt()
So , suppose for a dialog Fragment you can do that as
public class DialogTest extends DialogFragment{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
dialog.setContentView(R.layout.test);
dialog.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.dialog_title_test);
return dialog;
}
}
Happy Coding ! :)
add the following to your styles xml file:
<style name="FullHeightDialog" parent="android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
use this to create the dialog (modify as you wish and set your ids for buttons)
private void showDialog() {
final Dialog dialog = new Dialog(this, R.style.FullHeightDialog);
dialog.setContentView(R.layout.alert_dialog); //replace with your layout xml
dialog.setCancelable(false);
Button ignoreButton = (Button) dialog.findViewById(R.id.ignore_button);
ignoreButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
Button answerButton = (Button) dialog.findViewById(R.id.answer_button);
answerButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
The easiest way would be to use this library. I read other answers in this post and wanted to add one like to the code similar to theirs. You can provide custom title bar in your layout file itself. See this code:
public void createDialogLanguage() {
ListView listViewLanguage;
final Dialog dialog = new Dialog(getActivity());
dialog.getWindow().addFlags(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawableResource(
android.R.color.transparent);
dialog.setContentView(R.layout.dialog_language);
dialog.show();
}
Please note this line:
dialog.getWindow().setBackgroundDrawableResource(
android.R.color.transparent);
This causes your dialog border and title bar to be transparent. It is useful if you are setting custom background with border(mycase: rounded corners) to display appropriately. No edges visible.
To customize completely your Dialog the best would be probably to extend it and create a custom Dialog.
This will give you all the freedom that you need, but of course you have to do more work.
public class MyDialog extends Dialog {
public MyDialog(Context context) {
super(context, android.R.style.Theme_Black_NoTitleBar);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setCanceledOnTouchOutside(false);
setContentView(R.layout.my_dialog_layout);
TextView title = (TextView) findViewById(R.id.my_dialog_title);
title.setText(context.getString(R.string.my_dialog_title_text));
findViewById(R.id.error_dialog_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something
MyDialog.this.dismiss();
}
});
findViewById(R.id.success_dialog_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something
MyDialog.this.dismiss();
}
});
}
}
You need to create a custom AlertDialog for this:
public class CustomAlertDialogBuilder extends AlertDialog.Builder {
private final Context mContext;
private TextView mTitle;
private ImageView mIcon;
private TextView mMessage;
public CustomAlertDialogBuilder(Context context) {
super(context);
mContext = context;
View customTitle = View.inflate(mContext, R.layout.dialog_title, null);
mTitle = (TextView) customTitle.findViewById(R.id.alertTitle);
mIcon = (ImageView) customTitle.findViewById(R.id.icon);
setCustomTitle(customTitle);
}
#Override
public CustomAlertDialogBuilder setTitle(int textResId) {
mTitle.setText(textResId);
return this;
}
#Override
public CustomAlertDialogBuilder setTitle(CharSequence text) {
mTitle.setText(text);
return this;
}
#Override
public CustomAlertDialogBuilder setIcon(int drawableResId) {
mIcon.setImageResource(drawableResId);
return this;
}
#Override
public CustomAlertDialogBuilder setIcon(Drawable icon) {
mIcon.setImageDrawable(icon);
return this;
}
}
You also need a custom layout:
res/layout/dialog_title.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/title_template"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_marginTop="6dip"
android:layout_marginBottom="9dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingTop="6dip"
android:paddingRight="10dip"
android:src="#drawable/ic_dialog_alert" />
<TextView
android:id="#+id/alertTitle"
style="#style/?android:attr/textAppearanceLarge"
android:singleLine="true"
android:ellipsize="end"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageView android:id="#+id/titleDivider"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:scaleType="fitXY"
android:gravity="fill_horizontal"
android:src="#drawable/divider_horizontal_bright" />
</LinearLayout>
Hope this helps.
You can find more info here:
How to change theme for AlertDialog
FYI your StackOverflow account was created less than 24 hours ago, so it looks like you made a new account rather than accessing your original one.
Try this, This will definitly solve youre problem
AlertDialog.Builder builder = new AlertDialog.Builder(MeetingPointFix.this);
builder.setCancelable(false);
LayoutInflater reportAlertDialogLayoutInflater = LayoutInflater.from(getApplicationContext());
View reportAlertDialogInflatedView = reportAlertDialogLayoutInflater.inflate(R.layout.meetingpointfix_report_dialog, null);
builder.setView(reportAlertDialogInflatedView);
int [] viewsRefsIds = {R.id.reportLocNameValue, R.id.reportLocLatValue, R.id.reportLocLngValue, R.id.reportTimeValue,
R.id.reportDateValue, R.id.reportImgTitleValue, R.id.reportImgPathValue
};
builder.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.reporticon01);
TextView reportDialogMSG = (TextView) reportDialog.findViewById(R.id.reportDialogMessageValue);
Button reportOkBtn = (Button) reportDialog.findViewById(R.id.reportOkBtnID);
Button reportNavigateBtn = (Button) reportDialog.findViewById(R.id.reportNavigateBtnID);
reportDialog=builder.create();
Sorry if this redundant with the ton of questions/answers on inflate, but I could not get a solution to my problem.
I have a compound view (LinearLayout) that has a fixed part defined in XML and additional functionalities in code. I want to dynamically add views to it.
Here is the XML part (compound.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/compoundView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:id="#+id/myTextView"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:text="000" />
</LinearLayout>
I have defined in code a LinearLayout to refer to the XML:
public class CompoundControlClass extends LinearLayout {
public CompoundControlClass (Context context) {
super(context);
LayoutInflater li;
li = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
li.inflate(R.layout.compound_xml,*ROOT*, *ATTACH*);
}
public void addAView(){
Button dynBut = new Button();
// buttoin def+layout info stripped for brevity
addView(dynBut);
}
}
I tried to programmatically add a view with addAView.
If ROOT is null and ATTACH is false, I have the following hierarchy (per HierarchyViewer):
CompoundControlClass>dynBut
The original TextView in the XML is gone.
If ROOT is this and ATTACH is true, I have the following hierarchy:
CompoundControlClass>compoundView>myTextView
CompoundControlClass>dynBut
I would like to have
CompoundControlClass>myTextView
CompoundControlClass>dynBut
where basically the code and XML are only one unique View.
What have I grossly missed?
ANSWER BASED on feedback from D Yao ----------------------
The trick is to INCLUDE the compound component in the main layout instead of referencing it directly.
activity_main.xml
<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">
<include layout="#layout/comound"
android:id="#+id/compoundView"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
mainActivity.java
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CompoundControlClass c = (CompoundControlClass) this.findViewById(R.id.compoundView);
c.addAView(this);
}
}
CompoundControlClass.java
public class CompoundControlClass extends LinearLayout {
public CompoundControlClass(Context context) {
super(context);
}
public CompoundControlClass(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CompoundControlClass(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void addAView(Context context){
ImageView iv = new ImageView(context);
iv.setImageResource(R.drawable.airhorn);
addView(iv);
}
}
compound.xml
<com.sounddisplaymodule.CompoundControlClass xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/compoundView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="110dp"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="40sp"
android:textStyle="bold"
android:text="0:00" />
</com.sounddisplaymodule.CompoundControlClass>
Why not just call addView on the linearlayout? I don't see the need for CompoundControlClass based on the needs you have listed.
LinearLayout v = (LinearLayout)findViewById(R.id.compoundView);
v.addView(dynBut);
In this case, v will contain myTextView, then dynBut.
if you wish to have other functions added and thus really feel a need for creating the compound control class, just leave the constructor as super(etc) and remove the rest
Then your xml would look like this:
<com.yourpackage.CompoundControlClass xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/compoundView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:id="#+id/myTextView"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:text="000" />
</com.yourpackage.CompoundControlClass>
you will also have to ensure your CompoundControlClass.java contains the appropriate Constructor which takes both a Context and an attribute set.
Then, in your java, after you've called setContentView, you can do the following:
CompoundControlClass c = (CompoundControlClass)findViewById(R.id.compoundView);
Button b = new Button(context);
//setup b here or inflate your button with inflater
c.addView(b);
this would give you your desired heirarchy.