I am trying to do a drag and drop application. For that I first created a main.xml with the following code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<com.hide.move.MyImageView
android:id="#+id/Image1"
android:src="#drawable/sarathphoto7"
android:layout_weight="50"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
<com.hide.move.MyImageView
android:id="#+id/Image2"
android:src="#drawable/sarathphoto6"
android:layout_weight="50"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="200px"
/>
</LinearLayout>
and created a Java file to view the main.xml content.
public class hidenmove extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}.
but I am getting
First note that drag and drop feature is included in API level 11.
Check this : Dragging and Dropping
Related
I am working on an Android project and I creating an activity that should use a dialog theme, but it's not displaying correctly.
Below is my activities layout XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="#+id/dialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="#android:style/TextAppearance.DialogWindowTitle"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/dialog_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
style="#android:style/DeviceDefault.ButtonBar.AlertDialog">
<Button android:id="#+id/btnCopyToClipbard"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Copy to Clipboard"
style="#android:style/Widget.DeviceDefault.Button.Borderless"/>
<Button android:id="#+id/btnClose"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Close"
style="#android:style/Widget.DeviceDefault.Button.Borderless"/>
</LinearLayout>
</LinearLayout>
Below is how my activity is defined
<activity
android:name=".CustomAlertDialogActivity"
android:theme="#style/Theme.AppCompat.Light.Dialog">
</activity>
The activity class is as follows
public class CustomAlertDialogActivity extends BaseActionBarActivity
{
TextView txtDialogTitle;
TextView txtDialogMessage;
Bundle bundle;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_alert_dialog);
txtDialogTitle = (TextView)findViewById(R.id.dialog_title);
txtDialogMessage = (TextView)findViewById(R.id.dialog_message);
bundle = getIntent().getExtras();
if (bundle != null)
{
txtDialogTitle.setText("An error occurred");
txtDialogMessage.setText("No error was specified");
return;
}
txtDialogTitle.setText(bundle.getString(CustomAlertDialog.DIALOG_TITLE));
txtDialogMessage.setText(bundle.getString(CustomAlertDialog.DIALOG_MESSAGE));
}
}
BaseActionBarActivity is my own class which extends AppCompatActivity
Below is how my alert dialog is shown
In case it makes any difference this activity is within a library project which is being included into my app.
This is how the your Activity looks for me:
(Android 6.0 and Android 4.4 respectively)
To test, I just copied your code into an empty project and, as you see, it works (even though it requires some UI polishing).
My best guess is that you unintentionally do setTheme(), etc. somewhere in super-class BaseActionBarActivity. So as a step 1, replace BaseActionBarActivity with AppCompatActivity and see, if it changes anything.
I'm learning Android and I was trying out FrameLayout containing ImageViews, I tried to do a little app that switchs between two images when you click on them the code is the following:
My xml looks like this:
>
<FrameLayout 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"
tools:context=".Hola" >
<ImageView
android:id="#+id/segunda"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="metodosegunda"
android:scaleType="fitCenter"
android:src="#drawable/img1" />
<ImageView
android:id="#+id/primera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="metodoprimera"
android:scaleType="fitCenter"
android:src="#drawable/img2" />
</FrameLayout>
And my main program:
public class Hola extends Activity {
ImageView primera;
ImageView segunda;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.teta_layout);
primera = (ImageView) findViewById(R.id.primera);
segunda = (ImageView) findViewById(R.id.segunda);
}
public void metodoprimera (View view){
primera.setVisibility(View.GONE);
segunda.setVisibility(View.VISIBLE);
}
public void metodosegunda (View view){
segunda.setVisibility(View.GONE);
primera.setVisibility(View.VISIBLE);
}
}
This program should show an image and as soon as you click on it it should hide that image and show the other and so on.
The thing is that this won't work , but as soon as I switch the imageview order in the xml, it works, and i don't really understand why it should not work this way.
Thank you guys in advance
Try adding:
android:visibility="gone"
to the XML for one of the ImageView's.
I'll try to go more explicit with the explanation, here is the thing:
If I have the XML like this it will show the images (depending on visibility) but the oClick thing will not work:
<FrameLayout 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"
tools:context=".Hola" >
<ImageView
android:id="#+id/segunda"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="metodosegunda"
android:scaleType="fitCenter"
android:src="#drawable/img1"
android:visibility="gone" />
<ImageView
android:id="#+id/primera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="metodoprimera"
android:scaleType="fitCenter"
android:src="#drawable/img2"
android:visibility="visible" />
</FrameLayout>
If I swap the ImageView's order in the XML It will work perfectly:
<FrameLayout 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"
tools:context=".Hola" >
<ImageView
android:id="#+id/primera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="metodoprimera"
android:scaleType="fitCenter"
android:src="#drawable/img2"
android:visibility="visible" />
<ImageView
android:id="#+id/segunda"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="metodosegunda"
android:scaleType="fitCenter"
android:src="#drawable/img1"
android:visibility="gone" />
</FrameLayout>
It's not about the visibility of each Imageview since it does not matter what value I put on them It won't work the first way but i will work the second way.
Seems like a weird issue since the oder of the imageviews should not matter if you set them visible or gone ...
As the title shows, I'm trying to make a button in my Android app to change a TextView but it ain't working properly... Here's the code, I hope you guys see what's going wrong. FYI, I'm using NetBeans with the Android 2.3.3 emulator connected as I will be running on my phone (2.3.6) later.
Main java:
public class Rooster extends Activity
{
private Button buttonSearch;
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.buttonSearch = (Button)this.findViewById(R.id.buttonSearch);
this.buttonSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView textRooster = (TextView)findViewById(R.id.textRooster);
textRooster.setText("some text");
}
});
}
Well here is my main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="42px"
android:layout_weight="1"
android:gravity="center_vertical" >
<EditText
android:id="#+id/editSearch"
android:layout_width="0dip"
android:layout_height="37px"
android:layout_weight="2"
android:gravity="center"
android:hint="Zoekopdracht" />
<Button
android:id="#+id/buttonSearch"
android:layout_width="0dip"
android:layout_height="37px"
android:layout_weight="1"
android:gravity="center"
android:text="Zoek" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="39px"
android:layout_weight="1"
android:gravity="center" >
<Button
android:id="#+id/buttonChangeWijzigingen"
android:layout_width="fill_parent"
android:layout_height="37px"
android:gravity="center"
android:text="Zet wijzigingen aan" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="12" >
<TextView
android:id="#+id/textRooster"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Geen rooster opgezocht" />
</LinearLayout>
</LinearLayout>
I think the button that you think your clicking is actually getting drawn over. try changing
this.buttonSearch = (Button)this.findViewById(R.id.buttonSearch);
to
this.buttonSearch = (Button)this.findViewById(R.id.buttonChangeWijzigingen);
It doesnt actually fix the problem but just makes more clear what the issue is.
I declared a ImageView on my layout (XML) but when I try to grab it by the ID I'm getting a TextField!?! Then my code throw a ClassCastException.
Here is my layout code (splash.xml):
<?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"
style="#android:style/Theme.Light" android:layout_height="wrap_content" android:background="#drawable/bg">
<ImageView android:layout_height="wrap_content" android:layout_width="match_parent" android:src="#drawable/splash" android:layout_marginTop="20pt" android:id="#+id/splashLogo"></ImageView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="#string/app_name" android:id="#+id/splashTitle" android:textStyle="bold" android:textSize="#dimen/title_size" android:layout_gravity="center" android:lineSpacingExtra="#dimen/spacer" android:gravity="center"></TextView>
<TextView android:id="#+id/splashCopyleft" android:textSize="#dimen/version_size" android:lineSpacingExtra="#dimen/version_spacing" android:layout_width="wrap_content" android:gravity="center" android:layout_height="wrap_content" android:text="#string/app_version_info" android:layout_gravity="center" android:textColor="#color/version"></TextView>
</LinearLayout>
Here is my Java (Activity) code:
// imports
public class SplashActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
// Grab a ClassCastException here
ImageView logo = (ImageView) findViewById(R.id.splashLogo);
// Never get here :'(
Log.i("GOT", logo.toString());
}
}
Sometimes Eclipse shifts resource ids. Try cleaning your project and building it again.
First, clean your project and then build it.
If the problem persists, close Eclipse and re-open it.
I am developing an app with tabs. On one of the tabs,
I have two EditText fields and a couple of buttons. I want to add an ExpandableListView
to the tab, below the rest of the elements.
My problem is that the current class extends Activity. All the example I've found extend ExpandableListActivity, but I can't extend both for the same class.
How can I add this list to the tab? I've added an ExpandableListView element in the XML file, but whenever I run it, it doesn't show up. I understand that I need to bind the view to some data, but how do I go about doing this?
Thank you.
Here is my class code:
public class DirectionsTab extends Activity
{
private Button clear_btn;
private Button go_btn;
private EditText origin_txt;
private EditText dest_txt;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.directions_tab);
// initialize views and onClick event handler
origin_txt = (EditText)findViewById(R.id.origin_txt);
dest_txt = (EditText)findViewById(R.id.dest_txt);
clear_btn = (Button)findViewById(R.id.clear_btn);
// clear all text fields and return focus to dest_txt
clear_btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
origin_txt.setText("");
dest_txt.setText("");
origin_txt.requestFocus();
}
});
} // end public void onCreate(Bundle savedInstanceState)
} // end public class DirectionsTab extends Activity
and XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/origin_lbl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Origin"/>
<EditText
android:id="#+id/origin_txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background"
android:layout_below="#id/origin_lbl"/>
<TextView
android:id="#+id/dest_lbl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/origin_txt"
android:text="Destination" />
<EditText
android:id="#+id/dest_txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background"
android:layout_below="#id/dest_lbl" />
<ExpandableListView
android:id="#+id/listView"
android:layout_below="#+id/dest_txt"
android:layout_height="wrap_content"
android:layout_width="wrap_content" /><!--android:id="#android:id/list"-->
<Button style="?android:attr/buttonStyleSmall"
android:id="#+id/clear_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/dest_txt"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:layout_marginTop="10dip"
android:text="Clear" />
<Button style="?android:attr/buttonStyleSmall"
android:id="#+id/go_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/clear_btn"
android:layout_alignTop="#id/clear_btn"
android:text="Go!" />
</RelativeLayout>
I got this!, If anyone is having problems with this, make sure you follow these steps:
Copy and paste ExpandableList3 example (located in ApiDemos project from Samples folder in your Android SDK). This is just a good example of how to set up a list.
Make your class inherit from ExpandableListActivity
Create the following in your XML layout file:
<ExpandableListView
android:id="#android:id/list"
android:layout_below="#+id/go_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
It is VERY important that the ID property matches what you see above, otherwise, nothing will work. This little detail wasted plenty of hours for me and was discovered seating neatly as a small detail in the android docs.