Android TimePicker looks cropped - android

I create a dialog box with TimePicker inside it. This works very well on portrait mode, however, once it is changed into landscape mode, the TimePicker minus button looks cropped.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time = " android:textSize="28dp"/>
<TimePicker android:id="#+id/PickTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/text1"
android:layout_alignLeft="#id/text1"/>
<Button android:id="#+id/StartStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:layout_below="#id/text1"
android:layout_toRightOf="#id/PickTime"/>
</RelativeLayout>
My code for displaying the dialog box:
LayoutInflater factory;
factory = LayoutInflater.from(this);
View textEntryView = factory.inflate(R.layout.test, null);
AlertDialog dialog = new AlertDialog.Builder(this)
// .setTitle("Auto-Shutdown Setting")
.setView(textEntryView)
.setPositiveButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
/* User clicked OK so do some stuff */
}
})
.create();
dialog.show();
TimePicker timePicker = (TimePicker) dialog.findViewById(R.id.PickTime);
timePicker.setIs24HourView(true);
timePicker.setCurrentHour(12);
timePicker.setCurrentMinute(15);
timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// ...
}
});
Anyone knows why this is happening? Is it possible to customize the size of the dialog box (make it bigger)? Any other solution would be appreciated.
Edit by JJD:
Since I have a similar case with a combined date and time picker layout in a ScrollView I want to add screenshots here. #lwijono Please feel free to remove them if they do not show what you originally described. Or leave a comment and I will do so.

Related

Calling AlertDialog from MPAndrioid RadarChart

I am able to make the RadarChart work with some static data. Now i want to display a alert dialog when users click on the radarchart to ask the user to input some data and then use this input data to redraw the chart.
I have created a MyMarkerView class which extends MarkerView and in the refreshContent method add the code for the Alert Dialog view.
But the problem is that whenever uses click the AlerDialog, the refreshContent is again called and a new alert Dialog is created - and the user is not able to enter anything.
Below is the code in the refreshContent method
LayoutInflater li = LayoutInflater.from(mContext);
View promptsView = li.inflate(R.layout.month_target_analysis_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput1 = (EditText) promptsView.findViewById(R.id.textView1Edit);
final EditText userInput2 = (EditText) promptsView.findViewById(R.id.textView2Edit);
// set dialog message
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// get user input and set it to result
// edit text
//result.setText(userInput.getText());
String input1 = userInput1.getText().toString();
String input2 = userInput1.getText().toString();
if (userInput1.getError() == null && userInput2.getError() == null) {
//Do something
} else {
}
}
});
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
Below is the Xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:text="#string/l_target"
android:textAppearance="#style/ItemMedium" />
<EditText
android:id="#+id/textView1Edit"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:paddingLeft="50dp"
android:clickable="false"
android:inputType="number"
>
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:text="#string/g_target"
android:textAppearance="#style/ItemMedium" />
<EditText
android:id="#+id/textView2Edit"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:paddingLeft="50dp"
android:clickable="false"
android:inputType="number"
>
</EditText>
</LinearLayout>
Any ideas how to disable the calling of refreshContent ?
Or is there any other better way of handling this ?
Thanks
Praveen
I was not able to make it work using the MarkerView. Instead i defined a setOnChartGestureListener to catch all the possible action on the chart and then handled the necessary action when the action was caught.
Seems to be working.
mChart.setOnChartGestureListener(new OnChartGestureListener() {
#Override
public void onChartSingleTapped(MotionEvent me) {
// TODO Auto-generated method stub
Log.e(LOG_TAG, "single tap");
}
#Override
public void onChartDoubleTapped(MotionEvent me) {
// TODO Auto-generated method stub
Log.e(LOG_TAG, "double tap");
}
}
Thanks

How to center positive button for AlertDialog (android.support.v7.app.AlertDialog) have only one button

I used AlertDialog (android.support.v7.app.AlertDialog) for showing levels of game as below
AlertDialog dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.DialogLevelsStyle);
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.dialog_game_level, null);
builder.setView(view)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (dialog != null) {
dialog.cancel();
}
}
});
dialog = builder.create();
dialog.show();
//styles.xml
<style name="DialogLevelsStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#3caaf0</item>
</style>
But the dialog display the positive button OK at the button-right of the alert dialog, Please help me!
I update the question by added image:
This happens because of Rules for aligning buttons.
Action buttons are typically Cancel and/or OK, with OK indicating the preferred or most likely action. However, if the options consist of specific actions such as Close or Wait rather than a confirmation or cancellation of the action described in the content, then all the buttons should be active verbs. Order actions following these rules:
The dismissive action of a dialog is always on the left. Dismissive actions return to the user to the previous state.
The affirmative actions are on the right. Affirmative actions continue progress toward the user goal that triggered the dialog.
You can't change alignment for your positive/negative buttons. Simply add Button to your view which you inflate for dialog aligned to the center and process it as below:
Button btn = (Button) view.findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (dialog != null) {
dialog.cancel();
}
}
});
Update
This is how it should look like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<TextView
android:text="something"
android:id="#+id/id1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
<Button
android:id="#+id/id2"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</RelativeLayout>
P.S. I cannot check if everything properly, cuz my design window have some problems in Studio, but this is the right way and seems to work fine.

How can I create a custom dialog with two datepicker?

I just started to learn Android as a hobby and I would like to create a dialog with two datepicker
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.data_picker_dialog);
dialog.setTitle(R.string.date_period_picker);
dialog.show();
return true;
How can I get the selected values from the dialog? Is there a possibility to include the OK/Cancel button automatically on the dialog?
Is there a library which has such a functionality(Start and end date/period selection)?
It's probably best to read about Dialogs and Pickers first.
As for the implementation, you can have two buttons: One to show a date picker for the start date and another for the end date.
Edit: If you really want to show 2 date pickers in 1 dialog, here's an example of how to do it. First, create a custom XML layout.
/res/layout/custom_date_picker.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<DatePicker
android:id="#+id/dpStartDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:calendarViewShown="false" />
<DatePicker
android:id="#+id/dpEndDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:calendarViewShown="false" />
</LinearLayout>
Next is to use the above layout in a dialog:
// These variables will hold the date values later
private int startYear, startMonth, startDay, endYear, endMonth, endDay;
/**
* Displays the start and end date picker dialog
*/
public void showDatePicker() {
// Inflate your custom layout containing 2 DatePickers
LayoutInflater inflater = (LayoutInflater) getLayoutInflater();
View customView = inflater.inflate(R.layout.custom_date_picker, null);
// Define your date pickers
final DatePicker dpStartDate = (DatePicker) customView.findViewById(R.id.dpStartDate);
final DatePicker dpEndDate = (DatePicker) customView.findViewById(R.id.dpEndDate);
// Build the dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(customView); // Set the view of the dialog to your custom layout
builder.setTitle("Select start and end date");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
startYear = dpStartDate.getYear();
startMonth = dpStartDate.getMonth();
startDay = dpStartDate.getDayOfMonth();
endYear = dpEndDate.getYear();
endMonth = dpEndDate.getMonth();
endDay = dpEndDate.getDayOfMonth();
dialog.dismiss();
}});
// Create and show the dialog
builder.create().show();
}
Finally, you can show this dialog by simply calling showDatePicker().
Same for your layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<DatePicker
android:id="#+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<DatePicker
android:id="#+id/datePicker2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Just you want create date picker to your xml layout(data_picker_dialog).
And get data from your id

Customizing android DialogFragment

iam a beginner level programmer in Android.Now iam after a small app development and i have a dialogFragment.Everything is perfectly working and its displaying Dialog box also.But i have some difficulties with color scheme. I have changed the background color of layout and but its title bar color remains same white and also title text color blue and a blue line under that(need to change it to green).How i can achieve this?
please help me
here is my fragment code
public class ClientInfofrag extends DialogFragment {
public ClientInfofrag()
{
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.clientinfolayout, container);
getDialog().setTitle("Client Info");
return view;
}
}
Thank you
Since you are using the .setTitle() method it is only setting the title with the defualt settings, such as the white background. If you want to customize the title background color you will need to have an xml to do that. Also, for DialogFragments, from my knowledge and experience, you should use public Dialog onCreateDialog instead of public View onCreateView. That way you return a Dialog as opposed to just a View that you can then just call .show() on and it will display your dialog. Here is an example:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
Bundle args = getArguments();
currentName = args.getString(ARG_CURRENT_NAME);
builder.setView(inflater.inflate(R.layout.name_dialog, null));
builder.setTitle("Rename Rapper Program");
builder.setMessage("Enter a new name for " + currentName + ":");
builder.setPositiveButton("Rename", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
newName = (EditText) getDialog().findViewById(R.id.new_name);
newProgName = newName.getText().toString();
mRename.renameProgram(currentName, newProgName);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
return builder.create();
}
Here is an example dialog xml, though it is not the xml that is being inflated in the above DialogFragment:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:drawableLeft="#drawable/login"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="#FCD116"
android:text="#string/login"
android:textSize="36sp"/>
<EditText android:id="#+id/username"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="#string/un"/>
<EditText android:id="#+id/password"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="#string/pw"/>
</LinearLayout>
The LinearLayout is setting up the rest of the child items to be placed accordingly. The first TextView acts as my "title" bar and then the EditTexts are the "body" of the dialog. I have no buttons in the xml because I set those programmatically within the onCreateDialog like in the other snippet of code above.
The example of the above (TronicZomB) could work if you disable the default windows title:
// Remove the title
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
Try it!

How do I put a seek bar in an alert dialog?

I want an alert dialog box to pop up after I click a button to have a seek bar so that the person can change the value from 1-48. I've made a custom xml that has a seek bar and two text view and I would like the dialog box to have two buttons, one just cancels the dialog and the other does more work. Here is the code I have so far.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SeekBar android:max="48" android:layout_height="wrap_content" android:id="#+id/seekBar1" android:layout_width="fill_parent" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_marginTop="74dp"></SeekBar>
<TextView android:text="Change Hour Range" android:layout_height="wrap_content" android:id="#+id/textView1" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"></TextView>
<TextView android:text="Only most recent positions" android:layout_height="wrap_content" android:id="#+id/textView2" android:layout_width="wrap_content" android:layout_below="#+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="15dp"></TextView>
</RelativeLayout>
Here is the alert dialog that I have so far
new AlertDialog.Builder(ImTracking.this)
//is there something like setcontentview for alert dialogs?
.setPositiveButton("Cancel", null)
.setNegativeButton("OK",
new DialogInterface.OnClickListener() {
// does some work here
Yep builder.setView(View v); here is how you can use it.
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.yourLayoutId, (ViewGroup) findViewById(R.id.yourLayoutRoot));
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setView(layout);
AlertDialog alertDialog = builder.create();
alertDialog.show();
SeekBar sb = (SeekBar)layout.findViewById(R.id.yourSeekBar);
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
//Do something here with new value
}
});
Edit: Added progressListener to sample code. Do note that you cannot get a reference to your SeekBar until after you call alertDialog.show(), if the SeekBar is not being shown findViewById() will return null. Also note that you must use layout.findViewById(); because the SeekBar is a child of the RelativeLayout that 'layout' is a reference to.
//This is the code you seek!
public void ShowDialog(){
final AlertDialog.Builder popDialog = new AlertDialog.Builder(this);
final SeekBar seek = new SeekBar(this);
seek.setMax(255);
seek.setKeyProgressIncrement(1);
popDialog.setIcon(android.R.drawable.btn_star_big_on);
popDialog.setTitle("Please Select Into Your Desired Brightness ");
popDialog.setView(seek);
seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
txtView.setText("Value of : " + progress);
}
Or you can look on this tutorial

Categories

Resources