change position when button clicked? - java

I've two object in android xml, my object locate in horizontal orientation, imageview in left and textview in right,my code look like
<LinearLayout
android:id="#+id/rowFile"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/img_dilvChat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
/>
<TextView
android:id="#+id/txtFileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dip"
android:gravity="left"
android:text="Nama File : test.zip"
android:textSize="10dip" />
</LinearLayout>
i want to switch 2 object when button fire (imageview to right,textview to left)
how i achieve that ?

The following code should do it. Remove the ImageView and add it to the LinearLayout again. Do this indside the OnClick listener.
LinearLayout row = (LinearLayout) findViewById(R.id.rowFile);
ImageView image = (ImageView) findViewById(R.id.img_dilvChat);
row.removeView(image);
row.addView(image);
row.invalidate();

write these in onClickListener of the Button
(findViewById(R.id.imageView)).getLocationOnScreen(posXY);
int imgx = posXY[0];
int imgy = posXY[1];
(findViewById(R.id.textView)).getLocationOnScreen(posXY);
int txtx = posXY[0];
int txty = posXY[1];
(findViewById(R.id.imageView)).setX(txtx);
(findViewById(R.id.imageView)).setY(txty);
(findViewById(R.id.textView)).setX(imgx);
(findViewById(R.id.textView)).setY(imgy);

Related

(Android) Set Width/Height of (Image-)Button in Java Class

I want to set the width and height of an ImageButton in the Java Class, the width of the button should be the width of the display / 4, the height of the button should be the height of the display / 4.
Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
ImageButton button = (ImageButton) findViewById(R.id.imageButton);
// button.setWidth(width/4)
// button.setHeight(height/4)
}
Apparently there's no method for a button called setWidth() or setHeight(), so how can I accomplish it?
Possible weight is what are you looking for. For example this code will give you four rectangles, everyone takes quarter of a screen
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/black"/>
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/white"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/holo_red_dark"/>
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/holo_blue_bright"/>
</LinearLayout>
</LinearLayout>
You can define weightSum in linearlayout and views will be scaled to be percent of weightSum.
Not letting me comment but if you look in the activity_main.xml, you should find something like this in it.
<ImageButton
android:contentDescription="#+id/imageButton1"
android:id="#+id/imageButton1" />
from here, you can add into this android:layout_width = 50p, where 50p is 50 pixels wide. You can change this to `android:layout_height = 100p, just change the 50 and 100 to numbers of your liking. so, it would look like this after you add them in,
<ImageButton
android:contentDescription="#+id/imageButton1"
android:id="#+id/imageButton1"
android:layout_width ="50p"
android:layout_height ="50p" />
If you want to set the width and height of the ImageView from Java code you can do something like this:
LayoutParams params = button.getLayoutParams();
params.width = width/4;
params.height = height/4;
button.requestLayout();

progressbar on top of Button in relative layout issue in Android Studio

Ok this is a weird one I hope someone can explain to me.
I have a custom button layout which creates a button with a circular progress bar in the middle of the button. My XML code is below. What I can't work out however is that the ProgressBar seems to be appearing behind the button. If I set the button background to anything other than transparent the progressbar cannot be seen. With the button background as transparent I can then see the ProgressBar but it still appears behind the button text. I was under the understanding that views appeared in the order they are added. I have even tried setting the view to be on top (view.bringToFront();) and I've tried removing the view and recreating it.
Why does the progressbar appear behind the button and what can I do to solve it?
Many thanks
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#android:color/holo_blue_bright"
android:padding="2dp">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:text="Button"
android:gravity="center"
android:textColor="#android:color/white"
android:singleLine="true"
android:clickable="false">
</Button>
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:layout_centerInParent="true"
android:visibility="visible"
/>
</RelativeLayout>
Code using the above layout
private void setupTableLayout(int NumberOfRows, int NumberOfButtons){
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(0, android.widget.TableRow.LayoutParams.MATCH_PARENT, 3f);
TableLayout tableLayout = (TableLayout) findViewById(R.id.thetablelayout);
tableLayout.removeAllViews();
for (int i = 0; i < NumberOfRows; i++) {
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(tableParams);
RelativeLayout btnOneLayout = (RelativeLayout)getLayoutInflater().inflate(R.layout.custom_button, null);
RelativeLayout btnTwoLayout = (RelativeLayout)getLayoutInflater().inflate(R.layout.custom_button, null);
ProgressBar btnOneProgressBar = (ProgressBar)btnOneLayout.findViewById(R.id.progressBar);
ProgressBar btnTwoProgressBar = (ProgressBar)btnTwoLayout.findViewById(R.id.progressBar);
btnOneLayout.setLayoutParams(rowParams);
btnTwoLayout.setLayoutParams(rowParams);
Button btnOne = (Button)btnOneLayout.findViewById(R.id.button);
btnOne.setText("Btn 1, Row " + i);
btnOne.setId(1001 + i);
Button btnTwo = (Button)btnTwoLayout.findViewById(R.id.button);
btnTwo.setText("Btn 2, Row " + i);
btnTwo.setId(2001 + i);
setButtonClickListener(btnOneLayout, btnOneProgressBar);
setButtonLongClickListener(btnOneLayout, btnOneProgressBar);
tableRow.addView(btnOneLayout); //Add layout, instead of just Button
View adivider = new View(this);
adivider.setLayoutParams(new TableRow.LayoutParams(20, TableRow.LayoutParams.MATCH_PARENT));
adivider.setBackgroundColor(Color.TRANSPARENT);
// This bit of code deals with odd/even numbers of buttons.
if (((i + 1) * 2) < NumberOfButtons + 1) {
tableRow.addView(adivider);
tableRow.addView(btnTwoLayout);
} else {
tableRow.addView(adivider);
btnTwoLayout.setBackgroundResource(android.R.color.transparent);
tableRow.addView(btnTwoLayout);
}
tableLayout.addView(tableRow);
}
}
You are propably running this on android >= 5.0. In 5.0 they added elevation field for views. Elevation defines z-order of views in ViewGroup.
In that case button have non-zero elevation value and progress bar have zero value elevation.
Set elevation of progress bar to e.g. 10dp
<ProgressBar
...
android:elevation="10dp"/>
Put your button into another layout (best choice for this case is probably FrameLayout).
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
... >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/button"
... />
</FrameLayout>
<ProgressBar
android:id="#+id/progressBar"
... />
</RelativeLayout>
I can't tell you why exactly you get that effect, but I suppose that is a bug. Notice that if you replace Button with other view, for example TextView that problem doesn't exits. But when you change RelativeLayout to any other (tested with FrameLayout) this bug still appears. I guess it's going about background property and order of drawing or measurement in any layout.
try using FrameLayout like this
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#android:color/holo_blue_bright"
android:padding="2dp">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:text="Button"
android:gravity="center"
android:textColor="#android:color/white"
android:singleLine="true"
android:clickable="false">
</Button>
<ProgressBar
android:layout_gravity="center"
android:id="#+id/progressBar"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#android:color/transparent"
android:layout_centerInParent="true"
android:visibility="visible"
/>
</FrameLayout>
See this link
Generally, FrameLayout should be used to hold a single child view,
because it can be difficult to organize child views in a way that's
scalable to different screen sizes without the children overlapping
each other. You can, however, add multiple children to a FrameLayout
and control their position within the FrameLayout by assigning gravity
to each child, using the android:layout_gravity attribute.
Child views are drawn in a stack, with the most recently added child on top.
By adding marginTop you can do that.. otherwise you can change the structure of button and progress bar...
<linearLayout android:orientation="horizontal" ... >
<ImageView
android:id="#+id/thumbnail"
android:layout_weight="0.8"
android:layout_width="0dip"
android:layout_height="fill_parent"
>
</ImageView>
<TextView
android:id="#+id/description"
android:layout_marginTop="-20dip"
android:layout_weight="0.2"
android:layout_width="0dip"
android:layout_height="wrap_content"
>
</TextView>
this code is working fine for me :D

Insert a TextView under an others in a RelativeLayout in a ScrollView

I would like to know, how can i add a lot of number of TextView to an RelativeLayout in a ScrollView (for textview can scroll up, of course)
In my XML code, it's easy :
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/bde" >
<RelativeLayout
android:id="#+id/layout_"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_below="#id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</RelativeLayout>
</ScrollView>
Except i would like add in Java code, for that each TextView from a DB (no problems with db), add himself in layout and the more important, the one after the other.
Now, i have :
//Layout in ScrollView
RelativeLayout r = (RelativeLayout) findViewById(R.id.layout_);
//for add TextView
for(int i = 0 ; i < nbr_limite ; i++){
TextView v1 = new TextView(r.getContext());
v1.setText("Test n_"+i);
v1.setId(i+1);
r.addView(v1);
}
In fact, i would like a : android:layout_below="#id/textView1" but in Java and replace the : #id/textView1 by a getLastId() roughly said.
And more roughly, it will be something like : r.addLastView(v1)
I waiting for your answer or your comment :)
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(r.getContext());
v1.setText("Test n_"+i);
v1.setId(i+1);
params.addRule(RelativeLayout.BELOW, tv.getId()-1);
r.addView(tv);
also this is an answer for your question .... Programatically add view one below other in relative layout

Android dynamically add custom component to LinearLayout

i want add a custom component on a LinearLayout whenever i touch a button.
This is my code:
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout root = (LinearLayout) findViewById(R.id.layout_cartelle_immagini);
View custom = vi.inflate(R.layout.custom_list_folder, null);
TextView textView = (TextView) custom.findViewById(R.id.label_pathFolder);
textView.setText(pathDirectory);
Spinner spinnerLevel = (Spinner) custom.findViewById(R.id.spinner_level);
try{
root.addView(custom);
}catch(Throwable e) {
Log.e("BurgerClub", "MEX: " + e.getMessage());
e.printStackTrace();
}
In this way, only first custom component is added, why?
Thanks
edit:
I've modified my code:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
((ViewGroup) root).addView(custom, myCount, params);
This is my custom component:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/border_bottom"
android:layout_marginBottom="2dp"
android:paddingRight="20dp"
android:paddingLeft="20dp"
style="#style/Theme.BurgerClubStyle" >
<TextView
android:id="#+id/label_pathFolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:gravity="center"
android:padding="20dp"
android:textSize="25sp"
style="#style/customText" />
<Spinner
android:id="#+id/spinner_level"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/btn_show_desc_img"
android:entries="#array/n_level_array"
android:padding="20dp"
android:prompt="#string/n_level_prompt"
android:textAlignment="3"
style="#style/customText" />
<ImageButton
android:id="#+id/btn_show_desc_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/btn_remove_folder"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"
android:background="#drawable/button_press"
android:contentDescription="#string/showDescImg"
android:src="#drawable/ic_desc" />
<ImageButton
android:id="#+id/btn_remove_folder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/button_press"
android:contentDescription="#string/showDescImg"
android:src="#drawable/ic_delete" />
</RelativeLayout>
The first time that i press the button, custom component was added, but all other times NOT.
The first custom element is not overwrite, it's the only visible!
Where's your layout_cartelle_immagini declared?
Probably it's a horizontal LinearLayout, just set orientation to vertical and your code should work (considering the code to add a custom view is inside a onClickListener).
Try this. It will help you.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
root.addContentView(custom, params);

Adding editText to customView

I want to add EditText to canvas in specific coordinates.
Something like this :
I tried to use code :
LinearLayout layout = new LinearLayout(context);
EditText textView = new EditText(context);
textView.setVisibility(View.VISIBLE);
textView.setText("Hello world");
layout.addView(textView);
layout.measure(canvas.getWidth(), canvas.getHeight());
layout.layout(0, 0, canvas.getWidth(), canvas.getHeight());
layout.setGravity(Gravity.BOTTOM);
layout.draw(canvas);
But this EditText wasn't show keyboard on click. Can you help me?
Assuming you really need to draw it on a canvas...
Create a new canvaslayout.xml inside your res/layout folder.
<?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:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="5" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText1"
android:layout_toRightOf="#+id/editText1"
android:text="Edit Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Change code too
LinearLayout layout = findViewById(R.layout.canvaslayout);
//LinearLayout layout = youractivity.findViewById(R.layout.canvasLayout);
layout.draw(canvas);
Programmaticly:
If you don't want to use XML (I don't see why not) you can use this method. You change the linear layout to a relative layout and use something like this to put views to the right off another view.
RelativeLayout.LayoutParams relativeLayoutParams=
new RelativeLayout.LayoutParams((RelativeLayout.LayoutParams.WRAP_CONTENT),(RelativeLayout.LayoutParams.WRAP_CONTENT));//create params for new textview
relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF, textView.getId());//to align the textview side by side

Categories