Using multiple views in android - android

So I have the following onCreate function
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mylocation = new MyLocation(this);
double distance = distance (mylocation.lat, mylocation.lng, messagelat, messagelong);
Log.d("Distance",Double.toString(distance));
mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
mView = new SampleView(this,Double.toString(distance));
**this.setContentView(R.layout.activity_clouds);**
this.button = (Button)this.findViewById(R.id.close);
this.button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(Activity1.this, CameraPreview.class);
Activity1.this.startActivity(myIntent);
}
});
**setContentView(mView);**
}
My problem is that I want to use both view the one from activity_cloud.xml and the SampleView but If I do it this way I only get the sample view! I want to add a button over and overlay that is implemented in the SampleView class... Thanks a lot

You can use your custom view within regular xml layouts.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<com.something.something.SampleView
android:id="#+id/sample_view"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
<Button android:layout_height="0dp"
android:layout_width="fill_parent"
android:layout_weight="1"
android:id="#+id/close"/>

Related

When using ViewPropertyAnimation, property of the object does not change

I have a test code using the View Property Animation to switch locations of two buttons. The code is as below:
public class TestAnimationActivity extends Activity {
private View mainView;
private View TempView;
private Button B1, B2, B3;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate your view
setContentView(R.layout.main_test);
mainView = findViewById(R.id.mainTest);
B1 = (Button) mainView.findViewById(R.id.B1);
B2 = (Button) mainView.findViewById(R.id.B2);
B3 = (Button) mainView.findViewById(R.id.B3);
OnClickListener OCL = new OnClickListener(){
#Override
public void onClick(View v) {
if(TempView == null){
TempView = v;
}else{
int originalPos1[] = new int[2];
TempView.getLocationOnScreen( originalPos1 );
int originalPos2[] = new int[2];
v.getLocationOnScreen( originalPos2 );
v.animate().translationX((float)originalPos1[0] - (float)originalPos2[0]);
v.animate().translationY((float)originalPos1[1] - (float)originalPos2[1]);
TempView.animate().translationX((float)originalPos2[0] - (float)originalPos1[0]);
TempView.animate().translationY((float)originalPos2[1] - (float)originalPos1[1]);
TempView = null;
}
}};
B1.setOnClickListener(OCL);
B2.setOnClickListener(OCL);
B3.setOnClickListener(OCL);
}
}
The XML file is as below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainTest"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/B1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#B4B4B4"
android:gravity="center" />
<Button
android:id="#+id/B2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#470047"
android:gravity="center" />
<Button
android:id="#+id/B3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="center" />
</LinearLayout>
When I was testing this code on my phone. The animation did not work properly.
Two buttons will switch position the first time, but the button will go to wrong position if I continue clicking them. It seems that the button still calculate the position it should move to based on its original location when the app launched.
Anyone knows what is going wrong?

when sending strings via put extra the underline under some words has gone

when I send strings via put extra the underlined words will not be underlined
<string name="s_hello_txt">\n{ <u>hello all</u> }\n</string>
MainActivity Button Code
public void c_hello(View v){
Intent hello= new Intent(MainActivity.this,
MainTextActivity.class);
intent_collection_e3tiraf.putExtra("key",getResources().getString(R.string.s_hello_txt));
startActivity(hello);
finish();
}
MainActivityText onCreate Code
textview = (TextView) findViewById(R.id.id_text_txt);
Intent n = getIntent();
String mrng = n.getStringExtra("key");
textview.setText(mrng);
if I put a text with direct string it will be underlined
For Example if I put in the layout of MainActivityText(activity_maintext.xml)
<TextView
android:id="#+id/id_dailyprayers_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/s_hello_txt"
android:textSize="30sp" />
the textview in MainActivityText Show the text(hello all) underlined
any help!!!!
As long as the string still has the underline html you should be able to utilize the Html.fromHtml method to style the string.
textview.setText(Html.fromHtml(mrng));
Actually, the string getResource().getString(R.string.s_hello_txt) is not be underlined.
The best way to add html source code in strings.xml is to use <![CDATA[html source code]]>. Here is an example:
<string name="s_hello_txt"><![CDATA[<u>Text</u>]]></string>
And then use Html.fromHtml(mrng) to show the underlined string
// Try This One This Will Help For Your Acheivement
**String.xml**
<string name="s_hello_txt"><br/>{ <u>hello all</u> }<br/></string>
**activity_main1.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:orientation="vertical" >
<TextView
android:id="#+id/txtValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/s_hello_txt"/>
<Button
android:id="#+id/btnClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GoTo Second Activity"/>
</LinearLayout>
**MainActivity1 Activity**
public class MainActivity1 extends Activity {
private Button btnClick;
private TextView txtValue;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
txtValue = (TextView)findViewById(R.id.txtValue);
btnClick = (Button)findViewById(R.id.btnClick);
txtValue.setText(Html.fromHtml(getString(R.string.s_hello_txt)));
btnClick.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity1.this, MainActivity2.class);
intent.putExtra("EXTRA",getString(R.string.s_hello_txt));
startActivity(intent);
}
});
}
}
**activity_main2.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:orientation="vertical" >
<TextView
android:id="#+id/txtValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
**MainActivity2 Activity**
public class MainActivity2 extends Activity {
private TextView txtValue;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
txtValue = (TextView)findViewById(R.id.txtValue);
txtValue.setText(Html.fromHtml(getIntent().getStringExtra("EXTRA")));
}
}

java.lang.IllegalStateException: The specified child already has a parent. (After editing my post)

building android,location based application. I have my class that initiate google maps api work (MygoogleMapsActivator). this is constractor of this class
public MyGoogleMapsActivator(Context mContext)
{
this.mContext = mContext;
}
.
Context mContext is a activity where i need to display map and address,a pass it here for getting services that i need to activate.Activity displaying the results - my location and address(location and address i am getting from MyGoogleMapsActivator)
when i want to display it on my activity i get this exception in LogCat: Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
i am getting ti from this function:
public void SetUpTextViewWithAddress()
{
text = (TextView) findViewById(R.id.address);
text.append(addressString);
setContentView(text);
}
How can i solved it,thanks.
This is full code of my activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new_place);
mGoogleMapsActivator = new MyGoogleMapsActivator(this);
Button buttonAddPlace = (Button) findViewById(R.id.buttonAddPlace);
Button buttonAddByAddress = (Button) findViewById(R.id.buttonAddByAddress);
fm = (SupportMapFragment ) getSupportFragmentManager().findFragmentById(R.id.map);
buttonAddPlace.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(AddNewPlaceActivity.this, AddByAddress.class);
mMyPlace = new Places("", city, street, number, "", LoggedWithFacebookMainActivity.GetLoggedInUserName(), myPosition.latitude, myPosition.longitude);
intent.putExtra("Place", mMyPlace);
//intent.putExtra("TheStreet", street);
//intent.putExtra("TheNumber", number);
intent.putExtra("Clarification");
startActivity(intent);
}
});
buttonAddByAddress.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(AddNewPlaceActivity.this, AddByAddress.class);
mMyPlace = new Places("", city, street, number, "", LoggedWithFacebookMainActivity.GetLoggedInUserName(), myPosition.latitude, myPosition.longitude);
intent.putExtra("Place", mMyPlace);
// intent.putExtra("TheStreet", "Street");
// intent.putExtra("TheNumber", "Number");
intent.putExtra("Clarification");
startActivity(intent);
}
});
mGoogleMapsActivator.SetUpMapIfNeed(fm);
mGoogleMapsActivator.SetMyGoggleActivatorLocationEnabled();
mGoogleMapsActivator.SetUpService();
mGoogleMapsActivator.setUpListener();
mGoogleMapsActivator.SetUpAndGetProvider();
mGoogleMapsActivator.SetMyLocationWithListener();
mGoogleMapsActivator.SetMyPosition();
mGoogleMapsActivator.SetUpMyMarker();
mGoogleMapsActivator.SetUpMapView();
mGoogleMapsActivator.SetUpAddressFromGeocoder();
city = mGoogleMapsActivator.getCity();
street = mGoogleMapsActivator.getStreet();
number = mGoogleMapsActivator.getNumber();
addressString = mGoogleMapsActivator.getAddressString();
SetUpTextViewWithAddress();
mGoogleMapsActivator.AnimateCamera();
public void SetUpTextViewWithAddress()
{
text = (TextView) findViewById(R.id.address);
text.setText(addressString);
// setContentView(text);
}
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="end"
tools:context=".AddNewPlaceActivity" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="370dp" />
<TextView
android:id="#+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/buttonAddByAddress"
android:layout_alignParentLeft="true"
android:layout_below="#+id/map"
android:layout_toLeftOf="#+id/buttonAddPlace"
android:text="#string/place_address_he" />
<Button
android:id="#+id/buttonAddPlace"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/buttonAddByAddress"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/address"
android:background="#drawable/custom_button_main"
android:paddingBottom="10dp"
android:text="#string/add_place_he" />
<Button
android:id="#+id/buttonAddByAddress"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#drawable/custom_button_main"
android:paddingTop="10dp"
android:text="#string/add_place_manually_he" />
</RelativeLayout>
Thanks
The issue is the TextView is already attached to your view hierarchy. I'm guessing the view with address id is already inside the main layout file you are using for your activity, without the full activity code it's hard to tell. If this is true you should remove the setContentView call as it isn't needed. Make sure you want to append the text to what may already be in the TextView, if you want to replace the text you should use setText instead of append.

How to create a buttonlistener for an inflated button

Am quite new to Android programming and hope someone can help me out in this:
1. I am coding from one of the demos here:
http://developer.android.com/training/animation/screen-slide.html
I have a main activity_screen_slide.xml file:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
In another xml file, main.xml, I have a few buttons.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="#+id/buttonStart"
android:text="test"/>
</LinearLayout>
Within the main java activity file, the context is set as following:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
When I tried to set a listener for testButton, am unable to do so. This means that when the button is generated subsequently (through an inflator in another java file), there is no response when I click the button.
The java file to generate the inflator is simply:
ViewGroup rootView;
rootView = (ViewGroup) inflater.inflate(R.layout.main, container, false);
Thank you and appreciate your help
Cheers
D
I assume your button is defined in your xml layout activity_screen_slide.xml similar to this:
<Button
android:id="#+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
In your Activity you can add a ClickListener like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("", "I've been clicked");
}
});
}
Try this:
public class Settings extends Activity implements View.OnClickListener {
public void onCreate(Bundle savedInstanceState) {
//...ALL THE OTHER CODE..\\
Button button = (Button) findViewById(R.id.YOUR_BUTTON_ID);
button.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.YOUR_BUTTON_ID: {
ALPHA.YourFunction(parameters);
}
}
}
}

How to add components to current view in android

I am new to android and just trying to modify a basic example.
main.xml is like
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<Button android:layout_width="228dp" android:layout_height="wrap_content" android:id="#+id/addButton" android:text="#string/addBtn"></Button>
</LinearLayout>
And want to add TextField on Click of the button. How to add Child to view?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.addButton);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
EditText editText = new EditText(v.getContext());
//Append the editText to View
}
How to append it to current view/layout?
Something like this would help you i think.
ParentView.post(new Runnable() {
public void run() {
ParentView.addView(newView,0);
LinearLayout.LayoutParams rlparams = (LinearLayout.LayoutParams)rldeal.getLayoutParams();
rlparams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
rlparams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
newView.setLayoutParams(rlparams);
}
});
Try to use include and merge:
http://mfarhan133.wordpress.com/2010/10/01/reusing-layout-include-merge-tag-for-androd/

Categories

Resources