this is the floagingview.It can drag...
I need to set the layout to hide or show in the code.
problem : listview_left、listview_left no way to hide or show
FloatingView.java
This code has hidden settings but has no effect.
listview_left.setVisibility(View.GONE) and listview_right.setVisibility(View.GONE)
public FloatingView(Context context, FloatingViewConfig config) {
this.mContext = context;
mWindowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);
rootView = LayoutInflater.from(context).inflate(R.layout.playground_float_view, null, false);
this.config = config;
if (config.displayWidth == Integer.MAX_VALUE) {
DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
config.displayWidth = metrics.widthPixels;
}
if (config.displayHeight == Integer.MAX_VALUE) {
DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
config.displayHeight = (int) (metrics.heightPixels - 25 * metrics.density);
}
config.paddingLeft = dp2px(config.paddingLeft);
config.paddingTop = dp2px(config.paddingTop);
config.paddingRight = dp2px(config.paddingRight);
config.paddingBottom = dp2px(config.paddingBottom);
rootView.measure(0, 0);
width = rootView.getMeasuredWidth();
height = rootView.getMeasuredHeight();
screen_widht = mWindowManager.getDefaultDisplay().getWidth();
screen_height = mWindowManager.getDefaultDisplay().getHeight();
mFloatLayout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.playground_float_view, null);
listview_left = mFloatLayout.findViewById(R.id.listview_left);
listview_right = mFloatLayout.findViewById(R.id.listview_right);
x = 0;
y = 0;
listview_left.setVisibility(View.GONE);
listview_right.setVisibility(View.GONE);
Log.d(TAG,"初始化(left,right) : " + listview_left.getVisibility() + ", " + listview_right.getVisibility());
}
float_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/float_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/floating_layout_shape"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/listview_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/floating_view_list2" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/floating_view_list3" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/floating_view_list1" />
</LinearLayout>
<ImageView
android:id="#+id/float_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/floating_view_menu"/>
<LinearLayout
android:id="#+id/listview_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/float_image"
android:visibility="visible">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/floating_view_list1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/floating_view_list3" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/floating_view_list2" />
</LinearLayout>
</LinearLayout>
This is the complete code and it is run!!!
link: https://github.com/sheng855174/Test
How can i solve?
The answer here is good, but maybe it was misinterpreted. I will try to explain it differently here https://stackoverflow.com/a/60161868/910718
mFloatLayout should be completely replaceable with rootView, inflating the layout twice means you have 2 completely separate versions of that layout, one in mFloatLayout and another in rootView. Changes to mFloatLayout would not change anything to the Views in rootView.
I see that the view is added here:
public void showOverlayActivity() {
if (isShowing) {
return;
}
type = TYPE.OVERLAY_ACTIVITY;
initParams();
initPosition();
initWindowView();
isShowing = true;
mWindowManager.addView(rootView, mParamsWindowManager);
}
So the View that is rendered on the screen is not the View where you have extracted the listview_left and listview_right from, because as stated, the second inflate is creating a completely new instance of the layout with no association with the layout that is being added to the screen in this code.
I would encourage that FloatingView extend LinearLayout, that the outer LinearLayout is replaced with a merge tag, and that View.inflate like this: inflate(context, R.layout.playground_float_view, this). This makes the FloatingView equal the float_view layout and the mFloatLayout and rootView become unnecessary.
you don't need to inflate playground_float_view 2 times.
just remove below line from your code -
mFloatLayout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.playground_float_view, null);
and get listview_left and listview_left like below -
listview_left = (LinearLayout)rootView.findViewById(R.id.listview_left);
listview_right = (LinearLayout)rootView.findViewById(R.id.listview_right);
hope this will help!!
Related
I want to set width and height of each item programmatically (textview + recyleview) so that I can place three ones no matter which device renders it (when devices differ I got different width and height because of density metrics). My code for doing this issue is as following:
public class ItemRowHolder extends RecyclerView.ViewHolder {
protected TextView playlistTitle;
protected Button playlistMoreButton;
protected RecyclerView playlist_recycler_view_list;
public ItemRowHolder(View view) {
super(view);
this.playlistTitle = (TextView) view.findViewById(R.id.playlist_title);
this.playlist_recycler_view_list = (RecyclerView) view.findViewById(R.id.playlist_recycler_view_list);
this.playlistTitle.measure(0,0);
DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
int screenHeight = (int )displayMetrics.heightPixels;
int titleHeight = this.playlistTitle.getMeasuredHeight();
int rcvHeight = this.playlist_recycler_view_list.getLayoutParams().height;
int layoutHeight = 0;
if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
p.setMargins(5, 20, 5, 20);
view.requestLayout();
}
layoutHeight = (int)(screenHeight - displayMetrics.density* titleHeight-displayMetrics.density*120)/3;
Log.d("MOMOPix","ydpi: "+ displayMetrics.density);
Log.d("MOMOPix","Screen height: "+ screenHeight);
Log.d("MOMOPix","RCV height: "+ rcvHeight);
Log.d("MOMOPix","Title height: "+titleHeight);
Log.d("MOMOPix","Layout height: "+layoutHeight);
this.playlist_recycler_view_list.getLayoutParams().height= layoutHeight;
}
}
layout is as following:
<?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:background="?android:selectableItemBackground"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/playlist_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:text="Sample title"
android:textColor="#android:color/white"
android:textSize="18dp" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/playlist_recycler_view_list"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center_vertical"
android:orientation="horizontal" />
</LinearLayout>
for some devices I get desired results and I want for all devices
Thanks in advances.
You're doing it in very weird and error prone way.
If you want to alter how items are laid out, you should override RecyclerVIew.LayoutManager (which is specifically made to lay out the views as its name suggests) instead of performing weird measurement hacks in your ViewHolder.
To have RecyclerView fit three items vertically (I assume it's LinearLayoutManager, your code sample does not mention it) we only have to override one method in LayoutManager:
recyclerView.setLayoutManager(new LinearLayoutManager(context){
#Override
public boolean checkLayoutParams(RecyclerView.LayoutParams lp) {
// force height of viewHolder to be a third of RecyclerView
// this will override layout_height from xml
lp.height = getHeight() /3;
return true;
}
});
I have 19 horizontal linearlayouts that are arranged vertically within another linearlayout. They are nearly empty and onclick show text. I am animating the layouts with
parentOne.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
For some reason the first 7 layouts seem to be opening onClick a little slower than the 8th. Only by a split second but I have asked others to look at it when I open it to make sure I'm not crazy. I would post the entire file but it is HUGE and is over the maximum allowable text on this. So here is the meat and potatoes. So, why are my layouts opening slower than others? Numbers 8-19 open instantly onClick and 1-7 have a small delay that is noticeable.
Java On Create
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.tacticalfieldcarestudy);
LinearLayout parentOne = (LinearLayout) findViewById(R.id.TFCLayoutOne);
LinearLayout parentTwo = (LinearLayout) findViewById(R.id.TFCLayoutTwo);
LinearLayout parentTwoBullets = (LinearLayout)findViewById(R.id.TFCLayoutTwoBullets);
LinearLayout parentThree = (LinearLayout)findViewById(R.id.TFCLayoutThree);
LinearLayout parentThreeBullets = (LinearLayout)findViewById(R.id.TFCLayoutThreeBullets);
LinearLayout parentFour = (LinearLayout)findViewById(R.id.TFCLayoutFour);
LinearLayout parentFourBullets = (LinearLayout)findViewById(R.id.TFCLayoutFourBullets);
LinearLayout parentFive = (LinearLayout)findViewById(R.id.TFCLayoutFive);
LinearLayout parentFiveBullets = (LinearLayout)findViewById(R.id.TFCLayoutFiveBullets);
LinearLayout parentSix = (LinearLayout)findViewById(R.id.TFCLayoutSix);
LinearLayout parentSixBullets = (LinearLayout)findViewById(R.id.TFCLayoutSixBullets);
LinearLayout parentSeven = (LinearLayout)findViewById(R.id.TFCLayoutSeven);
LinearLayout parentSevenBullets = (LinearLayout)findViewById(R.id.TFCLayoutSevenBullets);
LinearLayout parentEight = (LinearLayout)findViewById(R.id.TFCLayoutEight);
LinearLayout parentEightBullets = (LinearLayout)findViewById(R.id.TFCLayoutEightBullets);
LinearLayout parentNine = (LinearLayout)findViewById(R.id.TFCLayoutNine);
LinearLayout parentNineBullets = (LinearLayout)findViewById(R.id.TFCLayoutNineBullets);
LinearLayout parentTen = (LinearLayout)findViewById(R.id.TFCLayoutTen);
LinearLayout parentTenBullets = (LinearLayout)findViewById(R.id.TFCLayoutTenBullets);
LinearLayout parentEleven = (LinearLayout)findViewById(R.id.TFCLayoutEleven);
LinearLayout parentTwelve = (LinearLayout)findViewById(R.id.TFCLayoutTwelve);
LinearLayout parentThirteen = (LinearLayout)findViewById(R.id.TFCLayoutThirteen);
LinearLayout parentThirteenBullets = (LinearLayout)findViewById(R.id.TFCLayoutThirteenBullets);
LinearLayout parentFourteen = (LinearLayout)findViewById(R.id.TFCLayoutFourteen);
LinearLayout parentFifteen = (LinearLayout)findViewById(R.id.TFCLayoutFifteen);
LinearLayout parentFifteenBullets = (LinearLayout)findViewById(R.id.TFCLayoutFifteenBullets);
LinearLayout parentSixteen = (LinearLayout)findViewById(R.id.TFCLayoutSixteen);
LinearLayout parentSixteenBullets = (LinearLayout)findViewById(R.id.TFCLayoutSixteenBullets);
LinearLayout parentSeventeen = (LinearLayout)findViewById(R.id.TFCLayoutSeventeen);
LinearLayout parentSeventeenBullets = (LinearLayout)findViewById(R.id.TFCLayoutSeventeenBullets);
LinearLayout parentEighteen = (LinearLayout)findViewById(R.id.TFCLayoutEighteen);
LinearLayout parentEighteenBullets = (LinearLayout)findViewById(R.id.TFCLayoutEighteenBullets);
LinearLayout parentNineteen = (LinearLayout)findViewById(R.id.TFCLayoutNineteen);
LinearLayout parentNineteenBullets = (LinearLayout)findViewById(R.id.TFCLayoutNineteenBullets);
parentOne.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentTwo.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentTwoBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentThree.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentThreeBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFour.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFourBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFive.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFiveBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSix.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSixBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSeven.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSevenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentEight.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentEightBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentNine.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentNineBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentTen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentTenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentEleven.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentTwelve.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentThirteen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentThirteenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFourteen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFifteen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFifteenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSixteen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSixteenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSeventeen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSeventeenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentEighteen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentEighteenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentNineteen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentNineteenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
}
One of 7 Java methods that has unwanted delay onClick
private boolean mOpenOne = false;
public void toggleOne(View view){
TextView stepOne = (TextView)findViewById(R.id.stepOne);
ImageView toggle = (ImageView)findViewById(R.id.toggleStepOne);
if(mOpenOne){
stepOne.setText("Step 1:");
toggle.setImageResource(R.drawable.open);
}
else{
stepOne.setText("1. Casualties with an altered mental status should be disarmed immediately.");
toggle.setImageResource(R.drawable.close);
}
mOpenOne = !mOpenOne;
}
XML with unwanted delay onClick
<LinearLayout
android:id="#+id/TFCLayoutOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
android:animateLayoutChanges="true"
android:onClick="toggleOne"
android:background="#606060"
>
<ImageView
android:id="#+id/toggleStepOne"
android:src="#drawable/open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_gravity="center_vertical"
/>
<TextView
android:id="#+id/stepOne"
android:text="Step 1:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="0dp"
style="#style/Bullet"
/>
</LinearLayout>
Java for 8th method that works without delay
private boolean mOpenEight = false;
public void toggleEight(View view){
TextView stepEight = (TextView)findViewById(R.id.stepEight);
ImageView toggle = (ImageView)findViewById(R.id.toggleStepEight);
if(mOpenEight){
stepEight.setText("Step 8:");
TextView stepEightBulletOne = (TextView)findViewById(R.id.stepEightBulletOne);
TextView stepEightBulletTwo = (TextView)findViewById(R.id.stepEightBulletTwo);
TextView stepEightBulletThree = (TextView)findViewById(R.id.stepEightBulletThree);
TextView stepEightBulletFour = (TextView)findViewById(R.id.stepEightBulletFour);
TextView stepEightBulletFive = (TextView)findViewById(R.id.stepEightBulletFive);
TextView stepEightBulletSix = (TextView)findViewById(R.id.stepEightBulletSix);
stepEightBulletOne.setText("");
stepEightBulletTwo.setText("");
stepEightBulletThree.setText("");
stepEightBulletFour.setText("");
stepEightBulletFive.setText("");
stepEightBulletSix.setText("");
stepEightBulletOne.getLayoutParams().height = 0;
stepEightBulletTwo.getLayoutParams().height = 0;
stepEightBulletThree.getLayoutParams().height = 0;
stepEightBulletFour.getLayoutParams().height = 0;
stepEightBulletFive.getLayoutParams().height = 0;
stepEightBulletSix.getLayoutParams().height = 0;
toggle.setImageResource(R.drawable.open);
}
else{
stepEight.setText("8. Prevention of hypothermia");
TextView stepEightBulletOne = (TextView)findViewById(R.id.stepEightBulletOne);
TextView stepEightBulletTwo = (TextView)findViewById(R.id.stepEightBulletTwo);
TextView stepEightBulletThree = (TextView)findViewById(R.id.stepEightBulletThree);
TextView stepEightBulletFour = (TextView)findViewById(R.id.stepEightBulletFour);
TextView stepEightBulletFive = (TextView)findViewById(R.id.stepEightBulletFive);
TextView stepEightBulletSix = (TextView)findViewById(R.id.stepEightBulletSix);
stepEightBulletOne.setText("a. Minimize casualty's exposure to the elements. Keep protective gear on or with the casualty if feasible.");
stepEightBulletTwo.setText("b. Replace wet clothing with dry if possible. Get the casualty onto an insulated surface as soon as possible.");
stepEightBulletThree.setText("c. Apply the Ready-Heat Blanket from the Hypothermia Prevention and Management Kit (HPMK) to the casualty's torso (not directly on the skin) and cover the casualty with the Heat-Reflective Shell (HRS).");
stepEightBulletFour.setText("d. If an HRS is not available, the previously recommended combination of the Blizzard Survival Blanket and the Ready Heat blanket may also be used.");
stepEightBulletFive.setText("e. If the items mentioned above are not available, use dry blankets, poncho liners, sleeping bags, or anything that will retain heat and keep the casualty dry.");
stepEightBulletSix.setText("f. Warm fluids are preferred if IV fluids are required.");
stepEightBulletOne.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
stepEightBulletTwo.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
stepEightBulletThree.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
stepEightBulletFour.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
stepEightBulletFive.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
stepEightBulletSix.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
toggle.setImageResource(R.drawable.close);
}
mOpenEight = !mOpenEight;
}
XML of one of the layouts that works without a delay
<LinearLayout
android:id="#+id/TFCLayoutEightBullets"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#A0A0A0"
android:animateLayoutChanges="true"
android:onClick="toggleEight"
>
<LinearLayout
android:id="#+id/TFCLayoutEight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
android:animateLayoutChanges="true"
android:background="#A0A0A0"
>
<ImageView
android:id="#+id/toggleStepEight"
android:src="#drawable/open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_gravity="center_vertical"
/>
<TextView
android:id="#+id/stepEight"
android:text="Step 8:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="0dp"
style="#style/Bullet"
/>
</LinearLayout>
<TextView
android:id="#+id/stepEightBulletOne"
android:layout_width="wrap_content"
android:layout_height="0dp"
style="#style/BulletIndented"
/>
<TextView
android:id="#+id/stepEightBulletTwo"
android:layout_width="wrap_content"
android:layout_height="0dp"
style="#style/BulletIndented"
/>
<TextView
android:id="#+id/stepEightBulletThree"
android:layout_width="wrap_content"
android:layout_height="0dp"
style="#style/BulletIndented"
/>
<TextView
android:id="#+id/stepEightBulletFour"
android:layout_width="wrap_content"
android:layout_height="0dp"
style="#style/BulletIndented"
/>
<TextView
android:id="#+id/stepEightBulletFive"
android:layout_width="wrap_content"
android:layout_height="0dp"
style="#style/BulletIndented"
/>
<TextView
android:id="#+id/stepEightBulletSix"
android:layout_width="wrap_content"
android:layout_height="0dp"
style="#style/BulletIndented"
/>
</LinearLayout>
Well I figured it out. It looks as though it was a processing issue. If I had to guess I would say that each time I called a method it would search through the giant list of linearlayouts in the onCreate method for the animation to open it.
In my onCreate method I define a CRAP TON of linear layouts as such
LinearLayout parentOne = (LinearLayout) findViewById(R.id.TFCLayoutOne);
parentOne.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
The Fix:
I separated ALL of them and placed them all in their respective onClick methods. Now, when I click them they actually open faster than my other pages that are setup similarly, so I will be changing those as well. My java methods were:
private boolean mOpenOne = false;
public void toggleOne(View view){
TextView stepOne = (TextView)findViewById(R.id.stepOne);
ImageView toggle = (ImageView)findViewById(R.id.toggleStepOne);
if(mOpenOne){
stepOne.setText("Step 1:");
toggle.setImageResource(R.drawable.open);
}
else{
stepOne.setText("1. Casualties with an altered mental status should be disarmed immediately.");
toggle.setImageResource(R.drawable.close);
}
mOpenOne = !mOpenOne;
}
I have changed them to:
private boolean mOpenOne = false;
public void toggleOne(View view){
TextView stepOne = (TextView)findViewById(R.id.stepOne);
ImageView toggle = (ImageView)findViewById(R.id.toggleStepOne);
LinearLayout parentOne = (LinearLayout) findViewById(R.id.TFCLayoutOne);
parentOne.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
if(mOpenOne){
stepOne.setText("Step 1:");
toggle.setImageResource(R.drawable.open);
}
else{
stepOne.setText("1. Casualties with an altered mental status should be disarmed immediately.");
toggle.setImageResource(R.drawable.close);
}
mOpenOne = !mOpenOne;
}
Hope this helps others!
I'm trying to find the center coordinates of a RelativeLayout that for the moment, is empty. I need to find the center before I can put anything into the Relative Layout. The Relative Layout is not the whole screen, so I can't use metrics to find the screen dimensions. Since my minimum API is 10, I can't use rLayout.getX() or rLayout.getY(). So what I've tried is:
int xScreenInt = gBoard_RL.getWidth()/2;
int yScreenInt = gBoard_RL.getHeight()/2;
int xInt = gBoard_RL.getLeft() + xScreenInt;
int yInt = gBoard_RL.getTop() + yScreenInt;
and all I get is 0,0. Does anyone have an any ideas how I can get this done? Here's my layout and the Activity so far:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/gb1_root_RL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context=".GB1" >
<RelativeLayout
android:id="#+id/gb1_topInfo_RL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#drawable/blu_grade_bg"
android:orientation="vertical"
android:paddingBottom="15dp" >
<TextView
android:id="#+id/gb1_scValue_TV"
style="#style/mainScoreValueStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/gb1_score_TV"
android:layout_marginLeft="10dp" />
<TextView
android:id="#+id/gb1_timeValue_TV"
style="#style/mainTimeValueStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/gb1_time_TV"
android:layout_marginRight="10dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/gb1_gf_RL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/gb1_topInfo_RL"
android:padding="10dp" >
</RelativeLayout>
the Relative Layout I need to find the center of is: android:id="#+id/gb1_gf_RL"
public class GB1 extends Activity {
TextView GScore_TV;
RelativeLayout gBoard_RL;
int xScreenInt, yScreenInt, xInt, yInt;
String xStr, yStr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.cutin, R.anim.cutout);
setContentView(R.layout.gb_layout);
GScore_TV = (TextView) findViewById(R.id.G1_scoreValue_TV);
gBoard_RL = (RelativeLayout) findViewById(R.id.gb1_gf_RL);
gBoard_RL.setBackgroundColor(Color.MAGENTA);
xScreenInt = gBoard_RL.getWidth()/2;
yScreenInt = gBoard_RL.getHeight()/2;
xInt = gBoard_RL.getLeft() + xScreenInt;
yInt = gBoard_RL.getTop() + yScreenInt;
xStr = String.valueOf(xInt);
yStr = String.valueOf(yInt);
GScore_TV.setText(xStr + ", " + yStr);
}
}
Try putting your code in a onGlobalLayout listener:
myRelativelayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
this.layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//Your code here
}
});
EDIT:
Since, as #xorgate pointed out, the layout isnt drawn yet in the onCreate method. The onGlobalLayout method will be called when the layout is drawn and calculations such as yours should be done in there.
onCreate is called before a layout pass is done. All Views will have no size yet.
Try setting a global layout listener, and do the positioning code there.
Good evening! I'm trying to setPadding on a custom View i built and the native setPadding() did nothing so i wrote my own... After a while i realized that setPadding gets called several times after my original call and i have no idea why... Please help :) (I realize that my custom setPadding maybe quite excessive ^^)
Here is the XML containing the View. It's the PieChart.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/PieDialog_llParent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/PieDialog_tvHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Header"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/PieDialog_tvDiv1"
android:layout_width="match_parent"
android:layout_height="2dp"
android:textSize="0sp"/>
<TextView
android:id="#+id/PieDialog_tvDiv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="0sp" />
<com.SverkerSbrg.Spendo.Statistics.Piechart.PieChart
android:id="#+id/PieDialog_Pie"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/PieDialog_tvDiv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="0sp" />
<FrameLayout
android:id="#+id/PieDialog_flClose"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/PieDialog_tvClose"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Large Text" />
</FrameLayout>
</LinearLayout>
And here is the code where i use the xml:
package com.SverkerSbrg.Spendo.Transaction.TransactionList.PieDialog;
imports...
public class PieDialog extends SpendoDialog{
private TransactionSet transactionSet;
private TransactionGroup transactionGroup;
private GUI_attrs gui_attrs;
private PieData pieData;
private PieChart pie;
private TextView tvHeader;
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.transaction_list_pie_dialog, null);
LinearLayout llParent = (LinearLayout) view.findViewById(R.id.PieDialog_llParent);
llParent.setBackgroundColor(gui_attrs.color_Z0);
tvHeader = (TextView) view.findViewById(R.id.PieDialog_tvHeader);
tvHeader.setTextSize(gui_attrs.textSize_header);
TextView tvDiv1 = (TextView) view.findViewById(R.id.PieDialog_tvDiv1);
tvDiv1.setBackgroundColor(gui_attrs.color_Z2);
TextView tvDiv2 = (TextView) view.findViewById(R.id.PieDialog_tvDiv2);
tvDiv2.setPadding(0, gui_attrs.padding_Z0, 0, 0);
PieChart pie = (PieChart) view.findViewById(R.id.PieDialog_Pie);
pie.setPadding(40, 10, 40, 10);
builder.setView(view);
AlertDialog ad = builder.create();
return ad;
}
public void initialize(GUI_attrs gui_attrs, TransactionSet transactionSet, long groupIdentifier){
this.gui_attrs = gui_attrs;
this.transactionSet = transactionSet;
}
}
Just to extrapolate on my comment, it is your custom View object's responsibility to respect the padding that is set. You can do something like the following to make sure that you handle that case:
onMeasure()
int desiredWidth, desiredHeight;
desiredWidth = //Determine how much width you need
desiredWidth += getPaddingLeft() + getPaddingRight();
desiredHeight = //Determine how much height you need
desiredHeight += getPaddingTop() + getPaddingBottom();
int measuredHeight, measuredWidth;
//Check against the MeasureSpec -- if it's MeasureSpec.EXACTLY, or MeasureSpec.AT_MOST
//follow those restrictions to determine the measured dimension
setMeasuredDimension(measuredWidth, measuredHeight);
onLayout()
int leftOffset = getPaddingLeft();
int topOffset = getPaddingTop();
//layout your children (if any) according to the left and top offsets,
//rather than just 0, 0
onDraw()
canvas.translate (getPaddingLeft(), getPaddingTop());
//Now draw your stuff as normal
I have an XML file with the following attritubes:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/al_cs_layout1"
android:gravity="center_vertical">
<ImageView android:id="#+id/cs_track"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/fader_background5"
android:layout_marginLeft="0dp"
android:layout_marginTop="20dp" >
</ImageView>
...
and here is my java code:
public void initialize(Context context)
{
Log.d("initialize (MySeekBar)","initialize");
setWillNotDraw(false);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view1 = inflater.inflate(R.layout.custom_seekbar, this);
layout1 = (RelativeLayout)view1.findViewById(R.id.al_cs_layout1);
track = (ImageView)layout1.findViewById(R.id.cs_track);
thumb = (ImageView)layout1.findViewById(R.id.cs_thumb);
...
I am working on a custom seekbar which determines the top by using a variable marginTop = 20; however this was oringally made on a phone much older by a different programmer. The 20 suppose to represent the margin top defined in the XML however it is using dp and not pixels. how can I find the marginTop attribute of R.id.cs_track? It works great on the old phone but on any phone does doesn't have the same screen size of dp it will create an undesired offset.
Use MarginLayoutParams to get value in pixels:
track = (ImageView)layout1.findViewById(R.id.cs_track);
MarginLayoutParams params = (MarginLayoutParams)track.getLayoutParams();
int marginTopPixelSize = params.topMargin;