Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Android Java: create a view element as a float

25.09.2018 (👁5425)
 


 

Task: At runtime, a text box above the current position of the button should appear when clicking on a button.

 

Solution:

You have to determine the position of the button with getLeft ();

Then create a relative layout and assign the position.

Then assign the Relative layout to a new View element.

 

(Button or text input)

 

public void btnTest_onClick(View view) {
   
//------------< btnTest_onClick() >-----------
    //< srcButton >
   
Integer intLeft=view.getLeft();
    Integer intTop=view.getTop();
    Integer intWidth=view.getWidth();
    Integer intHeight=view.getHeight();
   
//</ srcButton >

    //< set_Position >
   
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
   
//params.setMargins(100, 200, 0,0);
   
params.setMargins(intLeft+intWidth, intTop-150, 0,0);
    params.addRule(RelativeLayout.
ALIGN_LEFT);
    params.addRule(RelativeLayout.
ALIGN_TOP);
   
//</ set_Position >

    //< Float_Element >
    //Button btnFloat=new Button(this);
   
EditText ctlFloat=new EditText(this);
    String sPosition= String.valueOf(intLeft);
    ctlFloat.setText(
"Button Left=" + sPosition);
    ctlFloat.setBackgroundColor(Color.
YELLOW);
    ctlFloat.setLayoutParams(params);
   
//</ Float_Element >

    //< Show_Element >
   
RelativeLayout root_Panel= findViewById(R.id.base_Layout);
    root_Panel.addView(ctlFloat);
   
//</ Show_Element >
    //------------</ btnTest_onClick() >-----------
}

 

 

 

 

Activity_main.xml

Default page in Android app

 

Important: here is only the basic layout as relative layout. This means that all elements are determined by distances Top, Left in the position.

 

Activity_main.xml

Under res / layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
tools:context="com.codedocu.test03_floatedit.MainActivity">

    <
RelativeLayout
       
android:id="@+id/base_Layout"
       
android:layout_height="match_parent"
       
android:layout_width="match_parent"
       
android:background="#eeeeee"
       
>

        <
Button
           
android:id="@+id/btnTest"
           
android:layout_width="200px"
            
android:layout_height="200px"
           
android:layout_alignParentStart="true"
           
android:layout_alignParentTop="true"
           
android:layout_marginStart="43dp"
           
android:layout_marginTop="118dp"
           
android:text="Test"
            
android:onClick="btnTest_onClick"
           
/>
    </
RelativeLayout>
</
android.support.constraint.ConstraintLayout>

 

 

 

 

 

Complete java code:

package com.codedocu.test03_floatedit;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewParent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import java.util.Date;

public class MainActivity extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
    }

   
public void btnTest_onClick(View view) {
       
//------------< btnTest_onClick() >-----------
        //< srcButton >
       
Integer intLeft=view.getLeft();
        Integer intTop=view.getTop();
        Integer intWidth=view.getWidth();
        Integer intHeight=view.getHeight();
       
//</ srcButton >

        //< set_Position >
       
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        
//params.setMargins(100, 200, 0,0);
       
params.setMargins(intLeft+intWidth, intTop-150, 0,0);
        params.addRule(RelativeLayout.
ALIGN_LEFT);
        params.addRule(RelativeLayout.
ALIGN_TOP);
       
//</ set_Position >

        //< Float_Element >
        //Button btnFloat=new Button(this);
       
EditText ctlFloat=new EditText(this);
        String sPosition= String.valueOf(intLeft);
        ctlFloat.setText(
"Button Left=" + sPosition + " Overlay");
        ctlFloat.setBackgroundColor(Color.
YELLOW);
        ctlFloat.setLayoutParams(params);
       
//</ Float_Element >

        //< Show_Element >
       
RelativeLayout root_Panel= findViewById(R.id.base_Layout);
        root_Panel.addView(ctlFloat);
       
//</ Show_Element >
        //------------</ btnTest_onClick() >-----------
   
}

}



//*wrong nomatch
//btnFloat.setLeft(500);
//btnFloat.setTop(500);
//btnFloat.setPadding(300,300,0,0);
//btnFloat.layout(300,300,220,220);
//btnFloat.setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
//btnFloat.setGravity(Gravity.LEFT);

//String sPosition= String.valueOf(btnFloat.getLeft());
//String sPosition= String.valueOf(btnFloat.getLayoutParams().width);

//Integer intLeft=view.getPaddingLeft();
//Integer intTop=view.getPaddingTop();