My code:
public class MyBottomDialog extends Dialog {
LinearLayout container;
public MyBottomDialog(Context context) {
super(context);
container = new LinearLayout(context);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
container.setLayoutParams(params);
container.setOrientation(LinearLayout.VERTICAL);
// container.setBackgroundColor(Color.TRANSPARENT);
int padding = context.getResources().getDimensionPixelSize(R.dimen.navigation_padding_bottom);
container.setPadding(padding, padding, padding, padding);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(container);
// setContentView(button);
setCancelable(true);
setCanceledOnTouchOutside(true);
getWindow().setGravity(Gravity.BOTTOM);
// getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
getWindow().setWindowAnimations(R.style.BottomDialogAnimation);
}
public void addMenuItems(String[] menuItemTitles, int[] menuItemTextColors) {
for (int i = 0; i < menuItemTitles.length; i++) {
// Button button = (Button) LayoutInflater.from(getContext()).inflate(R.layout.bottom_menu_item, container, false);
// button.setText(menuItemTitles[i]);
// button.setTextColor(menuItemTextColors[i]);
// container.addView(button);
addMenuItem(menuItemTitles[i], menuItemTextColors[i]);
}
}
public void addMenuItem(String menuItemTitle, int menuItemTextColor) {
// Button button = (Button) LayoutInflater.from(getContext()).inflate(R.layout.bottom_menu_item, container, false);
Button button = produceButton();
button.setText(menuItemTitle);
button.setTextColor(menuItemTextColor);
container.addView(button);
}
private Button produceButton() {
Button button = new Button(getContext());
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setGravity(Gravity.CENTER);
// button.setBackground(getContext().getResources().getDrawable(R.drawable.bottom_menu_button2));
float[] outerRadii = new float[]{24, 24, 24, 24, 24, 24, 24, 24};
RoundRectShape rect = new RoundRectShape(outerRadii, null, null);
ShapeDrawable drawable = new ShapeDrawable(rect);
drawable.getPaint().setColor(Color.WHITE);
drawable.getPaint().setStyle(Paint.Style.FILL);
button.setBackground(drawable);
return button;
}
}
then I use it:
MyBottomDialog dlg = new MyBottomDialog(this);
dlg.addMenuItems(new String[]{"delete", "cancel"}, new int[]{android.R.color.holo_red_light, android.R.color.holo_blue_light});
dialog.show();
but there is no any text in these two button in dialog, why?
Related
How to add a switch button in the title bar of alert dialog? As an example see the Wi-Fi options screenshot. As you can see there is a switch toggle button inside the title in Wi-Fi options. Is it possible to create a custom Alert Dialog to add a switch toggle button inside the title bar of the Alert Dialog? The code given below is for the screen shot number 2 and I have marked in it where I want it to be added. Please help me and put me in the right direction.
alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Recording Timer");
LinearLayout LL = new LinearLayout(context);
LL.setFocusable(true);
LL.setFocusableInTouchMode(true);
LL.setOrientation(LinearLayout.HORIZONTAL);
final TextView secondsTextView = new TextView(context);
final TextView minutesTextView = new TextView(context);
final TextView hoursTextView = new TextView(context);
secondsTextView.setText("seconds");
minutesTextView.setText("minutes");
hoursTextView.setText("hours");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
lp.setMargins(0, 15, 0, 0);
LL.setLayoutParams(lp);
LinearLayout.LayoutParams lp11 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp11.setMargins(79, 0, 0, 0);
LL.addView(hoursTextView, lp11);
LinearLayout.LayoutParams lp22 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp22.setMargins(107, 0, 0, 0);
LL.addView(minutesTextView, lp22);
LinearLayout.LayoutParams lp33 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp33.setMargins(98, 0, 0, 0);
LL.addView(secondsTextView, lp33);
LinearLayout LL1 = new LinearLayout(context);
LL1.setOrientation(LinearLayout.HORIZONTAL);
secondsPicker = new NumberPicker(context);
minutesPicker = new NumberPicker(context);
hoursPicker = new NumberPicker(context);
secondsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
if(newVal==0 && minutesPicker.getValue()==0 && hoursPicker.getValue()==0) {
alertDialog1.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
} else {
alertDialog1.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
}
}
});
minutesPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
if(newVal==0 && secondsPicker.getValue()==0 && hoursPicker.getValue()==0) {
alertDialog1.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
} else {
alertDialog1.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
}
}
});
hoursPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
if(newVal==0 && minutesPicker.getValue()==0 && secondsPicker.getValue()==0) {
alertDialog1.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
} else {
alertDialog1.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
}
}
});
secondsPicker.setMaxValue(59);
secondsPicker.setMinValue(0);
minutesPicker.setMaxValue(59);
minutesPicker.setMinValue(0);
hoursPicker.setMaxValue(23);
hoursPicker.setMinValue(0);
if (timedRecordingIsOn || timedRecordingDialogOpened) {
secondsPicker.setValue(secondsNumberPickerInt);
minutesPicker.setValue(minutesNumberPickerInt);
hoursPicker.setValue(hoursNumberPickerInt);
}
timedRecordingDialogOpened = true;
LinearLayout.LayoutParams lp110 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp110.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
lp110.setMargins(0, 40, 0, 0);
LL1.setLayoutParams(lp110);
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp1.setMargins(52, 0, 0, 0);
LL1.addView(hoursPicker, lp1);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp2.setMargins(62, 0, 0, 0);
LL1.addView(minutesPicker, lp2);
LinearLayout.LayoutParams lp3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp3.setMargins(72, 0, 0, 0);
LL1.addView(secondsPicker, lp3);
RelativeLayout relativeLayout1 = new RelativeLayout(context);
relativeLayout1.addView(LL);
relativeLayout1.addView(LL1);
alertDialogBuilder.setView(relativeLayout1);
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
secondsNumberPickerInt = secondsPicker.getValue();
minutesNumberPickerInt = minutesPicker.getValue();
hoursNumberPickerInt = hoursPicker.getValue();
timeRecordingInMilliSeconds = (hoursPicker.getValue() * 3600000) + (minutesPicker.getValue() * 60000) + (secondsPicker.getValue() * 1000);
timedRecordingIsOn = true;
timedRecordingDialogOpened = false;
timedRecordingTextView.setText("Timer set for: " + (hoursPicker.getValue() == 0 ? "00" : hoursPicker.getValue()) + ":" + (minutesPicker.getValue() == 0 ? "00" : minutesPicker.getValue()) + ":" + (secondsPicker.getValue() == 0 ? "00" : secondsPicker.getValue()));
timedRecordingTextView.setVisibility(View.VISIBLE);
// button.performClick();
break;
case DialogInterface.BUTTON_NEGATIVE:
if (timedRecordingIsOn) {
timedRecordingIsOn = false;
timedRecordingTextView.setVisibility(View.INVISIBLE);
}
timeRecordingInMilliSeconds = 0;
secondsNumberPickerInt = 0;
minutesNumberPickerInt = 0;
hoursNumberPickerInt = 0;
timedRecordingDialogOpened = false;
break;
}
}
};
alertDialogBuilder.setPositiveButton("SET TIMER", dialogClickListener);
if (timedRecordingIsOn) {
alertDialogBuilder.setNegativeButton("CANCEL TIMER", dialogClickListener);
} else {
alertDialogBuilder.setNegativeButton("CANCEL", dialogClickListener);
}
alertDialog1 = alertDialogBuilder.create();
alertDialog1.setOnCancelListener(
new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) { //When you touch outside of dialog bounds, the dialog gets canceled and this method executes.
timeRecordingInMilliSeconds = 0;
secondsNumberPickerInt = 0;
minutesNumberPickerInt = 0;
hoursNumberPickerInt = 0;
timedRecordingDialogOpened = false;
}
}
);
alertDialog1.show();
LL.requestFocus();
secondsPicker.clearFocus();
if(!timedRecordingIsOn && savedInstanceState!=null) {
alertDialog1.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
}
if(savedInstanceState!=null) {
if(secondsNumberPickerInt==0 && minutesNumberPickerInt ==0 && hoursNumberPickerInt == 0) {
alertDialog1.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
}
Note: Follow any one of the method & MainActivity.this replace with your context
If U need java code follow this
final AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setCancelable(false);
final TextView titleTextView = new TextView(MainActivity.this);
titleTextView.setText("Title");
titleTextView.setTextColor(Color.RED);
Switch switchval=new Switch(MainActivity.this);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
switchval.setLayoutParams(params);
RelativeLayout relative=new RelativeLayout(MainActivity.this);
relative.addView(titleTextView);
relative.addView(switchval);
dialog.setCustomTitle(relative);
/*Your Numberpickerview example is following*/
//dialog.setView(yourcustomview);
dialog.create().show();
--------------------------------------------------------------------------------
Type XML: if u want xml code follow this
final AlertDialog.Builder dialog1 = new AlertDialog.Builder(MainActivity.this);
dialog1.setCancelable(false);
dialog1.setCustomTitle(getLayoutInflater().inflate(R.layout.btn_share,null));
//dialog1.setView(/*Your number picker view*/);
dialog1.create().show();
btn_share.xml
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:paddingLeft="10dp"
android:layout_gravity="center"
android:textColor="#color/colorAccent"/>
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"/>
</RelativeLayout>
Add switch button in Alert Dialog title bar in Android
You can use a custom layout which includes both title and body and then hide the default implementation.
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.custom_dialog, null);
dialogBuilder.setView(dialogView);
You can hide the title of a dialog using:
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
My suggestion for this is to create layout file for Custom dialog and do like this it will be helpful.
final Dialog dialog = new Dialog(getContext());
dialog.setCancelable(false);
dialog.setContentView(R.layout.your_dialog_layout);
dialog.setTitle("Dialog Title");
//views inside your layout declare and cast here
//e.g. final Button btn = (Button)dialog.findViewById(R.id.btn);
//after all view casting and onClickListeners show your dialog
dialog.show();
try to implement one layout file and inflate when alert dialog is open and give toggle button in this layout
final Dialog dialog = new Dialog(getContext());
dialog.setCancelable(false);
dialog.setContentView(R.layout.your_dialog_layout);
dialog.setTitle("Dialog Title");
//views inside your layout declare and cast here
//e.g. final Button btn = (Button)dialog.findViewById(R.id.btn);
//after all view casting and onClickListeners show your dialog
dialog.show();
i have created a layout programmatically and set it into secontentview within oncreate i am create chat layout using simple edittext and a send image button right from edittext whenever user click on edittext suddenly hide by softkeyboard i tried all option which is required to mention in menifest like adjustPan or screenresize etc but no effect the edittext still hidden by keyboard
here is my layout code that contain the edittext
private LinearLayout getTextChatViewTest() {
// MainActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// LibraryMainActivity.this.getActionBar().show();
LinearLayout linWrapperLayout;
LinearLayout.LayoutParams linWrapperLayoutParams;
LinearLayout linHeaderLayout;
LinearLayout.LayoutParams linHeaderParams;
TextView txtHeaderText;
linWrapperLayout = new LinearLayout(MainActivity.this);
linWrapperLayout.setOrientation(LinearLayout.VERTICAL);
linWrapperLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
linWrapperLayout.setLayoutParams(linWrapperLayoutParams);
linWrapperLayout.setBackgroundColor(Color.parseColor("#ffffff"));
linHeaderLayout = new LinearLayout(MainActivity.this);
linHeaderLayout.setBackgroundColor(Color.parseColor("#E3E9F3"));
linHeaderLayout.setPadding(pixToDp(10), pixToDp(5), pixToDp(10), pixToDp(5));
linHeaderParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linHeaderLayout.setLayoutParams(linHeaderParams);
txtHeaderText = new TextView(MainActivity.this);
txtHeaderText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
txtHeaderText.setPadding(pixToDp(5), pixToDp(10), pixToDp(10), pixToDp(10));
txtHeaderText.setTextColor(Color.parseColor("#004A8F"));
linHeaderLayout.addView(txtHeaderText);
txtHeaderText.setText("Text Chat");
RelativeLayout rel = new RelativeLayout(MainActivity.this);
rel.setBackgroundColor(Color.parseColor("#ffffff"));
RelativeLayout.LayoutParams relPArams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
rel.setLayoutParams(relPArams);
linWrapperLayout.addView(rel);
rel.addView(linHeaderLayout);
txtChatArea = new TextView(MainActivity.this);
txtChatArea.setId(011);
txtChatArea.setBackgroundColor(Color.parseColor("#ffffff"));
txtChatArea.setGravity(Gravity.BOTTOM);
int scroll_amount = (int) (txtChatArea.getLineCount() * txtChatArea.getLineHeight()) - (txtChatArea.getBottom() - txtChatArea.getTop());
txtChatArea.setLinksClickable(true);
txtChatArea.setAutoLinkMask(Linkify.WEB_URLS);
txtChatArea.setText("Anything");
txtChatArea.scrollTo(0, scroll_amount);
txtChatArea.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
txtChatArea.setScrollY(0);
txtChatArea.setMovementMethod(new ScrollingMovementMethod());
/** for allowing copy from text chat area */
txtChatArea.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
txtChatArea.setCursorVisible(true);
return true;
}
});
// Button btn = new Button (this);
android.view.Display display = ((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
// btn.setHeight((int)(display.getHeight()*0.68));
int height = display.getHeight();
RelativeLayout.LayoutParams relImageParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
(MainActivity.getPixelHeight(MainActivity.this))/2+60);
relImageParams.setMargins(pixToDp(15), pixToDp(100), pixToDp(15), 0);
relImageParams.addRule(RelativeLayout.BELOW, 111);
txtChatArea.setLayoutParams(relImageParams);
rel.addView(txtChatArea);
/* The following section was copied from incoming call view */
relIncomingBottomLayout = new RelativeLayout(MainActivity.this);
relIncomingBottomLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
relIncomingBottomLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
//relIncomingBottomLayoutParams.addRule(RelativeLayout.BELOW, 011);
relIncomingBottomLayoutParams.setMargins(pixToDp(10), 0, pixToDp(10), pixToDp(10));
relIncomingBottomLayout.setLayoutParams(relIncomingBottomLayoutParams);
rel.addView(relIncomingBottomLayout);
/* Now add a linearlayout with vertical orientation, which will contain two linearlayouts with horizontal orientation */
LinearLayout linBottomHolder = new LinearLayout(MainActivity.this);
linBottomHolder.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams linHolderParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linBottomHolder.setLayoutParams(linHolderParams);
relIncomingBottomLayout.addView(linBottomHolder);
/* an warning view on characters left for end users */
txtWarning = new TextView(MainActivity.this);
txtWarning.setBackgroundColor(Color.parseColor("#ffffff"));
txtWarning.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
txtWarning.setText("150 characters left");
txtWarning.setTextColor(Color.GRAY);
linBottomHolder.addView(txtWarning);
/* Now add the first linear layout to contain an edittext to get message and an imageview */
LinearLayout linWriteHolder = new LinearLayout(MainActivity.this);
linWriteHolder.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams linWriteHolderPArams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
linWriteHolder.setLayoutParams(linWriteHolderPArams);
linBottomHolder.addView(linWriteHolder);
/* Now add an edittext and an imageview inside this linearlayout */
GradientDrawable gd = new GradientDrawable();
gd.setStroke(2, Color.parseColor("#E3E9F3"));
gd.setShape(GradientDrawable.RECTANGLE);
gd.setColor(Color.WHITE);
gd.setCornerRadius(5);
edtWriteMesage = new EditText(MainActivity.this);
edtWriteMesage.setHint("Type text here");
edtWriteMesage.setBackgroundColor(Color.parseColor("#ffffff"));
edtWriteMesage.setBackground(gd);
edtWriteMesage.setSingleLine(true);
/** filtering inputs */
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter.LengthFilter(150);
edtWriteMesage.setFilters(filters); /** now a maximum of 150 characters can be taken as input */
edtWriteMesage.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int currentLength = edtWriteMesage.getText().toString().length();
int remainingLength = 150 - currentLength;
if(remainingLength <= 15){
txtWarning.setTextColor(Color.RED);
}else{
txtWarning.setTextColor(Color.GRAY);
}
txtWarning.setText(""+remainingLength + " characters left");
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
LinearLayout.LayoutParams linWriteParams = new LinearLayout.LayoutParams(0,
ViewGroup.LayoutParams.WRAP_CONTENT, 0.9f);
linWriteParams.gravity = Gravity.CENTER_VERTICAL;
edtWriteMesage.setLayoutParams(linWriteParams);
linWriteHolder.addView(edtWriteMesage);
/* Now add an imageview with an arrow in it by the edittext's side */
imgSendArrow = new ImageView(MainActivity.this);
byte[] sendArrow = Base64.decode(SecondImageStore.send, Base64.DEFAULT);
//byte[] sendArrow = Base64.decode(SecondImageStore.papersend, Base64.DEFAULT);
imgSendArrow.setImageBitmap(BitmapFactory.decodeByteArray(sendArrow, 0, sendArrow.length));
LinearLayout.LayoutParams linSendParams = new LinearLayout.LayoutParams(0,
ViewGroup.LayoutParams.WRAP_CONTENT, 0.1f);
linSendParams.gravity = Gravity.CENTER;
linSendParams.setMargins(pxToDp(3), 0, pxToDp(2), 0);
imgSendArrow.setLayoutParams(linSendParams);
linWriteHolder.addView(imgSendArrow);
/* Now add the Second relative layout to contain two buttons */
LinearLayout linTextChatButtonHolder = new LinearLayout(MainActivity.this);
LinearLayout.LayoutParams linTextChatButtonHolderParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
linTextChatButtonHolderParams.setMargins(0, pixToDp(10), 0, 0);
linTextChatButtonHolder.setLayoutParams(linTextChatButtonHolderParams);
linBottomHolder.addView(linTextChatButtonHolder);
/* Button to end ongoing conference */
btnTextChatEndCall = new Button(MainActivity.this);
btnTextChatEndCall.setText("Call Held");
btnTextChatEndCall.setBackgroundColor(Color.parseColor("#C11E0F"));
btnTextChatEndCall.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
btnTextChatEndCall.setGravity(Gravity.CENTER);
btnTextChatEndCall.setTextColor(Color.parseColor("#ffffff"));
LinearLayout.LayoutParams linTextChatEndCallParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0.5f);
linTextChatEndCallParams.setMargins(0, pixToDp(10), 0, 0);
btnTextChatEndCall.setLayoutParams(linTextChatEndCallParams);
linTextChatButtonHolder.addView(btnTextChatEndCall);
/* Button to display menu in chat mode */
btnTextChatMenu = new Button(MainActivity.this);
btnTextChatMenu.setText("Menu");
btnTextChatMenu.setGravity(Gravity.CENTER);
btnTextChatMenu.setBackgroundColor(Color.parseColor("#014A8E"));
btnTextChatMenu.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
btnTextChatMenu.setTextColor(Color.parseColor("#ffffff"));
LinearLayout.LayoutParams linTextChatMenuParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0.5f);
linTextChatMenuParams.setMargins(pixToDp(10), pixToDp(10), 0, 0);
btnTextChatMenu.setLayoutParams(linTextChatMenuParams);
linTextChatButtonHolder.addView(btnTextChatMenu);
btnTextChatEndCall.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try{
}catch(Exception e){
e.printStackTrace();
}
}
});
edtWriteMesage.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
int result = actionId & EditorInfo.IME_MASK_ACTION;
switch (result) {
case EditorInfo.IME_ACTION_DONE:
break;
case EditorInfo.IME_ACTION_NEXT:
break;
}
return false;
}
});
return linWrapperLayout;
// setContentView(linWrapperLayout);
}
Try on this way to hide keyboard
// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
In your Manifest.xml add this to you activity
<activity
android:windowSoftInputMode="adjustResize|stateHidden" ... >
...
</activity>
for Click here for more details about handling Input Methods
i have just remove getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); after that edittext comming below the edittext whenever soft keyboard appear
I have a LinearLayout into my PopupView, and a button into this LinearLayout. Event listener of this button works correctly, but animation of pressing (highlighting) is not starting. What I did wrong? Please, gelp!
public class PopupView extends View
{
private PopupWindow popUp;
private Button buttonOk;
private OnClickListener onClick;
private LayoutParams params;
private int popUpWidth = 0;
private int popUpHeight = 0;
private int popUpX, popUpY;
public PopupView(Context context, View parent)
{
super(context);
popUp = new PopupWindow(context);
popUpLayout = new LinearLayout(context);
buttonOk = new Button(context);
buttonOk.setText("OK");
buttonOk.setId(1);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
popUpLayout.setOrientation(LinearLayout.VERTICAL);
popUp.setContentView(popUpLayout);
onClick = new OnClickListener()
{
public void onClick (View v)
{
switch (v.getId())
{
case 1:
System.out.println("OK pressed!");
break;
case 2:
break;
}
}
};
buttonOk.setOnClickListener(onClick);
popUpLayout.removeAllViews();
popUpLayout.addView(buttonOk, params);
popUpWidth = 200;
popUpHeight = 400;
popUpX = 300;
popUpY = 300;
popUp.showAtLocation(parent, Gravity.NO_GRAVITY, popUpX, popUpY);
popUp.update(popUpX, popUpY, popUpWidth, popUpHeight);
}
}
In default PopupView is not focusble. Need to call method popup.setFocusable(true). Thanks for all!
I am adding few TextViews to a RelativeLayout. I have extended RelativeLayout. I could add the TextView in the constructor definition. I could see the item on the viw. But the same code, when I use in onPostExecute of AyncTask or use post() method, TextView is not seen added to the layout. Same coe is executed both place. I just changed the top margin.
Here is my code
TextView tv = new TextView(context);
addView(tv);
tv.setHeight(49);
tv.setWidth(100);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.leftMargin = (int) 0;
params.topMargin = (int) 600;
tv.setLayoutParams(params);
================ Implementation ================================
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
DateDetailsScrollView sc = new DateDetailsScrollView(container.getContext());
new SyncInThread().execute(sc);
return sc;
}
================= AsyncTask ===================================
private static class SyncInThread extends AsyncTask<Object, Void, String> {
private ArrayList<EVT> _mEvents = new ArrayList<EVT>();
DateDetailsScrollView _lv = null;
#Override
protected String doInBackground(Object... cal) {
_lv = (DateDetailsScrollView)cal[0];
//The SQL cannot be shared here... Please forgive me.
boolean f = cursor.moveToFirst();
while(f) {
EVT evt = new EVT();
evt.loadFromCursor(cursor);
f = cursor.moveToNext();
_mEvents.add(evt);
cc++;
}
return "";
}
#Override
protected void onPostExecute(String result) {
for(EVT ev : _EVT) {
_lv.addEvent(ev);
}
}
}
public class DateDetailsScrollView extends ScrollView {
private EventRelativeLayout _mEventLayout = null;
public DateDetailsScrollView(Context context) {
super(context);
_mEventLayout = new EventRelativeLayout(context);
ll.addView(_mEventLayout);
LinearLayout ll = new LinearLayout(context);
ll.setOrientation(LinearLayout.HORIZONTAL);
addView(ll);
}
public void addEvent(final VEVENT evt) {
_mEventLayout.addEvent(evt);
invalidate();
this.refreshDrawableState();
}
}
public class EventRelativeLayout extends RelativeLayout {
public EventRelativeLayout(Context context) {
//This works fine.
super(context);
TextView tv = new TextView(context);
addView(tv);
int[] colors = { 0xFF052d42, 0xFF096da0 };
GradientDrawable drawable = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM, colors);
tv.setBackgroundColor(0xFF290202);
tv.setBackgroundDrawable(drawable);
tv.setHeight(49);
tv.setWidth(100);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.leftMargin = (int) 0;
params.topMargin = (int) 100;
tv.setLayoutParams(params);
}
public void addEvent(EVT evt) {
TextView tv = new TextView(getContext());
addView(tv);
int[] colors = { 0xFF052d42, 0xFF096da0 };
GradientDrawable drawable = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM, colors);
tv.setBackgroundColor(0xFF290202);
tv.setBackgroundDrawable(drawable);
tv.setHeight(100);
int dm = em -eh;
int dh = eh - sh;
if( dm < 0) {
dm += 60;
dh--;
}
tv.setWidth(100);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.leftMargin = 0;
params.topMargin = 600;
tv.setHeight(49);
tv.setLayoutParams(params);
}
}
I got the issue. It was a programmatic mistake and due to a wrong calculation, the veiw was added to a different page.
I have a pop up that displays an image upon clicking a button. I got to display the pop up but the image is so small and I want it to occupy the 90% of the screen and add a close button to it.
Here is my code:
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params;
ImageView imageView;
boolean click = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bmi);
popUp = new PopupWindow(this);
layout = new LinearLayout(this);
tv = new TextView(this);
imageView = new ImageView(this);
imageView.setImageResource(R.drawable.animalbite);
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int fullScreenWidth = display.getWidth();
int dialogWidth = (int) (fullScreenWidth * 0.9);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.WRAP_CONTENT;
params.width = dialogWidth;
getWindow().setAttributes(params);
imageView.setLayoutParams(params);
//tv.setText("Hi this is a sample text for popup window");
layout.addView(imageView, params);
popUp.setContentView(layout);
Button ViewBmi = (Button) findViewById(R.id.ViewBmi);
ViewBmi.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (click) {
popUp.showAtLocation(layout, Gravity.CENTER, 10 ,10);
click = false;
} else {
popUp.dismiss();
click = true;
}
}
});
}
Here is the display:
ANy ideas?
Try this...
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params,params2;
ImageView imageView;
boolean click = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Display display = getWindowManager().getDefaultDisplay();
final int height = (display.getHeight()*90)/100;
final int width = (display.getWidth()*90)/100;
popUp = new PopupWindow(this);
layout = new LinearLayout(this);
tv = new TextView(this);
imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_launcher);
params = new LayoutParams(width,height-50);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(imageView, params);
Button button = new Button(this);
button.setText("Close");
params2 = new LayoutParams(100,50);
layout.addView(button,params2);
popUp.setContentView(layout);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
popUp.dismiss();
click = true;
}
});
Button ViewBmi = (Button) findViewById(R.id.button1);
ViewBmi.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (click) {
popUp.showAtLocation(layout, Gravity.CENTER, 10, 10);
popUp.update(10, 20, width,height);
click = false;
} else {
popUp.dismiss();
click = true;
}
}
});
}
Try This....
Button button = new Button(this);
button.setText("Close");
params2 = new LayoutParams(100,50);
--->params2.setMargins(100, 0, 0, 0);
layout.addView(button,params2);
I'd do it like this:
height = (int)(0.9*getWindow().getWindowManager().getDefaultDisplay().getHeight());
params.height = height;
Try:
params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
...
layout.addView(imageView, params);
This way the ImageView will be as wide, as possible.
If the underlying dialog is not wide enough, then you can set it's width also full screen:
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.WRAP_CONTENT;
params.width = LayoutParams.MATCH_PARENT;
getWindow().setAttributes(params);
Or if you want it to be for example 90% of the screen, then:
// get the width of the whole screen
Display display = getWindow().getDefaultDisplay();
int fullScreenWidth = display.getWidth();
//then set 90% of it to the width of the Dialog:
int dialogWidth = (int) fullScreenWidth * 0.9);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.WRAP_CONTENT;
params.width = dialogWidth;
getWindow().setAttributes(params);