How do I open an image by clicking on a text - android

i am making my very first app and i need some help please.
How do i open an image which is already preset in my app by clicking on a hyperlinked text?
I am trying to
For example,
"Refer to Image001"
when the user taps on the word "Image001", a window opens up the preset picture and it closes when i press the back button.
This is what i have so far
In strings.xml
<string name="refer">Refer to Image001</string>
In activity_main.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="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="left"
android:text="#string/refer" />
</ScrollView>
</LinearLayout>
thanks

You will have to have 2 TextViews. The first one will have the text: "Refer to " and the second one will have the text "Image001". Then you set a click listener on the second textview with code like this:
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);

you must first set Id to your textView in xml file like below:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="left"
android:text="#string/refer" />
then set setOnClickListener for your textview like following code:
TextView tv= (TextView) findViewById(R.id.textView1);
tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//DO you work here
}
});
after doing that in onClick method you can call you image like following code
String url = "http://www.xxx.com";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);

The best way to do this is to use DialogFragments. For example, this class extends DialogFragment and displays a picture inside ImageView.
public class MyDialogFragment extends DialogFragment {
public MyDialogFragment newInstance (Bitmap targetPicture) {
MyDialogFragment frag = new MyDialogFragment();
Bundle args = new Bundle();
args.putExtra("picture", targetPicture);
frag.setArguments(args);
return frag;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dialogfrag, container, false);
Bitmap b = (Bitmap) getArguments().getExtra("picture");
ImageView iv = (ImageView) v.findViewById(R.id.targetImageView);
iv.setImageBitmap(b);
return v;
}
However, you need to define dialogfrag.xml inside your layout folder, like this:
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/targetImageView" />
</LinearLayout>
And finally inside your activity:
OnClickListener myClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
MyDialogFragment d = MyDialogFragment.newInstance(targetPicture) //replace targetPicture with the picture you want to display
d.show();
}
};
TextView tv = (TextView) findViewById(R.id.txt1);
tv.setOnClickListener(myClickListener);

Related

Displaying internal intent within a fragmentnot working

I have a code to call the intent to display the running services screen. But i wanted it to be displayed within a fragment. But am not able to do that. I call this intent from the java class "bottompage.java" which is the fragment for bottom half of the page. But still when the button is clicked this intent occupies the full screen.
Activity main:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_display);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragmentContainer1, new TopPage());
fragmentTransaction.add(R.id.fragmentContainer2, new BottomPage());
fragmentTransaction.commit();
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
Bottompage.java
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View secondview = inflater.inflate(R.layout.bottom_fragment,container,false);
Button dummy = (Button) secondview.findViewById(R.id.dummy);
dummy.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
final String APP_DETAILS_PACKAGE_NAME ="com.android.settings";
// Here you need to define the package name
final String SCREEN_CLASS_NAME ="com.android.settings.RunningServices";
Intent servicesintent = new Intent();
servicesintent.setAction(Intent.ACTION_VIEW);
//intent.setAction(Intent.ACTION_VIEW);
servicesintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
servicesintent.setClassName(APP_DETAILS_PACKAGE_NAME, SCREEN_CLASS_NAME);
BottomPage.this.getActivity().startActivity(servicesintent);
}
});
return secondview;
}
Main.xml
The xml file for 2 fragments
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:layout_width="157dp"
android:layout_height="wrap_content"
android:text="Second page"
android:id="#+id/tv2"
android:layout_gravity="center_horizontal"
android:layout_weight="0.06" />
<FrameLayout
android:id ="#+id/fragmentContainer1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:layout_weight="0.06"></FrameLayout>
<FrameLayout
android:id ="#+id/fragmentContainer2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_below="#+id/fragmentContainer1"></FrameLayout>
The xml file for the bottom fragment
BottomPage.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/bottom_fragment">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="call services"
android:id="#+id/dummy"
android:layout_gravity="center_horizontal" />
How do i make the intent "servicesintent" in bottompage.java to display the running services within the fragment "fragmentcontainer2". Currently its taking full screen.
I am not shure what you exactly want
I use this code to display the Intent that activated my Fragment in the Log.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
Activity parent = this.getActivity();
Intent intent = (parent == null) ? null : parent.getIntent();
if (intent != null) {
Log.d(Global.LOG_CONTEXT, "onCreateView " +
intent.toUri(Intent.URI_INTENT_SCHEME));
}
Instead of adding it to the Log you can set the value to a textview.
How do i make the intent "servicesintent" in bottompage.java to display the running services within the fragment "fragmentcontainer2".
You don't. You cannot embed third-party UIs inside your own, except in very specific circumstances (e.g., app widgets and a home screen).
Also note that there is no requirement that any device have a com.android.settings.RunningServices activity in com.android.settings that is exported.

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")));
}
}

how set the text of textview with listitem when the listitem is clicked

I want to set the text of textview with listitem when a list item is clicked.
i have list of items. when i click on an item it displays the html page. i have layout to display the HTML page(Using WebView), heading(TextView) and back button(Imagebutton).i am able to display the html page in webview without any problem. Problem is with the heading. when i click on list item of list. i want the same item to be set as heading in the textview. Please suggest me. i am able to toast the listitem, but i do not need it. i want to set in text of textview.please see the below code.
Topics.java
public class Topics extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String[] topic_sections = getResources().getStringArray(R.array.topic_sections);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, topic_sections));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
change(position);
}
void change(int position){
switch(position){
case 0 :{
Intent intent = new Intent(getApplicationContext(), TopicsDisplay.class);
intent.setData(Uri.parse("file:///android_asset/home.html"));
startActivity(intent);
}
break;
case 1 :{
Intent intent = new Intent(getApplicationContext(), TopicsDisplay.class);
intent.setData(Uri.parse("file:///android_asset/program.html"));
startActivity(intent);}
break;
} } }
TopicsDisplay.java
public class TopicsDisplay extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.topic_display);
WebView webview1 = (WebView) findViewById(R.id.webView1);
webview1.loadUrl(getIntent().getDataString());
webview1.getSettings().setBuiltInZoomControls(true);
webview1.setInitialScale(1);
webview1.setPadding(0, 0, 0, 0);
}
public void finishActivity(View v){
finish();
}
}
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:textColor="#ea9999"
android:background="#000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="20sp"
android:textStyle="bold"
android:focusable="false"
android:clickable="false">
</TextView>
topic_display.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"
android:layout_height="fill_parent"
android:background="#color/myBackground">
<RelativeLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="42dp"
android:layout_height="42dp"
android:onClick="finishActivity"
android:contentDescription="#string/back"
android:src="#drawable/back_button" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:textStyle="bold"
android:textColor="#000000"
android:gravity="center_vertical"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingLeft="10dp"
android:id="#+id/header"
/>
</RelativeLayout>
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/webView1"/>
</LinearLayout>
After getting the item from the list view how to set that listitem in the text of textview which is there in the topic_display layout.
My suggestion is to put the title you want to show in the Intent's extras.
It means, that when you call
Intent intent = new Intent(getApplicationContext(), TopicsDisplay.class);
intent.setData(Uri.parse("file:///android_asset/home.html"));
startActivity(intent);
you should put the title in the Intent's extra before calling the startActivity() method (in both cases you are calling the TopicsDisplay activity)
Intent intent = new Intent(getApplicationContext(), TopicsDisplay.class);
intent.setData(Uri.parse("file:///android_asset/home.html"));
intent.putExtra("title", topic_sections[position]);
startActivity(intent);
And then, in the TopicsDisplay activity you can get the title by calling
getIntent().getStringExtra("title")
Finally, you should end up with something like this in the TopicsDisplay.onCreate method:
TextView titleTV = (TextView) findViewById(R.id.header)
titleTV.setText(getIntent().getStringExtra("title"));
Hope this helps!

DialogFragment not floating, acts embeded or as another fragment

I have this app, that I created a custom dialog for. I must of goofed something up cause while the .show call on the dialog does indeed bring it up, it looks like a whole new fragment and it is not floating but instead replacing the ui with its contents. I did see in their help for DialogFragment:
http://hi-android.info/docs/reference/android/app/DialogFragment.html#Lifecycle
that one can embed a dialog as a regular fragment or not. Though I am not doing anything to do this so I cannot figure out why its acting like an embedded fragment and not floating. After thinking on it, is it the way I defined my XML definition? The dialogfragment example above didn't really give a definition for the xml layout, so maybe that is where my issue is? (Even added the gravity to the xml file, still no dice)
My xml definition for this Dialog is here:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textSize="20sp"
android:text = "Location:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"/>
<Spinner
android:id="#+id/location_spinner"
android:layout_width = "450sp"
android:layout_height="wrap_content"/>
<!-- fill out the data on the package total cost etc -->
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:id="#+id/location_dlg_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Okay"/>
<Button android:id="#+id/location_dlg_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"/>
<Button android:id="#+id/location_dlg_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create new..."/>
</LinearLayout>
</LinearLayout>
Like I said displays just fine, the code for the fragment:
package com.viciousbytes.studiotab.subactivities.dialogfragments;
import ... ...
public class LocationPicker extends DialogFragment {
ArrayList<Location> mLocations;
public static LocationPicker newInstance()
{
LocationPicker loc = new LocationPicker();
return loc;
}
private void setLocations(ArrayList<Location> loc)
{
mLocations=loc;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Pick a style based on the num.
int style = DialogFragment.STYLE_NORMAL, theme = android.R.style.Theme;
setStyle(style, theme);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.location_dialog, container, false);
Spinner spinner = (Spinner)v.findViewById(R.id.location_spinner);
ArrayAdapter<Location> adapter = new ArrayAdapter<Location>(v.getContext(), android.R.layout.simple_spinner_item, mLocations);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
if(mLocations==null)
spinner.setPrompt("No Locations");
else
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new LocationSelectedListener());
// Watch for button clicks.
Button newBtn = (Button)v.findViewById(R.id.location_dlg_new);
newBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When button is clicked, call up to owning activity.
//create new start that activity...
}
});
// Cancel do nothing dismissthis
Button cancelBtn = (Button)v.findViewById(R.id.location_dlg_cancel);
cancelBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When button is clicked, call up to owning activity.
//create new start that activity...
}
});
// okay button means set listener with the selected location.
Button okBtn = (Button)v.findViewById(R.id.location_dlg_ok);
okBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When button is clicked, call up to owning activity.
//create new start that activity...
}
});
return v;
}
}
It is called from a fragment itself? though does that matter? because I am calling a TimePIckerDialog and a DatePickerDialog and those work fine, but my calling code from my other fragment is:
void showLocationDialog() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag("locpicker");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
// Create and show the dialog.
DialogFragment newFragment = LocationPicker.newInstance();
newFragment.show(ft, "locpicker");
}
Your constructors are wrong. Try to have just one static method newInstance to instantiate the fragment for all cases and use a Bundle to store the arguments that you want to use in the fragment. Refer to Basic Dialog section here and extend it to your case.

Layouts in Android [ id or field cannot be resolved]

public class Page1 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
final Button button = (Button) findViewById(R.id.welcome);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = null;
myIntent = new Intent(view.getContext(), Page1.class);
startActivity(myIntent);
}
});
}
}
I want to load contents from another XML file named welcome.xml, but i do get an error welcome cannot be resolved or is not a field
This Page1.java class is next screen of my Android Application.
My Welcome.xml
<Button android:text="#+id/Button01" android:id="#+id/welcome"
android:layout_width="wrap_content" android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
</Button>
It should be working.
If you don't set the handler, do you see the button in the screen?
Is the file actually named «*W*elcome.xml»? Try to remove the capital letter (rename it to welcome.xml). Then do a clean, rebuild and check if it works now...
Could you paste your complete xml file and log? my first guess is you have a case issue , your layout file is named "Welcome" and you have setContentView to "welcome" . Also dont have same names for layouts and controls , it will get confusing.
firend you are making silly mistake:see this
public class Page1 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
final Button button = (Button) findViewById(**R.id.Button01**);//use id of button here not layout name
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = null;
myIntent = new Intent(view.getContext(), Page1.class);
startActivity(myIntent);
}
});
}
}
Is this all what your welcome.xml has?
Your button isn't under a layout. thus, the layout file itself will be throwing out exceptions.
secondly, android:text is not correct. the entry you have made there, should be under android:id
and it shouldn't be:
final Button button = (Button) findViewById(R.id.welcome);
but:
final Button button = (Button) findViewById(R.id.Button01);
The Welcome.xml contains Button with id welcome which is not an layout to setContentView
Views can be List, relative, absolute table etc .. in which you can add a button.
And also check for the Case in filename and specified R.layout.*
Sample xml file with linearlayout and a button. Save it as welcome.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/linearlayoutmain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="#+id/ButtonWelcome"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/button"
>
</Button>
</LinearLayout>
in Your Code
public class Page1 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
final Button button = (Button) findViewById(R.id.ButtonWelcome);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = null;
**//You have called Page1.class again which is the name of this class //again**
myIntent = new Intent(view.getContext(), Page1.class);
startActivity(myIntent);
}
});
}
}
Create another activity similarly and the call that class in the intent marked bold.
Your Welcome.xml is not complete, should be something like this :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:padding="3dip"
android:orientation="vertical">
<Button android:text="#+id/Button01" android:id="#+id/welcome"
android:layout_width="wrap_content" android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
</Button>
</LinearLayout>
Also, if your are still having problemas, try to clean your project so the R.java get updated with new id values like welcome id (R.id.welcome),because if R.java does not contain welcome id, you will get errors like that.

Categories

Resources