I've relative layout i.e. main.xml which I set as follows. But now I've to put view1 on view2 with width=200dp and height =100dp, so that view2 will be large one and view1 will be small one on it.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
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" >
<RelativeLayout
android:id="#+id/MainPanel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/panel_bottom"
android:layout_gravity="center_horizontal" >
<com.proj.demo.view1
android:id="#+id/sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignWithParentIfMissing="true"
android:layout_centerInParent="true"
android:layout_toLeftOf="#+id/panel_quick_buttons"
/>
<com.proj.demo.view2
android:id="#+id/sheet2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignWithParentIfMissing="true"
android:layout_centerInParent="true"
android:layout_toLeftOf="#+id/panel_quick_buttons"
/>
</RelativeLayout>
</RelativeLayout>
What do you want to achieve? If you just want to change width and heigh than:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.yourId);
rl.getLayoutParams().height = 100;
rl.getLayoutParams().width = 100;
Related
I have FrameLayout: Width =match_parent, I want set Height = 70% * Width
Ex:
<FrameLayout
android:id="#+id/fiew"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp">
</FrameLayout>
How can set Height =70% * Width?
Use ConstraintLayout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:background="#android:color/black"
app:layout_constraintDimensionRatio="H,3:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</FrameLayout>
</android.support.constraint.ConstraintLayout>
Here just use ratio in app:layout_constraintDimensionRatio to specify the percentage
You can use a ViewTreeObserver to know when the layout has completed, and then change the layout params of the FrameLayout to adjust the height.
frame_layout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/fiew"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#color/colorPrimary"
android:layout_marginTop="20dp">
</FrameLayout>
</LinearLayout>
FrameLayoutActivity
public class FrameLayoutActivity extends AppCompatActivity {
private FrameLayout fiew;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.frame_layout);
fiew = (FrameLayout) findViewById(R.id.fiew);
final View layout = (View)fiew.getParent();
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener (new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int width = layout.getMeasuredWidth();
//int height = layout.getMeasuredHeight();
ViewGroup.LayoutParams params = fiew.getLayoutParams();
params.height = (int) (width * 0.7);
fiew.setLayoutParams(params);
fiew.invalidate();
}
});
}
}
A simple approach is to take that Framelayout inside a Linearlayout and play with weight. No need to use Constraint layout.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10">
<FrameLayout
android:id="#+id/fiew"
android:layout_width="match_parent"
android:layout_weight="7"
android:layout_height="0dp">
</FrameLayout>
</LinearLayout>
How can I change params of a LinearLayout inside a DrawerLayout?
I've tried it with:
LinearLayout linearLayout;
after onCreate:
drawerLinearLayout = (LinearLayout)findViewById(R.id.drawer);
// drawer is the id of the linearlayout, where I want to change its params
DrawerLayout.LayoutParams lp = new DrawerLayout.LayoutParams(100,100);
linearLayout.setLayoutParams(lp);
the xml is like:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main" />
<LinearLayout
android:id="#+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
There is no error, but the size of drawer is not 100 x 100 - it takes the whole width and heigth :S
EDIT:
public class MainActivity extends FragmentActivity {
LinearLayout drawerLinearLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
drawerLinearLayout = (LinearLayout)findViewById(R.id.drawer);
int mode = getResources().getConfiguration().orientation;
if (mode == 1) {
DrawerLayout.LayoutParams lp = new DrawerLayout.LayoutParams(2,2);
drawerLinearLayout.setLayoutParams(lp);
}
}}
in my Android app i'm defining the following layout which is working fine.
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black">
<com.test.zoom.ImageZoomView
android:id="#+id/zoomview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ZoomControls
android:id="#+id/zoomControls"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_gravity="bottom|right"/>
</FrameLayout>
But when set the layout parameters on the FrameLayout the ZoomControls are gone.
Do you have a hint what i'm doing wrong?
ViewTreeObserver viewTreeObserver = frame.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
frame.getViewTreeObserver().removeGlobalOnLayoutListener(this);
frame.setLayoutParams(new LinearLayout.LayoutParams(frame.getMeasuredWidth(), frame.getMeasuredWidth()));
}
});
}
Your ZoomControls is a child of FrameLayout. So if you remove the FrameLayout, all of its children(com.test.zoom.ImageZoomView & ZoomControls) will be removed as well.
Try to make your ZoomControl be indepent from FrameLayout, like this:
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black">
<com.test.zoom.ImageZoomView
android:id="#+id/zoomview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
<ZoomControls
android:id="#+id/zoomControls"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_gravity="bottom|right"/>
I have the follwing layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<View
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:id="#+id/bottomNav"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username: " />
</LinearLayout>
Programatically I'm replacing the view "content" with my Fragment:
View view = inflater.inflate(R.layout.kwiz_game, container, false);
contentView = view.findViewById(R.id.content);
currentFragment = new KwizCardBackFragment(android.R.drawable.btn_plus);
getFragmentManager().beginTransaction().replace(R.id.content,
currentFragment);
However the fragment is taking all the space and the Layout with the TextView is not shown.
I also tried to use RelativeLayouts with alignParentBottom=true and other stuff...
View of the Fragment which replaces the content:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_kwiz_item" >
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:scaleType="centerCrop" />
</RelativeLayout>
OnCreateView of the fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_card_back, container,
false);
ImageView imageView = (ImageView) view.findViewById(R.id.image);
if (drawable != null) {
imageView.setImageDrawable(drawable);
} else {
imageView.setBackgroundResource(drawableId);
}
return view;
}
After hours of debugging I finally found the mistake... Some of my layouts had views with the id "content". I thought when I look for R.id.content that the "nearest" element will be used... that's not the case!
Thanks anyone!
I modified your layout a little bit and this seems to achieve what you want:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_above="#+id/bottomNav" />
<LinearLayout
android:id="#+id/bottomNav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username: " />
</LinearLayout>
</RelativeLayout>
And a small side note, I see you used fill_parent in your layout, but that's deprecated since API level 8, so you should use match_parent instead :)
Hope this helps, cheers!
You should use FrameLayout as your fragment container, and put (and then replace) your fragments into that FrameLayout container.
I've a layout activity_main.xml which contains this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
</LinearLayout>
And I've a layout, new_bucket.xml which contains this code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/newEntry"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/main"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="#253514"
android:clickable="true"
android:onClick="toggleAmagar"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text1"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:id="#+id/child"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_below="#id/main"
android:background="#666666"
android:clickable="true"
android:onClick="toggleAmagar"
android:orientation="vertical">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text2" />
</LinearLayout>
</LinearLayout>
What I want to achieve, is to programatically add several "instances" of new_bucket.xml to the main layout in activity_main.xml. Those instances, should appear inside the LinearLayout of activity_main.xml.
I'm a bit stuck at the moment. I've tried to just add a TextView to activity_main.xml but I can't even handle that...
LinearLayout my_root = (LinearLayout) getLayoutInflater().inflate(R.layout.activity_main, null);
LinearLayout A = new LinearLayout(this);
A.setOrientation(LinearLayout.HORIZONTAL);
TextView tv = new TextView(this);
tv.setText("This text should appear somewhere");
A.addView(tv);
my_root.addView(A);
But that TextView is never shown.
Can you help me out?
You mean like this?
LinearLayout container = (LinearLayout)findViewById(R.id.container);
View child = getLayoutInflater().inflate(R.layout.new_bucket, container);
container.addView(child);
See documentation.
layout activity_main.xml
// i added an Id for LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
</LinearLayout>
This code will add your new_bucket to your main LinearLayout
LinearLayout my_root = (LinearLayout) findViewById(R.id.container, null);
LayoutInflater mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View bucket= (View) mInflater.inflate(R.layout.new_bucket, null);
my_root.addView(bucket);
Hope this work for you.
LinearLayout container = (LinearLayout)findViewById(R.id.container);
View child = getLayoutInflater().inflate(R.layout.new_bucket, container, false);
child.setId(0);
container.addView(child);
if u want to inflate multiple layout use for loop for this.
After so many help from so nice guys, I managed to do that.
I have not changed any Layout from the main question.
BUT java code is like this now:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout my_root = (LinearLayout) findViewById(R.id.container);
LayoutInflater mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View bucket= (View) mInflater.inflate(R.layout.new_bucket, null);
my_root.addView(bucket);
}