create a dialog in android with Mvvmcross based on layout - android

Sorry if this looks so easy, but I looked to the N+1 video about dialogs and it's showing creating a dialog in code and not by using the layout.
Here's what I did:
ChangePasswordView.axml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="16dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="#+id/oldpassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/YourOldPassword"
android:inputType="textPassword"
local:MvxBind="Text OldPassword" />
<EditText
android:id="#+id/newpassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="#string/YourNewPassword"
local:MvxBind="Text NewPassword" />
<Button
android:text="#string/ChangePassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
local:MvxBind="Click ChangePasswordCommand"
android:paddingRight="42dp"
android:paddingLeft="42dp" />
</LinearLayout>
</FrameLayout>
ChangePasswordView.cs:
[Activity]
public class ChangePasswordView : MvxDialogActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
DroidResources.Initialise(typeof(Resource.Layout));
this.SetContentView(Resource.Layout.ChangePassword);
}
}
Calling the display of the dialog from a view model:
void ChangePassword()
{
this.ShowViewModel<ChangePasswordViewModel>();
}
I also have Setup.cs:
public class Setup : MvxAndroidDialogSetup
{
...
}
Running results in an error complaining about:
Your content must have a ListView whose id attribute is 'android.R.id.list

I think there is some confusion here about what 'Dialog' is
Dialog 1 - https://github.com/migueldeicaza/MonoTouch.Dialog and https://github.com/kevinmcmahon/MonoDroid.Dialog
A Dialog in the sense of http://slodge.blogspot.co.uk/2013/05/n23-dialogs-n1-days-of-mvvmcross.html is a MonoTouch.Dialog or MonoDroid.Dialog based 'data form'.
These are defined in code - because that's what MonoTouch.Dialog and MonoDroid.Dialog are about. For Droid, you can override individual Elements of the display - each Element is loaded from a resource - but you can't load the whole 'Activity' as a resource.
Dialog 2 - http://developer.android.com/guide/topics/ui/dialogs.html
If you are not looking for MonoTouch.Dialog style pages, but are instead looking for Android native popup dialogs, then there isn't much about these in MvvmCross. There is a sample of a fragment resource-based dialog in https://github.com/slodge/MvvmCross-Tutorials/blob/master/Fragments/FragmentSample.UI.Droid/Views/HomeView.cs and some interesting recent discussion about how to display them in MvvmCross Dialog

Related

Activity goes below status bar when soft keyboard opened

I am developing android custom keyboard and faced this problem. I am using candidates view to show suggestions. But in some cases when I am trying to use keyboard in applications Layout is going under status bar.
I have attached two images of problem. In both screenshots with red rectangle I have marked problem and with green rectangle Keyboard layout.
I have done research about this and find that it can be caused if Candidate view is not created in right way, but I have implemented candidate view as shown in examples.This is an Image from Facebook messenger
In this image keyboard is overlapping messengers edit text layout too.
Here are my code snippets from SoftKeyboard where i am creating candidates view.`
#Override public View onCreateCandidatesView() {
mCandidateView = new CandidateView(this);
mCandidateView.setService(this);
mCandidateView.setOnTranslitModeChangedListener(this);
return mCandidateView;
}`
my keyboard xml file
<?xml version="1.0" encoding="utf-8"?>
<com.innorise.kitkeysarmenian.LatinKeyboardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/keyboard"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="50dp"
android:keyTextSize="18dp"
android:keyTextColor="#000"
android:background="#color/keyboardBackground"
android:keyPreviewLayout="#layout/preview"
android:popupLayout="#layout/keyboard_popup_layout"
android:drawingCacheQuality="high">
and here is my candidates view xml file`
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:padding="5dp"
android:gravity="center">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="#+id/candidatesList"
android:orientation="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/candidateSwitch"/>
<TextView
android:textSize="18sp"
android:visibility="gone"
android:text="Translit is off."
android:gravity="center"
android:id="#+id/translitModeisOff"
android:layout_toLeftOf="#+id/candidateSwitch"
android:layout_width="match_parent"
android:layout_height="30dp" />
<com.rm.rmswitch.RMSwitch
android:id="#+id/candidateSwitch"
app:switchDesign="slim"
app:switchBkgCheckedColor="#4ed736"
app:switchBkgNotCheckedColor="#cccccc"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
app:switchToggleCheckedColor="#aca2a2"
app:switchToggleNotCheckedColor="#aca2a2"
android:layout_width="50dp"
android:layout_height="35dp"/>
</RelativeLayout>
add this to your manifest :
<activity
android:name="activity.name"
android:windowSoftInputMode="adjustPan|adjustResize"/>

Resize layout to get into other layout

I created a menuView.xml layout to be in all of the layouts of my activity. This layout has one column on each border and a title bar like this:
ComposeView http://img845.imageshack.us/img845/2121/d6zp.png
I insert this layout in the other layouts this way:
<!-- Show menu -->
<com.example.MenuView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
But if one of the layouts has full screen view, part of this view gets covered by the MenuView, so...
How could I tell to this view to adapt its size to the blank space inside the MenuView to not get covered by it?
UPDATE -- full XML included
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#drawable/degradado">
<!-- Show menu -->
<com.example.MenuView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RelativeLayout
android:id="#+id/Left_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true" >
//Here go buttons, views, etc...
</RelativeLayout>
<RelativeLayout
android:id="#+id/Right_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true" >
//Here go buttons, views, etc...
</RelativeLayout>
What happens here is that these 2 Relative layouts get covered by the MenuView (The darkest gre borders and the top black bar), and the ideal way would be that these 2 layouts get fitted to the blank space (the clearest gray).
I can solve this setting margin sizes to the Relative layouts to fit inside of it, but i know this is not the best way to do it, so I don't know if there is another way.
I think the best way to solve your issue is with inheritance.
If you define an Activity that can be used as a template for all your fleshed out Activitys to add their content to.
I don't know what you custom menu is 'made of' but as a simple example:
Create a basic Activity with code:
public class ActivityWithMenu extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_with_menu_layout);
}
}
and xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ActivityWithMenu" >
<TextView
android:layout_width="match_parent"
android:layout_height="20dip"
android:background="#ff000000"
android:textColor="#ffffffff"
android:text="Main Menu Title Bar"
android:id="#+id/mainmenutitle" />
<LinearLayout
android:layout_width="20dip"
android:layout_height="match_parent"
android:layout_below="#+id/mainmenutitle"
android:layout_alignParentLeft="true"
android:background="#ff999999"
android:orientation="vertical"
android:id="#+id/lefthandmenu" />
<LinearLayout
android:layout_width="20dip"
android:layout_height="match_parent"
android:layout_below="#+id/mainmenutitle"
android:layout_alignParentRight="true"
android:background="#ff999999"
android:orientation="vertical"
android:id="#+id/righthandmenu" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="#+id/righthandmenu"
android:layout_toRightOf="#+id/lefthandmenu"
android:layout_below="#+id/mainmenutitle"
android:orientation="vertical"
android:id="#+id/activitycontent"
/>
</RelativeLayout>
Then create your xml for a specific Activity, in this case a simple 'Hello World' layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff00ff00"
android:text="Hello World!"
/>
</LinearLayout>
</ScrollView>
But now when you write the code for this Activity, extend 'ActivityWithMenu' instead of the Activity class direct and inflate this xml layout as follows:
public class Activity1 extends ActivityWithMenu
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
LinearLayout ll = (LinearLayout)findViewById(R.id.activitycontent);
ScrollView sv = (ScrollView)this.getLayoutInflater().inflate(R.layout.activity1_layout, ll, false);
ll.addView(sv);
}
}
I have added the code for making the Activity fullscreen here instead of in the parent ActivityWithMenu class assuming that you wouldn't want them all displayed that way but you could move it into the parent class if appropriate.
Hope this helps.

ListView's Item Layout error

I am using following layout for my list view items.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://scheme.android.com/apk/res/android"
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="10dip"
android:textSize="18sp"
android:minHeight="40sp"
/>
But I am getting an error in the logcat java.lang.RuntimeException ... that you must supply a layout_width argument.
My main.xml is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget28"
android:background="#ff000033"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="70px"
android:layout_height="30px"
android:paddingLeft="2px"
android:paddingTop="2px"
android:background="#drawable/ic_launcher"
></ImageView>
<ListView
android:id="#+id/myListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
and this is my activity's onCreate code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context=this.getApplicationContext();
this.setTitle("NP Headline News");
myListView=(ListView)findViewById(R.id.myListView);
myListView.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View v, int index,long id) {
// TODO Auto-generated method stub
String urlAddress=urlAddresses[index];
String urlCaption = urlsCaptions[index];
}
});
int layoutId=R.layout.simple_list_item_1;
// int layoutId=android.R.layout.simple_list_item_1;
aa = new ArrayAdapter<String>(this,layoutId,this.urlsCaptions);
myListView.setAdapter(aa);
}
I think that it is something related to my simple_list_item_1 because if I use android.R.layout.simple_list_item_1 then every thing works ok
Here is your error cleared layout:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="10dip"
android:textSize="18sp"
android:minHeight="40sp"
/>
Can you able to differentiate your error contained code and this above one.
I wish you guys to don't tense and urge while code something, that must be lead you to blocked and confused minded.
For these type of errors, you could use to debug yourself with the help of DDMS.
Because the code is very very simple to view right.
And some words about the previous answers:
Guys that is not necessary to declare a TextView or any View only inside of the Layout file.
It is also a View, So it can capable to sit alone in a Canvas.
And also the problem is not by the "fill_parent", and "match_parent" Something.
I think you might be tensed now also, because i am taking this much time to give you the reason of your error.
Here we go,
the error is because simply in the following line,
xmlns:android="http://schemas.android.com/apk/res/android"
In the above line you mistakenly typed scheme instead of schemas.
That is the error.
So only i mentioned if you had look at the code with out tense you can get it easily.
xmlns denotes the namespace , that should be proper in order make the android: prefixed attributes as valuable.
Hope you understand and the answer helpful to you.
YOU should puy your TextView under a Layout, since your are using a unique TextView you could add a LinearLayout
<?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="fill_parent"
android:orientation="vertical" >
<TextView xmlns:android="http://scheme.android.com/apk/res/android"
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="10dip"
android:textSize="18sp"
android:minHeight="40sp"
/>
</LinearLayout>
I guess problem comes from something different.
You are doing a custom item view, but you use standard AdapterView.
For a standard arrayAdapter you need to fulfil the expected structure.
Alternatively you build an own ArrayAdapter and supply your dedicated getView
Android Custom Listview

Android Layout : On Top it takes too much of space

In my layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="#+id/user_pswd_new_root" android:scrollbars="vertical"
android:soundEffectsEnabled="true">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/ScrollViewLogin" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbarStyle="outsideInset" android:scrollbars="vertical|horizontal" android:visibility="visible">
<RelativeLayout android:layout_width="fill_parent" android:id="#+id/relativeLayout1" android:layout_height="fill_parent">
<ImageView android:background="#drawable/logo_login" android:layout_height="wrap_content" android:id="#+id/imageView1"
android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"
android:padding="0dp" android:layout_margin="0dp"/>
...............
</RelativeLayout>
</ScrollView>
</RelativeLayout>
With the above code, I set in a Dialog and things are shown proeprly, but there is lot of unwanted space above the image which unnecessarily increases the height of the dialog. See the results :
Any idea why the top space is occupied. And how do I get rid of it. Where am I going wrong ?
It's the title of the Dialog, which is empty because you didn't specify a title (but the view is still there). You have to remove it, for example like this:
class MyDialog extends Dialog {
#Override
protected void onCreate(Bundle savedInstanceState) {
// make sure to call requestWindowFeature before setContentView
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.my_dialog_layout);
// other initialization code
}
// ...
}
But that depends on whether you are using a simple Dialog or an AlertDialog. If this doesn't work for you, post your dialog-creation code (Java) and I'll update my answer to show how to remove the title in your case.

findViewById(int) returns null for custom views, but not Android built-ins

The Situation: I have some custom components in my layout. I have a common layout frame that I load in my Activity base class's onCreate(), and then load my content layouts in my implementations using an inflater, setting the root to the content column of the main layout.
The Problem: When I grab a reference to the Views, to actually extract the user's input, Activity.findViewById() returns null. It is perhaps a clue that the CheckBox and Button I have in the layout do NOT return null; I get a valid reference to the widget.
Things I've Tried: I know I am properly loading and inflating the layout xml, because everything shows up, and the standard Views in the same layout can be found by ID. I can interact with my Views and put content into them and everything, but I can't get a reference to them in my code.
I have tried cleaning the project, multiple times. R.id is fresh and up-to-date.
I have checked the Console and Error Log, and there's no UI/XML errors reported.
I tried getting a handle to the root layout of the content I loaded for this activity, and calling View.findViewById() to get my references, and that returns null, too. If I examine the layout in the debugger, I can drill down and find my Views in mChildren.
Perhaps another clue:
public VideoChooser(Context pCtxt, AttributeSet pAttrs)
{
super(pCtxt, pAttrs);
Log.d("VideoChooser", "Expected ID: " + R.id.vchShareVids + " | actual: " + getId());
}
will result in the following output:
DEBUG/VideoChooser(10372): Expected ID: 2131296271 | actual: 268435456
The ID assigned to the View doesn't match the ID in R.id! Why would that be? I know it's loading the android:id attribute, or else it would be -1 (View.NO_ID).
The Common Layout Frame:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:foo="http://schemas.android.com/apk/res/com.foo"
android:id="#+id/common_frame" android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent">
<!-- top banner -->
<LinearLayout android:id="#+id/frame_header" android:orientation="horizontal"
android:layout_height="wrap_content" android:layout_width="match_parent"
android:layout_marginBottom="16dp">
<ImageView android:src="#drawable/banner"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" android:layout_weight="1" />
</LinearLayout>
<!-- content column -->
<LinearLayout android:id="#+id/frame_content" android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_marginLeft="32dp" android:layout_marginRight="32dp" />
</LinearLayout>
The Content Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:foo="http://schemas.android.com/apk/res/com.foo"
android:id="#+id/content_panel" android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent">
<com.foo.view.VideoChooser android:id="#+id/vchShareVids"
foo:prompt_text="#string/prompt_share_vid" foo:prompt_size="16dp"
foo:preview_height="80dp"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_marginBottom="12dp" android:hapticFeedbackEnabled="true" />
<com.foo.view.ContactChooser android:id="#+id/cchRecipients"
foo:prompt_text="#string/prompt_share_email" foo:prompt_size="16dp"
foo:preview_lines="3" foo:dialog_title="Pretend you are picking contacts"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_marginBottom="12dp" android:hapticFeedbackEnabled="true" />
<com.foo.view.TextChooser android:id="#+id/tchDescription"
foo:prompt_text="#string/prompt_share_description" foo:prompt_size="16dp"
foo:preview_lines="1" foo:dialog_title="#string/title_msg_chooser_dlg"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_marginBottom="12dp" android:hapticFeedbackEnabled="true" />
<CheckBox android:id="#+id/chkReshare" android:text="#string/prompt_reshare"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:checked="true" android:hapticFeedbackEnabled="true" />
<Button android:id="#+id/btnSend" android:text="#string/btn_send"
android:layout_width="#dimen/btn_width" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" android:hapticFeedbackEnabled="true" />
</LinearLayout>
Activity Base Class onCreate():
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.common_frame);
}
Activity Implementation onCreate():
#Override
protected void onCreate(Bundle pState)
{
super.onCreate(pState);
load_content_view(R.layout.content_layout);
ViewGroup tLayout = (ViewGroup)findViewById(R.id.content_panel);
// These all return null
mCchVideo = (ContentChooser)tLayout.findViewById(R.id.vchShareVids);
mCchContact = (ContentChooser)tLayout.findViewById(R.id.cchRecipients);
mCchDescription = (ContentChooser)tLayout.findViewById(R.id.tchDescription);
// These return valid references
mChkReshare = (CheckBox)findViewById(R.id.chkReshare);
mBtnSend = (Button)findViewById(R.id.btnSend);
// ...
}
protected void load_content_view(int pResId)
{
LinearLayout tColumn = (LinearLayout)findViewById(R.id.frame_content);
getLayoutInflater().inflate(pResId, tColumn);
}
I had a similar problem and the solution for mine was to make sure the constructor of the custom widget calls
super(context, attrs);
My constructor didn't pass attrs to super and thus the view id was messed up and the findviewbyId returned null.
It's very difficult to find problem without actual sources. I've created sample project based on your posts and its fully works.
I believe that there is very simple mistake and you'll find it.
If you want, you may try it
Yeah... I'm an idiot. View was of course setting the correct ID, and then one of my init methods went back and clobbered it.
facepalm

Categories

Resources