XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<RelativeLayout
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
</RelativeLayout>
Java:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.activity_main);
layout.setBackgroundResource(R.drawable.something);
layout.getLayoutParams().width = 900;
layout.getLayoutParams().height = 900;
The width changes to what I set it to, but the height doesn't no matter what I change it to. I tried changing the layout_height in the XML to 10 dp, but that did nothing as well. I have also tried:
LayoutParams params = layout.getLayoutParams();
params.height = 320;
params.width = 320;
but the same problem occurred.
Related
I want to move my layout up when the keyboards shows on my app . I used adjustResize , adjustPan and both of them for the activity but non of them works and it still shows the keyboard on the form and does not move the layout above .
this is my layout :
<?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">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
............... my views
</LinearLayout>
</ScrollView>
</RelativeLayout>
how can I make it works ?
try like this
final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
if (heightDiff > dpToPx(getApplicationContext(), 200)) { // if more than 200 dp, it's probably a keyboard...
// ... do something here
}
}
});
public static float dpToPx(Context context, float valueInDp) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, metrics);
}
andjust add this in your xml
android:id="#+id/activityRoot"
<?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:id="#+id/activityRoot"**>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
............... my views
</LinearLayout>
</ScrollView>
</RelativeLayout>
I am trying to add textviews dynamically to my activity and I am able to do so but the added textviews are getting added next to each other on the right side and what I want is to have each textview underneath each other.
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearLayout);
for(int x = 0;x < 10; x++){
LinearLayout layout = new LinearLayout(this);
TextView tv = new TextView(this);
tv.setText("Text View "+x);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.addView(tv);
linearLayout.addView(layout);
}
}
Main XML:
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout"
android:orientation="horizontal"></LinearLayout>
</RelativeLayout>
You can do this way:
You have to set orientation vertical of your parent Linear Layout:
Your xml should looks like this:
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout"
android:orientation="vertical"></LinearLayout>
</RelativeLayout>
Your java code should looks like this:
for(int x = 0;x < 10; x++){
TextView tv = new TextView(this);
tv.setText("Text View "+x);
linearLayout.addView(tv);
}
Hope this will help you.
Instead of this:
layout.setOrientation(LinearLayout.HORIZONTAL);
Do this to your linear layout:
layout.setOrientation(LinearLayout.VERTICAL);
im trying to set background to my activity but it throws nullpointer exception everytime
here is my code
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
RelativeLayout layout = (RelativeLayout) findViewById(R.layout.activity_main);
layout.setBackground(getResources().getDrawable(R.drawable.welcome_2));
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
layout.setBackgroundDrawable( getResources().getDrawable(R.drawable.welcome_1) );
} else {
layout.setBackground(getResources().getDrawable(R.drawable.welcome));
}
setContentView(R.layout.activity_main);
and this is my layout
<RelativeLayout 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"
tools:context=".MainActivity" >
move
setContentView(R.layout.activity_main);
before
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
and defined relativelayout Id in XML
<RelativeLayout 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:id="#+id/layout"
tools:context=".MainActivity" >
Firstly Give id to Layout in Xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/layout"
android:layout_height="match_parent"
tools:context=".MainActivity" >
Then Inside java Code Use like this
setContentView(R.layout.activity_main);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
layout.setBackground(R.drawable.welcome_2);
I add two elements in the view for code. An imageView and a Spinning Wheel. The two elements shown but in the same place. And I desire to put the ImageView above the other element, in the center of screen.
The code that add the views:
RelativeLayout container= (RelativeLayout)findViewById(R.id.container);
RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position.addRule(RelativeLayout.CENTER_IN_PARENT);
imgCenter.setLayoutParams(position);
container.addView(imgCenter);
RelativeLayout.LayoutParams position2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position2.addRule(RelativeLayout.CENTER_IN_PARENT);
position2.addRule(RelativeLayout.BELOW,imgCenter.getId());
spinner.setLayoutParams(position2);
container.addView(spinner);
The xml of relativelayout:
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.app.exemple.LoadingActivity"
android:id="#+id/container" >
Remove center in parent in second child
RelativeLayout container= (RelativeLayout)findViewById(R.id.container);
RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position.addRule(RelativeLayout.CENTER_IN_PARENT);
imgCenter.setLayoutParams(position);
container.addView(imgCenter);
RelativeLayout.LayoutParams position2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position2.addRule(RelativeLayout.BELOW,imgCenter.getId());
spinner.setLayoutParams(position2);
container.addView(spinner);
Thanks to #HareshChhelana I found the solution.
I change the xml as follow:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.app.exemple.LoadingActivity"
android:id="#+id/container"
android:orientation="vertical"
android:gravity="center" >
And the code:
LinearLayout container= (LinearLayout)findViewById(R.id.container);
ProgressBar spinner = new ProgressBar(this,null,android.R.attr.progressBarStyleLarge);
container.addView(imgCenter);
container.addView(spinner);
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;