Programmatically create scroll view in android studio with buttons.! - java

I am trying to make an app with this look enter image description here
but programmatically such that I can change the number of shown boxes in runtime.
I have tried the following, but no success-
1)MainActivity.java
package com.bigx.dynamictesting;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout relativeLayout=(RelativeLayout)findViewById(R.id.activity_relative);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
for(int i = 0; i < 4; i++)
{
TextView textView = new TextView(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int)(300 * getResources().getDisplayMetrics().density));
layoutParams.setMargins(0, 0, 0, (int)(20 * getResources().getDisplayMetrics().density));
textView.setLayoutParams(layoutParams);
textView.setGravity(Gravity.CENTER);
textView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
textView.setText("TextView" + i);
linearLayout.addView(textView);
Button buttonA = new Button(this);
Button buttonB = new Button(this);
RelativeLayout.LayoutParams layoutParamsA = new RelativeLayout.LayoutParams((int)(60 * getResources().getDisplayMetrics().density),(int)(60 * getResources().getDisplayMetrics().density));
layoutParamsA.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, textView.getId());
layoutParamsA.addRule(RelativeLayout.ALIGN_PARENT_END, textView.getId());
RelativeLayout.LayoutParams layoutParamsB = new RelativeLayout.LayoutParams((int)(60 * getResources().getDisplayMetrics().density),(int)(60 * getResources().getDisplayMetrics().density));
layoutParamsA.addRule(RelativeLayout.ALIGN_PARENT_LEFT, textView.getId());
layoutParamsA.addRule(RelativeLayout.ALIGN_PARENT_START, textView.getId());
buttonA.setLayoutParams(layoutParamsA);
buttonA.setText(String.valueOf(i));
linearLayout.addView(buttonA);
buttonB.setLayoutParams(layoutParamsB);
buttonB.setText(String.valueOf(i));
linearLayout.addView(buttonB);
}
relativeLayout.addView(linearLayout);
}
2) activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="0dp"
android:fillViewport="false"
android:background="#color/colorAccent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_relative"
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"
tools:context="com.bigx.dynamictesting">
</RelativeLayout>
</ScrollView>
Which yields this result-enter image description here
Kindly suggest me how to achieve my objective.
Thank you.
P.S.-Sorry for my bad English.

Related

ScrollView auto-scroll off on android app open

I'm appending 5 video players in my app via java. When the app opens, the ScrollView's scroll-Y is positioned to show the 5th video player. How can I avoid this? How can I turn off auto-scroll so when the app opens, the 1st video player appears and the scroll-Y is 0?
Colors:
<color name="secondary_color">#6CABF7</color>
<color name="light_black">#202125</color>
<color name="dark_black">#0F0F0F</color>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:background="#color/light_black"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:background="#color/secondary_color"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="80dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:tag="yo">
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
MainActivity.java:
package com.example.test;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.*;
import android.view.*;
import android.widget.*;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
LinearLayout content;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
content = findViewById(R.id.content);
for (int x=0;x<5;x++) {
RelativeLayout video_player_box = new RelativeLayout(this);
RelativeLayout.LayoutParams params;
VideoView player = new VideoView(this);
TextView details = new TextView(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 600);
param.setMargins(0,0,0,50);
video_player_box.setLayoutParams(param);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
params.setMargins(5,5,5,5);
player.setLayoutParams(params);
player.setMediaController(null);
video_player_box.addView(player);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 50);
params.setMargins(5,5,5,0);
details.setLayoutParams(params);
details.setPadding(15,0,0,0);
details.setText(Integer.toString(x+1));
details.setTextSize(25);
details.setTextColor(getResources().getColor(R.color.dark_black));
details.setGravity(Gravity.CENTER_VERTICAL);
video_player_box.addView(details);
content.addView(video_player_box);
}
((VideoView) ((RelativeLayout) content.getChildAt(0)).getChildAt(0)).setVideoURI(Uri.parse("https://dash.akamaized.net/akamai/bbb/bbb_320x180_60fps_600k.mp4"));
((VideoView) ((RelativeLayout) content.getChildAt(0)).getChildAt(0)).start();
}
}
Pasting this on my parent linear layout that comes right as the main child of the scrollview solved the issue for me.
android:descendantFocusability="blocksDescendants

Button not moving from top-left to bottom-right when user touch screen

I want button to change its size and move from top left to bottom right when user touches screen.But my app stop running when i touch screen.If i delete part of code that change position of the button everything is ok and the button change its size.
Here is MainActivity.java
package com.example.user.a34;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
ViewGroup main_layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main_layout = (ViewGroup)findViewById(R.id.main_layout);
main_layout.setOnTouchListener(new RelativeLayout.OnTouchListener(){
public boolean onTouch(View v,MotionEvent event){
moveButton();
return true;
}
});
}
public void moveButton(){
View button1 = (View)findViewById(R.id.button1);
//Change position---------------------
RelativeLayout.LayoutParams positionRules = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
positionRules.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
positionRules.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
button1.setLayoutParams(positionRules);
//Change size-------------------------
ViewGroup.LayoutParams sizeRules = button1.getLayoutParams();
sizeRules.width = 350;
sizeRules.height = 200;
button1.setLayoutParams(sizeRules);
}
}
and here is activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.a34.MainActivity">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="38dp"
android:layout_marginLeft="66dp"
app:layout_constraintLeft_toLeftOf="parent" />
</android.support.constraint.ConstraintLayout>
The direct cause of your crash is likely trying to assign RelativeLayout.LayoutParams to a view that is in a ConstraintLayout and should thus have layout params of type ConstraintLayout.LayoutParams.
You either need to use a RelativeLayout in your xml, or assign your button layout params of type ConstraintLayout.LayoutParams.
I would suggest using a RelativeLayout in your xml as follows:
<?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:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.a34.MainActivity">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_marginTop="38dp"
android:layout_marginBottom="38dp"
android:layout_marginLeft="66dp"
android:layout_marginRight="66dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
</RelativeLayout>
and change your moveButton method as follows:
public void moveButton() {
View button1 = findViewById(R.id.button1);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)button1.getLayoutParams();
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
layoutParams.width = 350;
layoutParams.height = 200;
button1.setLayoutParams(layoutParams);
}

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams

I have a RecyclerView with a LinearLayout inside. The LinearLayout consists out of 9 buttons. Before some changes I could click the buttons and it didn't crashed. But after I added this code below it gives the error and does not show where it comes from:
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams
The imports in Adapter:
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout.LayoutParams;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
Added this in the onClickListener:
tempTag = (int) v.getTag();
mAdapter.grayButton(tempTag / 9, tempTag % 9);
zahl1 = Integer.parseInt(mAdapter.getText(tempTag).toString());
Added this method in the RecyclerView.Adapter:
public void grayButton(int row, int element){
recycleViewDatas.get(row).setGray(element);
notifyItemChanged(row);
}
Added this code in the onBindViewHolder:
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
viewHolder.ll.setLayoutParams(lp);
lp.height = viewHolder.ll.getLayoutParams().height - (int) (viewHolder.buttons.get(j).getLayoutParams().height - recycleViewDatas.get(i).getSize()) + 30;
viewHolder.ll.setLayoutParams(lp);
if(recycleViewDatas.get(i).getGray() == j)
viewHolder.buttons.get(j).setTextColor(Color.GRAY);
else if(recycleViewDatas.get(i).getGray() == -1)
viewHolder.buttons.get(j).setTextColor(Color.BLACK);
This is my ViewHolder:
public static class ViewHolder extends RecyclerView.ViewHolder {
LinearLayout ll;
private ArrayList<Button> buttons = new ArrayList<>();
public ViewHolder(View v, Context context) {
super(v);
ll = (LinearLayout) v.findViewById(R.id.llRecycleView);
for (int i = 0; i < ll.getChildCount(); i++) {
buttons.add((Button) ll.getChildAt(i));
}
}
}
The layout file of the recycleviewdata:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="9"
android:background="#drawable/zeile"
android:id="#+id/llRecycleView">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#android:color/black"
android:background="#android:color/transparent"
android:textSize="23sp"
android:clickable="false"
android:layout_gravity="center"
android:gravity="center" />
...more buttons
The layout of the MainActivity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:background="#drawable/top">
...some buttons
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/></LinearLayout>
I do not understand why the error comes after the update. And the app only crashes on my M9. On the HTC One V it does not crash and all works as expected.
You know the problem? Do you need any more code?
Your problem is in onBindViewHolder:
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
LayoutParams is implemented for (almost) all the layouts.
When you set LayoutParams on a view, you need to set it with a type of the view's parents. So if you have a LinearLayout and you are trying to add a Button, you need to set LinearLayout.LayoutParams on the Button. The problem in this case is that you don't know the parent type. And by that I mean it can different on different Android versions.
The safe bet is to use the LayoutParams from a common class (ViewGroup?) since you only set width and height.
i had same problem, you can use this
ViewGroup.LayoutParams layoutParams =v.itemView.getLayoutParams();
layoutParams.width= ViewGroup.LayoutParams.MATCH_PARENT;
layoutParams.height= 200;
v.itemView.setLayoutParams(layoutParams);
if you need the kotlin version :
with(view.layoutParams){
width = ViewGroup.LayoutParams.WRAP_CONTENT
height = ViewGroup.LayoutParams.MATCH_PARENT
}

How to make ViewPager fullscreen?

I have a ViewPager that I want to take the full-screen, but it only takes part of it. How can I make it take the full-screen? Here's my code, notice that it is filled with many calls to make the layout take the full-screen, to no avail.
walkthrough_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="1080dp"
android:minHeight="1920dp"/>
</RelativeLayout>
walkthrough_single_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/image_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
WalkthroughActivity.java:
package org.core.game.activities;
import org.core.game.Log;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
public class WalkthroughActivity extends Activity
{
private static final int MAX_VIEWS = 1;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.walkthrough_activity);
getWindow().setLayout(Main.screenWidth, Main.screenHeight);
mViewPager = (ViewPager) findViewById(R.id.view_pager);
mViewPager.setAdapter(new WalkthroughPagerAdapter());
mViewPager.setOnPageChangeListener(new WalkthroughPageChangeListener());
mViewPager.setX(0);
mViewPager.setY(0);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = width;
LayoutParams lp = new LayoutParams(width, height);
mViewPager.setLayoutParams(lp);
}
class WalkthroughPagerAdapter extends PagerAdapter
{
#Override
public int getCount()
{
return MAX_VIEWS;
}
#Override
public boolean isViewFromObject(View view, Object object)
{
return view == (View) object;
}
#SuppressLint("InflateParams")
#Override
public Object instantiateItem(View container, int position)
{
Log.e("instantiateItem(" + position + ");");
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View imageViewContainer = inflater.inflate(R.layout.walkthrough_single_view, null);
ImageView imageView = (ImageView) imageViewContainer.findViewById(R.id.image_view);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setAdjustViewBounds(true);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = width;
imageView.setLayoutParams(new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.FILL_PARENT));
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(1080, 1920);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(1080, 1920);
imageView.setLayoutParams(layoutParams);
switch (position)
{
case 0:
imageView.setImageResource(R.drawable.should_be_full_screen);
break;
}
((ViewPager) container).addView(imageViewContainer, 0);
return imageViewContainer;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object)
{
((ViewPager) container).removeView((View) object);
}
}
}
And this is how it looks (the image is 1080*1920):
Edit: I basically want to do a mini tutorial. One page with images, one page with a movie. What do you think is the best way?
Try this:
int deviceWidthInPixels = getResources().getDisplayMetrics().widthPixels;
int deviceHeightInPixels = getResources().getDisplayMetrics().heightPixels;
imageView.getLayoutParams().width = deviceWidthInPixels;
imageView.getLayoutParams().height = deviceHeightInPixels;
Also, use this for ViewPager:
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
And this for ImageView
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/image_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY" />
Try this :
<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" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="5dp"
/>
</LinearLayout>
our code
android:layout_width="fill_parent"
android:layout_height="fill_parent"
Change it to
android:layout_width="match_parent"
android:layout_height="match_parent"
Or you can try this adding to your ViewPager
android:layout_height="0dp"
android:layout_weight="1"
Let me know if further issue exists
-Now i saw your lines
LayoutParams lp = new LayoutParams(width, height);
Change it to Below code:-
Layoutparams lp = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
I feel so stupid... Notice that in my onCreate I have getWindow().setLayout(Main.screenWidth, Main.screenHeight) Annoyingly I made Main.screenHeight) to be 75 % :( Removing it fixed my problem.

Android: Why TextField is not showing?

TextView t = new TextView(this);
t.setX(0);
t.setY(0);
t.setText("Tap");
t.setTextColor(Color.GREEN);
t.setTextSize(25);
t.setLayoutParams(new FrameLayout.LayoutParams(100, 100));
setContentView(t);
I'am in ActionBarActivity, that why I'am using "this" as context, can't understand why the textField isn't showing, is here something wrong?
Try this one
TextView t= new TextView(this);
t.setX(0);
t.setY(0);
t.setText("Tap");
t.setTextColor(Color.GREEN);
t.setTextSize(25);
addContentView(t, new FrameLayout.LayoutParams(100, 100));
Create xml file
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frame_layout"
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="in.appkart.flashlight.MainActivity" >
</FrameLayout>
and MainActivity
package in.appkart.flashlight;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private static final String TAG = MainActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View frameLayout = findViewById(R.id.frame_layout);
TextView t = new TextView(this);
t.setX(0);
t.setY(0);
t.setText("Tap");
t.setTextColor(Color.GREEN);
t.setTextSize(25);
t.setLayoutParams(new FrameLayout.LayoutParams(100, 100));
t.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
((FrameLayout) frameLayout).addView(t);
}
}

Categories

Resources