Android LayoutInflater not working - android

I created an xml file (a dialog) and I wanted to reduce it with LayoutInflater but its always big almost as my screen. Here's the code:
protected Dialog onCreateDialog(int id) {
switch(id) {
case NICKNAME_DIALOG_ID:
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View layout = inflater.inflate(R.layout.nickname_dialog,
(ViewGroup) findViewById(R.id.linearLayout_root));
final EditText nickname =
(EditText)findViewById(R.id.editText_nickname);
AlertDialog.Builder nicknameBuilder = new AlertDialog.Builder(this);
nicknameBuilder.setView(layout);
nicknameBuilder.setTitle(R.string.nicname_dialog_title);
nicknameBuilder.setNegativeButton(R.string.nickname_dialog_cancel,
new DialogInterface.OnClickListener() {
#SuppressWarnings("deprecation")
public void onClick(DialogInterface dialog, int whichButton) {
SettingsActivity.this.removeDialog(NICKNAME_DIALOG_ID);
}
});
nicknameBuilder.setPositiveButton(R.string.nickname_dialog_ok,
new DialogInterface.OnClickListener() {
#SuppressWarnings("deprecation")
public void onClick(DialogInterface dialog, int which) {
String text = nickname.getText().toString();
Editor editor = m_GameSettings.edit();
editor.putString(VolimHrvatsku.GAME_PREFERENCES_NICKNAME,
text);
editor.commit();
SettingsActivity.this.removeDialog(NICKNAME_DIALOG_ID);
}
});
AlertDialog nicknameDialog = nicknameBuilder.create();
return nicknameDialog;
}
return null;
}
nickname dialog xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/sahovnica"
android:orientation="vertical" >
<TextView
android:id="#+id/textView_enter_nickname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/settings_enter_nick"
android:textColor="#color/dark_grey"
android:textSize="#dimen/scores_text_dim"
android:textStyle="bold" />
<EditText
android:id="#+id/editText_nickname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<RelativeLayout
android:id="#+id/relativeLayout_nickname"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</RelativeLayout>
</LinearLayout>
email dialog xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout_email"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/sahovnica"
android:orientation="vertical" >
<TextView
android:id="#+id/textView_body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/email_dialog_text"
android:textColor="#color/dark_grey"
android:textSize="#dimen/scores_text_dim"
android:textStyle="bold" />
<EditText
android:id="#+id/editText_body"
android:layout_width="match_parent"
android:layout_height="150dp"
android:ems="10" >
<requestFocus />
</EditText>
<RelativeLayout
android:id="#+id/relativeLayout_email"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</RelativeLayout>
</LinearLayout>
And I would like to ask btw, if it is possible to change the look of YES and NO buttons in AlertDialog?
Thanks in advance...

In emaildialog.xml do the layout width and height wrap_content
I think so, this might help you.

Related

Android dialog layout broken how to fix?

I had problems with this when I was programming it. I thought I'd cured it by specifying a fixed percentage height for the body of the dialog. I thought this was bad form, because the user might have a slim display, or set large fonts, which would cut of the EditText box.
Anyway that solution has failed beyond the emulator.
It looks like Android is assigning a fixed height to a dialog, and if my custom title has devoured too much of this height, squeezing everything else off. Is this correct and how do I fix it?
Problem
public class GetUserNameDialogFragment extends DialogFragment {
final String TAG = "GetUserNameDialog";
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyAlertDialogStyle);
//TODO getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = getActivity().getLayoutInflater();
final View sunburst =inflater.inflate(R.layout.dialog_user_name, null);
builder.setView(sunburst);
builder.setCancelable(false)
.setPositiveButton("Let's go!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.wtf(TAG, "button press");
EditText name = (EditText) sunburst.findViewById(R.id.nameEditText);
String userName = name.getText().toString().trim();
//TODO needs to be validated
SharedPreferences sharedPref = getActivity().getSharedPreferences("userDetails", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("userName", userName );
editor.commit();
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
Here's the xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
app:srcCompat="#drawable/ic_ball_sunburst_classic"
android:background="#color/colorAccent"
/>
<LinearLayout
android:layout_width="250dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:layout_height="125dp">
<EditText
android:id="#+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:hint="Enter your first name"/>
</LinearLayout>
</LinearLayout>
All help very much appreciated, the first impression of my app - what a disaster!
Why don't you use Dialog instead of AlertDialog ?
I built the example using a Dialog and it works perfectly
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:layout_width=" 290dp"
android:layout_height="wrap_content"
android:background="#4CAF50">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:src="#drawable/base"
android:background="#color/colorAccent"
/>
<EditText
android:textColor="#fff"
android:textColorHint="#fff"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:id="#+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your first name"/>
<Button
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:id="#+id/letsGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Let's Go"
android:textColor="#fff"
android:background="#android:color/transparent"/>
final Dialog dialog = new Dialog(context);
if(dialog.getWindow() != null){
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
dialog.setContentView(R.layout.dialog);
Button letsGO = (Button) dialog.findViewById(R.id.letsGo);
EditText nameEditText = (EditText) dialog.findViewById(R.id.nameEditText);
letsGO.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Lets GO!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
dialog.show();

Android ListVIew Swipe to dismiss

I need some help.I'm using this EnhanceListView for swipe to dismiss in my alertDialog but the swipe is not working. Does the swipe to dismiss doesn't work on alertdialog? or Am i missing something.? I tried setting the directions but still not working.
here is my code
public void viewDataList() {
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.alert_opendata, null);
lv = (EnhancedListView) view
.findViewById(R.id.data_list);
TextView emptyText = (TextView) view.findViewById(android.R.id.empty);
lv.setEmptyView(emptyText);
DisplayDataAdapter adapter = displayData();
lv.setAdapter(adapter);
lv.setDismissCallback(new OnDismissCallback() {
#Override
public Undoable onDismiss(EnhancedListView arg0, int arg1) {
// TODO Auto-generated method stub
return null;
}
});
lv.setSwipingLayout(R.id.swiping_layout);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setView(view);
builder.setNegativeButton("Close",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
return;
}
});
builder.create().show();
}
my xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<de.timroes.android.listview.EnhancedListView
android:id="#+id/data_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C0C0C0"
android:minHeight="30dp" >
</de.timroes.android.listview.EnhancedListView>
<TextView
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="No Data"
android:textSize="20sp" />
</LinearLayout>
my List Item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swiping_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:background="#F8f8f8"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="#+id/datalist_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F8f8f8"
android:padding="10dp"
android:textSize="20sp" >
</TextView>
</LinearLayout>
i don't see you setting anywhere lv.enableSwipeToDismiss();
also, you may want to add stuff like:
lv.setUndoStyle(EnhancedListView.UndoStyle.MULTILEVEL_POPUP);
lv.setSwipeDirection(EnhancedListView.SwipeDirection.BOTH);

AlertDialog custom view is not appearing

I'm building a simple AlertDialog with custom layout from XML. Here is how it's supposed to look (taken from Eclipse): It's simply 4 columns:
Here is the xml of the image above:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="1.0" >
<ImageView
android:id="#+id/colorpicker_dialog_color1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:background="#FFF"
android:tag="0xFFF" />
<ImageView
android:id="#+id/colorpicker_dialog_color2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:background="#FFDD66"
android:tag="0xFFDD66" />
<ImageView
android:id="#+id/colorpicker_dialog_color3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:background="#66CCFF"
android:tag="0x66CCFF" />
<ImageView
android:id="#+id/colorpicker_dialog_color4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:background="#B6C0D2"
android:tag="0xB6C0D2" />
</LinearLayout>
Here is the code of the costume Alert Dialog:
public void showColorPickerDialog() {
LayoutInflater inflater = LayoutInflater.from(this);
View layout = inflater
.inflate(R.layout.color_picker_dialog, null);
ImageView clr1 = (ImageView) layout
.findViewById(R.id.colorpicker_dialog_color1);
clr1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// .. code
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("cccc");
builder.setView(layout)
.setCancelable(true)
.setPositiveButton("ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
}).show();
}
The problem is that in my dialog I dont see any column.
Try using Views instead of ImageViews in your layout. The problem using ImageViews in your case is that since you do not specify the src attribute for the ImageView, it will display a 0x0-sized image, which leaves your views invisible.

Android Null Pointer,where shoud I put the code for activity?

I set a swith card with three tab in the main activity,there is a listview in the third tab,click the listview will pop up time options dialog box.
public void onSetWarn(){
LayoutInflater factoryInflater = LayoutInflater.from(MainActivity.this);
final View dialogView = factoryInflater.inflate(R.layout.dialog, null);
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setTitle("set")
.setView(dialogView)
.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "set success!", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).create();
/*
timePicker = (TimePicker) this.findViewById(R.id.TimePicker1);
timePicker.setIs24HourView(true);
*/
dialog.show();
}
where should i put this code
timePicker = (TimePicker) this.findViewById(R.id.TimePicker1);
timePicker.setIs24HourView(true);
I trid several places,but all the NULL pointer errors.
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="reminder the time"
/>
<TimePicker
android:id="#+id/TimePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="remind way"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ring"
/>
<CheckBox
android:id="#+id/checkBox2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="shake"
/>
</LinearLayout>
</LinearLayout>
You should use dialogView.findViewById(R.id.TimePicker1) instead of this.findViewById(R.id.TimePicker1).That is because TimePicker1 exists in the view tree of dialogView (You load it from dialog.xml by LayoutInflator)

How to create and show dialog in the android application?

Good evening, programmers. I have a problem. Please help me to solve it.
I am writing an android application. I can't create custom dialogs. I have errors or my application looks very awfully. I have some dialogs in my application. Here is of a part of my programm:
The view is very terrible, isn't it? I want that one of the layouts to be in the dialog but it appears in the main activity. Why?
dialogtable.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
>
<LinearLayout
android:id="#+id/LinearLayout02"
android:orientation="horizontal"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
>
<TextView
android:id="#+id/textRows"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Amount of rows "/>
<TextView
android:id="#+id/textColumns"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="Amount of columns"/>
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout03"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
<EditText
android:id="#+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:inputType="number"
android:gravity="right"/>
<EditText
android:id="#+id/EditText02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"/>
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout04"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
<TextView
android:id="#+id/textBorder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="Width of border"/>
<EditText
android:id="#+id/EditText03"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"/>
</LinearLayout>
</LinearLayout>
The code from MainActivity
#Override
protected Dialog onCreateDialog(int id) {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.dialogtable, (ViewGroup)findViewById(R.id.LinearLayout01));
AlertDialog.Builder alert = null;
alert = new AlertDialog.Builder(this);
alert.setView(layout);
alert.setTitle("Create table");
alert.setCancelable(false);
alert.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alert.setPositiveButton("OK",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Editable rows = fieldForRows.getText();
Editable columns = fieldForColumns.getText();
Editable border = fieldForWidthOfBroder.getText();
TableAction tableAction = new TableAction(MainActivity.this.visualPane);
tableAction.insertTable(rows, columns, border);
}
});
Dialog dialog = null;
dialog = new Dialog(this);
switch (id) {
case IDD_CUSTOM_INSERT_TABLE:
dialog.setTitle("Insert table");
dialog.setContentView(R.layout.dialogtable);
return dialog;
case IDD_CUSTOM_INSERT_IMAGE:
dialog.setTitle("Insert image");
return dialog;
default:
return alert.create();
}
}
The appearance of the application before calling showDialog():
http://s015.radikal.ru/i330/1101/d0/f7cc38852dc6.jpg
The appearance of the application after calling showDialog():
http://s003.radikal.ru/i201/1101/f0/d1ebc436427f.jpg
Why it looks so terrible? What is wrong?
In your case you should use RelativeLayout instead of LinearLayout. So that you can set android:layout_weight and other relative properties on the TextViews to make their width equal.
Have a look at http://developer.android.com/resources/tutorials/views/hello-relativelayout.html and http://developer.android.com/reference/android/widget/RelativeLayout.html

Categories

Resources