I recently updated a custom view so I could potentially add an EditText to the center of my canvas (so to speak).
After adding my onDraw code to dispatchDraw my custom LinearLayout works in the same way as my previous custom view did.
Now, how can I add an EditText smack in the middle of the layout?
So far I am trying this:
EditText edit = new EditText(getContext());
edit.setText("My EditText");
edit.setTextSize((int)Math.ceil(thickness/2));
edit.setWidth((int)(diameter*0.07f));
edit.setX(centerX);
edit.setY(centerY);
addView(edit);
Forgive some of the variables, they are not too important but I'm trying to add the EditText using the X and Y coordinates.
Thanks for any help.
UPDATE:
I have updated my LinearLayout constructor to inflate the comment_edit.xml file to see if I could get it to work this way.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_comment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/edittext_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:imeOptions="actionDone" />
</LinearLayout>
The LinearLayout constructor extract is as follows:
LayoutInflater inflater = LayoutInflater.from(context);
LinearLayout view = (LinearLayout) inflater.inflate(R.layout.comment_edit, this, false);
EditText edit = (EditText) view.findViewById(R.id.edittext_comment);
edit.setText("Add Comment");
edit.setX(112);
edit.setY(117);
addView(view);
I have also tried:
LayoutInflater inflater = LayoutInflater.from(context);
LinearLayout view = (LinearLayout) inflater.inflate(R.layout.comment_edit, this, false);
EditText edit = (EditText) view.findViewById(R.id.edittext_comment);
edit.setText("Add Comment");
view.setX(112);
view.setY(117);
addView(view);
The EditText still does not appear
In case the answer is not found yet. Every dynamically created view should have layout parameters (WRAP_CONTENT, FILL_PARENT, etc) that is why is not shown. For more information please edit your question accordingly.
An example might be:
editText.setLayoutParams(new LayoutParams(WRAP_CONTENT,WRAP_CONTENT));
EDIT:
Now your problem is that the edit text is not centered. Ok, I can give you an overview but I believe you must research from now on because there are a lot of things to do. First of all the LinearLayout is what it says, is a layout that it stacks the views vertically or horizontally. The position of the text shouldn't be given with the actual pixels of the screen but with some other specific layout parameters. Take Gravity for example, it aligns the inner view of a layout to the center, left, etc. Add gravity property of the LinearLayout.
I hope it helped
Related
I'm trying to make the whole screen clickable so I'm trying to set an id for a layout and find it in java class. When I do this, it says incompatiable types (layout vs a view). I understand they are different types but upon googling this, several posts have suggested selecting the layout in an activity in this manner. They can make a call such as this -
RelativeLayout theLayout = (RelativeLayout) this.findViewById(R.id.Layout)
and they don't seem to be getting this error.
Another stackoverflow post with this - onTouchListener for entire screen
My code referencing this is below -
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:id="#+id/thelayout" >
<TextView android:text="This will change with speech" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/textview"/>
</RelativeLayout>
And in activity -
RelativeLayout theLayout = this.findViewById(R.id.thelayout);
Am I missing somethinng?
Instead of this,
RelativeLayout theLayout = this.findViewById(R.id.thelayout)
Try this in your Activity,
RelativeLayout theLayout = (RelativeLayout) this.findViewById(R.id.thelayout)
Instead of using setContentView(int layoutResID) with the layout id, inflate the layout get the view reference and then use the setContentView(View view).
View layout = View.inflate(context, R.layout.theLayout, null)
setContentView(layout)
You don't need to cast the layout to RelativeLayout, as you can add the click/touch listener to a View class.
I want to know, there is a linearlayout and use that with setContentView function. Also there is a spinner inside of linearlayout. What I want to do is, create a new layout inside of /res/layout folder and add it into layout that I set with setContentView.
Is there anyway or I need to do that programmatically?
EDIT:
I think I couldn't tell.
I have 2 two layouts(ready). I use the first layout with setContentView.For example, there is a buton and if user click that button, I want to add second layout bottom of first layout when application running.
Easiest you do that with include in the xml of your main layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="#layout/second" />
</LinearLayout>
It´s also possible to do it programmatically, but this way I think it is more clear.
Edit:
To do this programmatically, put this code in listener of the first button.
RelativeLayout view = (RelativeLayout) findViewById(R.id.RelativeLayout1);
Button b = new Button(getApplicationContext());
b.setText("Click me too!");
view.addView(b);
Instead of creating a button (or whatever you want) you can also inflate a premade layout.
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.second, null);
view.addView(v);
I don't think you can change the res folder programmatically. You need to add any layout programmatically only.
Edited:
Get the 2nd layout's instance using findViewById and use setVisibility method to control the layout's visibility.
ANDROID
I have a layout defined in xml and have a static textview, edittext and checkboxes which are all formatted as below:
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="2"
android:singleLine="true"
android:textSize="14dp"
android:width="180dp"
I add textview, edittext and checkbox dynamically. I need the new added ones to have the same visual display as the ones already present(static ones) on the layout! Could someone guide or point me how to go about it?
You can add the layout by inflating each time you need textview, edittext and chekbox.This way your layout will have same look.Because you are reusing your static layout in xml.
See Layout Inflating for details
You have a layout with textview,edittext and checkbox.Now you will use the layout as a view.Like you said for each row you have to inflate the layout and add it to the row.So for every row you will have the copy of the layout.
TableLayout layout=(TableLayout)findViewById(R.id.tblLayout);
Now you can add the view after inflating the layout
View rowView = inflater.inflate(R.layout.row_layout, null, true);
TableRow tblrow=new TableRow(this);
tblRow.add(rowView);
layout.add(tblRow);
You can create any control Dynamic like this way.
Here i show for Edit Text same way you can do for others.
EditText et = new EditText(this);
et.setText("");
et.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
Use the following code for dynamic configurations about EditText.
EditText my_edit = (EditText) findViewById(R.id.my_edit);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.weight = 2.0f;
my_edit.setLayoutParams(params);
my_edit.setTextSize(20);
my_edit.setWidth(180);
my_edit.setSingleLine(true);
For this you must need LinearLayout as a parent of your EditText.
I hope this helps you somehow.
Thanks.
Is there a way to get the parameters from a XML view, modify some stuff on it and then use it as content view ?
Let's say I have a normal LinearLayout and I want to make that work:
LinearLayout layout = (LinearLayout) findViewById(R.layout.main);
setContentView(layout);
Instead of :
setContentView(R.layout.main);
Yes.
To be more specific, we need more specific info from you.
Edit
You can, for example, do the following.
Say you have in your xml specification a TextView:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/mytv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:textStyle="bold"/>
</RelativeLayout>
Now you want to center horizontal the TextView programmatically:
setContentView(R.id.main);
TextView myTV = (TextView) findViewById(R.id.mytv);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) myTV.getLayoutParams();
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
myTV.setLayoutParams(lp);
You just set the contentview at the start, and don't need to set it again when you change the variables.
You can do anyything you want to the layouts even after setContentView. When you do operations like add items to a layout or set a background, the views in the screen are redrawn.
onCreate method is where you can modify layouts as it it about to begin drawing on to a screen.
I have a layout issue. What I do is this:
create TableLayout in xml with zero children:
<TableLayout android:id="#+id/t_layout_contents"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/l_layout_tags"
android:stretchColumns="1"
android:paddingLeft="5dip"
android:paddingRight="5dip" />
Insert first row programmatically in onCreate():
TableLayout tLayoutContents = (TableLayout)findViewById(R.id.t_layout_contents);
NoteElement nr_1 = new NoteElement(this);
tLayoutContents.addView(nr_1);
Class "NoteElement" extends TableRow. The 1st row just consists of a blank ImageView as a placeholder and an EditText to enter text. NoteElement's constructor looks like this:
public NoteElement(Context c) {
super(c);
this.context = c;
defaultText = c.getResources().getString(R.string.create_note_help_text);
imageView = new ImageView(context);
imageView.setImageResource(android.R.color.transparent);
LayoutParams params = new LayoutParams(0);
imageView.setLayoutParams(params);
addView(imageView);
addView(addTextField());
}
Method addTextField() specifies the attributes for the EditText widget:
private EditText addTextField() {
editText = new EditText(context);
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
editText.setMinLines(4);
editText.setRawInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
editText.setHint(R.string.create_note_et_blank_text);
editText.setAutoLinkMask(Linkify.ALL);
editText.setPadding(5, 0, 0, 0);
editText.setGravity(Gravity.TOP);
editText.setVerticalScrollBarEnabled(true);
LayoutParams params = new LayoutParams(1);
editText.setLayoutParams(params);
return editText;
}
So far, so good. But my problem occurs as soon as the available space for the chars is depleted. The EditText does not resize itself but switches to a single line EditText.
I am desperatly looking for a way in which the EditText resizes itself in its height dynamically, being dependant on the inserted text length.
Does anyone have a hint on this?
Okay, I got it. This seems to be an issue of TableLayout in general. I reimplemented the Layout with a simple LinearLayout. I serves the purpose equally and the EditText is displayed properly. In fact I don't see a reason to use a TableLayout and right now I can't think of a situation in which one would actually need it, i.e. a LinearLayout would be insufficient.
So I recommend using other Layouts like LinearLayout or RelativeLayout whenever possible. But note that these are just my two cents...
I just took another look at TableLayout (for an entirely different purpose) and stumbled upon setColumnShrinkable(int columnIndex, boolean isShrinkable) which should have helped me out on my former issue.
See the documentation for details:
https://developer.android.com/reference/android/widget/TableLayout.html#setColumnShrinkable%28int,%20boolean%29
Please note that I haven't tested this.