Android Custom Dialog Class Title Problems - android

public class MessageDisplayDialog extends Dialog implements OnClickListener
{
public MessageDisplayDialog(Context context, String title, String message)
{
super(context);
setTitle(title);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.color.default_text_color);
Log.v(getClass().getSimpleName(), "MessageDisplayDialog");
LinearLayout objLinearLayout = new LinearLayout(context);
LinearLayout objButtonLayout = new LinearLayout(context);
TextView objMesaageView = new TextView(context);
objMesaageView.setText(message);
objMesaageView.setTextColor(Color.WHITE);
objMesaageView.setGravity(Gravity.CENTER_HORIZONTAL);
objMesaageView.setPadding(0, 0, 0, 10);
Button okButton = new Button(context);
okButton.setText(" OK ");
okButton.setOnClickListener(this);
okButton.setWidth(100);
objButtonLayout.addView(okButton);
objButtonLayout.setGravity(Gravity.CENTER_HORIZONTAL);
objButtonLayout.setPadding(0, 5, 0, 0);
objButtonLayout.setBackgroundColor(Color.LTGRAY);
objLinearLayout.setOrientation(LinearLayout.VERTICAL);
objLinearLayout.addView(objMesaageView);
objLinearLayout.addView(objButtonLayout);
setContentView(objLinearLayout);
//LayoutParams param = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//this.addContentView(objLinearLayout, param);
}
public void onClick(View v)
{
this.dismiss();
}
}
But the Dialog is not showing bar below the Title, how to crack it.

I think your question has already been answered in this thread
Android - change custom title view at run time
please do some searching and accept answers before asking questions.

I think the horizontal border between the title and the message in the built in dialogs is part of AlertDialog, not the base Dialog class, although I could be totally wrong about that. Regardless, whenever I attempt to do something similar to what you are doing, that horizontal line disappears and I've never been able to get it back.
I ended up just writing my own dialog layout XML file and creating my own horizontal line using a <shape> drawable. It's actually fairly painless to create your own completely custom Dialog layouts like this, and gives you more control over the look of your dialogs.

This is old I know, but FYI the solution of drawing your own line on a custom dialog is not compatible with ICS.
It will display both your line and the line that ICS is now including by default. So you'd get two lines on the screen.

use two lines of code to remove dialoge title
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(dialog.getWindow().FEATURE_NO_TITLE);

Related

Android - Floating Alert Dialog which still enables user input in the main layout/activity

Good day, apologies for the confusing title.
I am creating an android application and I need a dialog/pop up to appear at a specific X Y Position. I already have a working DialogFragmet as shown below:
public class ItemDialog extends DialogFragment implements OnClickListener{
//a lot of public and private variables here
public interface onSubmitListener {
void setOnSubmitListener(String qty, String disc, String instructions, UOMClass uom);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog dialog = new Dialog(getActivity());
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
LayoutInflater layoutInflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout2 = layoutInflater.inflate(R.layout.fragment_item_dialog, null);
dialog.setContentView(layout2);
setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialog);
//snip snip more code
dialog.show();
//buttons - I set the buttons here
//editTexts - I set the edit texts here
//TextViews - I set the text views here
return dialog;
}
//more functions here
}
And I call it from my base activity as follows:
ItemDialog itemDialog = new ItemDialog();
//pass values from base activity to the ItemDialog here
itemDialog.show(getFragmentManager(), "");
However, I need to be able to see the base activity and input values in the edit texts in it. As of now, my DialogFragment covers a huge part of the screen and I'm unable to see the activity behind it. Clicking outside the Dialog Fragment cancels it as well.
I've searched on how to make an alert dialog float but to no avail.
Is there a way to display a Dialog Fragment over a certain area of the screen to allow the user to see and still input values in the base activity?
Any help is very much appreciated, thank you.
you can get window object and set it's layout parameters below code might help you.
Window window = getDialog().getWindow();
// set "origin" to top left corner, so to speak
window.setGravity(Gravity.TOP|Gravity.LEFT);
// after that, setting values for x and y works "naturally"
WindowManager.LayoutParams params = window.getAttributes();
params.x = 300;
params.y = 100;
window.setAttributes(params);
for more info Position of DialogFragment in Android
you can also set background color or bitmap to dialog

Android: Dynamically create image button with click event

I'm new to android and I require to have a list of image buttons in an activity which are created based on the data in a database. I haven't created anything like this in android before and so far I've been using HTML tables to show my data in a grid view. I'm not asking for any special code, I'm just clueless on how to implement this. I wanna know what the best approach is.
One problem I've faced is with the click events(in the way I've been doing them so far) which do not take in any EventArg, so I can't get the name of the button out of them.
If you're sure that the OnClickListener instance is applied to a Button, then you could just cast the received view to a Button and get the text:
public void onClick(View v) {
// 1) Possibly check for instance of first
Button b = (Button)v;
String buttonText = b.getText().toString();
}
// create the layout params that will be used to define how your button will be displayed
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// Create Button
final Button btn = new Button(this);
// Give button an ID
btn.setId(someinteger);
btn.setText("Add Text");
// set the layoutParams on the button
btn.setLayoutParams(params);

Android: how to make dialogs look the same on emulator / real phone

On my T-mobile MOVE, dialogs have black background and black text - not good.
On the emulator, all the dialogs have white background and black text.
How to fix this? Why is this happening? Am I supposed to explicitly set background for dialogs?
As Royston said you have to make custom dialogs. To remove black corner use following line
super(context, android.R.style.Theme_Translucent_NoTitleBar);
instead of making custom style.
A simple example of custom dialog will be like
public class customDialog extends Dialog implements android.view.View.OnClickListener{
Context context = null;
public customDialog(Context context, String etc ) {
super(context, android.R.style.Theme_Translucent_NoTitleBar);
this.context = context;
setBasicContents();
}
private void setBasicContents() {
this.setContentView(R.layout.custom_dialog);
this.findViewById(R.id.btn_ok).setOnClickListener(this);
this.findViewById(R.id.btn_cancel).setOnClickListener(this);
((RelativeLayout)this.findViewById(R.id.img_bg_tranperant)).
setBackgroundColor(Color.argb(166, 0, 0, 0));
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_ok:
anythingyouwant();
break;
case R.id.btn_cancel:
this.dismiss();
break;
default:
break;
}
}
}
A line
((RelativeLayout)this.findViewById(R.id.parent_layout)).
setBackgroundColor(Color.argb(166, 0, 0, 0));
is important. It take parent layout of dialog xml and set its background to little transparent. So when user see the dialog the back screen will little dim. In additon to this layout over dialog xml will contain two buttons and one textView to show message. You can customize this custom class and xml to your requirements.
Thanks,
Basically if you use default Dialog in Android, then since each vendor have set the colors and backgrounds for their dialogs, they will look different. For e.g. Samsung dialog is completely different from the default Nexus devices dialogs.
The best bet for you is to define your own colors and theme for dialogs to look uniform across all devices. Else brace yourself for a new look dialog as the vendor wants it to be displayed.

Clear previous View and show new View on button click in the same Activity

I have created an activity with two buttons at the top. One button to show "SMS Logs" and second to show "Call Logs".
On clicking "SMS Logs" button, i am dynamically creating textviews and linear layout to show sms logs.
On Clicking "Call Logs", i am dynamically creating another textviews and linear layout to show call logs.
But the problem is that, once if we click "sms log" button and then we click "call log" button, the previously created linear layouts are not removed and the both(previous layouts and the current layouts) are shown simultaneously.
But i want that the previous layouts should be removed on clicking the second button.
Which function, should i use to remove the previous viewgroups or the layouts. Tell me if you need to read my class file.
Edit:
This is my Activity's code,
public class General extends Activity
{
String phone, message;
TextView Logs;
View layout, callLayout;
TextView data, callData, line, callLine;
Button smsLog, callLog;
LinearLayout ll, callll;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.general_main);
Logs = (TextView)findViewById(R.id.Logs);
layout = findViewById(R.id.layout);
callLayout = findViewById(R.id.layout);
smsLog = (Button)findViewById(R.id.smsLogs);
callLog = (Button)findViewById(R.id.callLogs);
smsLog.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
callLayout.setVisibility(View.GONE);
ll = new LinearLayout(getApplicationContext());
ll.setOrientation(LinearLayout.VERTICAL);
data = new TextView(getApplicationContext());
data.setText("First Line");
data.setTextColor(Color.YELLOW);
line = new TextView(getApplicationContext());
line.setText("Second Line");
((ViewGroup) ll).addView(data);
((ViewGroup) layout).addView(line);
((ViewGroup) layout).addView(ll);
}
});
callLog.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
layout.setVisibility(View.GONE);
callll = new LinearLayout(getApplicationContext());
callll.setOrientation(LinearLayout.VERTICAL);
callData = new TextView(getApplicationContext());
callLine = new TextView(getApplicationContext());
callData.setText("Third Line");
callLine.setText("Fourth Line");
((ViewGroup) callll).addView(callData);
((ViewGroup) callLayout).addView(callLine);
((ViewGroup) callLayout).addView(callll);
}
});
}
}
I have removed the extra code and made it simple to understand.
You can use FrameLayout to solve your problem. But I recommend you to use tabview.Here is the link that demonstrates how to develop tabbed applications.Good Luck
You could implement a TabView.
But having your current setup just change the visibility of one view group to GONE and the other to VISIBLE.
GONE will make the view invisible and it won't take up any space anymore.
EDIT based on the code added to the question
Both your layout and callLayout are using the same XML view. Implement 2 identical views in your xml and keep one visible and one gone. This way when you set layout or callLayout visibility to GONE they are 2 different ones not the same. So your onClick() will have something like this:
for smsLog:
layout.setVisibility(View.GONE);
callLayout.setVisibility(View.VISIBLE);
for callLog:
callLayout.setVisibility(View.GONE);
callLayout.setVisibility(View.VISIBLE);

Creating custom components using regular android components

basically I want to encapsulate a simple component from code that I already have.
Basically it's a LinearLayout with buttons inside. These buttons will make changes to a ListView, and there is also some other small stuff that it will do.
Currently I have a XML layout with those, and I programmatically setup everything else: the buttons, the interaction between the list and the other small stuff.
Obviously I thought to myself, let's encapsulate this.
I started out trying to extend the LinearLayout and adding the buttons.
Already I have no idea how to inflate the buttons to add to the view
What method do I override to create this buttons just before the view gets created without messing with the measures and inflations, etc.
I've looked around but the custom components I see are either completely new components or components that simply add small functionality to the custom ones.
Is there some guidelines for doing this?
Good tutorials/examples?
Any help is appreciated. Thanks !
EDIT:
Okay, here is a little more specific stuff.
Basically I want to create a View that holds filter buttons for a ListView. This will be used in different places with different filters, so I need flexibility for the buttons.
Basically I'd like to do something like this:
CustomView view = new CustomView(activity);
view.addButton("Lala", new OnFilterClickListener {
onClick(ListView list, View v) {
// Do the filtering
}
});
mListView.addHeaderView(view);
I want the view to adapt it's weights for showing the buttons, show the user which filter is active, stuff like that.
But I still don't really know how to make those dynamically added buttons appear, where do I generate them, how to inflate them and stuff like that.
public class myLayout extends LinearLayout {
//...
public void addButton(String text, OnClickListener listener) {
Button newButton = new Button(mContext);
newButton.setText(text);
newButton.setOnClickListener(listener);
//Say we want the weights to be equal
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.Fill_PARENT, 1);
addView(newButton, params);
}
//...
}
You can even do something to the view before dispatching the click like this:
public class myLayout extends LinearLayout {
//...
public void addButton(String text, final OnClickListener listener) {
Button newButton = new Button(mContext);
newButton.setText(text);
newButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
//do whatever you want
//like change background of button or something
//finally
listener.onClick(v);
}
});
//Say we want the weights to be equal
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.Fill_PARENT, 1);
addView(newButton, params);
}
//...
}

Categories

Resources