
Environment : Android Studio 2.2.1, API 19
In the case of Android, remove view from layout and set view to layout again.
Then index is set 0 and view move to back. Index is not set and view move to front.
Example
package net.studioks.sample1;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import static android.support.v7.widget.ListPopupWindow.WRAP_CONTENT;
public class sample1 extends Activity implements View.OnClickListener {
private TextView textView1;
private TextView textView2;
private Button button1;
private Button button2;
RelativeLayout relativeLayout;
@Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
relativeLayout = new RelativeLayout(this);
setContentView(relativeLayout);
textView1 = new TextView(this);
textView1.setText("Text1");
GradientDrawable gradient_textView1 = new GradientDrawable();
gradient_textView1.setColor(Color.BLUE);
textView1.setBackground(gradient_textView1);
RelativeLayout.LayoutParams param_textView1 = new RelativeLayout.LayoutParams(300, 300);
param_textView1.leftMargin = 0;
param_textView1.topMargin = 0;
param_textView1.addRule(RelativeLayout.ALIGN_TOP);
relativeLayout.addView(textView1,param_textView1);
textView2 = new TextView(this);
textView2.setText("Text2");
GradientDrawable gradient_textView2 = new GradientDrawable();
gradient_textView2.setColor(Color.YELLOW);
textView2.setBackground(gradient_textView2);
RelativeLayout.LayoutParams param_textView2 = new RelativeLayout.LayoutParams(300, 300);
param_textView2.leftMargin = 50;
param_textView2.topMargin = 50;
param_textView2.addRule(RelativeLayout.ALIGN_TOP);
relativeLayout.addView(textView2,param_textView2);
button1 = new Button(this);
button1.setId(1);
button1.setText("Send Label1 to Back");
button1.setGravity(Gravity.CENTER);
button1.setPadding(0,0,0,0);
button1.setTextColor(Color.WHITE);
button1.setOnClickListener(this);
GradientDrawable gradient_button1 = new GradientDrawable();
gradient_button1.setColor(Color.BLUE);
button1.setBackground(gradient_button1);
RelativeLayout.LayoutParams param_button1 = new RelativeLayout.LayoutParams(200, 50);
param_button1.leftMargin = 0;
param_button1.topMargin = 400;
param_button1.addRule(RelativeLayout.ALIGN_TOP);
relativeLayout.addView(button1,param_button1);
button2 = new Button(this);
button2.setId(2);
button2.setGravity(Gravity.CENTER);
button2.setPadding(0,0,0,0);
button2.setText("Send Label1 to Front");
button2.setTextColor(Color.WHITE);
button2.setOnClickListener(this);
GradientDrawable gradient_button2 = new GradientDrawable();
gradient_button2.setColor(Color.RED);
button2.setBackground(gradient_button2);
RelativeLayout.LayoutParams param_button2 = new RelativeLayout.LayoutParams(200, 50);
param_button2.leftMargin = 210;
param_button2.topMargin = 400;
param_button2.addRule(RelativeLayout.ALIGN_TOP);
relativeLayout.addView(button2,param_button2);
}
public void onClick(View v) {
switch (v.getId()){
case 1:
//To Back
relativeLayout.removeView(textView1);
relativeLayout.addView(textView1,0);
break;
case 2:
//To Front
relativeLayout.removeView(textView1);
relativeLayout.addView(textView1);
break;
}
}
}