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
Related
On the following code, I programatically added the textview on the layout.addview
textView = new TextView(Chat.this);
/*textView.setTypeface(null, Typeface.BOLD);*/
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
getResources().getDimension(R.dimen.original));
textView.setText(message);
final ImageView imgView = new ImageView(Chat.this);
LinearLayout childLayout = new LinearLayout(Chat.this);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp2.setMargins(0, 0, 0, 0);
LinearLayout.LayoutParams LPimgView = new LinearLayout.LayoutParams(
100,
100 /*ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT*/);
I also implemented a menu where a user can choose to resize the textviews on his screen by choosing one of the options given. here's the code:
public void createDialogBoxWithRbutton(){
/*totalMsg = countChatWithMsg1 + countChatWithMsg2;*/
AlertDialog.Builder builder = new AlertDialog.Builder(Chat.this);
builder.setTitle("Select From the Options");
builder.setSingleChoiceItems(values, -1, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
/*for(int i = 0; i < layout.getChildCount(); i++){
Toast.makeText(Chat.this, "Total messages: " + i, Toast.LENGTH_SHORT).show();
}*/
switch (item){
case 0:
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.font1));
/*textSize = 15;*/
break;
case 1:
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.font2));
/*textSize = 20;*/
break;
case 2:
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.font3));
/* textSize = 30;*/
break;
}
alertDialog1.dismiss();
}
});
alertDialog1 = builder.create();
alertDialog1.show();
}
But what happen is that when I click an option, it is only the least textview on the layout view that is being changed. The other textview remains on its size.
What could be the cause of this?
you can use simple property for set textsize programatically
your_textview.setTextSize(24); // in sp
I have created a dynamic layout for edittext with add and minus icons. When i click on add, layout should recreated again and in place of add image, minus should shown.Add icon is changed to minus on first click but minus is not changed to plus.When i clicked on minus, sub layout is removed but not able to change icon.
This is my dynamic layout:
public void dLayout(){
count++;
lay_frame = new LinearLayout(this);
lay_frame.setOrientation(LinearLayout.VERTICAL);
lay_frame.setId(count);
for (int i =0; i<numClass; i++){
lay_main = new LinearLayout(this);
lay_uncle = new LinearLayout(this);
lay_cousin = new LinearLayout(this);
lay_main.setOrientation(LinearLayout.VERTICAL);
lay_uncle.setOrientation(LinearLayout.HORIZONTAL);
lay_uncle.setGravity(Gravity.CENTER);
lay_cousin.setOrientation(LinearLayout.HORIZONTAL);
lay_cousin.setGravity(Gravity.CENTER);
txt_uncle = new TextView(this);
txt_uncle.setText("Uncle Name");
txt_uncle.setTextColor(Color.BLACK);
txt_uncle.setPadding(0, 20 , 0, 20);
txt_uncle.setTextSize(14);
txt_cousin = new TextView(this);
txt_cousin.setText("Cousin Name");
txt_cousin.setTextColor(Color.BLACK);
txt_cousin.setPadding(0, 20 , 0, 0);
txt_cousin.setTextSize(14);
img_add = new ImageView(this);
img_add.setImageResource(R.drawable.add01);
img_add.setPadding(8, 0, 0 ,0);
img_minus = new ImageView(this);
img_minus.setImageResource(R.drawable.minus);
img_minus.setPadding(8, 0, 0 ,0);
img_minus.setVisibility(View.GONE);
img_cousinadd = new ImageView(this);
img_cousinadd.setImageResource(R.drawable.add01);
img_cousinadd.setPadding(8, 0, 0 ,0);
img_cousinminus = new ImageView(this);
img_cousinminus.setImageResource(R.drawable.minus);
img_cousinminus.setPadding(8, 0, 0 ,0);
ed_uncle = new EditText(this);
ed_uncle.setInputType(InputType.TYPE_CLASS_TEXT);
ed_uncle.setPadding(12, 8 ,8 ,8);
ed_uncle.setTextColor(Color.BLACK);
ed_uncle.setTextSize(14);
ed_uncle.setBackgroundResource(R.drawable.border);
ed_cousin = new EditText(this);
ed_cousin.setInputType(InputType.TYPE_CLASS_TEXT);
ed_cousin.setPadding(12, 8 ,8 ,8);
ed_cousin.setTextColor(Color.BLACK);
ed_cousin.setTextSize(14);
ed_cousin.setBackgroundResource(R.drawable.border);
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 80, 8.5f
);
params.setMargins(80, 0, 0, 0);
ed_cousin.setLayoutParams(params);
txt_cousin.setLayoutParams(params);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 80, 8.5f
);
params.setMargins(40, 0, 0, 0);
ed_uncle.setLayoutParams(params1);
img_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
img_add.setVisibility(View.GONE);
img_minus.setVisibility(View.VISIBLE);
dLayout();
}
});
img_minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (count > 0) {
final LinearLayout temp = (LinearLayout) mainLayout.findViewById(count);
mainLayout.removeView(temp);
count--;
}
img_minus.setVisibility(View.GONE);
img_add.setVisibility(View.VISIBLE);
}
});
lay_main.addView(txt_uncle);
lay_uncle.addView(ed_uncle);
lay_uncle.addView(img_add);
lay_uncle.addView(img_minus);
lay_main.addView(lay_uncle);
lay_main.addView(txt_cousin);
lay_cousin.addView(ed_cousin);
lay_cousin.addView(img_cousinadd);
lay_main.addView(lay_cousin);
lay_frame.addView(lay_main);
}
mainLayout.addView(lay_frame);
}
Change your btn add click logic to this
img_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dLayout();
img_add.setVisibility(View.GONE);
img_minus.setVisibility(View.VISIBLE);
}
});
Because after setting visibility again your setting the minus button visibility gone in the dLayout(); which means the property is overridden by next value
In onClick listener for minus button,lay_frame LinearLayout been removed from the main layout. Minus button is child of lay_frame layout, which is no more attached to view tree. If you are removing the view on click on minus button,changing the icon from minus to plus image doesn't make any sense.
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();
Whats wrong with this code?
It only shows the grey popup window, no ScrollView or TextView appear.
However, if I add the button denoted by the false branch, the button appears.
Here I am adding more text, because SO won't let me post without more text.
Thanks!
static PopupWindow DebugScreen( Activity a )
{
final PopupWindow popUp = new PopupWindow( a );
final View cv = a.getWindow().getDecorView().findViewById( android.R.id.content );
FrameLayout.LayoutParams p = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, Gravity.TOP );
FrameLayout fl = new FrameLayout( a );
fl.setLayoutParams(p);
final ScrollView sv = new ScrollView( a );
final TextView tv = new TextView( a );
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) (cv.getHeight() * 0.9), Gravity.TOP);
tv.setText("Hello World");
tv.setMovementMethod(LinkMovementMethod.getInstance());
tv.setLayoutParams( lp );
sv.addView(tv, lp);
if ( false ) {
final Button b;
b = new Button(a);
b.setText("abc123");
b.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
fl.addView(b);
}
popUp.setContentView(fl);
popUp.showAtLocation(cv, Gravity.BOTTOM, 0, 0);
popUp.setFocusable(false);
popUp.setOutsideTouchable(true);
popUp.setTouchable(true);
popUp.setTouchInterceptor(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if ( event.getAction() == MotionEvent.ACTION_OUTSIDE) {
popUp.dismiss();
return true;
}
return false;
}
});
popUp.update( 0, 0, (int) (cv.getWidth() * 0.9), cv.getHeight() );
return( popUp );
}
You missed fl.addView(sv); to add ScrollView to FrameLayout. Add it before popUp.setContentView(fl); to see the TextView.
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);