Im am trying to create a dynamic object with imageviews and textviews
First I created a XML file to see what parameters I need to have to produce what I need.
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/layout_new_event"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:background="#color/eventColor"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/linearLayoutEventText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/LinearLayoutEventIndicator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView_event_from"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5sp"
android:layout_weight=".5"
android:background="#color/backgroundColor3"
android:scaleType="centerCrop"
app:srcCompat="#drawable/arrow_up_white" />
<ImageView
android:id="#+id/imageView_event_to"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:background="#color/backgroundColor2"
android:scaleType="centerCrop"
app:srcCompat="#drawable/arrow_down_white" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Below is the java code I am using to create the above when pressing a floating button. I know that maybe there is unnecessary hierarchy in the layout, but this is part of a larger but same layout. So before I settle this I cannot proceed.
public class tabTueActivity extends Fragment {
LinearLayout layoutNewEvent, linearLayoutEventText, linearLayoutEventVoyage, linearLayoutEventIndicator, eventLayout;
ImageView imageViewEventFrom, imageViewEventTo, imageViewSearch;
TextView textViewEventFrom, textViewEventTo;
FloatingActionButton createEvent;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_tue_frag, container, false);
return rootView;
}
#Override
public void onViewCreated(View rootView, Bundle savedInstanceState) {
super.onViewCreated(rootView, savedInstanceState);
eventLayout = (LinearLayout) rootView.findViewById(R.id.event_layout);
createEvent = (FloatingActionButton) rootView.findViewById(R.id.add_event_button);
layoutNewEvent = (LinearLayout) rootView.findViewById(R.id.layout_new_event);
linearLayoutEventText = (LinearLayout) rootView.findViewById(R.id.linearLayoutEventText);
linearLayoutEventIndicator = (LinearLayout) rootView.findViewById(R.id.LinearLayoutEventIndicator);
imageViewEventFrom = (ImageView) rootView.findViewById(R.id.imageView_event_from);
imageViewEventTo = (ImageView) rootView.findViewById(R.id.imageView_event_to);
linearLayoutEventVoyage = (LinearLayout) rootView.findViewById(R.id.LinearLayoutEventVoyage);
textViewEventFrom = (TextView) rootView.findViewById(R.id.textView_event_from);
textViewEventTo = (TextView) rootView.findViewById(R.id.textView_event_to);
imageViewSearch = (ImageView) rootView.findViewById(R.id.imageView_search);
final LinearLayout.LayoutParams layoutNewEventParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutNewEventParams.setMargins(40, 40, 40, 40);
final LinearLayout.LayoutParams linearLayoutEventTextParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
final LinearLayout.LayoutParams linearLayoutEventIndicatorParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
final LinearLayout.LayoutParams imageViewEventFromParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
imageViewEventFromParams.setMargins(0,0,0,10);
imageViewEventFromParams.weight = (float) 0.5;
final LinearLayout.LayoutParams imageViewEventToParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
imageViewEventToParams.weight = (float) 0.5;
createEvent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.e("EVENT BUTTON", "Create event button pressed.....");
layoutNewEvent.getLayoutParams().height = 140;
layoutNewEvent.setBackgroundResource(R.color.eventColor);
layoutNewEvent.setOrientation(LinearLayout.HORIZONTAL);
linearLayoutEventText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
linearLayoutEventText.setOrientation(LinearLayout.HORIZONTAL);
linearLayoutEventIndicator.setOrientation(LinearLayout.VERTICAL);
imageViewEventFrom.setBackgroundResource(R.color.backgroundColor3);
imageViewEventFrom.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageViewEventFrom.setImageResource(R.drawable.arrow_up_white);
imageViewEventTo.setBackgroundResource(R.color.backgroundColor2);
imageViewEventTo.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageViewEventTo.setImageResource(R.drawable.arrow_down_white);
linearLayoutEventIndicator.addView(imageViewEventTo, imageViewEventToParams);
linearLayoutEventIndicator.addView(imageViewEventFrom, imageViewEventFromParams);
linearLayoutEventText.addView(linearLayoutEventIndicator, linearLayoutEventTextParams);
layoutNewEvent.addView(linearLayoutEventText, layoutNewEventParams);
}
});
}
}
and the below is the error I'm having
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.widget.LinearLayout.getLayoutParams()' on a null object reference
The problem is probably in this line:
layoutNewEvent.getLayoutParams().height = 140;
Your reference to the the view layoutNewEvent is null and that is the cause for the NullPointerException when you are trying to invoke getLayoutParams().
To resolve this I suggest you double check (either by debugging or just by looking at the code) that you are actually have the view with id R.id.layout_new_event in your xml. I am not sure that the xml you are inflating tab_tue_frag.xml is actually the one you put in the question.
Related
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);
}
});
}
I have an xml like this
<android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/SlidingPanel"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left">
<ListView
android:id="#+id/MenuList"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#101010"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button"
/>
</LinearLayout>
</android.support.v4.widget.SlidingPaneLayout>
Now I want to add it to my main layout, which is a framelayout I created by code. Here is what I did:
SlidingPaneLayout mSlidingPanel;
Button bt;
View slide_menu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
slide_menu = mInflater.inflate(R.layout.activity_main, null);
//CREATE FRAMELAYOUT HERE
FrameLayout fr = new FrameLayout(this);
fr.setId(1);
ViewGroup scene1 = (ViewGroup) findViewById(fr.getId());
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
scene1.addView(slide_menu, lp);
//////////////
mSlidingPanel = (SlidingPaneLayout) findViewById(R.id.SlidingPanel);
mMenuList = (ListView) findViewById(R.id.MenuList);
appImage = (ImageView)findViewById(android.R.id.home);
TitleText = (TextView)findViewById(android.R.id.title);
bt = (Button)findViewById(R.id.button1);
for(int i = 0;i<imgID.length;i++){
CreateObject ob = new CreateObject(imgID[i], titleContext[i], null );
mObject.add(ob);
}
mListViewAdapter = new CreateListViewAdapter(this, mObject);
mMenuList.setAdapter(mListViewAdapter);
mSlidingPanel.setPanelSlideListener(panelListener);
mSlidingPanel.setParallaxDistance(200);
/*
mListViewAdapter.notifyDataSetChanged();
*/
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(mSlidingPanel.isOpen()){
mSlidingPanel.closePane();
}
else{
mSlidingPanel.openPane();
}
}
});
// SET CONTENTVIEW HERE
setContentView(scene1,lp);
But I always get
NullPointer error.
I think the problem is caused by findviewByID, because in description it says that: "Finds a view that was identified by the id attribute from the XML that was processed in onCreate", so it will always be null, but I don't know how to correct it.
The error is because you are calling findViewById() before you call setContentView(). Therefore there is no view, and it will return null.
Also, it's rarely a good idea to call .setId().
Why not just <#include/> the layout inside another? And setContentView the main layout?
I need to measure the width of an ImageView, but when i measure it after setting
android:adjustViewBounds on the ImageView the measuredWidth is incorrect...
What am i doing wrong and how should i fix this?
My layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/lines2"
android:orientation="vertical"
android:weightSum="917">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="143">
<RelativeLayout
android:id="#+id/rectangleLayout"
android:background="#drawable/customborder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/circle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/circle"
android:src="#drawable/circle"
/>
</RelativeLayout>
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
view = inflater.Inflate (Resource.Layout.pager1Fragment, container, false);
rectangleLayout = view.FindViewById<ViewGroup> (Resource.Id.rectangleLayout);
circle = view.FindViewById<ImageView> (Resource.Id.circle);
circle.Measure ( Android.Views.View.MeasureSpec.MakeMeasureSpec(0, MeasureSpecMode.Unspecified),
Android.Views.View.MeasureSpec.MakeMeasureSpec(0, MeasureSpecMode.Unspecified));
Initialize ();
return view;
}
public void Initialize ()
{
var rectanglePadding = (int)((double)circle.MeasuredWidth / 2d);
//measuredWidth is wrong when adjustviewbounds is set
Console.WriteLine ("padding " + rectanglePadding);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams (RelativeLayout.LayoutParams.MatchParent, RelativeLayout.LayoutParams.MatchParent);
p.LeftMargin = rectanglePadding;
rectangleLayout.LayoutParameters = p;
}
Ok getting View asap when my fragment is created.
ImageView Example = (ImageView) parentview.findViewById(R.id.Example);
Example.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#SuppressWarnings("deprecation")
#Override
public void onGlobalLayout() {
Example.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int width = Example.getWidth() <--- this gives you width.
}
});
Just convert your code to that and your set.
I am using view stub in my application. I am inflating the view stub to the layout and it is working fine. Now if you see the dummy_layout.xml there I have given the android:layout_height=20dp. What I need to do is to change that view stub height from 20 dp to Wrap_Content on a button click. How can I achieve this?
My ultimate goal is this. I have a button in it. when i click on it for first time I need to show the full layout of stub. and again when I click on it for the second time I need to animate the stub to slide down and show only a part of it.
dummy_layout.xml
<RelativeLayout
android:id="#+id/topLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_watch_pic" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#color/dark_grey"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
</RelativeLayout>
<ViewStub
android:id="#+id/viewStub1"
android:layout="#layout/stub_layout"
android:layout_width="wrap_content"
**android:layout_height="20dp"**
android:layout_alignParentBottom="true"/>
</RelativeLayout>
And here is my class:
public class DummyFragment extends Fragment {
private View mRootView;
private Context mContext;
private Button mButton;
private boolean isVisible = true;
private RelativeLayout mParentLayout;
#Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.dummy_layout,
container, false);
mContext = getActivity();
mButton = (Button) mTopLayout.findViewById(R.id.button1);
final ViewStub stub = (ViewStub) mRootView.findViewById(R.id.viewStub1);
**final View inflated = stub.inflate();**
inflated.setVisibility(View.VISIBLE);
final View viewstublayout = mRootView.findViewById(R.id.viewStub1);
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(isVisible)
{
isVisible=false;
inflated.setVisibility(View.VISIBLE);
}
else
{
isVisible=true;
inflated.setVisibility(View.INVISIBLE);
}
}
});
return mRootView;
}
}
You can change the width/height of a view by editing its LayoutParams. Example:
View stub = findViewById(R.id.viewStub1);
ViewGroup.LayoutParams lp = stub.getLayoutParams();
lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
stub.setLayoutParams(lp);
The better question is why you want to do this? Maybe there's a better way of achieving your goal.
Try this
final ViewStub stub = (ViewStub) mRootView.findViewById(R.id.viewStub1);
View view = stub .inflate();
inside button click
LayoutParams params = (LayoutParams) view.getLayoutParams();
params.width = LayoutParams.WRAP_CONTENT;
view.setLayoutParams(params);
Try this and let me know if works well.
LayoutParams lp = (LayoutParams) (YourView).getLayoutParams();
lp.height = newheight;
YourView.setLayoutParams(lp);
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"