How do I Add an EditText Dynamically to xamarin android - android

XAMARIN ANDROID DYNAMIC CLICK: I want the spinner, edit text and positive button to be placed side by side such that when the button is clicked by any user, it regenrates the same content (spinner, edittext and a negative button) and for every click on the positive button the same thing happens while for every click on the negative button, the content where the negative button is will be removed.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layoutTeste"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Spinner
android:id="#+id/spn"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#id/et" />
<EditText
android:id="#+id/et"
android:layout_height="wrap_content"
android:layout_width="200dp"
android:hint="Enter a Value"
android:layout_gravity="center"
android:layout_marginLeft="70dp" />
<Button
android:id="#+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btnDisplay"
android:layout_alignParentRight="true"
android:layout_alignBaseline="#id/et" />
</RelativeLayout>

Dynamically you can create view in xamarin android programmatically for your need.
For Example
LinearLayout LL = FindViewById<LinearLayout(Resource.Id.Linear_MS); //root layout
var param = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
param.SetMargins(20, 20, 20, 10);
var et = new EditText(this);
LL.AddView(et, param);
In Xml side by side design use this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layoutTeste"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<Spinner
android:id="#+id/spn"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:layout_alignTop="#id/et" />
<EditText
android:id="#+id/et"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:hint="Enter a Value"
android:layout_gravity="center"
android:layout_weight="1"
/>
<Button
android:id="#+id/btnDisplay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/btnDisplay"
android:layout_weight="1"
/>
</LinearLayout>
</RelativeLayout>

Related

Normal layout - adapting layout_toRightOf for 4" and 4,7" screen

I have the following example: an edittext and on the right an imagebutton. The only solution I have now is to design for the 4" screen.
So the look on the 4" screen is like that:
and the 4,7" screen:
Is there a solution to draw the width of the edittext to the end right and still see the imagebutton? I've tried to play with width = wrap_content or match_parent + marginRight but I don't get anywhere:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/edtl"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:hint="Test enter text to modify"
android:textSize="#dimen/a_normal"
android:textColor="#0000ff"
android:inputType="textWebEditText"
android:paddingLeft="10dp"
android:layout_marginTop="20dp"
android:layout_alignStart="#id/sel_email"
/>
<ImageButton
android:id="#+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/edtl"
android:layout_alignBottom="#id/edtl"
android:src="#mipmap/info"
/>
</RelativeLayout>
I tried this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/edtl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Test enter text to modify text1 text2 text3 text4"
android:textSize="#dimen/a_normal"
android:textColor="#0000ff"
android:inputType="textWebEditText"
android:paddingLeft="10dp"
android:layout_marginTop="20dp"
/>
<ImageButton
android:id="#+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#id/edtl"
android:layout_alignEnd="#id/edtl"
android:layout_alignBottom="#id/edtl"
android:src="#mipmap/info"
/>
</RelativeLayout>
which looks like that. close: i'm missing the gap between the edittext and the imagebutton.
Use this, tried and working -
<ImageButton
android:id="#+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/edtl"
android:layout_alignParentRight="true"
/>
<EditText
android:id="#+id/edtl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Test enter text to modify"
android:textColor="#0000ff"
android:inputType="textWebEditText"
android:paddingLeft="10dp"
android:layout_marginTop="20dp"
android:layout_toLeftOf="#id/info"
/>
Please try this:
<?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">
<EditText
android:id="#+id/edtl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/info"
android:hint="Test enter text to modify"
android:textSize="#dimen/a_normal"
android:textColor="#0000ff"
android:inputType="textWebEditText"
android:paddingLeft="10dp"
android:layout_marginTop="20dp"/>
<ImageButton
android:id="#+id/info"
android:margin_left="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alignParentRight="true"
android:alignParentEnd="true"
android:src="#mipmap/info"
/>
</RelativeLayout>
This will make the EditText stay left of the ImageButton and take up the remainder of the width.

Three buttons evenly divided linear layout

So, I am trying to use a linear layout to hold three different buttons, each should take up 33% of the width on one line. This linear layout will be below all the other content in my relative layout, which holds all the other widgets on this activity. Unfortunately, when I add the third button into the layout, the other two have a white bar along the bottom of them and the third button (in this case the home button) is positioned higher than the others.
Can someone explain this behavior and how to rectify it? Thanks.
This is the XML file for the linear layout, I've removed all the text for the other widgets. If that would be helpful, I can post it as well.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<Button
android:layout_width="0dp"
android:layout_weight=".33"
android:layout_height="wrap_content"
android:text="#string/adfazsdfasdfadfsagjlfkdlgjklfsadgfjgps"
android:onClick="resetDates"
android:background="#drawable/sumbitstyleing"
android:id="#+id/resetDatesButton" />
<Button
android:layout_width="0dp"
android:layout_weight=".33"
android:layout_height="wrap_content"
android:text="#string/home"
android:onClick="home"
android:id="#+id/homeButtonSearch"
android:background="#drawable/generalbutton" />
<Button
android:layout_weight=".33"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/submitchanges"
android:onClick="submitChanges"
android:background="#drawable/sumbitstyleing"
android:id="#+id/submitchanges" />
</LinearLayout>
The first picture is WITHOUT the third button, the second picture is WITH the third button.
Try this, I have removed Properties you should use with RelativeLayout and unnecessary with LinearLayout, and buttons aligned horizontally,
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
and assigned Weightsum to LinearLayout along with button height to match parents
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:weightSum="1">
<Button
android:id="#+id/resetDatesButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".33"
android:onClick="resetDates"
android:text="Rese check dates" />
<Button
android:id="#+id/homeButtonSearch"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".33"
android:onClick="home"
android:text="home" />
<Button
android:id="#+id/submitchanges"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".33"
android:onClick="submitChanges"
android:text="submit changes" />
</LinearLayout>
</RelativeLayout>
Result
Here you can go this way :
<?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:background="#color/white"
>
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerHorizontal="true"/>
<!--Buttons -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3"
>
<android.support.v7.widget.AppCompatButton
android:id="#+id/motor_team_send_sms_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/send_sms"
/>
<android.support.v7.widget.AppCompatButton
android:id="#+id/motor_team_sleep_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/sleep"
/>
<android.support.v7.widget.AppCompatButton
android:id="#+id/motor_team_rise_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/rise"
/>
</LinearLayout>
Output is:
This might help you.
Try this, in your LinearLayout add android:gravity="center"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center">
...
</LinearLayout>

adding views to android layout on button click

i want to dynamically add views (textview or EditText) to my layout as per user instructions.
i am developing this just to figure out things and learn how they work.
my XML file is as follows
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/email"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="email1"
/>
<EditText
android:id="#+id/edit1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="enter mail "
/>
</LinearLayout>
<LinearLayout
android:id="#+id/phones"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="phone number"
/>
<EditText
android:id="#+id/edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="enter phone "
/>
</LinearLayout>
<LinearLayout
android:id="#+id/myButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add mail"
android:onClick="addmail"
/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add phone"
/>
</LinearLayout>
</LinearLayout>
now here , i have two buttons. when the user clicks on addmail i want an edittext to be added to my layout (below my first email EditText in LinearLayout with id "email".
i have set android:onClick to addmail for the button
and my addmail function is as follows:
public void addmail(){
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((LayoutParams. WRAP_CONTENT) , (LayoutParams.WRAP_CONTENT));
EditText edittv = new EditText(getApplicationContext());
edittv.setLayoutParams(lp);
LinearLayout ll1=(LinearLayout)findViewById(R.id.email);
ll1.addView(edittv);
}
but the code is not working . what modifications are required? where am i going wrong.
Are my concepts at fault?
thankyou in advance
Don't use Application Context here: EditText edittv = new EditText(getApplicationContext());, just use specific Activity Context and it will solve your problem. I hope so :D!

Android text will not align to center

I have a TextView and 3 Buttons centered in a RelativeLayout, and the text in all three of them is left-justified with the parent center as the leftmost line.
I've tried changing the gravity of the TextView, Buttons, and layout but nothing is working and I'm baffled because I've never seen Android align text like this before.
I want the Button text to be centered within each Button and the top text to be center-justified.
An image of what it looks like is here: http://i.stack.imgur.com/9lHEd.png
Here's my 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" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_margin="20dp"
android:gravity="center_horizontal"
android:text="#string/greeting"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/startQuizButton"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:onClick="#string/startQuiz"
android:text="#string/start_quiz" />
<Button
android:id="#+id/lessonsButton"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_below="#+id/startQuizButton"
android:layout_centerHorizontal="true"
android:onClick="#string/startLesson"
android:text="#string/lessonsString" />
<Button
android:id="#+id/viewAllButton"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_below="#+id/lessonsButton"
android:layout_centerHorizontal="true"
android:onClick="#string/showAll"
android:text="#string/view_all" />
I included a RelativeLayout because you have it as your parent layout. However, i am not sure that it is needed. See if the following code is what you are looking for.
<?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:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Greetings" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start Quiz" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lessons" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View all questions" />
</LinearLayout>
</RelativeLayout>
add in button tab android:gravity="center"...see if it works..
u need to set layout width and height to fill_parent and everything will work fine
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="your text" />
if you want to centre some text, then you could also do it this way:
<TextView
android:text="Quantity"
android:textSize="34sp"
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:layout_centerHorizontal="true"
android:paddingTop="10dp"/>
here i have changed made it horozontal by using android:layout_centerHorizontal="true"which takes a boolean value of true or false, by making it true, it will automatically be centred on all devices
hope that helps

Aligning all the app to the right

I've searched how can I align the app to the right side and the text to be written from right to left.
The only thing I've found and succeeded to do is to make the text be written from right to left by adding android:gravity="right" in the EditText and the Button, but still their sort in the line if from the left to the right.
The activity_main.xml file -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="right"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<EditText android:id="#+id/text_message"
android:gravity="right"
android:layout_weight="1.0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/text_message" />
<Button android:layout_height="wrap_content"
android:gravity="right"
android:layout_width="wrap_content"
android:text="#string/send_button"
android:onClick="ButtonClicked" />
<Button android:layout_height="wrap_content"
android:gravity="right"
android:layout_width="wrap_content"
android:text="#string/delete_button"
android:onClick="DeleteClicked" />
</LinearLayout>
What I meant in "the sort is from the left to the right" is that the EditText is written before the "Send" button, and the "Send" button id written before the "Delete" button - so what's shown is "[____x] [Send][Delete]" and not "[Delete][Send][_____]", as I would want to.
You can use instead of LinearLayout, RelativeLayout and give your buttons some ids using
android:id="#+id/your_button_id
and then use the tags android:layout_toRightOf and android:layout_toLeftOf. In your case, something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button android:id="#+id/delete_button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/delete_button"
android:onClick="DeleteClicked" />
<Button android:id="#+id/send_button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="#+id/delete_button"
android:text="#string/send_button"
android:onClick="ButtonClicked" />
<EditText android:id="#+id/text_message"
android:layout_weight="1.0"
android:layout_width="0dp"
android:layout_toRightOf="#+id/send_button"
android:layout_height="wrap_content"
android:hint="#string/text_message" />
</RelativeLayout>
Try using
android:layout_gravity
You could also just use a RelativeLayout add
android:align_ParentRight="true"
Change
android:gravity="right"
to
android:layout_gravity="right"

Categories

Resources