I am new to Android and I want to draw 3 circles in a fixed position.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imgCircle = (ImageView) findViewById(R.id.imgCircle);
final ImageView imgCircle1 = (ImageView) findViewById(R.id.imgCircle1);
final ImageView imgCircle2 = (ImageView) findViewById(R.id.imgCircle2);
//first
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);
paint.setStyle(Paint.Style.FILL);
paint.setAlpha(50);
Bitmap bmp = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
canvas.drawCircle(bmp.getWidth()/2, bmp.getHeight()/2, 150, paint);
imgCircle.setImageBitmap(bmp);
(the second and the third circles have the same code as the first one)
XML:
<?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/activity_main"
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.example.andav.wisersteps_v2.MainActivity"
android:background="#drawable/foot">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher"
android:id="#+id/imgCircle2"
tools:ignore="ContentDescription"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="34dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher"
android:id="#+id/imgCircle1"
tools:ignore="ContentDescription"
android:layout_marginTop="21dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/imgCircle2"
android:layout_toEndOf="#+id/imgCircle2"
android:layout_marginLeft="18dp"
android:layout_marginStart="18dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher"
android:id="#+id/imgCircle"
tools:ignore="ContentDescription"
android:layout_marginTop="53dp"
android:layout_below="#+id/imgCircle1"
android:layout_alignLeft="#+id/imgCircle1"
android:layout_alignStart="#+id/imgCircle1" />
</RelativeLayout>
The problem is that main_activity positions (where I want to draw the circles) does not match the emulator positions:
before && after
How can I resolve this?
Related
I have a TextView with an arrow set on the right of it. I want the arrow to rotate when it is clicked and return it back to the previous position when it is clicked again to show and hide text. I have been able to show and hide the information I just can't get the rotation animation to work.
XML
<TextView
android:id="#+id/expandable_first_next_last_air_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:drawableEnd="#drawable/ic_keyboard_arrow_down_white_24dp"
android:drawableTint="?attr/textColor"
android:text="First, Next & Last Air Date"
android:textColor="?attr/textColor"
android:textSize="26sp" />
TextView OnClick Listener
tvExpandableFirstNextLastAirDate = findViewById(R.id.expandable_first_next_last_air_date);
tvExpandableFirstNextLastAirDate.setOnClickListener(v -> {
if (isTextViewClicked){
tvFirstNextLastAirDate.setMaxLines(0);
isTextViewClicked = false;
}
else{
tvFirstNextLastAirDate.setMaxLines(Integer.MAX_VALUE);
isTextViewClicked = true;
}
});
java:
final TextView textView = findViewById(R.id.text_view);
final ImageView imageView = findViewById(R.id.image_view);
final TextView textView2 = findViewById(R.id.text_view2);
final ImageView imageView2 = findViewById(R.id.image_view2);
final AnimationSet animSetUp = new AnimationSet(true);
animSetUp.setInterpolator(new DecelerateInterpolator());
animSetUp.setFillAfter(true);
animSetUp.setFillEnabled(true);
final AnimationSet animSetDown = new AnimationSet(true);
animSetDown.setInterpolator(new DecelerateInterpolator());
animSetDown.setFillAfter(true);
animSetDown.setFillEnabled(true);
final RotateAnimation animRotateUp = new RotateAnimation(0.0f, 180.0f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
animRotateUp.setDuration(1500);
animRotateUp.setFillAfter(true);
animSetUp.addAnimation(animRotateUp);
final RotateAnimation animRotateDown = new RotateAnimation(180.0f, 0.0f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
animRotateDown.setDuration(1500);
animRotateDown.setFillAfter(true);
animSetDown.addAnimation(animRotateDown);
final boolean[] isTextViewClickedForTextView1 = {true};
final boolean[] isTextViewClickedForTextView2 = {true};
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (isTextViewClickedForTextView1[0]) {
textView.setMaxLines(0);
isTextViewClickedForTextView1[0] = false;
imageView.startAnimation(animSetUp);
} else {
imageView.startAnimation(animSetDown);
textView.setMaxLines(Integer.MAX_VALUE);
isTextViewClickedForTextView1[0] = true;
}
}
});
textView2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (isTextViewClickedForTextView2[0]) {
textView2.setMaxLines(0);
isTextViewClickedForTextView2[0] = false;
imageView2.startAnimation(animSetUp);
} else {
imageView2.startAnimation(animSetDown);
textView2.setMaxLines(Integer.MAX_VALUE);
isTextViewClickedForTextView2[0] = true;
}
}
});
XML :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity">
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_margin="10dp"
android:padding="10dp"
android:text="text view 1"
android:textAlignment="viewStart"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/image_view"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:padding="5dp"
android:src="#drawable/ic_baseline_arrow_drop_down_24"
app:layout_constraintBottom_toBottomOf="#+id/text_view"
app:layout_constraintEnd_toEndOf="#+id/text_view"
app:layout_constraintTop_toTopOf="#+id/text_view" />
<TextView
android:id="#+id/text_view2"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_margin="10dp"
android:padding="10dp"
android:text="text view 2"
android:textAlignment="viewStart"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_view"/>
<ImageView
android:id="#+id/image_view2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:padding="5dp"
android:src="#drawable/ic_baseline_arrow_drop_down_24"
app:layout_constraintBottom_toBottomOf="#+id/text_view2"
app:layout_constraintEnd_toEndOf="#+id/text_view2"
app:layout_constraintTop_toTopOf="#+id/text_view2" />
</androidx.constraintlayout.widget.ConstraintLayout>
res/anim/ic_baseline_arrow_drop_down_24.xml :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="#android:anim/cycle_interpolator">
<rotate android:fromDegrees="0"
android:toDegrees="180"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000" />
</set>
I am creating a simple tasklist app, and I want to programmatically add a TextView to an embedded LinearLayout. However, it is not showing up for some reason.
I try to add the TextView in MainActivity (irrelevant code removed).
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
processExtraData();
}
private void processExtraData(){
Integer hours = 4;
Integer minutes = 35;
Integer seconds = 0;
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linlayout);
TextView txt1 = new TextView(this);
final float scale = this.getResources().getDisplayMetrics().density;
int pixels = (int) (64 * scale + 0.5f);
ViewGroup.LayoutParams params = new LinearLayout.LayoutParams(0, pixels, 1f);
txt1.setLayoutParams(params); txt1.setText(hours.toString()+":"+minutes.toString()+":"+seconds.toString());
linearLayout.addView(txt1);
}
The embedded LinearLayout is declared in activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Super Reminder App!"
android:textAppearance="#style/TextAppearance.AppCompat.Large" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:orientation="horizontal">
<EditText
android:id="#+id/taskAdd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3.5"
android:hint="New Task" />
<Button
android:id="#+id/add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:text="Add Task" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="12.5"
android:orientation="horizontal">
<ListView
android:id="#+id/tasks"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2.5"/>
<LinearLayout
android:id="#+id/linlayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical"/>
</LinearLayout>
<Button
android:id="#+id/clearButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Clear All" />
</LinearLayout>
I have already tested and made sure that onNewIntent and processExtraData are called. No errors are thrown either. Could this be a GUI issue? Any help would be greatly appreciated.
I'm trying to display layouts randomly in a view flipper (I have 4 layouts - they will be many more). This is what I have so far
public class MainActivity extends AppCompatActivity {
Random mRandom = new Random();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewFlipper simpleViewFlipper = (ViewFlipper) findViewById(R.id.simpleViewFlipper);
Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
simpleViewFlipper.setInAnimation(in);
simpleViewFlipper.setOutAnimation(out);
simpleViewFlipper.setFlipInterval(3000);
simpleViewFlipper.setAutoStart(true);
simpleViewFlipper.setDisplayedChild(mRandom.nextInt(4));
}
}
I want them to randomly show and never stop.
Please bare in mind that I'm a beginner :)
Thanks.
Where are you getting your images from? Are the images in your DB or only in SRC folder? Any attemps on the coding? Give us a little more information.
For example...
setContentView(R.layout.yourxml);
ArrayList<uploadsclass> currentuploads = new ArrayList<uploadsclass>();
currentuploads.add(new uploadsclass("MYBRAND NEW AUDI", "1.6TDI, 220 CCM, diesel", R.drawable.audi));
uploadsclassadapter addtoList = new uploadsclassadapter(this, currentuploads);
// Get a reference to the ListView, and attach the adapter to the listView.
ListView listView = (ListView) findViewById(R.id.listview_ids);
listView.setAdapter(addtoList);
yourxml.xml:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listview_ids"
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" />
your listitems.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:minHeight="?android:attr/listPreferredItemHeight">
<ImageView
android:id="#+id/oneCarImage"
android:layout_width="50dp"
android:layout_height="64dp"
android:layout_weight="0.04" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="#+id/quickInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/quickCharacteristics"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
My button component takes space of my custom view class,if i put more it takes up more space,any reason why is that
My oncreate method :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_control);
MyCustomPanel view = new MyCustomPanel(this);
DisplayMetrics metrics;
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = getDPI(500, metrics);
int width = getDPI(800,metrics);
layout = (LinearLayout) findViewById(R.id.r2) ;
layout1 = (LinearLayout) findViewById(R.id.l2);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(width,
height);
layout.addView(view,params);
}
This is my xml code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/r2"
android:gravity="bottom|start"
android:layout_gravity="bottom|start"
tools:context="company.ciso.com.wheelchair.Control">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="top|end"
android:layout_gravity="top|end"
android:id="#+id/l2"
>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/b1"
android:text="S1" />
</LinearLayout>
</LinearLayout>
these are the screen shots
Additionally is there anyway to set gravity of my custom view.??
Update your layout with this code.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/l2"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="top|end"
android:gravity="top|end"
android:orientation="horizontal">
<Button
android:id="#+id/b1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="S1" />
</LinearLayout>
<LinearLayout
android:id="#+id/r2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="horizontal"></LinearLayout>
</RelativeLayout>
I have a StartActivity which has a play button, when pressed it takes you to the MainActivty where there are 9 ImageView objects which are set to the bitmap img "aliencreature" as you will see in my xml file below. The imageview objects are to be set to random positions on startup i used display metrics and random to do this however my first problem is that they are out of bounds at times. Not all 9 imageviews show in the screen and sometimes half an imageview is shown and even if all do show up 1 or two are imageviews get smaller for some reason. I've attached my .java and xml file please tell me if you need me to be more detailed in my question.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frameLayout"
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.example.tapoo.MainActivity$PlaceholderFragment"
>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="22dp"
android:src="#drawable/aliencreature"
tools:ignore="ContentDescription" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="147dp"
android:layout_marginLeft="100dp"
android:src="#drawable/aliencreature"
tools:ignore="ContentDescription" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="108dp"
android:layout_marginLeft="200dp"
android:src="#drawable/aliencreature"
tools:ignore="ContentDescription" />
<ImageView
android:id="#+id/imageView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:layout_marginLeft="170dp"
android:src="#drawable/aliencreature"
tools:ignore="ContentDescription" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="300dp"
android:layout_marginLeft="108dp"
android:src="#drawable/aliencreature"
tools:ignore="ContentDescription" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="16dp"
android:layout_marginLeft="740dp"
android:src="#drawable/aliencreature"
tools:ignore="ContentDescription" />
<ImageView
android:id="#+id/restartButton"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="#drawable/restartbutton"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/countDownTimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Seconds remaining: 10"
tools:ignore="HardcodedText" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="350dp"
android:layout_marginLeft="188dp"
android:src="#drawable/aliencreature"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/scoreNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:visibility="invisible"
android:textSize="19sp" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:layout_marginLeft="188dp"
android:src="#drawable/aliencreature"
tools:ignore="ContentDescription" />
<ImageView
android:id="#+id/imageView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="250dp"
android:layout_marginTop="350dp"
android:src="#drawable/aliencreature"
tools:ignore="ContentDescription" />
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent intent = getIntent();
setContentView(R.layout.fragment_main);
counter = 0;
score = (TextView) findViewById(R.id.scoreNumber);
countDownTimer = (TextView) findViewById(R.id.countDownTimer);
view1 = (ImageView) findViewById(R.id.imageView1);
view2 = (ImageView) findViewById(R.id.imageView2);
view3 = (ImageView) findViewById(R.id.imageView3);
view4 = (ImageView) findViewById(R.id.imageView4);
view5 = (ImageView) findViewById(R.id.imageView5);
view6 = (ImageView) findViewById(R.id.imageView6);
view7 = (ImageView) findViewById(R.id.imageView7);
view8 = (ImageView) findViewById(R.id.imageView8);
view9 = (ImageView) findViewById(R.id.imageView9);
allImages = new ArrayList<ImageView>();
allImages.add(view1);
allImages.add(view2);
allImages.add(view3);
allImages.add(view4);
allImages.add(view5);
allImages.add(view6);
allImages.add(view7);
allImages.add(view8);
allImages.add(view9);
for(final ImageView views : allImages){ //allimages is an arraylist of imageviews that contains my 9 imageviews
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) views.getLayoutParams();
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
int height = displaymetrics.heightPixels ;
Random r = new Random();
params.leftMargin = r.nextInt(width-48) ; // 48 is the width of my bitmap img
params.topMargin = r.nextInt(height-50) ; //50 is the height of my bitmap img
views.setLayoutParams(params);
If you check your value in the file /value/dimen of your project you will find that hte following
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
are not null. So due to your RelativeLayout definition in your main layout you will find that the drawable area is smaller than the entire screen so
params.leftMargin = r.nextInt(width-48) ; // 48 is the width of my bitmap img
params.topMargin = r.nextInt(height-50)
won't be sufficient.
My suggestion is to remove the padding definition in your RelativeLayout, like this your images will always be on the screen.
Second thing, remove in your ImageView definition
tools:ignore="ContentDescription"
After these corrections everything would be fine;)