Android: Programatically rotating the text inside a button - java

{Visual Aid}
I am making an application with a drag and drop menu. User can fill out three edit texts (width, height, and rotation) then press on "add new" button and a new button with the specified values is created at the origin. However if the rotation attribute is anything other than 0 say 45 degrees, then it rotates the whole button along with the text. I would like for the text to remain horizontal with no rotation. I have looked up but the post all refer back to the rotation attribute which i am already using.
onViewCreated()
tvAddTable = view.findViewById(R.id.btn_add_table);
tvWidth = view.findViewById(R.id.et_width);
tvHeight = view.findViewById(R.id.et_height);
tvRotation = view.findViewById(R.id.et_rotation);
mSize = new Point();
mDisplay = Objects.requireNonNull(getActivity()).getWindowManager().getDefaultDisplay();
mDisplay.getSize(mSize);
final View.OnTouchListener touchListener = this;
tvAddTable.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {[enter image description here][1]
ConstraintLayout layout = view.findViewById(R.id.floor_layout);
//set the properties for button
Button btnTag = new Button(getContext());
btnTag.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
if(ids.isEmpty()){
ids.add(0);
}else{
ids.add(ids.size());
}
btnTag.setId(ids.size());
btnTag.setText(Integer.toString(btnTag.getId()));
//add button to the layout
layout.addView(btnTag);
btnTag.setLayoutParams(new ConstraintLayout.LayoutParams(Integer.parseInt(tvWidth.getText().toString()), Integer.parseInt(tvHeight.getText().toString())));
btnTag.setRotation(Integer.parseInt(tvRotation.getText().toString()));
btnTag.setOnTouchListener(touchListener);
}
});

The rotation attribute rotates the entire view, not just the text or just the background. That's by design. There is no text or background rotation attribute. If you need that, make a custom view.
If you only want the background to rotate, you can probably do it by setting the background drawable to a RotateDrawable that wraps the background you want.

Related

how do I drawline on canvas with a button press

The Title says it all. drawline works outside the setOnClick Listener, but not inside, so I can set it to run with a button press.
//Bitmap Object creation
bitmap = Bitmap.createBitmap(600,800,Bitmap.Config.RGB_565);
//Create Canvas object and set to bitmap variable
canvas = new Canvas(bitmap);
canvas.drawColor(Color.BLACK);
paint.setColor(Color.BLUE);
paint.setStrokeWidth(50);
imageView.setImageBitmap(bitmap);
up.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
paint.setColor(Color.BLUE);
canvas.drawLine(100,100,100,200,paint);
}
});
Something else I want to look into is drawing multiple lines, and making sure each one lines up to the tip of the last one drawn, similar to this:
This will work for multiple buttons, but I only need it for one button right now. I hope this is sufficient information, please let me know if more is needed

Popover hint for imageview

Please take a look at image.
I am littile bit confused how to implement this pop over view on an Image view. Actually the design is an stepper module. so once the user choose an option, the layout showing next step. So each step should have an Image View. I implemented the stepper layout, but have some confusion on how to set the popover on each steps. ie just below to the imageview.
generated dynamic image view for showing steps.
for (int i = 0; i < CreationData.size(); i++) {
imageContainer[i] = new ImageView(getContext());
imageContainer[i].setImageResource(getResId(i, 0));// 0 determines the state of the image icon.
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(100, 100);
imageContainer[i].setLayoutParams(param);
dividerLine[i] = new View(getContext());
dividerLine[i].setBackgroundColor(getResources().getColor(R.color.stepper_line_color));
}
.
Next I want to append the pop over to each image view
Maybe using setOnLongClickListener on the ImageView help you:
myImageView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
Toast.makeText(context,"Text to show", Toast.LENGTH_LONG).show();
return true;
}
});
In the xml set the ImageView long-clickable:
<ImageButton
...
android:longClickable="true"
/>
Or you can do the same via code:
myImageView.setLongClickable(true);

How to set the size and the position of a dialog button libGDX

I want to have a pop up window in my app. Therefore I wrote the following code:
public void show() {
skin4 = new Skin (Gdx.files.internal("NoAdsButton.json"));
button4 = new Button(skin4);
button4.setPosition(25,1250);
button4.setSize(120,120);
button4.addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y) {
skin5 = new Skin(Gdx.files.internal("Test.json"));
dialog = new Dialog("", skin5);
dialog.setTransform(true);
dialog.button("REMOVE ADS: 1.99$");
dialog.button("UNLOCK ALL MODES: 1.99$");
dialog.setSize(700,500);
dialog.setPosition(50,400);
stage.addActor(dialog);
}
});
stage.addActor(button4);
}
Unfortunately, dialog.setSize() only stretches the dialog but not the buttons. I also don´t know how to set the position of the two buttons and how to change the size of the text which is in the buttons. Could somebody help me to reach that?
Create a custom dialog
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_layout);
textView = (TextView) dialog.findViewById(R.id.tv); //dialog component
dialog.setTitle("Title"); //set Title
after then u can use the component just like normal component. don't forget to dismiss the dialog when it need to be dismissed.
dialog.dismiss();

Android Button Not moving onclick

I have an up button which I want to move up onClick but when I click on it it moves way to far and gets to the end of the screen and then shrinks down untill you cant see it. It moves up too much, but why? I only increase it by one unit?
final Button upbutton = (Button)findViewById(R.id.upbutton);
upbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams)
upbutton.getLayoutParams();
mParams.bottomMargin += 1;
upbutton.setLayoutParams(mParams);
}
});
}
Because you're not assigning mParams to your buttons params.
mParams = upbutton.getLayoutParams();
You seem to be increasing parameters for your RelativeLayout then assigning those to your button. So the button gets confused. Try looking for a set margin option or something on the actual button view.

Adding buttons dynamically to linearView

I'm trying to write an application that’s adding buttons dynamically.
The application receives an image from the server and adding a button (no functionality for now).
The main activity is holding linear layout which will contain the buttons and a surface view that would receive the images and would create the buttons in the main activity.
The problem is that after the first image has been received the application crash while trying to add a new button to the view.
This is the surface view code:
public void run() {
while ( isRunning){
if ( !ourHolder.getSurface().isValid()){
continue;
}
Canvas canvas = ourHolder.lockCanvas(); // Semafor for the canvas
canvas.drawRGB(20,20,80);
if (getNumOfBoards() > 0){
canvas.drawBitmap(getCurrentBoard(), 0, 0, null);
}
ourHolder.unlockCanvasAndPost(canvas);
}
}
This is the Main Activity add button code
public void addButtons(int numOfButton) {
// create patameter
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
// create new button
Button newbutton = new Button(this);
// set background color
newbutton.setBackgroundColor(Color.BLUE);
// set width and height
newbutton.setWidth(30);
newbutton.setHeight(20);
// set position
newbutton.setY((float)numOfButton*20);
newbutton.setX(0);
// set text
newbutton.setText("new button");
// add button to the layout
buttons.addView(newbutton,p);// **the application crash here**
}
Due to the way android handles the Activities, the constructor onCreate is not guaranteed to have been called every time the addButtons method is executed
(See the Activity Lifecycle Section at developer.android.com)
You had better find the buttons LinearLayout
LinearLayout buttons = (LinearLayout) findViewById(R.id.LayoutButtons);
in addButtons and add the new button.

Categories