I am new in developing android apps using Xamarin. Am developing a app which contains many dialog and activities, one of which dialog has layout like this, a timepicker, two buttons labeled as "CANCEL" and "OK" and a textview. I want to do something like this, when user click on "OK" button the time selected in timepicker will display on textview. (or i want to extract value of timepicker in button click event)
It's pretty simple. Check this out:
Your Main.axml(layout)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TimePicker
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/picker" />
<Button
android:text="OK"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btnOK" />
<Button
android:text="Cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btnCancel" />
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txt"
android:textSize="40dp" />
</LinearLayout>
and your OnCreate() method must contains this:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button OK = FindViewById<Button>(Resource.Id.btnOK);
Button Cancel = FindViewById<Button>(Resource.Id.btnCancel);
TimePicker picker = FindViewById<TimePicker>(Resource.Id.picker);
TextView txt = FindViewById<TextView>(Resource.Id.txt);
OK.Click += (sender, e) =>
{
//getting values from TImePicker via CurrentHour/CurrentMinutes
txt.Text = String.Format("{0} : {1}",picker.CurrentHour,picker.CurrentMinute);
};
}
Related
Hello guys I'm working on application and my layout structure is as following :
RelativeLayout:
CompoundView:
CompoundView:
RelativeLayout:
Button
Button
RecyclerView
BrowseFragment:
Only rows
My problem is when I get to first row of browse fragment and first item in it and I want to go up (D-PAD-UP) to focus button it does nothing it works only when I push left ( D-PAD-LEFT). Anyone has solution for this ?
So the problem was in BrowseFrameLayout for some reason and to solve this issue I had to override onFocusSearchListener and manage focus myself.
In BrowseFragment which I extended I have this method :
public void workaroundFocus(){
if(getView() != null) {
View viewToFocus = getActivity().findViewById(R.id.view_to_focus);
BrowseFrameLayout browseFrameLayout = getView().findViewById(android.support.v17.leanback.R.id.browse_frame);
browseFrameLayout.setOnFocusSearchListener((focused, direction) -> {
if (direction == View.FOCUS_UP) {
return viewToFocus;
}
else {
return null;
}
});
}
}
And then:
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
workaroundFocus();
/*
Rest of the code
*/
}
And voila it works.
Since you are using the RelativeLayout you should layout the components in the order that you would like to be navigated.
Using the XML attributes presented in the RelativeLayout reference document, you will be able to establish an navigation order too:
<?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="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/reminder" />
<Spinner
android:id="#+id/dates"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#id/name"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/times" />
<Spinner
android:id="#id/times"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="#id/name"
android:layout_alignParentRight="true" />
<Button
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="#id/times"
android:layout_alignParentRight="true"
android:text="#string/done" />
</RelativeLayout>
Here the dates Spinner is below the name EditText and at left of times Spinner, the times component is below the name one and the done Button is below times. See the image below:
See the Build Basic TV Layouts guide for more tv related details.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Adding a vertical scrollbar to an AlertDialog in Android?
In my project, there is some button.Whenever I click on one button ,it shows a alert dialog box with the description of that button.i have wrote description in string.xml with . whenever i click the button, it fetch the description from string.xml and shows us.But my problem is that my message( which will show in dialog box)is too long.so,i want scrollable alertdialog box.with out creating another xml file with, how is it possible in java code.
You are able to customize the dialog box by using the setView method and apply a custom view to the dialog box.
AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setIcon(R.drawable.icon);
ad.setTitle("Instructions ...");
**ad.setView(LayoutInflater.from(this).inflate(R.layout.instructions_dialog,null));**
ad.setPositiveButton("OK",
new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
// OK, go back to Main menu
}
}
);
ad.setOnCancelListener(new DialogInterface.OnCancelListener(){
public void onCancel(DialogInterface dialog) {
// OK, go back to Main menu
}}
);
I hope this is helpful.
this is an example.you can change according to your requirement
you can add this on button event
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.alert);
Spinner spin = (Spinner)dialog.findViewById(R.id.spinner1);
Spinner spin2 = (Spinner)dialog.findViewById(R.id.spinner2);
EditText t1 =(EditText)dialog.findViewById(R.id.editText1);
EditText t2 =(EditText)dialog.findViewById(R.id.editText2);
EditText t3 =(EditText)dialog.findViewById(R.id.editText3);
EditText t4 =(EditText)dialog.findViewById(R.id.editText4);
dialog.show();
alert.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="#+id/widget54"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="211dp"
android:layout_height="wrap_content"
android:layout_x="10dp"
android:layout_y="11dp"
>
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="208dp"
android:layout_height="wrap_content"
android:layout_x="19dp"
android:layout_y="76dp"
/>
<EditText
android:id="#+id/editText3"
android:layout_width="208dp"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="138dp"
/>
<EditText
android:id="#+id/editText4"
android:layout_width="212dp"
android:layout_height="wrap_content"
android:layout_x="18dp"
android:layout_y="196dp"
/>
<Spinner
android:id="#+id/spinner1"
android:layout_width="222dp"
android:layout_height="wrap_content"
android:layout_x="5dp"
android:layout_y="254dp" />
<Spinner
android:id="#+id/spinner2"
android:layout_width="225dp"
android:layout_height="wrap_content"
android:layout_x="2dp"
android:layout_y="310dp" />
</LinearLayout>
</ScrollView>
I have an android app, and i want it to have two views which are similar to one another.
For example :
<Button
android:id="#+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="OK" />
and
<Button
android:id="#+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
notice that the only change is that i removed the centerHorizontal line. but this is a simplified example.
Now, i want to create an app, that sometimes (ex- using a random func) uses view A and sometimes uses view B.
Is it possible to do this "views switch" at runtime?
Is it possible to build this app using the two views (notice that the button should have the same ID, i don't want to implement the logic twice)?
thanks a lot!
The only way I imagine to do that is:
Having each button in its own layout file.
Inflate the corresponding based on your function result.
Append it to the view.
Sample code:
button_a.xml:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="OK" />
button_b.xml:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK_2" />
Your activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutInflater inflater = LayoutInflater.from(this);
Button button;
if (Math.random() > 0.5) {
button = (Button) inflater.inflate(R.layout.button_a, null);
} else {
button = (Button) inflater.inflate(R.layout.button_b, null);
}
/* ...
Set listeners to the button and other stuff
...
*/
//find the view to wich you want to append the button
LinearLayout view = (LinearLayout) this.findViewById(R.id.linearLayout1);
//append the button
view.addView(button);
}
If you want this to happen dynamically (i.e. not in the onCreate, but after some user input) you can always remove the button from layout, and inflate a new random-chosen one.
Hope this helps!
How can I show in AlertDialog two EditText and 2 TextView?
custom_dialog.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:orientation="horizontal"
android:id="#id/layout_root" android:padding="10.0dip"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Libellé"
android:id="#+id/Text_Libelle"
/>
<EditText android:id="#+id/Edit_Libelle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
<EditText android:id="#+id/Edit_Url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
On click on button I want to show this interface in alert dialog.
You basically have the layout created so all you have to do is assign it to your custom dialog like so..
Set the custom layout to the dialog after initializing..
Dialog dialog = new Dialog(someContext);
dialog.setContentView(R.layout.customdialoglayout); //Set dialog view to custom layout
To wire up your handlers you will have to do something to the effect of..
EditText myText = (EditText)dialog.findViewById(R.id.Edit_Libelle);
http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog
I am developing an app with tabs. On one of the tabs,
I have two EditText fields and a couple of buttons. I want to add an ExpandableListView
to the tab, below the rest of the elements.
My problem is that the current class extends Activity. All the example I've found extend ExpandableListActivity, but I can't extend both for the same class.
How can I add this list to the tab? I've added an ExpandableListView element in the XML file, but whenever I run it, it doesn't show up. I understand that I need to bind the view to some data, but how do I go about doing this?
Thank you.
Here is my class code:
public class DirectionsTab extends Activity
{
private Button clear_btn;
private Button go_btn;
private EditText origin_txt;
private EditText dest_txt;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.directions_tab);
// initialize views and onClick event handler
origin_txt = (EditText)findViewById(R.id.origin_txt);
dest_txt = (EditText)findViewById(R.id.dest_txt);
clear_btn = (Button)findViewById(R.id.clear_btn);
// clear all text fields and return focus to dest_txt
clear_btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
origin_txt.setText("");
dest_txt.setText("");
origin_txt.requestFocus();
}
});
} // end public void onCreate(Bundle savedInstanceState)
} // end public class DirectionsTab extends Activity
and XML:
<?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">
<TextView
android:id="#+id/origin_lbl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Origin"/>
<EditText
android:id="#+id/origin_txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background"
android:layout_below="#id/origin_lbl"/>
<TextView
android:id="#+id/dest_lbl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/origin_txt"
android:text="Destination" />
<EditText
android:id="#+id/dest_txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background"
android:layout_below="#id/dest_lbl" />
<ExpandableListView
android:id="#+id/listView"
android:layout_below="#+id/dest_txt"
android:layout_height="wrap_content"
android:layout_width="wrap_content" /><!--android:id="#android:id/list"-->
<Button style="?android:attr/buttonStyleSmall"
android:id="#+id/clear_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/dest_txt"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:layout_marginTop="10dip"
android:text="Clear" />
<Button style="?android:attr/buttonStyleSmall"
android:id="#+id/go_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/clear_btn"
android:layout_alignTop="#id/clear_btn"
android:text="Go!" />
</RelativeLayout>
I got this!, If anyone is having problems with this, make sure you follow these steps:
Copy and paste ExpandableList3 example (located in ApiDemos project from Samples folder in your Android SDK). This is just a good example of how to set up a list.
Make your class inherit from ExpandableListActivity
Create the following in your XML layout file:
<ExpandableListView
android:id="#android:id/list"
android:layout_below="#+id/go_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
It is VERY important that the ID property matches what you see above, otherwise, nothing will work. This little detail wasted plenty of hours for me and was discovered seating neatly as a small detail in the android docs.