text not showing in listview - Java for Android App Development

I am creating a listview with a custom object containing 1 property(i will be creating other activities using objects with more than 1 property). my problem is it shows the correct number of rows but no text.
main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="android link"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView android:id="@+id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
</LinearLayout>
grid_item.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="android link"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minHeight="50dp">
<TextView android:id="@+id/item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:text=""
android:layout_alignParentLeft="true"/>
</RelativeLayout>
object java
Code:
package com.example.sarahjmusicprotocol;
import java.io.Console;
public class CategoriesObject
{
String category;
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public CategoriesObject()
{
}
public CategoriesObject(String category)
{
this.category=category;
}
private void print()
{
System.out.printf("Category: {0}",category);
}
[user=439709]@override[/user]
public String toString()
{
return this.getCategory();
}
}
adapter java
Code:
package com.example.sarahjmusicprotocol;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class CategoryAdapter extends BaseAdapter
{
private List<CategoriesObject> categoryList;
private LayoutInflater layoutInflater;
public CategoryAdapter(final Context context, final List<CategoriesObject> categoriesList)
{
//super(context,0);
this.categoryList = categoriesList;
//this.activity=a;
layoutInflater = LayoutInflater.from(context);
}
public CategoriesObject getItem(int position)
{
return categoryList.get(position);
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView == null)
{
convertView = layoutInflater.inflate(R.layout.grid_item, null);
holder = new ViewHolder();
holder.titleView=(TextView)convertView.findViewById(R.id.item);
convertView.setTag(holder);
}
else
{
holder=(ViewHolder)convertView.getTag();
}
holder.titleView.setText(categoryList.get(position).getCategory());
return convertView;
}
public int getCount()
{
return categoryList.size();
}
private static class ViewHolder
{
public TextView titleView;
}
}
main java
Code:
package com.example.sarahjmusicprotocol;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Main extends Activity
{
private ListView view;
private CategoryAdapter adapter;
private List<CategoriesObject> categoriesList;
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//categoriesList = CategoryDomain.getCategoryDomain().getCategories();
categoriesList = getCategories();
view = (ListView)findViewById(R.id.list);
adapter = new CategoryAdapter(this,categoriesList);
view.setAdapter(adapter);
}
private ArrayList getCategories()
{
ArrayList<CategoriesObject> list = new ArrayList<CategoriesObject>();
list.add(new CategoriesObject("Publishing"));
list.add(new CategoriesObject("Publishing"));
list.add(new CategoriesObject("Publishing"));
list.add(new CategoriesObject("Publishing"));
list.add(new CategoriesObject("Publishing"));
list.add(new CategoriesObject("Publishing"));
return list;
}
[user=439709]@override[/user]
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
any help will be appreciated.

You might want to use a debugger: http://forum.xda-developers.com/showthread.php?t=2325164

nikwen said:
You might want to use a debugger: http://forum.xda-developers.com/showthread.php?t=2325164
Click to expand...
Click to collapse
I'm not getting an error, it just doesn't show the text.

larryse said:
I'm not getting an error, it just doesn't show the text.
Click to expand...
Click to collapse
Yeah, have a look at the posts about using the debuggers of Eclipse or AndroidStudio.
They help you to understand what your app does. You can see which commands get executed at which time and the you can see the values of variables.
That might help you finding the problem.
(This doesn't require an Exception.)

Related

webview help

I am having trouble creating a webview app for my already mobile ready site.
I keep getting this error in the emulator:
The application window cleaning forums (process com.windowcleaningforums) has stopped unexpectadly
The app never actualy loads just goes straight to this.
Basically i already have my sites mobile ready and browsing to them on your mobile works fine, but would like to put these into apps.
With a back, forward and refresh button when hitting menu button on phone.
(I am not sure what i need to add these yet but any advice would be great)
My project is set as bellow
Application name: Window Cleaning Forums
Package name: com.windowcleaningforums
Create activity: windowcleaningforums
Mini SDK version: 4
windowcleaningforums.java
Code:
package com.windowcleaningforums;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class windowcleaningforums extends Activity {
/** Called when the activity is first created. */
//@Override
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.windowcleaningforums.co.uk");
mWebView.setWebViewClient(new HelloWebViewClient());
}
}
Main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<webView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Manifest
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.windowcleaningforums"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".windowcleaningforums"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
I am very new and im not quite sure what is causing it any help would be great. Thanks
I get this if that is any help i am not sure what it means though.
[2011-01-12 22:35:07 - DeviceMonitor]Sending jdwp tracking request failed!
This is the log if that helps also i really could do with some help peeps, nobody seems to want to. I know i am new and prob asking stupid questions but how am i supposed to learn if i dont ask questions.
Hi cyberpedz,
Probably just a typo but in your main.xml the WebView tag should have a capital "W" like so:
<WebView xmlns:andro...
You did the right thing looking in the log. That's what helped to figure this one out: there was an exception stack trace in the log. (Keep an eye out for the "AndroidRuntime" tag)
Thanks i have changed that but no difference.
I do have a red dot over a hellowebview class though
When viewing windowcleaningforums.java and looking at the right in outline i have a read dot over hellowebviewclient
Could it be something to do with this bit of code?
Code:
public class windowcleaningforums extends Activity {
/** Called when the activity is first created. */
//@Override
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Try moving the
Code:
//@Override
just before onCreate and uncomment it.
Also Java classes should start with a capital (public class Windowcleaningforum). Make sure you modify the Manifest accordingly.
Ok finally i have it working, it was me being blind i missed a webView now changed to Webview and all works thanks so much.
Now i am trying to get a loading progress bar or even better spinning circle.
What code would i need for this and where abouts in my java bellow would i fit it in?
Code:
package com.windowcleaningforums;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class windowcleaningforums extends Activity {
/** Called when the activity is first created. */
//@Override
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.WebView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.windowcleaningforums.co.uk");
mWebView.setWebViewClient(new HelloWebViewClient());
}
}
Anyone know how to get the spinning circle while pages loads? and how to imput into my code above?
Would be a great help
Ok finally i have a running webview in the market what i would like to do now is add a soft menu for a back, refresh and forward button what code do i need and where would i put it in my java below?
Code:
package com.windowcleaningforums;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class windowcleaningforums extends Activity
{
final Activity activity = this;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
WebView webView = (WebView) findViewById(R.id.WebView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle(" Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://www.windowcleaningforums.co.uk");
}
}
So why is it no body wants to help is it the way i ask? is it that developers think they are above the noob? or you really cant b bothered!!
I have even offered to pay for help in the past yet nobody is interested maybe it is that no one really knows the answers.
cyberpedz said:
So why is it no body wants to help is it the way i ask? is it that developers think they are above the noob? or you really cant b bothered!!
I have even offered to pay for help in the past yet nobody is interested maybe it is that no one really knows the answers.
Click to expand...
Click to collapse
It's not that developers can't be bothered, but generally they will have other things to do than help out others. The thing is, you're asking on help for pretty basic stuff. This is something that you should know already, and if don't I suggest you should read through Android's developer website one more time and look at the API examples/demos too.
Hints for your problem; Add the buttons as a merge layout in your XML and then link them with your web view, or add them as menu options. Googling for both will surely give you enough results to get you on your way, these are pretty basic things you want to do after all.

[Q] Beginning Programmer, need help with personal app

I'm creating an app to keep track of all the messier objects in the sky. I have made a list view in the main activity that holds all the objects, when clicked it should open my other activity and display the objects info.
The program now goes to my second activity no matter what item i choose but doesn't change any of the TextView text without foreclosing.
also how can I recognize what object was clicked and change the information displayed for each object
The main Activity Java looks like this:
Code:
package com.kabluey.messier;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MessierCatalog extends ListActivity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Set string array
setListAdapter(new ArrayAdapter<String>(this, R.layout.main, objects));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
// When clicked, change text in object info to clicked object name
TextView objText = (TextView) findViewById(R.id.txtObjName);
objText.setText(((TextView) view).getText());
Intent i = new Intent(MessierCatalog.this,Objectinfo.class);
startActivity(i);
}
});
}
static final String[] objects = new String[]
{
"m1", "m2","m3","m4","m5", "m6","m7","m8","m9","m10","m11","m12","m13","m14"
,"m15","m16","m17","m18","m19","m19","m20","m21","m22","m23","m24","m25","m26","m27","m28","m29","m30","m31","m32","m33"
,"m34","m35","m36","m37","m38","m39","m40","m41","m42","m43","m44","m45","m46","m47","m48","m49","m50","m51","m52","m53"
,"m54","m55","m56","m57","m58","m59","m60","m61","m62","m63","m64","m65","m66","m67","m68","m69","m70","m71","m72","m73"
,"m74","m75","m76","m77","m78","m79","m80","m81","m82","m83","m84","m85","m86","m87","m88","m89","m90","m91","m92","m93"
,"m94","m95","m96","m97","m98","m99","m100","m101","m102","m103","m104","m105","m106","m107","m108","m109","m110"
};
}
The second activity has no code in it now and is just an xml layout:
Code:
<?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"
>
<ImageView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:src="@drawable/crab"
/>
<TableLayout
android:layout_width="fill_parent"
android:id="@+id/tableLayout1"
android:layout_height="wrap_content"
android:stretchColumns="1">
<TableRow>
<TextView
android:text="Name of Object:"
android:padding="3dip"/>
<TextView
android:id="@+id/txtObjName"
android:text="Object name here"
android:padding="3dip"
android:paddingLeft="5dip" />
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TableRow>
<TableRow>
<TextView
android:text="Apparent Magnitude:"
android:padding="3dip"/>
<TextView
android:id="@+id/txtMagnitude"
android:paddingLeft="5dip"
android:text="9.0"/>
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TableRow>
</TableLayout>
</LinearLayout>
I think it may have to do with your casting of "view" to type "TextView" in you onItemClick override. I don't think the "View" passed to this override is a "TextView" is it? I would think it is the ListView to which you added the OnItemClickListener event handler.
Have some suggestion / tips:
1. This is more organized, also initializes first the Strings
Code:
package com.kabluey.messier;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MessierCatalog extends ListActivity
{
/** Called when the activity is first created. */
private static final String[] objects = {
"m1", "m2","m3","m4","m5", "m6","m7","m8","m9","m10","m11","m12","m13","m14"
,"m15","m16","m17","m18","m19","m19","m20","m21","m22","m23","m24","m25","m26","m27","m28","m29","m30","m31","m32","m33"
,"m34","m35","m36","m37","m38","m39","m40","m41","m42","m43","m44","m45","m46","m47","m48","m49","m50","m51","m52","m53"
,"m54","m55","m56","m57","m58","m59","m60","m61","m62","m63","m64","m65","m66","m67","m68","m69","m70","m71","m72","m73"
,"m74","m75","m76","m77","m78","m79","m80","m81","m82","m83","m84","m85","m86","m87","m88","m89","m90","m91","m92","m93"
,"m94","m95","m96","m97","m98","m99","m100","m101","m102","m103","m104","m105","m106","m107","m108","m109","m110"
};
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Set string array
setListAdapter(new ArrayAdapter<String>(this, R.layout.main, objects));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
// When clicked, change text in object info to clicked object name
TextView objText = (TextView) findViewById(R.id.txtObjName);
objText.setText(((TextView) view).getText());
//Log here!, but im almost sure this is bad
Intent i = new Intent(MessierCatalog.this,Objectinfo.class);
startActivity(i);
}
});
}
2. Could you make a Log.i("APP", ((TextView) view).getText()) and upload output?. That appears like you're setting to your textview, the same text you got from it ( that's actually null / empty )
3. If output it's empty or not output, then this is the answer
Code:
package com.kabluey.messier;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MessierCatalog extends ListActivity
{
/** Called when the activity is first created. */
private static final String[] objects = {
"m1", "m2","m3","m4","m5", "m6","m7","m8","m9","m10","m11","m12","m13","m14"
,"m15","m16","m17","m18","m19","m19","m20","m21","m22","m23","m24","m25","m26","m27","m28","m29","m30","m31","m32","m33"
,"m34","m35","m36","m37","m38","m39","m40","m41","m42","m43","m44","m45","m46","m47","m48","m49","m50","m51","m52","m53"
,"m54","m55","m56","m57","m58","m59","m60","m61","m62","m63","m64","m65","m66","m67","m68","m69","m70","m71","m72","m73"
,"m74","m75","m76","m77","m78","m79","m80","m81","m82","m83","m84","m85","m86","m87","m88","m89","m90","m91","m92","m93"
,"m94","m95","m96","m97","m98","m99","m100","m101","m102","m103","m104","m105","m106","m107","m108","m109","m110"
};
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Set string array
setListAdapter(new ArrayAdapter<String>(this, R.layout.main, objects));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
// When clicked, change text in object info to clicked object name
TextView objText = (TextView) findViewById(R.id.txtObjName);
objText.setText(objects[position]);
Intent i = new Intent(MessierCatalog.this,Objectinfo.class);
startActivity(i);
}
});
}
Also for this cases it's not really needed TableRow, With a Relative layout it's enough, but I don't think that's a prob.
If you have some problem, feel free to contact me through PM.
Good luck, and cheers from colombia.
D4.
solved!
I searched the nets and found a solution, it seems to be working quite well.
thanks for all the help
My click listener now stores the value so both activities can access it
Code:
public void onItemClick(AdapterView<?> parent, View view,int position,long id)
{
Intent i = new Intent(MessierCatalog.this,Objectinfo.class);
//Get ListViews text and store it to "myText"
i.putExtra("myText", (((TextView) view).getText()));
startActivity(i);
}
The ObjectInfo activity now has code to store the ListViews text in String Variable and then set the text..
Code:
public class Objectinfo extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.objectinfo);
TextView objName = (TextView) findViewById(R.id.txtObjName);
//store ListViews Text to String
String str = getIntent().getStringExtra("myText");
//Set text to stored ListView string
objName.setText(str);
}
}

Text input with DialogFragment

I am trying to get a value that user enters into a Dialog, using the recommended DialogFragment class for it, the Dialog constructs and runs fine, but I cannot return the value of the EditText parameter to the parent class, without get a Null pointer exception.
My DialogHost class, this constructs, returns and links the parent to its buttons.
Code:
package jo.app.co;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
public class DialogHost extends DialogFragment {
public interface NoticeDialogListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}
NoticeDialogListener mListener;
[user=439709]@override[/user]
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (NoticeDialogListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString());
}
}
[user=439709]@override[/user]
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog_add, null))
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
[user=439709]@override[/user]
public void onClick(DialogInterface dialog, int id) {
mListener.onDialogPositiveClick(DialogHost.this);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
DialogHost.this.getDialog().cancel();
}
});
return builder.create();
}
}
My MainActivity
Code:
package jo.app.co;
import android.app.DialogFragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.EditText;
public class MainActivity extends FragmentActivity implements DialogHost.NoticeDialogListener {
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showNoticeDialog();
}
public void showNoticeDialog() {
DialogFragment dialog = new DialogHost();
dialog.show(getFragmentManager(), "DialogHost");
}
[user=439709]@override[/user]
public void onDialogPositiveClick(DialogFragment dialog) {
EditText myText = (EditText) findViewById(R.id.item_added);
try {
Log.d ("IN TRY", myText.getText().toString());
}
catch (Exception e) {
Log.e ("IN CATCH", e.toString());
}
}
[user=439709]@override[/user]
public void onDialogNegativeClick(DialogFragment dialog) {
Log.d ("INMAIN", "REACHED NEG");
}
}
This is my layout for the add item dialog.
Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="@+id/item_added"
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="@string/hint_add_item" />
</LinearLayout>
It's because in your main activity you are trying to call findViewById:
swapnilraj said:
Code:
[user=439709]@override[/user]
public void onDialogPositiveClick(DialogFragment dialog) {
EditText myText = (EditText) findViewById(R.id.item_added);
try {
Log.d ("IN TRY", myText.getText().toString());
}
catch (Exception e) {
Log.e ("IN CATCH", e.toString());
}
}
Click to expand...
Click to collapse
This is not possible since the layout of the activity is not the one the dialog is using. There are multiple ways of doing this, for instance call findViewById in the dialog's onPositiveButtonListener and pass that value through your interface. It might be that you need to use the LayoutInflator in the onCreateDialog, set
LinearLayout linearl = (LinearLayout) inflater.inflate(...) and get the EditText from there. You then call setView(linearL) instead.
SimplicityApks said:
It's because in your main activity you are trying to call findViewById:
This is not possible since the layout of the activity is not the one the dialog is using. There are multiple ways of doing this, for instance call findViewById in the dialog's onPositiveButtonListener and pass that value through your interface. It might be that you need to use the LayoutInflator in the onCreateDialog, set
LinearLayout linearl = (LinearLayout) inflater.inflate(...) and get the EditText from there. You then call setView(linearL) instead.
Click to expand...
Click to collapse
I tried calling findViewById method in the onClick method in the Dialog class, but the function is not defined for a DialogInterface.onClickListner, I modified it to linearl method you told but I cannot get it to work either.
Could you make the changes in the 2 snippets above, it would be very helpful!
You'll have to setup listeners fo this and pass the string or whetever you want to pass to back to the activity which in its turn can handle it (do it by itself or pass this to another fragement).
Although not so much votes (0) the last answer here on stackoverflow has exeactly what you need.

Need Assistance with Google Nav Drawer

Hello everyone, need some assistance with Google Nav Drawer. This is what Im trying to do. Im trying to create a new type of android settings with this new code or a shell as you would...Than if it works out, incorporate in into source for a future rom or current like CM or AOKP.
Heres my delima, I setup a fragment as a preferencefragment and it force closes and Eclipse says Source not found... Ive tried both Headers and Preference Screens. I perfer Preference Screens for what I want to do. Im testing this UI for a stock rom first to see if users like this idea.
Im going based off of this as a base.
This is what I have so far without the preferences showing up.
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
So I would assume my first fragment would look something like this? But its force closing...
Code:
package com.example.navi;
import java.util.List;
import android.content.Context;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceActivity.Header;
import android.preference.PreferenceFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainFragment extends PreferenceFragment {
public static Fragment newInstance(Context context) {
MainFragment f = new MainFragment();
return f;
}
[user=439709]@override[/user]
public void onBuildHeaders(List<Header> headers) {
loadHeadersFromResource(R.xml.wireless, headers);
}
private void loadHeadersFromResource(int wireless, List<Header> headers) {
// TODO Auto-generated method stub
}
}
Any feedback on the idea or code would be appreciated.
Nx Biotic said:
Hello everyone, need some assistance with Google Nav Drawer. This is what Im trying to do. Im trying to create a new type of android settings with this new code or a shell as you would...Than if it works out, incorporate in into source for a future rom or current like CM or AOKP.
Heres my delima, I setup a fragment as a preferencefragment and it force closes and Eclipse says Source not found... Ive tried both Headers and Preference Screens. I perfer Preference Screens for what I want to do. Im testing this UI for a stock rom first to see if users like this idea.
Im going based off of this as a base.
This is what I have so far without the preferences showing up.
So I would assume my first fragment would look something like this? But its force closing...
Code:
package com.example.navi;
import java.util.List;
import android.content.Context;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceActivity.Header;
import android.preference.PreferenceFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainFragment extends PreferenceFragment {
public static Fragment newInstance(Context context) {
MainFragment f = new MainFragment();
return f;
}
[user=439709]@override[/user]
public void onBuildHeaders(List<Header> headers) {
loadHeadersFromResource(R.xml.wireless, headers);
}
private void loadHeadersFromResource(int wireless, List<Header> headers) {
// TODO Auto-generated method stub
}
}
Any feedback on the idea or code would be appreciated.
Click to expand...
Click to collapse
Are you sure that this is the entire code? You do not load the menu resource.
Hmm...
Gesendet von meinem HTC One mit Tapatalk 4
No i didn't post all of it, just a sample fragment. The code im using is in the github link and disregard any xmls. I tendbto rename them alot.
Sent from my HTCONE using xda app-developers app
Nx Biotic said:
No i didn't post all of it, just a sample fragment. The code im using is in the github link and disregard any xmls. I tendbto rename them alot.
Sent from my HTCONE using xda app-developers app
Click to expand...
Click to collapse
I meant that it is not the whole fragment. Not all brackets are closed and obviously some parts of the code are missing.
Not much code is used to display a preference screen... Like 5 lines at least, if not.
The main activity fragment is what im thinking is screwing me up.
Sent from my HTCONE using xda app-developers app
Here's an updated outlook on things
This is what I presently have
Main Activity that generates Nav Drawer
Code:
package com.example.navi;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends FragmentActivity {
final String[] menuEntries = {"Wireless & Networks","Personal","Phone","System"};
final String[] fragments = {
"com.navi.fragments.Wireless",
"com.example.navi.OneFragment",
"com.example.navi.TwoFragment",
"com.example.navi.ThreeFragment",
};
private ActionBarDrawerToggle drawerToggle;
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, menuEntries);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView navList = (ListView) findViewById(R.id.drawer);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
drawerToggle = new ActionBarDrawerToggle(
this,
drawer,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
}
};
drawer.setDrawerListener(drawerToggle);
navList.setAdapter(adapter);
navList.setOnItemClickListener(new OnItemClickListener(){
[user=439709]@override[/user]
public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
[user=439709]@override[/user]
public void onDrawerClosed(View drawerView){
super.onDrawerClosed(drawerView);
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.main, Fragment.instantiate(MainActivity.this, fragments[pos]));
tx.commit();
}
});
drawer.closeDrawer(navList);
}
});
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.main,Fragment.instantiate(MainActivity.this, fragments[0]));
tx.commit();
}
[user=439709]@override[/user]
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
[user=439709]@override[/user]
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
[user=439709]@override[/user]
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
[user=439709]@override[/user]
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
PreferenceScreen (What did I do to allow this to not show and force close the app?)
Code:
package com.navi.fragments;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import com.example.navi.R;
public class Wireless extends PreferenceFragment {
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.wireless);
}
}package com.navi.fragments;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import com.example.navi.R;
public class Wireless extends PreferenceFragment {
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.wireless);
}
}
Fragments that do currently work but like the example presented, only shows a blank layout.
Code:
package com.example.navi;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class OneFragment extends Fragment {
public static Fragment newInstance(Context context) {
OneFragment f = new OneFragment();
return f;
}
[user=439709]@override[/user]
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_one, container, false);
return rootView;
}
}
Fragment Layout
Code:
<?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="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/fragment_one"
android:textColor="@color/holo_blue" />
</RelativeLayout>
Wireless XML
Code:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Wireless and Networks" >
<PreferenceScreen
android:title="Quick Wifi" >
<intent
android:action="android.intent.action.MAIN"
android:targetClass="com.android.settings.wifi.WifiSettings"
android:targetPackage="com.android.settings" />
</PreferenceScreen>
<PreferenceScreen
android:title="Quick Bluetooth" >
<intent
android:action="android.intent.action.MAIN"
android:targetClass="com.android.settings.bluetooth.BluetoothSettings"
android:targetPackage="com.android.settings" />
</PreferenceScreen>
<PreferenceScreen
android:title="Quick Hotspot" >
<intent
android:action="android.intent.action.MAIN"
android:targetClass="com.android.settings.TetherSettings"
android:targetPackage="com.android.settings" />
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>

[Q] Change text in another activity on click. how?

Hi there
I am new to this forum as an User, and I am also new to Java and Android, in developing ways. Sorry if there are any language or other mistakes.
So I am trying to make an app for a 'final' school project, which has the follow use:
The user sees an picture, a 'next' (and a 'finish') button. there are 11 pictures (user only sees one)(in the code there are for testing purposes only 3) when the user clicks 'next', the pic no. 2 appears, if he clicks another time next, the pictuer no. 3 appears and so on. (If he clicks finish, he should see a messages which shows him the text: "you've made it until pic xy" but i'm not so far yet.) I tried to do it with this code, but i failed.
Code:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class VisusActivity extends Activity implements OnClickListener {
ImageView testanzeige;
Button next;
Integer[] bildanzeige = {
R.drawable.visustest,
R.drawable.v2,
R.drawable.v3
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_visus);
testanzeige = (ImageView)findViewById(R.id.testanzeige);
next = (Button)findViewById(R.id.next);
next.setOnClickListener(this);
next.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.visus, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
try {
//Toast.makeText(this, getResources().getDisplayMetrics().toString(), Toast.LENGTH_LONG).show();
testanzeige.setImageResource(bildanzeige[0]);
}catch(Exception e){
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
};
public void onClick1(View v) {
try {
//Toast.makeText(this, getResources().getDisplayMetrics().toString(), Toast.LENGTH_LONG).show();
testanzeige.setImageResource(bildanzeige[2]);
}catch(Exception e){
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
}
}
so what have I to do? no tutorial shows up exactly my problem ( I think it's such a basic thing everyone can do it^^)
thx
rodentooth
Anser
rodentooth said:
Hi there
I am new to this forum as an User, and I am also new to Java and Android, in developing ways. Sorry if there are any language or other mistakes.
So I am trying to make an app for a 'final' school project, which has the follow use:
The user sees an picture, a 'next' (and a 'finish') button. there are 11 pictures (user only sees one)(in the code there are for testing purposes only 3) when the user clicks 'next', the pic no. 2 appears, if he clicks another time next, the pictuer no. 3 appears and so on. (If he clicks finish, he should see a messages which shows him the text: "you've made it until pic xy" but i'm not so far yet.) I tried to do it with this code, but i failed.
Code:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class VisusActivity extends Activity implements OnClickListener {
ImageView testanzeige;
Button next;
Integer[] bildanzeige = {
R.drawable.visustest,
R.drawable.v2,
R.drawable.v3
};
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_visus);
testanzeige = (ImageView)findViewById(R.id.testanzeige);
next = (Button)findViewById(R.id.next);
next.setOnClickListener(this);
next.setOnClickListener(this);
}
[user=439709]@override[/user]
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.visus, menu);
return true;
}
[user=439709]@override[/user]
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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
[user=439709]@override[/user]
public void onClick(View v) {
try {
//Toast.makeText(this, getResources().getDisplayMetrics().toString(), Toast.LENGTH_LONG).show();
testanzeige.setImageResource(bildanzeige[0]);
}catch(Exception e){
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
};
public void onClick1(View v) {
try {
//Toast.makeText(this, getResources().getDisplayMetrics().toString(), Toast.LENGTH_LONG).show();
testanzeige.setImageResource(bildanzeige[2]);
}catch(Exception e){
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
}
}
so what have I to do? no tutorial shows up exactly my problem ( I think it's such a basic thing everyone can do it^^)
thx
rodentooth
Click to expand...
Click to collapse
I have Make Fresh Java That will help You
Java
Code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
/**
* @author AndroidFire
*/
public class JumperPic extends Activity {
Button Nexty;
ImageView Imagey;
int i = 0;
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jumper);
Nexty = (Button)findViewById(R.id.nextey);
Imagey = (ImageView)findViewById(R.id.imagey);
Nexty.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View arg0) {
i++;
// To Set Your 1 Image Do it Thorough Layout
if (i == 1 ) {
//Your 2 Image
//Imagey.setImageResource(R.drawable.Your Image);
}
else if (i == 2) {
//Your 3 Image
//Imagey.setImageResource(R.drawable.Your Image);
}
else if (i == 3) {
//Your 4 Image
//Imagey.setImageResource(R.drawable.Your Image);
}
else if (i == 4) {
// Your 5 Image
//Imagey.setImageResource(R.drawable.Your Image);
}
else if (i == 5 ) {
//Your 6 Image
//Imagey.setImageResource(R.drawable.Your Image);
}
else if (i == 6) {
//Your 7 Image
//Imagey.setImageResource(R.drawable.Your Image);
}
else if (i == 7 ) {
//Your 8 Image
//Imagey.setImageResource(R.drawable.Your Image);
}
else if (i == 8 ) {
//Your 9 Image
//Imagey.setImageResource(R.drawable.Your Image);
}
else if (i == 9) {
//Your 10 Image
//Imagey.setImageResource(R.drawable.Your Image);
}
else if (i == 10) {
//Image 11 Image
Nexty.setText("Finish");
Toast.makeText(getApplicationContext(), "Your Final Text", Toast.LENGTH_LONG).show();
}
}
});
}
}
Layout
Code:
<?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="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imagey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/avatar_default_1" />
<Button
android:id="@+id/nextey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
In Your Java You can use Button.setOnClickLiestner(new View.OnClickLiestner It will bit easier than it because When We Implement Anything in Your Class we need delcare methods @override somethings you can use int to change Image
In My Java I have int values that will change every click on Button according to values it will change imageview you have write java that is tough from Me you can use my one it will bit easier Ok
Thank you new problem
Thank you so very much AndroidFire withoup you i've wouldn't made it this far. So you say I can use your code (with your name in it) in my Project?
AndroidFire said:
you can use my one it will bit easier Ok
Click to expand...
Click to collapse
Now while this problem is solved another problem has come up.
As I told you in my first thread, there is a finish button. It has this use:
when user clicks next 2 times (so if he made it until the 3rd picture) and then clicks finish, he'll Return to the main activity, and instead of the text 'Klicke auf start um zu beginnen' (click on start to begin) the text 'you made it until the 3rd picture' should show up.
If he clicked Next 5 times (6th picture) and clicks finish, he also returns to the main activity, but the text is 'you made it until the 5th picture'.
And so on with the others.
now i dont know, how to implement multiple buttons, how can I learn that? also how did you learn to code, AndroidFire? It's such a masterpiece *-*
I've tried this, but I failed.
the red lines are those, which I made at my own but they unfortunately don't work
Java Main Activity
Code:
package ch.OptiLab.visustest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.buttonSTART);
btn1.setOnClickListener(this);
[COLOR="red"] Intent intent = getIntent();
if( intent != null)
String inhalt = intent.GetStringExtra("0.1");[/COLOR]
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(this,VisusActivity.class));
}
}
XML Main Activity
Code:
<RelativeLayout xmlns:android="(external link)"
xmlns:tools="(external link)"
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="ch.OptiLab.visustest.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/Text1"
android:id="@+id/textView"
android:layout_marginTop="84dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:gravity="center_horizontal"
android:singleLine="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/Text2"
android:id="@+id/textView2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp" />
<Button
android:id="@+id/buttonSTART"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="@string/button1"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="142dp" />
</RelativeLayout>
Java 2nd activity
Code:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
/**
* @author AndroidFire
*/
public class VisusActivity extends Activity {
Button next;
ImageView testanzeige;
Button finish;
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_visus);
next = (Button)findViewById(R.id.next);
testanzeige = (ImageView)findViewById(R.id.testanzeige);
[COLOR="red"]finish = (Button)findViewById(R.id.finish);[/COLOR]
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
i++;
// To Set Your 1 Image Do it Thorough Layout
if (i == 1 ) {
//Your 2 Image
testanzeige.setImageResource(R.drawable.visustest);
[COLOR="Red"]finish.setOnClickListener(new View.OnClickListener() {
Intent i = new Intent(this, MainActivity.class);
String content = "You made it until 2nd picture".toString();
i.putExtra("0.1", content);
}
[/COLOR]
}
else if (i == 2) {
//Your 3 Image
testanzeige.setImageResource(R.drawable.v2);
}
else if (i == 3) {
//Your 4 Image
testanzeige.setImageResource(R.drawable.v3);
}
else if (i == 4) {
// Your 5 Image
//Imagey.setImageResource(R.drawable.Your Image);
}
else if (i == 5 ) {
//Your 6 Image
//Imagey.setImageResource(R.drawable.Your Image);
}
else if (i == 6) {
//Your 7 Image
//Imagey.setImageResource(R.drawable.Your Image);
}
else if (i == 7 ) {
//Your 8 Image
//Imagey.setImageResource(R.drawable.Your Image);
}
else if (i == 8 ) {
//Your 9 Image
//Imagey.setImageResource(R.drawable.Your Image);
}
else if (i == 9) {
//Your 10 Image
testanzeige.setImageResource(R.drawable.visustest);
}
else if (i == 10) {
//Image 11 Image
next.setText("Finish");
Toast.makeText(getApplicationContext(), "Your Final Text", Toast.LENGTH_LONG).show();
}
}
});
}
}
XML 2nd Activity
Code:
<RelativeLayout xmlns:android="(external link)"
(external link)"
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="ch.OptiLab.visustest.VisusActivity" >
<ImageView
android:id="@+id/testanzeige"
android:layout_width="231dp"
android:layout_height="231dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/v2" />
<Button
android:id="@+id/next"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"
android:text="@string/NEXTPIC" />
<Button
android:id="@+id/finish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/next"
android:layout_alignBottom="@+id/next"
android:layout_alignParentLeft="true"
android:text="@string/cantread" />
</RelativeLayout>
The most important strings:
Code:
<string name="Text1">Klicke auf Start um zu beginnen.</string>
<string name="cantread">finish</string>
Thank you so much
other way also not work
Well I have tried another solution, but it didn't work either. What do I have to do?
Code:
Code:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
/**
* [user=736105]@author[/user] AndroidFire
*/
public class VisusActivity extends Activity {
Button next;
ImageView testanzeige;
Button finish;
int i = 0;
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_visus);
next = (Button)findViewById(R.id.next);
testanzeige = (ImageView)findViewById(R.id.testanzeige);
finish = (Button)findViewById(R.id.finish);
next.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View arg0) {
i++;
// To Set Your 1 Image Do it Thorough Layout
if (i == 1 ) {
//Your 2 Image
testanzeige.setImageResource(R.drawable.visustest);
}
else if (i == 2) {
//Your 3 Image
testanzeige.setImageResource(R.drawable.v2);
}
else if (i == 3) {
//Your 4 Image
testanzeige.setImageResource(R.drawable.v3);
}
else if (i == 4) {
// Your 5 Image
//Imagey.setImageResource(R.drawable.Your Image);
}
else if (i == 5 ) {
//Your 6 Image
//Imagey.setImageResource(R.drawable.Your Image);
}
else if (i == 6) {
//Your 7 Image
//Imagey.setImageResource(R.drawable.Your Image);
}
else if (i == 7 ) {
//Your 8 Image
//Imagey.setImageResource(R.drawable.Your Image);
}
else if (i == 8 ) {
//Your 9 Image
//Imagey.setImageResource(R.drawable.Your Image);
}
else if (i == 9) {
//Your 10 Image
testanzeige.setImageResource(R.drawable.visustest);
}
else if (i == 10) {
//Image 11 Image
next.setText("Finish");
Toast.makeText(getApplicationContext(), "Your Final Text", Toast.LENGTH_LONG).show();
}
}
});
[COLOR="Red"]finish.setOnClickListener(new View.OnClickListener() {
Intent i = new Intent(this, MainActivity.class);
[user=439709]@override[/user]
public void onClick(View arg0) {
i++;
if (i == 1 ) {
String content = "You got 0.1".toString();
i.putExtra("0.1", content);
[/COLOR]
}
}
}
}
I'm assuming you declared your activity in the Manifest?
F'n noob said:
I'm assuming you declared your activity in the Manifest?
Click to expand...
Click to collapse
uhm I think so.
Edit: there are 2 activity-groups in my manifest, so I think they've been declared automatically?
After looking at your code further, you are using onClick methods but aren't implenting an onClickListener. Are your buttons working at all?
yes, they are working fine. I just dont know how to change the text.
rodentooth said:
yes, they are working fine. I just dont know how to change the text.
Click to expand...
Click to collapse
There are two ways I can think of to solve this one.
The first is to create an Application class, declare that in the manifest, and add a variable in there to save how many images have been seen. Just set it in the one activity and in the other read it, and if it's 0, show the start message, and if it's greater than 0 show the message you want.
The cooler way is to use startActivityForResult to go to the picture viewing activity. The picture viewing activity keeps track of how many the user saw and then, when they leave:
Code:
Intent returnData = new Intent();
returnData.putExtra("PICTURES_SEEN", count);
setResult(RESULT_OK, returnData);
Then, in the first activity:
Code:
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
int picturesSeen = data.getExtra("PICTURES_SEEN");
// do stuff
}
}
Hopefully that's helpful.
(There's a page in Google's API called "Getting a Result from an Activity" but I can't link to it because I'm a spammernew.)

Categories

Resources