I have a FrameLayout which is the child of ScrollView,
I set an image as frame layout background and draw dynamic EditText on random coordinates of framelayout by setting x and y coordinates of EditText. The issue is when an edittext behind the keyboard gets focus, the layout doesn't scroll up. I have to scroll it manually and as soon as i start typing the layout scroll back to original position.
Instead of FrameLayout, if i take LinearLayout with vertical orientation and add edittext dynamically(without setting coordinates), it works perfectly.
"AsjustPan" is already set in manifest file.
Here is the test layout and java file which works:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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/sv_root"
android:fillViewport="true"
android:scrollbars="none"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/fl_canvas"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout_canvas = findViewById(R.id.layout_canvas);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ppd);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, getResources().getDisplayMetrics().widthPixels, 1908, false);
layout_canvas.setBackground(new BitmapDrawable(getResources(), scaledBitmap));
for(int i=0; i<50; i++){
EditText editText = new EditText(this);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,100);
layoutParams.topMargin = 10;
editText.setLayoutParams(layoutParams);
layout_canvas.addView(editText);
}
}
}
Below is the test layout and java file which doesn't works:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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/sv_root"
android:fillViewport="true"
android:scrollbars="none"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/layout_canvas"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
</ScrollView>
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout layout_canvas = findViewById(R.id.layout_canvas);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ppd);
Bitmap sBitmap = Bitmap.createScaledBitmap(bitmap, getResources().getDisplayMetrics().widthPixels, 1908, false);
layout_canvas.setBackground(new BitmapDrawable(getResources(), sBitmap));
int y = 0;
for(int i=0; i<60; i++){
EditText editText = new EditText(this);
editText.setX(20);
editText.setY(y);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 80);
layout_canvas.addView(editText, layoutParams);
y = y + 100;
}
}
}
Can someone help me with this issue. Thanks
Related
Hello i am trying to make a design of chess board, I am using GridLayout containing buttons that corresponds to blocks of the boards. But the layout is so much bigger that it is not fitting the screen, how can i reduce the button size so that the layout fits the screen.
here is the code
public class BoardActivity extends AppCompatActivity {
private GridLayout mBoard;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_board);
mBoard = (GridLayout)findViewById(R.id.board_grid);
addItems();
}
public void addItems(){
int index = 0;
Button button;
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
button = new Button(this);
button.setId(index);
button.setPadding(0,0,0,0);
button.setText(""+index);
mBoard.addView(button);
index++;
}
}
}
}
and below is the xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context="com.example.harsh.chessgame.BoardActivity">
<GridLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:rowCount="8"
android:columnCount="8"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="0dp"
android:layout_marginTop="0dp"
android:id="#+id/board_grid">
</GridLayout>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/test_text"/>
</LinearLayout>
and the screenshot of the problem is below
Please help, sorry for mistakes and thanks for help.
In your code you have created Buttons programmatically and you haven't specified any width or height for them. You can add the below line in your code and the width and height will be adjusted.
button.setLayoutParams(new LinearLayout.LayoutParams(100, 100));
But for implementing a chess game, I you should better create a custom adapter for the gridview and images for black and white boxes should be inflated. Anyway for your current try this would help you.
Note: The width and height are just a random number currently given as 100.
I have problems with setting margin to a custom made linear layout class that I use multiple times in a GridLayout. The Gridlayout is placed in a fragment.
This is the code of fragment_grid.xml:
<FrameLayout 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="app_a_tize.expressme.Fragment.GridFragment"
android:layout_gravity="center">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/orange"
android:layout_margin="5dp"
android:id="#+id/gridlayout_grid"></GridLayout>
</FrameLayout>
This is the code of the GridFragment.java:
public class GridFragment extends Fragment {
public GridFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_grid, container, false);
}
#Override
public void onStart() {
super.onStart();
GridLayout grid = (GridLayout) getView().findViewById(R.id.gridlayout_grid);
grid.setRowCount(3);
int tileHeight = (CategoryTileActivity.gridContentHeight -3 * 10) / 3;
int amountofColumns = (int) CategoryTileActivity.gridContentWidth / tileHeight;
grid.setColumnCount(amountofColumns);
grid.setMinimumWidth((amountofColumns * tileHeight) + (5 * 20 ));
for (int i = 0; i < 3 * amountofColumns; i++) {
//fill the grid with the custom LinearLayout:
grid.addView(new TileClass(getActivity(), tileHeight, tileHeight, "ToBeImplemented", "Button"));
}
}
}
This is the code of the custom LinearLayout:
public class TileClass extends LinearLayout {
public TileClass(Context context, int height, int width, String image, String text) {
super(context);
this.setBackgroundResource(R.drawable.tile_button); //creates rounded layouts
this.setMinimumHeight(height);
this.setMinimumWidth(width);
this.setOrientation(LinearLayout.VERTICAL);
ImageView tileImage = new ImageView(context);
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.tilephoto);
Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap, 100, 100, true);
tileImage.setImageBitmap(bMapScaled);
tileImage.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
TextView tileText = new TextView(context);
tileText.setText(text);
tileText.setTextColor(Color.WHITE);
tileText.setGravity(Gravity.CENTER);
addView(tileImage);
addView(tileText);
}
}
When I run the Activity, I get this as result:
The code I showed above is responsible for the orange area in the middle. What I need: the blue "buttons"/LinearLayouts, in the orange area in the middle, to have a margin of 5dp. So the rest of the orange space is be taken by the custom LinearLayouts.
I don't know how to fix that, I tried a lot of options but they don't seem to work out for me.. Everything from MarginLayoutParams to params.setMargins(5,5,5,5); On almost every layout in my code.
I use Android Studio 2.1.2, supporting minimum of API 15.
Every help is appreciated!
For your imagination, this must be the end result, I need the margin like this:
You have to make custom view of gridview item as below:-
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="#dimen/categoryHeight"
android:layout_marginLeft="#dimen/margin_5dp"
android:layout_marginRight="#dimen/margin_5dp"
android:layout_marginTop="#dimen/margin_7dp"
android:background="#drawable/rounded_bg"
>
<ImageView
android:id="#+id/llRowItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scaleType="fitXY"
android:gravity="bottom"/>
<TextView
android:id="#+id/item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black_light"
android:padding="#dimen/margin_5dp"
android:layout_gravity="bottom"
android:singleLine="true"
android:textColor="#color/white"
android:textSize="#dimen/font_size_16sp" />
</FrameLayout>
and inside adapter set color of text view, background, text or image of imageview whatever you want to set.
I have listview load about 50 images:
<?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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:padding="10dp"
android:background="#drawable/background">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusableInTouchMode="true">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listViewImg">
</ListView>
</LinearLayout>
in activity , this code zoom listview
final ListView lvi = (ListView)findViewById(R.id.listViewImg);
try {
XmlPullParserImg parser = new XmlPullParserImg();
imgList = parser.parse(getAssets().open(get_xml));
BinderDataImg binderdata = new BinderDataImg(this, imgHashmap);
lvi.setAdapter(binderdata);
}catch(IOException e) {
e.printStackTrace();
}
FloatingActionButton fa1 = (FloatingActionButton) findViewById(R.id.fab1);
fa1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
float x1 = lvi.getScaleX();
float y1 = lvi.getScaleY();
lvi.setScaleX(x1 * 2);
lvi.setScaleY(y1 * 2);
}
});
I can zoom in and zoom out listview.
when zoom in, I can scroll listview up and down, and image bigger than layout.
but I can't scroll it to left and right ( or move left or right )
sorry for my bad English
thank for reading
set width of all layout and listView to wrap_content instead of match_parent
With the following Java and XML code, I was wondering how to retrieve the dimensions in pixels of an inner/child layout (LinearLayout, whereas the parent is RelativeLayout with paddings of 20dp) properly:
Java code:
public class TestActivity extends Activity {
private LinearLayout linLay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
linLay = (LinearLayout)findViewById(R.id.linLay);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
linLay.setLayoutParams(lp);
getDimensions();
}
private void getDimensions() {
System.out.println(linLay.getHeight());
System.out.println(linLay.getWidth());
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp" >
<LinearLayout
android:id="#+id/linLay"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
</LinearLayout>
</RelativeLayout>
... Since I get outputs of 0 for the dimensions, I am aware that the LinearLayout needs to be set first on the screen before I could retrieve the dimensions... But what am I doing wrong here?
It didn't get its dimensions at the time you try to output them.You can just add a delay or just use method below.
private void getDimensions() {
ViewTreeObserver vto = linLay.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
#Override
public voidonGlobalLayout() {
linLay.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int height =linLay.getMeasuredHeight();
int width =linLay.getMeasuredWidth();
System.out.println(height);
System.out.println(width);
}
});
}
I put a scrollview around a Relative Layout that I am adding to dynamically. If I set the height of the Relative Layout to a fixed amount that goes off of the page the scrollview will work.
Example:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<RelativeLayout
android:id="#+id/llcustomrow"
android:layout_width="match_parent"
android:layout_height="800dp" >
</RelativeLayout>
</ScrollView>
But I need the RelativeLayout to be able to hold however many items I add to it dynamically o I set The relativelayout height to wrap_content. But once I add enough items to the relative layout so that they are going off the screen the Scrollview doesnt register
Below is how I am dynamically adding to the relative layout
LinearLayout mLinearLayout;
RelativeLayout rlcopy;
RelativeLayout[] rArray = new RelativeLayout[20];
int counter = 0;
RelativeLayout llcustomrow;
RelativeLayout.LayoutParams paramsleft;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mLinearLayout = (LinearLayout) inflater.inflate(
R.layout.customworkout, container, false);
paramsleft = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
llcustomrow = (RelativeLayout)mLinearLayout.findViewById(R.id.llcustomrow);
for(int i = 0;i<=rArray.length-1;i++){
rArray[i] = (RelativeLayout)View.inflate(getActivity(), R.layout.addworkoutlayout, null);
paramsleft.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
paramsleft.setMargins(10, 0, 0, 0);
rArray[i].setLayoutParams(paramsleft);
}
Button bAdd = (Button) mLinearLayout.findViewById(R.id.bAddExcercise);
bAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
rArray[counter].setY(rArray[counter-1].getY() + (rArray[counter-1].getHeight() +25));
llcustomrow.addView(rArray[counter]);
counter++;
}
});
return mLinearLayout;
}
Thanks
Why are you using RelativeLayout? I'd advise switching to LinearLayout.
TRY THIS it will work for me
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/outer_ll"
android:layout_width="match_parent"
android:layout_height="900dp"
android:background="#E7D687"
>
</RelativeLayout>
</ScrollView>
change the layout_height of your RelativeLayout to match_parent.
it will solve the problem.