I want to use CircularLayout in my android app.
I'm getting following error in MainActivity.java file:
cannot resolve method onCreate(android.os.Bundle) even though I have extended AppCompatActivity.
As I need to access methods from both AppCompatActivity and View, I've used nested classes and the error is probably due of that. Not sure how to use that. I'm also the following warning: private inner class Activity is never used. Please let me know to extend two classes. I've found several answers and used them including Extending from two classes and How to extend 2 classes? but none of them seems to work for me. I would like to know what changes can I make in my code for it to work.
This is how my MainActivity.java file looks like:
package com.example.shalini.circlelayout;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
private class Activity extends View {
private final static int TOTAL_DEGREE = 360;
private final static int START_DEGREE = -90;
private Paint mPaint;
private RectF mOvalRect = null;
private int mItemCount = 5;
private int mSweepAngle;
private int mInnerRadius;
private int mOuterRadius;
private Bitmap mCenterIcon;
private int[] mColors = {Color.RED, Color.YELLOW, Color.GREEN, Color.BLUE, Color.CYAN};
private String[] mTitles = {"APPT CENTER", "MEDS CABINET", "CHECK-IN", "MY TRACKERS", "MY ACCOUNTS"};
public Activity(Context context) {
this(context, null);
}
public Activity(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public Activity(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStrokeWidth(2);
mSweepAngle = TOTAL_DEGREE / mItemCount;
mInnerRadius = 125;
mOuterRadius = 400;
mCenterIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
}
#Override
protected void onDraw(Canvas canvas) {
int width = getWidth();
int height = getHeight();
if (mOvalRect == null) {
mOvalRect = new RectF(width / 2 - mOuterRadius, height / 2 - mOuterRadius, width / 2 + mOuterRadius, height / 2 + mOuterRadius);
}
for (int i = 0; i < mItemCount; i++) {
int startAngle = START_DEGREE + i * mSweepAngle;
mPaint.setColor(mColors[i]);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawArc(mOvalRect, startAngle, mSweepAngle, true, mPaint);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawArc(mOvalRect, startAngle, mSweepAngle, true, mPaint);
int centerX = (int) ((mOuterRadius + mInnerRadius) / 2 * Math.cos(Math.toRadians(startAngle + mSweepAngle / 2)));
int centerY = (int) ((mOuterRadius + mInnerRadius) / 2 * Math.sin(Math.toRadians(startAngle + mSweepAngle / 2)));
canvas.drawBitmap(mCenterIcon, width / 2 + centerX - mCenterIcon.getWidth() / 2, height / 2 + centerY - mCenterIcon.getHeight() / 2, null);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawText(mTitles[i], width / 2 + centerX - mCenterIcon.getWidth() / 2, height / 2 + centerY + mCenterIcon.getHeight(), mPaint);
}
mPaint.setColor(Color.WHITE);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawCircle(width / 2, height / 2, mInnerRadius, mPaint);
canvas.drawBitmap(mCenterIcon, width / 2 - mCenterIcon.getWidth() / 2, height / 2 - mCenterIcon.getHeight() / 2, null);
super.onDraw(canvas);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And this is my content_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/lib/com.google.custom"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main" tools:context=".MainActivity">
<com.example.shalini.circlelayout.MainActivity.Activity
android:id="#+id/pie"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
custom:dividerWidth="5dp"
custom:innerCircle="#drawable/profile_pic_icon"
custom:innerRadius="50dp"
custom:layoutMode="pie"
custom:sliceDivider="#android:color/transparent" >
<RelativeLayout
android:id="#+id/appt_center_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher1" >
<TextView
android:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/appcenter"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher2" >
<TextView
android:id="#+id/two"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/medscabinet"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher3" >
<TextView
android:id="#+id/three"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/cjeckin"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher4" >
<TextView
android:id="#+id/four"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/mytrackers"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher5" >
<TextView
android:id="#+id/five"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/myaccounts"
android:textStyle="bold" />
</RelativeLayout>
</com.example.shalini.circlelayout.MainActivity.Activity>
</RelativeLayout>
I would like to suggest you to call your subclass of View in a more meaningfully way. In your case
private class Activity extends View {
Regarding onCreate, which is the source of your doubts, you put it inside your View's subclass. Since View has no method called onCreate, the compiler complains about it. The solution is to move the method, and its content in the outer class, the one that extends Activity
your subclass of View is inner and private. I doubt you will be ever able to reference it in your xml
Related
i have a textView in SliderView like this
activitity_slider.xml
<androidx.viewpager.widget.ViewPager
android:id="#+id/slideViewPager"
android:layout_width="match_parent"
slide_layout.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="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_gravity="center"
android:background="#color/white">
<LinearLayout
android:id="#+id/HareketEkranı"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_marginBottom="10dp"
android:orientation="vertical"
>
<androidx.cardview.widget.CardView
android:id="#+id/hareket"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
app:cardBackgroundColor="#color/white"
app:cardCornerRadius="10dp">
<ImageView
android:id="#+id/HareketResmi"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:contentDescription="Hareket"/>
//android:src="#drawable/ic_armcircle"
<com.jjoe64.graphview.GraphView
android:id="#+id/haraketdatasi"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Hareket"
android:layout_weight="1"
android:textColor="#color/darkTextColor"
android:textSize="24sp" />
<TextView
android:id="#+id/HareketAdi"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textAlignment="gravity"
android:gravity="right"
android:text="Armcircle"
android:textColor="#color/red"
android:layout_weight="1"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="#+id/TekrarYazisiSlide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Tekrar"
android:layout_weight="1"
android:textColor="#color/darkTextColor"
android:textSize="24sp" />
<TextView
android:id="#+id/TekrarSayisiSlide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:text="TekrarSayisi"
android:textAlignment="gravity"
android:gravity="right"
android:layout_weight="1"
android:textColor="#color/red"
android:textSize="24sp" />
<TextView
android:id="#+id/kesme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:text="/"
android:layout_weight="1"
android:textColor="#color/black"
android:textSize="24sp" />
<TextView
android:id="#+id/TekrarhedefiSlide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:text="25"
android:layout_weight="1"
android:textColor="#color/black"
android:textSize="24sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</RelativeLayout>
UPDATE !
SliderAdapter.java
package gymholix.assistx;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
public class SliderAdapter extends PagerAdapter {
Context context;
LayoutInflater layoutInflater;
public SliderAdapter(Context context){
this.context = context;
}
//Arrays
public int[] slide_images = {
R.drawable.ic_armcircle,
R.drawable.ic_ropejump,
R.drawable.ic_jumpingjack,
R.drawable.ic_burpee,
R.drawable.ic_squat
};
public String[] slide_headings = {
"ArmCircle",
"RopeJump",
"JumpingJack",
"Burpee",
"Squat"
};
/*public String[] tekrar_sayisi = {
"0",
"0",
"0",
"0",
"0"*/
public int[] tekrar_sayisi = {
1,
2,
3,
4,
5
};
#Override
public int getCount() {
return slide_headings.length;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == (RelativeLayout) object;
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view =layoutInflater.inflate(R.layout.slide_layout, container, false);
ImageView slideImageView = (ImageView) view.findViewById(R.id.HareketResmi);
TextView slideHeading = (TextView) view.findViewById(R.id.HareketAdi);
TextView slideTekraSayisi = (TextView) view.findViewById(R.id.TekrarSayisiSlide);
slideImageView.setImageResource((slide_images[position]));
slideHeading.setText(slide_headings[position]);
slideTekraSayisi.setText(String.valueOf(tekrar_sayisi[position]));
container.addView(view);
return view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
}
}
Slider.Java
package gymholix.assistx;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Slider extends AppCompatActivity {
private LinearLayout mDotLayout;
private String asd;
private int asdf;
TextView DenemeSayiCek;
int position;
View view;
int count;
View viewFix;
Context context;
//Sensors---------------------------------------------------------------------------------------
private Accelerometer accelerometer;
private Gyroscope gyroscope;
public double[] acc={3.00,2.00,1.00};
public double[] gyr={3.00,2.00,1.00};
//Sensors---------------------------------------------------------------------------------------
//Main------------------------------------------------------------------------------------------
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Antreman antreman = new Antreman();
context = this;
setContentView(R.layout.activity_slider);
ViewPager AktifHareket = findViewById(R.id.slideViewPager);
mDotLayout = findViewById(R.id.dotsLayout);
SliderAdapter sliderAdapter = new SliderAdapter(this);
AktifHareket.setAdapter((sliderAdapter));
addDotsIndicator();
//Saydir Buton------------------------------------------------------------------------------
TextView Deneme = findViewById(R.id.deneme);
TextView Deneme4 = findViewById(R.id.deneme4);
TextView Deneme3 = findViewById(R.id.deneme3);
TextView Deneme2 = findViewById(R.id.deneme2);
Button Saydir = findViewById(R.id.SaydirSlide);
Button Saydir2 = findViewById(R.id.Saydir2Slide);
Saydir.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
position = AktifHareket.getCurrentItem();
view = AktifHareket.getChildAt(position);
count = AktifHareket.indexOfChild(view);
viewFix = AktifHareket.getChildAt(count);
DenemeSayiCek = viewFix.findViewById(R.id.TekrarSayisiSlide);
asd = DenemeSayiCek.getText().toString();
asdf = Integer.parseInt(asd);
asdf++;
DenemeSayiCek.setText(String.valueOf(asdf));
Deneme.setText(String.valueOf(asd));
Deneme2.setText(String.valueOf(position));
Deneme3.setText(String.valueOf(asdf));
Deneme4.setText(String.valueOf(SliderAdapter.POSITION_NONE));
}
});
//Saydir Buton------------------------------------------------------------------------------
//ImageButton-------------------------------------------------------------------------------
ImageView logoImage = findViewById(R.id.logo);
logoImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
antreman.AntremanSayfasi(context);
}
});
antreman.AntremanSayfasi(context);//Açılışta Sayfayı açsın diye
//ImageButton-------------------------------------------------------------------------------
//Sensors-----------------------------------------------------------------------------------
accelerometer = new Accelerometer(this);
gyroscope = new Gyroscope(this);
accelerometer.setListner(new Accelerometer.Listner() {
#Override
public void onTranslation(float ax, float ay, float az) {
setAccValue(ax, ay, az);
Antreman.GetSensorValues.OnAccelerometerChangeValues = acc;
}
});
gyroscope.setListner(new Gyroscope.Listner() {
#Override
public void onRotation(float gx, float gy, float gz) {
setGyroValue(gx, gy, gz);
Antreman.GetSensorValues.OnGyroscopeChangeValues = gyr;
//OnSensorChangeValues.add(1, String.valueOf(gyr) );
}
});
/* Saydir2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});*/
//Sensors-----------------------------------------------------------------------------------
}
//Sensors---------------------------------------------------------------------------------------
protected void onResume(){
super.onResume();
accelerometer.register();
gyroscope.register();
}
protected void onPouse(){
super.onPause();
accelerometer.unregister();
gyroscope.unregister();
}
//Sensors---------------------------------------------------------------------------------------
//SensorsValueGetAndSet-------------------------------------------------------------------------
public double[] setAccValue(float ac, float bc, float cc){
this.acc[0] = ac;
this.acc[1] = bc;
this.acc[2] = cc;
return acc;
}
public double[] setGyroValue(float qq, float wq, float eq){
this.gyr[0] = qq;
this.gyr[1] = wq;
this.gyr[2] = eq;
return gyr;
}
public void getAccValue(float ac, float bc, float cc){
ac = (float) acc[0];
bc = (float) acc[1];
cc = (float) acc[2];
}
public void getGyroValue(float qq, float wq, float eq){
qq = (float) gyr[0];
wq = (float) gyr[1];
eq = (float) gyr[2];
}
//SensorsValueGetAndSet-------------------------------------------------------------------------
public void addDotsIndicator(){
TextView[] mDots = new TextView[3];
for(int i = 0; i < mDots.length; i++){
mDots[i] = new TextView(this);
mDots[i].setText(Html.fromHtml("•"));
mDots[i].setTextSize(35);
mDots[i].setTextColor(getResources().getColor(R.color.colorPrimaryDark));
mDotLayout.addView(mDots[i]);
}
}
//----------------------------------------------------------------------------------------------
}
the weird thing about this code is; while running the button changes the text value correctly in the first pager, and the seckond one but when it comes the third one while it changes the value it resets the first pagers shown value but the getText() method inherits the correct value but the text is frozen and it cant be changed any more, after that the other page's values cant be changed either, but the getText() method still works fine and gets the correct value.
any idea will speed up my debuging process thanx anyway...
adding this snipped solved the problem, it is a must to define borders to the pager
#Override
protected void onCreate(Bundle savedInstanceState) {
...
int size = sliderAdapter.slide_headings.length;
AktifHareket.setOffscreenPageLimit(size);
I'm trying to mark out 4 points on my ImageView using 4 dots. When I move one of the views, the other one moves automatically. The reason is because I'm mapping them to each other. But if I don't map them to each other, they just overlap which looks really ugly(not that it looks any less now). Please help me separate these 4 layouts. I've been banging my head against the wall for a while now. I've attached by XML and JAVA code below.
activity_mail.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutContainer"
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">
<ImageView
android:id="#+id/imgMapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/test"/>
<LinearLayout
android:id="#+id/layoutTopLeftMarker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top Left"
android:textSize="16sp" />
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="right"
android:src="#drawable/tag" />
</LinearLayout>
<LinearLayout
android:id="#+id/layoutTopRightMarker"
android:layout_toRightOf="#id/layoutTopLeftMarker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top Right"
android:textSize="16sp" />
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/tag" />
</LinearLayout>
<LinearLayout
android:id="#+id/layoutBottomLeftMarker"
android:layout_below="#id/layoutTopLeftMarker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bottom Left"
android:textSize="16sp" />
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/tag"
android:layout_gravity="right"/>
</LinearLayout>
<LinearLayout
android:id="#+id/layoutBottomRightMarker"
android:layout_below="#id/layoutTopRightMarker"
android:layout_toRightOf="#id/layoutBottomLeftMarker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bottom Right"
android:textSize="16sp" />
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/tag" />
</LinearLayout>
</RelativeLayout>
MainActivity.java
package com.example.pointmapperpoc;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
private ImageView imgMapper;
private LinearLayout layoutTopLeftMarker,layoutTopRightMarker,layoutBottomLeftMarker,layoutBottomRightMarker;
private int _yDeltaTopRight;
private int _xDeltaTopRight;
private int _yDeltaTopLeft;
private int _xDeltaTopLeft;
private int _xDeltaBottomLeft;
private int _yDeltaBottomLeft;
private int _xDeltaBottomRight;
private int _yDeltaBottomRight;
#SuppressLint("ClickableViewAccessibility")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgMapper = findViewById(R.id.imgMapper);
layoutTopRightMarker = findViewById(R.id.layoutTopRightMarker);
layoutTopLeftMarker = findViewById(R.id.layoutTopLeftMarker);
layoutBottomRightMarker = findViewById(R.id.layoutBottomRightMarker);
layoutBottomLeftMarker = findViewById(R.id.layoutBottomLeftMarker);
layoutTopLeftMarker.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
_xDeltaTopLeft = X - lParams.leftMargin;
_yDeltaTopLeft = Y - lParams.topMargin;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
layoutParams.leftMargin = X - _xDeltaTopLeft;
layoutParams.topMargin = Y - _yDeltaTopLeft;
v.setLayoutParams(layoutParams);
break;
}
return true;
}
});
layoutTopRightMarker.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
_xDeltaTopRight = X - lParams.leftMargin;
_yDeltaTopRight = Y - lParams.topMargin;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
layoutParams.leftMargin = X - _xDeltaTopRight;
layoutParams.topMargin = Y - _yDeltaTopRight;
v.setLayoutParams(layoutParams);
break;
}
return true;
}
});
layoutBottomLeftMarker.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
_xDeltaBottomLeft = X - lParams.leftMargin;
_yDeltaBottomLeft = Y - lParams.topMargin;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
layoutParams.leftMargin = X - _xDeltaBottomLeft;
layoutParams.topMargin = Y - _yDeltaBottomLeft;
v.setLayoutParams(layoutParams);
break;
}
return true;
}
});
layoutBottomRightMarker.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
_xDeltaBottomRight = X - lParams.leftMargin;
_yDeltaBottomRight = Y - lParams.topMargin;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
layoutParams.leftMargin = X - _xDeltaBottomRight;
layoutParams.topMargin = Y - _yDeltaBottomRight;
v.setLayoutParams(layoutParams);
break;
}
return true;
}
});
}
}
here is the idea manipulate their x and y, instead of margins. when you set your layout params your mapping atomatically gets updated and thats why they are following each other. if you set x and y of the view they will still move but their layout params wont be updated.
I have a simple layout that becomes visible once a drag event starts:
<RelativeLayout
android:id="#+id/onDragMenu"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="blablabla"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginTop="75dp"
android:textAlignment="center"
/>
<ImageView
android:id="#+id/smsDragButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="25dp"
android:layout_marginBottom="25dp"
android:src="#mipmap/sms_icon"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:background="#drawable/rounded_button"
android:padding="5dp"
/>
<ImageView
android:id="#+id/callDragButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="25dp"
android:layout_marginBottom="25dp"
android:src="#mipmap/phone_icon"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:background="#drawable/rounded_button"
android:padding="5dp"
/>
</RelativeLayout>
And some code to handle what happens when something is dragged onto one of the image views:
View callButton = onDragMenu.findViewById(R.id.callDragButton);
callButton.setOnDragListener(new View.OnDragListener() {
#Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()){
case DragEvent.ACTION_DRAG_ENTERED:
// blablabla
break;
case DragEvent.ACTION_DRAG_ENDED:
// blablabla
break;
}
return true;
}
});
However, onDrag is triggered the second I start the drag(outside the buttons), with ACTION_DRAG_STARTED, and ACTION_DRAG_ENTERED is never hit.
Finally, when I let go, ACTION_DRAG_ENDED is hit, with coordinates (0,0).
I'm quite new to Android, and probably missing something basic.
Please help.
Thanks
Set the visibility of Relative layout to INVISIBLE instead of gone.
Instead of Using Drag event,Use onTouch drag event.
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity implements View.OnTouchListener {
private ImageView mImageView;
private ViewGroup mRrootLayout;
private int _xDelta;
private int _yDelta;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRrootLayout = (ViewGroup) findViewById(R.id.root);
mImageView = (ImageView) mRrootLayout.findViewById(R.id.imageView);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 150);
mImageView.setLayoutParams(layoutParams);
mImageView.setOnTouchListener(this);
}
public boolean onTouch(View view, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setLayoutParams(layoutParams);
break;
}
mRrootLayout.invalidate();
return true;
}
}
Creating View layout activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/activity_vertical_margin" >
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="124dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
I have two TouchImageView. What I want to achieve is that, When I swipe first TouchImageView from left to right, second TouchImageView get swiped right to left automatically. I am using TouchImageView.java class which I have founded at GitHub. Below is my code.
activity_main.xml
<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:gravity="center"
android:orientation="horizontal"
android:weightSum="2"
tools:context="com.PinchZoom.pinchzoomexampletwo.MainActivity" >
<com.PinchZoom.pinchzoomexampletwo.TouchImageView
android:id="#+id/img_to_be_zoomed"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="1"
android:src="#drawable/fb" >
</com.PinchZoom.pinchzoomexampletwo.TouchImageView>
<com.PinchZoom.pinchzoomexampletwo.TouchImageView
android:id="#+id/img_to_be_zoomed_mirror"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="1"
android:src="#drawable/fb" >
</com.PinchZoom.pinchzoomexampletwo.TouchImageView>
</LinearLayout>
MainActivity.java
package com.PinchZoom.pinchzoomexampletwo;
import android.graphics.PointF;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import com.PinchZoom.pinchzoomexampletwo.TouchImageView.OnTouchImageViewListener;
public class MainActivity extends ActionBarActivity {
TouchImageView img_to_be_zoomed;
TouchImageView img_to_be_zoomed_mirror;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img_to_be_zoomed = (TouchImageView) findViewById(R.id.img_to_be_zoomed);
img_to_be_zoomed
.setOnTouchImageViewListener(new OnTouchImageViewListener() {
#Override
public void onMove() {
img_to_be_zoomed_mirror.setZoom(img_to_be_zoomed);
Log.e("X Scroll Point: ",
"" + img_to_be_zoomed.getScrollPosition());
}
});
img_to_be_zoomed_mirror = (TouchImageView) findViewById(R.id.img_to_be_zoomed_mirror);
img_to_be_zoomed_mirror
.setOnTouchImageViewListener(new OnTouchImageViewListener() {
#Override
public void onMove() {
// TODO Auto-generated method stub
img_to_be_zoomed.setZoom(img_to_be_zoomed_mirror);
PointF s = new PointF();
PointF q = new PointF();
s = img_to_be_zoomed.getScrollPosition();
q = img_to_be_zoomed_mirror.getScrollPosition();
img_to_be_zoomed.setScrollPosition(s.x
- (q.x - img_to_be_zoomed.getX()), s.y);
Log.e("img_1", s.x + "/" + s.y);
Log.e("img_2", "" + q.x + "/" + q.y);
}
});
}
}
TouchImageView.java
TouchImageView.java Find Here
Edit:
I have solved problem using below code in MainActivity.java.
PointF pointF_img1 = new PointF();
pointF_img1 = img_to_be_zoomed.getScrollPosition();
img_to_be_zoomed_mirror.setScrollPosition(1 - pointF_img1.x, pointF_img1.y);
Try this to get the center point of an imageview.
int width=Bitmap.getWidth();
int height=Bitmap.getHeight();
int centerX=width/2;
int centerY=height/2;
I have solved problem using below code in MainActivity.java.
PointF pointF_img1 = new PointF();
pointF_img1 = img_to_be_zoomed.getScrollPosition();
img_to_be_zoomed_mirror.setScrollPosition(1 - pointF_img1.x, pointF_img1.y);
Here is the bellow code and in want to make a zoom in and out facilities by using the zoom in and out button form android.
public class ShowDescription extends Activity{
#Override
public void onCreate(Bundle savedInstanceState){
new LoadDetails(this).execute();
super.onCreate(savedInstanceState);
setContentView(R.layout.showdescription);
String theStory = null;
String pubDate = null;
String storyTitle = null;
String writer = null;
//String test = null;
Intent intent = getIntent();
if (intent != null) {
Bundle b = intent.getExtras();
if (b == null) {
theStory = "bad bundle?";
} else {
storyTitle =ComplexScript.UTF2ANSI(ComplexScript.swap(b.getString("title")))+"\n\n";
//pubDate=ComplexScript.UTF2ANSI(ComplexScript.swap(ComplexScript.Replace(b.getString("pubdate"))))+ "\n\n";
pubDate=ComplexScript.Replace(b.getString("pubdate"))+"\n";
writer=ComplexScript.UTF2ANSI(ComplexScript.swap(b.getString("writers")))+",\n"+ComplexScript.UTF2ANSI(ComplexScript.swap(b.getString("initial")))+"\n";
theStory =ComplexScript.UTF2ANSI(ComplexScript.swap(b.getString("description"))).replace('\n', ' ').replaceAll(" "," ").replaceAll("‘","Ô").replaceAll("’","Õ");
//+ "\n\nMore information:\n" + b.getString("link");
//test=b.getString("image");
}
} else {
theStory = "Information not found.";
}
//Bitmap bimage= getBitmapFromURL(test);
//iv.setImageBitmap(bimage);
Typeface tf = Typeface.createFromAsset(getAssets(),
"font/SutonnyMJ.ttf");
TextView story = (TextView)findViewById(R.id.storybox);
story.setText(Html.fromHtml("<h1><font color='#B10000'>"+storyTitle+"</font></h1><small>"+pubDate+"<br/>"+writer+"</small><p>"+theStory+"</p>"));
story.setTypeface(tf);
story.setLineSpacing(1,(float) 1.5);
TextView moto =(TextView)findViewById(R.id.moto);
moto.setText("msev` we‡bv`b mviv¶Y");
moto.setTypeface(tf);
Button backButton = (Button)findViewById(R.id.back);
backButton.setTypeface(tf);
TextView tv = (TextView) findViewById(R.id.footer);
tv.setTypeface(tf);
backButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
finish();
}
});
}}
And here below is the XML file
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFF" android:orientation="vertical">
<ImageView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#86878A"
android:contentDescription="#string/app_name"
android:src="#drawable/logobn24"/>
<TextView android:id="#+id/moto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#86878A"
android:textColor="#FFF"
android:gravity="center"/>
<TextView android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#86878A"/>
<ScrollView android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFF"
android:orientation="vertical" >
<!-- android:textColor="#4e4e4e" -->
<ImageView android:id="#+id/img"
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:contentDescription="#string/des"/>
<TextView android:id="#+id/storybox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/img"
android:background="#FFFFFF"
android:paddingLeft="10dp"
android:textColor="#000"
android:textSize="18dp"
android:paddingTop="10dp"/>
</RelativeLayout>
</ScrollView>
<Button android:id="#+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/back"/>
<TextView android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#86878A"
android:gravity="center_vertical|center_horizontal"
android:text="#string/footer"
android:textColor="#FFF" android:lineSpacingMultiplier="1.3"/>
Can any one make a zoom in and zoom out facilities udder this class? or just increase or decrease the text size of Text View story? Can any one help me?
What happens if you call the methods of TextView?
public void setScaleX(float scaleX)
public void setScaleY(float scaleY)
This may help you...
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.TextView;
public class ResizableTextViewW extends TextView {
private static final int INVALID_POINTER_ID = -1;
public static final String TAG = ResizableTextViewW.class.getSimpleName();
private float mLastTouchX;
private float mLastTouchY;
private int mActivePointerId = INVALID_POINTER_ID;
private ScaleGestureDetector mScaleDetector;
private float mScaleFactor = 1.f;
private int count = 0;
public float mPreviousScaleFactor = 1.f;
public ResizableTextViewW(Context context) {
this(context, null, 0);
}
public ResizableTextViewW(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ResizableTextViewW(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
// Let the ScaleGestureDetector inspect all events.
mScaleDetector.onTouchEvent(ev);
return true;
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
this.setText(String.valueOf(count));
this.setTextSize(10+count);
}
private class ScaleListener extends
ScaleGestureDetector.SimpleOnScaleGestureListener {
#Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
// Don't let the object get too small or too large.
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));
if (mScaleFactor > mPreviousScaleFactor) {
count = count + 1;
invalidate();
} else if (mScaleFactor < mPreviousScaleFactor) {
if (count > 0) {
count = count - 1;
invalidate();
}
}
mPreviousScaleFactor = mScaleFactor;
return true;
}
}