How to switch between 2 TextViews places on the layout? - android

I have an OnClickListener that listens to Button A clicks. I also have 2 TextViews below the Button.
I want that on every click on Button A, the 2 TextViews will switch their places between them, like this:
Before clicking:
TextView A
TextView B
After clicking:
TextView B
TextView A
How can I do it? Is there a special function that meant to do that? or some kind of a trick? Thanks!
actvity_main.xml:
<?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"
android:gravity="start"
tools:context="com.intap.tof.MainActivity">
<TextView
android:id="#+id/txtA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp"
android:visibility="visible"/>
<TextView
android:id="#+id/txtB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtA
android:layout_marginTop="83dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="visible"
android:textSize="20dp" />
</RelativeLayout>
MainActivity.java:
txtA = (TextView) findViewById(R.id.txtA);
txtB = (TextView) findViewById(R.id.txtB);
float mAX = txtA.getX();
float mAY = txtA.getY();
float mBX = txtB.getX();
float mBY= txtB.getY();
txtA.setX(mBX);
txtA.setY(mBY);
txtB.setX(mAX);
txtB.setY(mAY);

Trick is changing the x and y axis of both views :
Your xml code:
<?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"
android:gravity="start"
tools:context="com.intap.tof.MainActivity">
<TextView
android:id="#+id/txtA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp"
android:text="A"
android:tag="A"
android:clickable="true"
android:visibility="visible"/>
<TextView
android:id="#+id/txtB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtA"
android:text="B"
android:tag="B"
android:clickable="true"
android:layout_marginTop="83dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="visible"
android:textSize="20dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="switch"
android:id="#+id/btn1"
android:onClick="onClickButton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Java Code :
public class MainActivity extends Activity {
TextView txtA,txtB;
boolean _isOnTxtA;
Button btn1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtA = (TextView)findViewById(R.id.txtA);
txtB = (TextView)findViewById(R.id.txtB);
btn1 = (Button)findViewById(R.id.btn1);
}
public void onClickButton(View v)
{
float mPos1x,mPos1y,mPos2x,mPos2y;
mPos1x = txtB.getX();
mPos1y = txtB.getY();
mPos2x = txtA.getX();
mPos2y= txtA.getY();
if(_isOnTxtA)
{
txtB.setX(mPos2x);
txtB.setY(mPos2y);
txtA.setX(mPos1x);
txtA.setY(mPos1y);
_isOnTxtA = false;
}
else
{
txtB.setX(mPos2x);
txtB.setY(mPos2y);
txtA.setX(mPos1x);
txtA.setY(mPos1y);
_isOnTxtA= true;
}
}
}

View.bringToFront method may be useful.
Where your layout is like:
<LinearLayout>
<TextView A>
<TextView B>
</LinearLayout>
To call bringToFront on TextView A will bring it front (the last position in the parent LinearLayout).
<LinearLayout>
<TextView B>
<TextView A>
</LinearLayout>
For more detailed example, below is layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/text_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="#+id/text_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_a" />
<TextView
android:id="#+id/text_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_b" />
</LinearLayout>
In your OnClickListener (in your Activity) call:
View textViewA = findViewById(R.id.text_a);
textViewA.bringToFront();
This should work.
Toggling behavior can be achieved by this application. For example:
ViewGroup textHolder = (ViewGroup) findViewById(R.id.text_holder);
textHolder.getChildAt(0).bringToFront();
ViewGroup.getChildAt(0) always returns the first child of the ViewGroup. So everytime you call bringToFront on the first child will be bring to front.

All previous answers do not work because you use a relative layout where a linear layout will suffice.
If you have to go RelativeLayout, do the following in your click listener
TextView textA = (TextView) findViewById(R.id.txtA);
TextView textB = (TextView) findViewById(R.id.txtB);
RelativeLayout.LayoutParams textALayoutParams = (RelativeLayout.LayoutParams) textA.getLayoutParams();
textALayoutParams.addRule(RelativeLayout.BELOW, R.id.txtB);
textA.setLayoutParams(textALayoutParams);
RelativeLayout.LayoutParams textBLayoutParams = (RelativeLayout.LayoutParams) textB.getLayoutParams();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
textBLayoutParams.removeRule(RelativeLayout.BELOW);
} else {
textBLayoutParams.addRule(RelativeLayout.BELOW,0);
}
textB.setLayoutParams(textBLayoutParams);
This will place A under B. You can do the same for reversing.

Related

Accessing element from same layout parent

i have a listview with different components (2 linear layouts and a button) , what i want to do is when i click on a button that's inside one of those linear layouts , i want to access a textview that's inside the other linearlayout , is it possible ?
<?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:orientation="vertical">
<ListView
android:id="#+id/listV2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/icon"
android:layout_width="85dp"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="150dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/nom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/textCname"
android:textSize="21dp"
android:textStyle="bold" />
<TextView
android:id="#+id/numero"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12dp"
android:textColor="#color/textCother"
android:textStyle="italic" />
<TextView
android:id="#+id/ville"
android:layout_width="match_parent"
android:textColor="#color/textCother"
android:layout_height="wrap_content"
android:textSize="12dp"
android:textStyle="italic" />
</LinearLayout>
<Button
android:layout_width="50dp"
android:layout_height="wrap_content"
android:visibility="invisible" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
>
<ImageView
android:id="#+id/remove"
android:onClick="contactRemove"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/edit"
android:onClick="contactEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="22dp"/>
<ImageView
android:onClick="contactCall"
android:id="#+id/call"
android:layout_marginLeft="22dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
what i want to do is access the value of android:id="#+id/numero"
when i click on one of the image views
You can set OnClickListener interface provided by View class. Something like this.
Implement listener on your activity class
public class SpinnerActivity extends AppCompatActivity implements
View.OnClickListener
Declare class level variables for your views
private TextView tvNumero;
private ImageView ivRemove, ivEdit, ivContactCall;
Find initialise value in onCreate() method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gildi_spinner);
tvNumero = (TextView) findViewById(R.id.numero)
ivRemove = (ImageView) findViewById(R.id.remove);
ivEdit = (ImageView) findViewById(R.id.edit);
ivContactCall = (ImageView) findViewById(R.id.contactCall);
ivRemove.setOnClickListener(this);
ivEdit.setOnClickListener(this);
ivContactCall.setOnClickListener(this);
}
Implemented class from Onclickistener, where you get on click event for your registered views
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.remove:
case R.id.edit:
case R.id.contactCall:
Toast.makeText(this, tvNumero.getText().toString(), Toast.LENGTH_SHORT).show();
break;
}
}
NOTE : tvNumero.getText().toString() gives you the value of your desired TextView.
UPDATE :
Firstly replace your ListView with RecyclerView
Refer RecyclerView Example tutorial.
Secondly to achieve onClick for your listed items
Refer recyclerview-onclick tutorial.
Pass context when you create your adapter, Use that context to get the inflated view.
Adapter adapter = new Adapter(this);
Then in Adpater Class constructor :
public Adapter(Context context){
context.findViewById(R.id.textview);//consider this as numero textview
}
Then access it as per your requirement. Hope it helps!

View width for dynamically added view

I want to show a notification counter like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:padding="#dimen/_5sdp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:alpha=".5"
android:id="#+id/gvIcon"
android:src="#drawable/ic_person_reading"
android:scaleType="centerCrop"
android:layout_width="#dimen/_70sdp"
android:layout_height="#dimen/_70sdp" />
<LinearLayout
android:id="#+id/llTexts"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
style="#style/gridItemsText"
android:id="#+id/gvText"
android:text="Guten Morgen"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/partNoticeTooltip"
android:orientation="vertical"
android:background="#drawable/bg_tooltip_red"
android:layout_width="#dimen/tooltipWH"
android:layout_height="#dimen/tooltipWH">
<TextView
android:id="#+id/tvCounter"
android:text="4"
android:textColor="#color/white"
android:textSize="#dimen/h6"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Which has an output like this:
This is my expectation, but with dynamic way. So I wrote this in the setOnItemClickListener of my GridView:
View tooltip = context.getLayoutInflater().inflate(R.layout.part_tooltip, null);
TextView tvCounter = (TextView) tooltip.findViewById(R.id.tvCounter);
tvCounter.setText("" + counter);
LinearLayout llText = (LinearLayout) view.findViewById(R.id.llTexts);
llText.addView(tooltip);
the part_tooltip has exactly the same code but with another layout. And here is the output:
The red layout does not being displayed with full width. What am I missing?
You need to use a global layout listener and also the view tree observer:
ViewTreeObserver mVTO = parentLayoutOfTheViewAddedDynamically.getViewTreeObserver();
mVTO.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if(viewAddedDynamically != null){
int viewWidth = viewAddedDynamically.getWidth();
// any operation based on viewWidth is to be done here
}
}
});

Characters appear in the order as i click

For example. There is picture on the top of the screen below that there are some empty boxes and below the boxes there are some buttons. Every button has a character for text("a","c","t"). You click on a button and the button's text appear in the box. You can click them in the order you want to but the answer is "cat" so when you put the characters in the correct order then you got a toast.
I tried to do it with TextViews and Buttons. I can make the button disappear when i click on it and a textview appear in the same time. But every textview has a fix place on the screen, so i need to put every character in every box invisible and when i click on the "c" character it appear in the first box and the other "c" characters stay invisible. But if i click on the "a" first, then it appears in the second box because there is too much variation to do all. I'm not good at explaining but if anyone has an idea how to do that easier please response!
Here is my code:
public class MainActivity extends ActionBarActivity implements OnClickListener{
Button b1;
Button b2;
Button b3;
TextView tg1;
TextView tg2;
TextView tg3;
TextView to1;
TextView to2;
TextView to3;
TextView tl1;
TextView tl2;
TextView tl3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.bg);
b1.setOnClickListener(this);
b2 = (Button)findViewById(R.id.bo);
b2.setOnClickListener(this);
b3 = (Button)findViewById(R.id.bl);
b3.setOnClickListener(this);
tg1 = (TextView)findViewById(R.id.tg1);
tg2 = (TextView)findViewById(R.id.tg2);
tg3 = (TextView)findViewById(R.id.tg3);
to1 = (TextView)findViewById(R.id.to1);
to2 = (TextView)findViewById(R.id.to2);
to3 = (TextView)findViewById(R.id.to3);
tl1 = (TextView)findViewById(R.id.tl1);
tl2 = (TextView)findViewById(R.id.tl2);
tl3 = (TextView)findViewById(R.id.tl3);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.bg:
b1.setVisibility(View.INVISIBLE);
tg1.setVisibility(View.VISIBLE);
tg2.setVisibility(View.INVISIBLE);
tg3.setVisibility(View.INVISIBLE);
break;
case R.id.bo:
b2.setVisibility(View.INVISIBLE);
to2.setVisibility(View.VISIBLE);
to1.setVisibility(View.INVISIBLE);
to3.setVisibility(View.INVISIBLE);
break;
case R.id.bl:
b3.setVisibility(View.INVISIBLE);
tl3.setVisibility(View.VISIBLE);
tl2.setVisibility(View.INVISIBLE);
tl1.setVisibility(View.INVISIBLE);
}
}
}
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="hu.szada.gombokelso.MainActivity"
android:orientation="horizontal">
<TextView
android:id="#+id/tl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="invisible"
android:text="l"/>
<Button
android:id="#+id/bo"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="86dp"
android:onClick="onClick"
android:text="o" />
<Button
android:id="#+id/bl"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignBaseline="#+id/bg"
android:layout_alignBottom="#+id/bg"
android:layout_alignParentLeft="true"
android:layout_marginLeft="36dp"
android:onClick="onClick"
android:text="l" />
<Button
android:id="#+id/bg"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginRight="14dp"
android:layout_toLeftOf="#+id/bo"
android:onClick="onClick"
android:text="g" />
<TextView
android:id="#+id/tg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tl"
android:layout_alignBottom="#+id/tl"
android:layout_alignLeft="#+id/tl"
android:layout_weight="1"
android:visibility="invisible"
android:text="g" />
<TextView
android:id="#+id/to1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tl"
android:layout_alignBottom="#+id/tl"
android:layout_alignLeft="#+id/tl"
android:layout_weight="1"
android:visibility="invisible"
android:text="o" />
/// Second
<TextView
android:id="#+id/to2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tl1"
android:layout_alignBottom="#+id/tl1"
android:layout_marginLeft="19dp"
android:layout_toRightOf="#+id/tl1"
android:layout_weight="1"
android:visibility="invisible"
android:text="o" />
<TextView
android:id="#+id/tg2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/to2"
android:layout_alignBottom="#+id/to2"
android:layout_alignLeft="#+id/to2"
android:layout_weight="1"
android:visibility="invisible"
android:text="g" />
<TextView
android:id="#+id/tl2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/to2"
android:layout_alignBottom="#+id/to2"
android:layout_alignRight="#+id/to2"
android:layout_weight="1"
android:visibility="invisible"
android:text="l" />
/// Third
<TextView
android:id="#+id/tg3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/to2"
android:layout_alignBottom="#+id/to2"
android:layout_alignRight="#+id/bl"
android:layout_weight="1"
android:visibility="invisible"
android:text="g" />
<TextView
android:id="#+id/tl3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tg3"
android:layout_alignBottom="#+id/tg3"
android:layout_alignLeft="#+id/tg3"
android:layout_weight="1"
android:visibility="invisible"
android:text="l" />
<TextView
android:id="#+id/to3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tg3"
android:layout_alignBottom="#+id/tg3"
android:layout_toRightOf="#+id/tl3"
android:layout_weight="1"
android:visibility="invisible"
android:text="o" />
You might want to try a slightly different approach.
If I understand you correctly, you want to "type" a word out using given lettered buttons. Like one of those hangman style games.
Why not append the text views on the fly.
Something like
#Override
public void onClick(View v) {
//Grab the surrounding layout for the textviews
GridView answerGrid = (GridView)getViewById(R.id.answerGrid);
//Get the text that was on the button
Button b = (Button)v;
String btnText = b.getText().toString();
//Make a text view with text
TextView txt = new TextView();
text.setText(btnText);
//Append to text view container
answerGrid.addView(txt);
//Invisible button
b.setVisibility(View.INVISIBLE);
}
Haven't tested to see if this is perfect, but its a start.
=====
I've looked at your xml
Why not use GridViews?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
....>
<GridView android:id="#+id/answerGrid"
....>
<!-- Put nothing here. This is for answers -->
</GridView>
<GridView android:id="#+id/lettersGrid"
android:layout_below="answerGrid"
....>
<!-- Buttons in here -->
</GridView>
</RelativeLayout>
This way you can customise the number of rows/columns based on the length of the word you're playing with. And GridView will automatically give you a neat layout and spacing.
Have a look at the GridView doc and get it customised the way you want it.
See my edits above for the Java code.

get parent's view from a layout

I have a FragmentActivity with this layout:
<?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="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/menulabel"
android:textSize="24sp" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:layout_below="#+id/textView1"
android:hint="#string/search_title_hint" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/spinnersearch"
android:layout_below="#+id/editText1" />
<LinearLayout
android:id="#+id/layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2" >
<Spinner
android:id="#+id/spinner_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:prompt="#string/spinnersearch" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/reset_search" />
</LinearLayout>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttonnewcat"
android:layout_below="#+id/layout1" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttondelcat"
android:layout_below="#+id/button2" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttoneditcat"
android:layout_below="#+id/button3" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttonnewtext"
android:layout_below="#+id/button4" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttondeltext"
android:layout_below="#+id/button5" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<side.menu.scroll.ScrollerLinearLayout
android:id="#+id/menu_content_side_slide_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#drawable/ab_solid_example"
android:orientation="horizontal" >
<ImageView
android:id="#+id/main_tmp_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/actionbar_background"
android:src="#drawable/download" />
</LinearLayout>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg_groud" />
</side.menu.scroll.ScrollerLinearLayout>
</RelativeLayout>
and the ScrollerLinearLayout class is something like this:
public class ScrollerLinearLayout extends LinearLayout {
// code of the class
}
How to access from ScrollerLinearLayout to the parent layout to get - for example - the editetxt? FragmentActivity and ScrollerLineraLayout are in different packages of the same projects.
I tried to use the getParent() function in this way within the ScrollerLinearLayout but it doesn't work.
RelativeLayout r = (RelativeLayout) this.getParent().getParent();
int w = r.findViewById(R.id.editText1).getLayoutParams().width;
Someone help me? Thanks!
The getParent method returns a ViewParent, not a View. You need to cast the first call to getParent() also:
RelativeLayout r = (RelativeLayout) ((ViewGroup) this.getParent()).getParent();
As stated in the comments by the OP, this is causing a NPE. To debug, split this up into multiple parts:
ViewParent parent = this.getParent();
RelativeLayout r;
if (parent == null) {
Log.d("TEST", "this.getParent() is null");
}
else {
if (parent instanceof ViewGroup) {
ViewParent grandparent = ((ViewGroup) parent).getParent();
if (grandparent == null) {
Log.d("TEST", "((ViewGroup) this.getParent()).getParent() is null");
}
else {
if (parent instanceof RelativeLayout) {
r = (RelativeLayout) grandparent;
}
else {
Log.d("TEST", "((ViewGroup) this.getParent()).getParent() is not a RelativeLayout");
}
}
}
else {
Log.d("TEST", "this.getParent() is not a ViewGroup");
}
}
//now r is set to the desired RelativeLayout.
If you are trying to find a View from your Fragment then try doing it like this:
int w = ((EditText)getActivity().findViewById(R.id.editText1)).getLayoutParams().width;
You can get ANY view by using the code below
view.rootView.findViewById(R.id.*name_of_the_view*)
EDIT: This works on Kotlin. In Java, you may need to do something like this=
this.getCurrentFocus().getRootView().findViewById(R.id.*name_of_the_view*);
I learned getCurrentFocus() function from: #JFreeman 's answer
The RelativeLayout (i.e. the ViewParent) should have a resource Id defined in the layout file (for example, android:id=#+id/myParentViewId). If you don't do that, the call to getId will return null. Look at this answer for more info.
Check my answer here
The use of Layout Inspector tool can be very convenient when you have a complex view or you are using a third party library where you can't add an id to a view
parent as View
fun View.printParentSize() {
log("parentWidth = ${(parent as View).sizeW}")
log("parentHeight = ${(parent as View).sizeH}")
}
This also works:
this.getCurrentFocus()
It gets the view so I can use it.

Dynamically add edit text to a layout

I am trying to add an edit text view dynamically to a layout from the java class. However from what I have implemented nothing changes on my action. This is what I have:
public final void onCreate(final Bundle i){
int value = 0;
isEdit = false;
try
{
super.onCreate(i);
this.setContentView(R.layout.contactedit);
ContactList.soundIndicator = 0;
etPhoneNumber = new ArrayList<EditText>();
this.etPhoneNumber.add((EditText) this.findViewById(R.id.etPhoneNumberInput));
this.addNumberBtn = (Button) this.findViewById(R.id.addNumberEditText);
addNumberBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//etPhoneNumber.add(new EditText(ContactEdit.this));
try{
LinearLayout layout = (LinearLayout) findViewById(R.id.contacteditll);
EditText temp = new EditText(ContactEdit.this);
temp.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
etPhoneNumber.add(temp);
layout.addView(temp);
}catch(Exception e){
Log.d(TAG, "Failed to create new edit text");
}
}
});
}
This is the 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="wrap_content"
android:baselineAligned="true"
android:orientation="vertical"
android:id="#+id/contacteditll"
android:background="#drawable/darkimg">
<TextView
android:id="#+id/titlePrint"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/titlePrompt"
/>
<Spinner
android:id="#+id/spContactTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/titlePrompt"
/>
<TextView
android:text="#string/namePrint"
android:id="#+id/namePrint"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText android:layout_width="match_parent"
android:hint="#string/returnName"
android:id="#+id/etFirstNameInput"
android:layout_height="wrap_content"
android:layout_toRightOf= "#+id/namePrint">
</EditText>
<TextView
android:text="#string/secondPrint"
android:id="#+id/secondPrint"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText android:layout_width="match_parent"
android:hint="#string/returnName"
android:id="#+id/etSecondNameInput"
android:layout_height="wrap_content"
android:layout_toRightOf= "#+id/namePrint">
</EditText>
<TextView android:id="#+id/numberPrint"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/numberPrint">
</TextView>
<EditText android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/etPhoneNumberInput"
android:hint="#string/returnNumber">
</EditText>
<Button android:id="#+id/addNumberEditText"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:freezesText="true"
android:editable="false"
android:text="ADD"
/>
<include
android:id="#+id/customedittext"
layout="#layout/myedittext"/>
<TextView android:id="#+id/emailPrint"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/emailPrint">
</TextView>
<EditText android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/etEmailAddressInput"
android:hint="#string/returnEmail">
</EditText>
<Button
android:freezesText="true"
android:editable="false"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:id="#+id/btnSaveButton"
android:layout_width="wrap_content"
android:text="#string/save"
android:layout_below="#+id/emailInput"
/>
</LinearLayout>
Whenever I click on my button the action is heard but nothing is done. The layout of the page does not change even though the new edit text is in my list of edit texts and I am adding the new edit text to the layout.
Any help would be appreciated thanks.
It sure gets added but you are not seeing it cause there is no space left. Two things to test:
Place the LinearLayout inside a ScrollView.
Use the weight parameter.
Using the weight parameter:
EditText temp = new EditText(ContactEdit.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT, 1);
temp.setLayoutParams(params);

Categories

Resources