Alignment of dynamic views - android

I want the alignment of the editext and the imageviewin the same horizontal line but I am getting the imageview below the edittext.
main fragment code
final LinearLayout lnrView = (LinearLayout)
child.findViewById(R.id.lnrView);
Button btnad = (Button) child.findViewById(R.id.btnad);
btnad.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
lnrView.addView(newedittext());
lnrView.addView(newImageview(getActivity()));
}
});
neweditext method
private View newedittext() {
final ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final EditText editText = new EditText(getActivity());
int id = 0;
editText.setId(id);
editText.setTextSize(14);
editText.setTextColor(Color.rgb(0, 0, 0));
editText.setMaxEms(2);
editText.setInputType(InputType.TYPE_CLASS_TEXT);
return editText;
}
newImageview method
public ImageView newImageview(Context context) {
final ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final ImageView imgView = new ImageView(context);
int id = 0;
imgView.setId(id);
imgView.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_close_red));
return imgView;
}
main fragment 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="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/btnad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add" />
<LinearLayout
android:id="#+id/lnrView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
when i click the Add button an edittext and Imageview is created dynamically and my output screen look like below
i want the imageview and the edittext in the same line. can anyone help me.
when i add "Horizontal" this is the output
this my expected result

You need add first a Horizontal Linearlayout in Your lnrView than add EditText and Imageview
Try this
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LinearLayout layout = new LinearLayout(MainActivity.this);
layout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(lp);
layout.addView(newedittext());
layout.addView(newImageview(MainActivity.this));
lnrView.addView(layout);
}
});

Or simpler use
ListView
with
ArrayAdapter
where you define you own layout (EditText and button)
There is better way to manage add and remove lines.
Also you have all EditTexts in one array and you can do your logic simpler.

Related

Layout attributes in XML code those are ignored when using addView method

I'm korean, The image under is used korean language. Well.. This fact isn't important. I tried to add the custom view, In Linear layout, And It's be in linear layout successfully. But It's not on center horizontal. I did surely set the gravity of linearlayout center horizontal. But It isn't work. I guess cause of problem is layout params. Custom view class haven't problem, And not appeared any error on debugging. How do I do??
Activity in XML code:
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="NestedScrolling">
<LinearLayout
android:id="#+id/select_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:gravity="center_horizontal"
android:orientation="vertical">
<!--Here is-->
</LinearLayout>
</ScrollView>
AddView method:
ThemeManager mTheme;
Button selectTopBoardButton;
LinearLayout selectBody;
ThemeView c0001;
ThemeView c0002;
ThemeView c0003;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select);
gestureDetectorCompat = new GestureDetectorCompat(this, new MainActivityGoer());
NetworkDetector networkDetector = new NetworkDetector(this);
if (!networkDetector.isConnected()) {
Toast.makeText(SelectActivity.this, "네트워크에 접속할 수 없습니다.", Toast.LENGTH_SHORT).show();
}
selectTopBoardButton = (Button) findViewById(R.id.select_top_board_button);
selectBody = (LinearLayout) findViewById(R.id.select_body);
mTheme = new ThemeManager(this);
c0001 = mTheme.getThemeView("c0001");
c0002 = mTheme.getThemeView("c0002");
c0003 = mTheme.getThemeView("c0003");
selectTopBoardButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String url = getResources().getString(R.string.main_request_btn06_url);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
});
selectBody.addView(c0001);
}
i think your subview layout_width="match_parent" please try this code
c0001.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
selectBody.addView(c0001);
Try to set the gravity for custom views like this
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
c0001.setLayoutParams(params); //your custom ThemeView

Add a child view to a parent layout with the same bounds

In a RelativeLayout I have 2 buttons: button and button2. These 3 itself lie inside the root view which is also a RelativeLayout.
What I am trying to accomplish here is that when button is clicked button2 should be removed from the inner RelativeLayout(its id is otherLayout) and get added to the root View which is rootView but with the same bounds as it was in the inner layout.
Here is my XML and Java code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:id="#+id/rootView"
tools:context="com.test.testproject.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/otherLayout"
>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="53dp"
android:layout_marginTop="94dp"
android:text="One" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button"
android:layout_marginStart="76dp"
android:layout_toEndOf="#+id/button"
android:text="Two" />
</RelativeLayout>
</RelativeLayout>
Java Code
public class MainActivity extends AppCompatActivity {
Button button,button2;
int mLeft,mRight,mTop,mBottom;
RelativeLayout otherLayout;
RelativeLayout rootView;
ViewGroup childParent;
int[] mBounds = new int[2];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
// rootView = (RelativeLayout) findViewById(R.id.rootView);
rootView = (RelativeLayout) findViewById(R.id.rootView);
otherLayout = (RelativeLayout) findViewById(R.id.otherLayout);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Rect r = new Rect();
button2.getLocalVisibleRect(r);
// button2.getDrawingRect(r);
// ((ViewGroup)button2.getParent()).offsetDescendantRectToMyCoords(button2, r);
mLeft = r.left;
mRight = r.right;
mBottom = r.bottom;
mTop = r.top;
childParent = (ViewGroup) button2.getParent();
((ViewGroup)button2.getParent()).removeView(button2);
Handler mHandler = new Handler(Looper.getMainLooper());
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
button2.setTop(mTop);
button2.setLeft(mLeft);
button2.setBottom(mBottom);
button2.setRight(mRight);
/*
button2.setX(mBounds[1]);
button2.setY(mBounds[0]);*/
((ViewGroup)rootView).addView(button2);
}
},1000);
}
});
}
}
I tried a different methods but would work. I used getLocalVisibleRect(), getDrawingRect() and all (You can see in the commented code).
So how do I achieve this? Remember, that the requirement is that the View residing in the inner layout and after adding to root should have the same bounds and params and the position on screen also must not change.
I think that your issue may be with the layout parameters of button2. Probably the easiest way to approach this is to capture the position of button2 in the layout and set up new layout parameters. mLeft and mTop to be the left and top margins.
A simplified onCreate method is below, but I am curious about why this would be useful.
Also take a look at these caveats. The document for View specifies why setTop, etc. should not be called outside the layout system. (See here.)
setTop
void setTop (int top)
Sets the top position of this view relative to its parent. This method is meant to be called by the layout system and should not generally be called otherwise, because the property may be changed at any time by the layout.
#Override
protected void onCreate(Bundle savedInstanceState) {
Button button;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
rootView = (RelativeLayout) findViewById(R.id.rootView);
otherLayout = (RelativeLayout) findViewById(R.id.otherLayout);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout.LayoutParams params;
int top = button2.getTop();
int left = button2.getLeft();
((ViewGroup) button2.getParent()).removeView(button2);
params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, 0, 0);
button2.setLayoutParams(params);
rootView.addView(button2);
}
});
}

Add layout in method android

I would like to know how can I create method to add linearlayout to other layout. I want to create method which when I use this method in activity, this method add linear layout in top of my activity layout. How can I do that?
Edit:
I do something like that:
public class SecondActivity extends Activity {
LinearLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
layout = (LinearLayout) findViewById(R.id.lay);
Button next = (Button) findViewById(R.id.nextActivity);
layout.addView(addNewLinearLayout(getApplicationContext()));
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
startActivity(intent);
}
});
}
private View addNewLinearLayout(Context context) {
LinearLayout linearLayout = new LinearLayout(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(params);
linearLayout.setBackgroundColor(getResources().getColor(R.color.black));
// Do something else here on your linear layout or to customize your linear layout
return linearLayout;
}
}
and this is xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/nextActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_marginTop="100dp"
/>
</LinearLayout>
but my layout is not changing his color. Why?
Try something like this :
private View _addNewLinearLayout(context) {
LinearLayout linearLayout = new LinearLayout(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(params);
// Do something else here on your linear layout or to customize your linear layout
return linearLayout;
}
In the main you could call this like :
getView().addView(_addNewLinearLayout(context));
You can easily create a new LinearLayout like this:
LinearLayout linLayout = new LinearLayout(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
And then set it with:
setContentView(linLayout, layoutParams);
You use this sample method. You can add more parameters if you want.
public LinearLayout addLinearLayout(Context context) {
LinearLayout newLayout = new LinearLayout(context);
//Add stuffs here, like LayoutParams
return newLayout;
}
yourLinearLayoutName.addView(addLinearLayout(yourClassName.this));
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View detailView = inflater.inflate(R.layout.yourNewLayout, l_details, false);
where l_details is the instance of the Linear layout in which you want add another linear layout.

I need help to implement xml

I found online the working code of a menu slider.
In this code, however, the layout is generated in the code, could someone help me to configure it in a layout file in *. Xml?
Sorry for my bad english.
code:`
private boolean Menu_Displayed=false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Display display = getWindowManager().getDefaultDisplay();
final int width = display.getWidth();
// menu:
LinearLayout li_menu = new LinearLayout(this);
li_menu.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
li_menu.setOrientation(1);//1 is vertical
li_menu.setBackgroundColor(Color.GREEN);
Button btn1 = new Button(this);
btn1.setText("button 1");
btn1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
li_menu.addView(btn1);
//body:
final HorizontalScrollView hsv = new HorizontalScrollView(this){
#Override
// do not let hsv consume the click itself. Then the view under the hsv will also consume the click
//so that the menu will be clicked
//when menu is not showed up, let hsv be the only view to consume the click.
//so that the menu will not be clicked
public boolean onTouchEvent(MotionEvent ev) {
if(Menu_Displayed){
return false;
}
else{
return true;
}
}
};
hsv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
hsv.setBackgroundColor(Color.TRANSPARENT);
hsv.setHorizontalFadingEdgeEnabled(false);
hsv.setVerticalFadingEdgeEnabled(false);
final LinearLayout li_body = new LinearLayout(this);
li_body.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
li_body.setOrientation(0);//0 is horizantal
li_body.setBackgroundColor(Color.TRANSPARENT);
hsv.addView(li_body);
//body: place holder transparent
TextView placeholder = new TextView(this);
placeholder.setTextColor(Color.TRANSPARENT);
placeholder.setLayoutParams(new LayoutParams(width-100, LayoutParams.FILL_PARENT));
placeholder.setVisibility(View.INVISIBLE);
li_body.addView(placeholder);
//body: real content
LinearLayout li_content = new LinearLayout(this);
li_content.setLayoutParams(new LayoutParams(width, LayoutParams.FILL_PARENT));
li_content.setOrientation(1);//1 is vertical
li_content.setBackgroundColor(Color.CYAN);
TextView tv1 = new TextView(this);
tv1.setText("txt 1");
tv1.setTextSize(40);
tv1.setTextColor(Color.BLACK);
TextView tv2 = new TextView(this);
tv2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tv2.setTextSize(50);
tv2.setText("txt 2");
tv2.setTextColor(Color.WHITE);
//use this button to scroll
Button btn_showMenu = new Button(this);
btn_showMenu.setText("Menu");
btn_showMenu.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btn_showMenu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
hsv.post(new Runnable() {
#Override
public void run() {
if(Menu_Displayed){
hsv.smoothScrollTo(width-100, 0);
}
else{
hsv.smoothScrollTo(0, 0);
}
Menu_Displayed = !Menu_Displayed;
}
});
}
});
li_content.addView(tv1);
li_content.addView(tv2);
li_content.addView(btn_showMenu);
li_body.addView(li_content);
//add menu and body in to frame
FrameLayout mainFrame = new FrameLayout(this);
mainFrame.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
mainFrame.addView(li_menu);
mainFrame.addView(hsv);
//scroll to the body real content to block the menu
hsv.post(new Runnable() {
#Override
public void run() {
hsv.scrollBy(width-100, 0);
}
});
setContentView(mainFrame);
}
`
This is not a good implementation of a SlidingMenu, you can try this open sources project:SideMenu
To implements the menu swap on you example, is calculating the screen width-100, width cannot be converted to xml, the base view will be like this file.
<?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="match_parent"
android:background="#ff669900"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:fadingEdge="none" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#android:color/transparent"
android:orientation="horizontal" >
<TextView
android:id="#+id/placeholder"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingRight="100dp" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>

Android: dynamically set the position of button into center of screen in RelativeLayout

There has been many questions on dynamically setting the position of a button, but I just want to change the position of a button into center of the screen. My layout is RelativeLayout.
Updated:
At the beginning, myButton has been set position in XML as:
<Button
android:id="#+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Menu" />
Any suggestion would be greatly appreciated
See an example below (click the button and it will be moved to the center).
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/my_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Menu" />
</RelativeLayout>
MainActivity.java:
public class MainActivity extends Activity {
private View mView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mView = findViewById(R.id.my_view);
mView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ViewGroup.LayoutParams vg_lp = mView.getLayoutParams();
// Make sure we are in RelativeLayout
if (vg_lp instanceof RelativeLayout.LayoutParams) {
RelativeLayout.LayoutParams rl_lp = new RelativeLayout.LayoutParams(vg_lp);
rl_lp.addRule(RelativeLayout.CENTER_IN_PARENT);
mView.setLayoutParams(rl_lp);
}
}
});
}
}
Try:
......
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) button.getLayoutParams();
layoutParams.leftMargin = parentLayout.getWidth()/2;
layoutParams.topMargin = parentLayout.getHeight()/2;
button.setLayoutParams(layoutParams);
......
Try this one:
RelativeLayout RL = (RelativeLayout)findViewById(R.id.relative_layout);
RL.gravity(Gravity.CENTER);
with this all subviews of RL will be in center;
Button anyButton = new Button(this);
RL.addSubview(anyButton);//this genereted button will be in center as well
Try this,
RelativeLayout my_layout = new RelativeLayout(this);
my_layout.setId(some_value);
Button my_btn = new Button(this);
RelativeLayout.LayoutParams my_params = new RelativeLayout.LayoutParams(btn_height,btn_width);
my_params.addRule(RelativeLayout.CENTER_IN_PARENT,my_layout.getId());
my_layout.addView(my_btn,my_params);
Add this 2 lines to the component you want to center in the xml file :
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"

Categories

Resources