I am a beginner and I am asking this just to gain knowledge. so in my app, I have a MainActivity And its layout activity_main.xml.
here is the code of activity main
<RelativeLayout 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="com.testapp.myapp.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1"
android:id="#+id/btn1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btn1"
android:text="Button 2"
android:id="#+id/btn2"/> </RelativeLayout>
And here is the MainActivity.java
package com.testapp.myapp;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}}
I Have created Two Blank activities named By Button1PDF And Button2PDF And Each Has layout named button1_pdf.xml and button2_pdf.xml respectively.
So what i wanna do is, i want to set a pdf file against these New Activities and when I will click on btn1 or btn2 On MainActivity, I want it to display the new activity Button1PDF or Button2PDF activity which will be containing the pdfs.
I tried this online using webview and it works flawlessly. but now i want it to display offline(means complete offline)
i have done some research and i found some libraries like
compile 'com.github.barteksc:android-pdf-viewer:2.0.3'
but i don't know how to use it. hope someone can clear my doubts.
thanks.
You will need to download the files first and then display them to the user using that library.
Read this question on how to download file from internet
Related
I have the following activity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity_layout);
TabHost mainTabHost = (TabHost) findViewById(R.id.mainTabHost);
mainTabHost.setup();
mainTabHost.addTab(mainTabHost.newTabSpec("Tab 1").setIndicator("", ContextCompat.getDrawable(this, R.drawable.d1)).setContent(new Intent(this, Activity1.class)));
mainTabHost.addTab(mainTabHost.newTabSpec("Tab 2").setIndicator("", ContextCompat.getDrawable(this, R.drawable.d2)).setContent(new Intent(this, Activity2.class)));
mainTabHost.addTab(mainTabHost.newTabSpec("Tab 3").setIndicator("", ContextCompat.getDrawable(this, R.drawable.d3)).setContent(new Intent(this, Activity3.class)));
}
}
And this is the XML
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#id/mainTabHost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activityHorizontalMargin"
android:paddingRight="#dimen/activityHorizontalMargin"
android:paddingTop="#dimen/activityVerticalMargin"
tools:context=".MainActivity">
<RelativeLayout
android:id="#id/mainHomeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</TabHost>
There is another activity, in which tapping a button launches this activity. However, the activity doesn't launch and I get the following error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nirvanapass/com.nirvanapass.MainActivity}: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
I have searched online - but whatever solutions I could find use ActivityGroup or TabActivity - both of which are deprecated. How to tackle this error and solve this problem?
The best solution is to use a modern tab solution. TabHost has been obsolete for quite some time. Using an Intent for a tab content has been deprecated for five years, and it was never a good idea even before then.
Most modern tab implementations use a ViewPager with some sort of tabbed indicator. PagerTabStrip and TabLayout are both in the Android SDK, and the Android Arsenal lists many other tab indicators in their ViewPager category.
If you really want the retro styling of a TabWidget, either use FragmentTabHost, or use TabHost where the tabs are views.
Here are sample apps for:
using PagerTabStrip
using TabLayout
using a third-party tab indicator
using TabHost with widgets for tabs
I started learning java a few days ago, so I'm quite lost with it.
I want to show a text received from an intent and make it look like this:
"You've written : ."
It isn't working at all and I'm only able to show the text from the intent.
Here's my code:
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
System.out.print("You've written: ");
textView.setText(message);
System.out.print(".");
// Set the text view as the activity layout
setContentView(textView);
}
Besides, I'm trying to display the text written above in the first line of the page (or as many lines it takes) and, below, a label to insert a text together with a Button. The problem is that I can only see the text from the intent.
Here's my code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android: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=".DisplayMessageActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<EditText android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage"/>
</RelativeLayout>
Thankyou very much, I wish I'll be answered soon.
Instead of System.out.println.
Do like this.
<TextView
android:id="#+id/topTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
and Get the view in activity.
public void onCreate(Bundle bundle) {
.........
setContentView(layout_xml);
TextView textView = (TextView)findViewById(R.id.topTextView);
textView.setText("You've written: " + message + " .");
.........
}
If you are trying to reference the TextView inside your layout it needs to referenced in the Activity. The above solution shows you how to handle that. setContentView(R.layout.file_name) is what should be used to reference a layout created inside an xml file stored in res/layout/file_name.xml or created manually inside your code. If you are going to call setContentView for a layout contructed inside your (Activity) it will take some more code. If the message is all is need to be displayed you can always call Toast.maketText(Context, message, Toast.LENGTH_SHORT).show(); for example. The other suggestion is Java developers are used to using System.out.println() to debug to the console. In Android they have a neat feature we use called LogCat that can display messages going on inside your code by saying (example for debugging purposes)
Log.d(TAG, message); Where TAG is usually a constant defined for the Activity,Fragment, etc. you are in so you can see where the message is coming from and display whatever value you normally would have used in System.out.println().
I've thrown together a simple test project that displays a PopupWindow containing an EditText (on Android 2.2). When I tap the EditText, the soft keyboard is shown, as I would expect. However, the soft keyboard covers the EditText, and I cannot get the screen to pan to keep the EditText in view in the way I would have thought it should. My code:
TestAdjustPanActivity.java:
package com.example;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.PopupWindow;
public class TestAdjustPanActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final PopupWindow pw = new PopupWindow(this);
pw.setContentView(getLayoutInflater().inflate(R.layout.popup, null, false));
pw.setWidth(400);
pw.setHeight(600);
pw.setFocusable(true);
pw.setOutsideTouchable(true);
pw.setTouchable(true);
pw.setBackgroundDrawable(new BitmapDrawable());
// This is the line referred to in the edit:
pw.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
findViewById(R.id.main).post(new Runnable() {
public void run() {
pw.showAtLocation(findViewById(R.id.main), Gravity.NO_GRAVITY, 0, 0);
}
});
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
popup.xml:
<?xml version="1.0" encoding="UTF-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#666666"
android:orientation="vertical" >
<EditText
android:id="#+id/current_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="300dp" />
</LinearLayout>
</ScrollView>
...and my AndroidManifest.xml does contain the line android:windowSoftInputMode="stateUnspecified|adjustPan" in the tag for the one and only activity in the app.
Any ideas? None of the other posts on SO related to this issue have done it for me. What am I missing that's keeping the view from panning the way I would expect it to?
EDIT: I tried adding a line in TestAdjustPanActivity as indicated, which caused the screen to pan perfectly on an Android 3.2 device I have. However, it still doesn't pan on my Android 2.2 device, which makes this even more confusing.
For anyone stumbling upon this in the future, I ended up just using a Dialog instead of a PopupWindow, and panning to keep the EditText in view works fine on 2.2 with a Dialog.
There is simpler way. Just provide alternative layout for active keyboard.
Left click on project select: "Android tools/New Resource File..." .
Chose layout, give file name "main" (don't worry about conflicts).
Click "next". Then on list on the left select "keyboard" and move it to right (click "->").
On right side select keyboard state.
Click finish.
Now copy content of your main.xml located in "res/layout" to new file in res/layout-keyssoft".
Correct new layout in such way that keyboard is not in that way. Remember to maintain same "id"s for respective components in those two layouts (that is why copy paste was needed).
Enjoy how it works
Read about configuration changes to understand how it works. Note that EVERY configuration change (orientation change, language change, ...) will cause recreation of Activity (in such case argument of onCreate is not null) so you can provide different layouts for different cases.
I doubt the problem is because of using that ScrollView in XML,
There has been another problem similar to this, You can check all different answers there, and may work for you.
change this
android:windowSoftInputMode="stateUnspecified|adjustPan"
to
android:windowSoftInputMode="adjustPan"
only in your manifest file
You should change
android:windowSoftInputMode="stateUnspecified|adjustPan
to
android:windowSoftInputMode="stateHidden|adjustPan"
It took couple of hours to figure out and I ended up using scrollview.
Apparently there is no way to overcome this problem when you use popup window and soft input keypad hide your view. Just go for Scroll View.
I'm working currently on a test project in order to practice some of the basic features of android layout before i get to work on my project.
For now i'm experiencing with some buttons and activity change, and got stack on one frustrating problem.
At first i wasn't sure why my new Layout just won't show some buttons I've placed on it, but now the problem get weirder.
As i will soon show in my code, I've got two layout xml files, one to handle the main activity and one to handle the 2nd. at first all was well, but then I've noticed i can't see the lone button on the 2nd layout, and i couldn't understand why. i've tried to change the 2nd layout to be the main layout (just my changing the setContentView() in the main activity), but weirdly enough, the program kept calling the original layout instead of the one specified in the code (of course i've check for errors, or if it was actually have been build, it did, i also inserted errors on purpose and check that the program fail to start).
Seeing that there might be some deeper problem, i've tried to add another button to the main layout xml file, but to no vial, the program kept starting with the old one.
I've search for answer, tried to clean my project, and rebuild it, looked for missing files in my string xml, but nothing could fix it up, I'm clueless at the moment for what went wrong.
I do assume that if i start over with the project everything will be fine, but i can't start a new project every time the problem rising. (especially if I'm doing something wrong).
here is my code:
main Activity:
$
package test.android.mark.III;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
public class AndroidMarkIII extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
final Button button1 = (Button) findViewById(R.id.button1); // bind button to the view from the xml
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openNewWindow(v);
}
});
final Button button2 = (Button) findViewById(R.id.button2);// bind button to the view from the xml
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openPopupWindow(button2);
}
});
}
protected void openNewWindow(View v) {
Intent listWindowIntent = new Intent(v.getContext(), ListWindowActivity.class);
startActivityForResult(listWindowIntent, 0);
}
}
My 2nd activity:
package test.android.mark.III;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ListWindowActivity extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Button returnButton = (Button) findViewById(R.id.return_button);
returnButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}
My main XML file:
<?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/title1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/title1"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/title1"
android:layout_centerHorizontal="true"
android:text="#string/push_button"
android:padding="50dp" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/button1"
android:text="#string/popup_button"
android:layout_centerHorizontal="true"
android:padding="75dp" />
<TextView //Was added later for check reason (wasn't seen on any run)
android:id="#+id/end"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/title1"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
my 2nd XML (for the 2nd activity)
<TextView
android:id="#+id/list_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hello World" />
<Button
android:id="#+id/return_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/return_button"
android:visibility="visible">
</Button>
</LinearLayout>
and for any case, my strings xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, AndroidMarkIIIActivity!</string>
<string name="app_name">AndroidMarkIII</string>
<string name="title1">This is a test program</string>
<string name="push_button">Push Button - 50dp</string>
<string name="popup_button">Popup Button - 75dp</string>
<string name="return_button">return Button - 10dp</string>
<string name="close_popup_button">X</string>
<string name="popup_text">this is popup - 25dp</string>
<string name="list_window_app_name">AndroidMarkIII2ndWindow</string>
</resources>
and my manfiest xml
<uses-sdk android:minSdkVersion="14" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".AndroidMarkIII" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ListWindowActivity">
</activity>
</application>
</manifest>
I really hope someone will be able to solve that problem, cause it seems actually like a really silly problem, but still, can't keep working while i'm having it.
Thanks in advance
Oren
The issue arises because your XML layouts are incorrect.
In main1.xml you are using a RelativeLayout, so you have to tell each view where to place itself relative to the other views, or the ViewGroup itself. Try adding android:layout_below="#id/button2" to the last TextView in main1.xml.
As #alextsc pointed out, in main2.xml you are telling the TextView to fill the entire screen with fill_parent. Change the height to wrap_content and set android:orientation="vertical" in the LinearLayout parent as the default is horizontal.
at first all was well, but then I've noticed i can't see the lone button on the 2nd layout, and i couldn't understand why.
First of all, your XML for the second activity seems to be incomplete. You are at least missing the opening LinearLayout here. I assume this is present and there is nothing more missing (probably just a copy&paste error).
What you did there is having a TextView which is set to fill_parent in both the width and the height. The reason you can't see your button is that there is simply no space left for it. The textview fills the parent LinearLayout completely (if you want to visualize that, add android:background="#ff0000" to the textview. It's background becomes red and fills the screen). You can change the width and height of the textview to wrap_content for starters, which should make the button visible. If you intent a certain layout distribution for the two, have a look at LinearLayouts weight attribute.
When it comes to the not-changing layouts: I'm not sure what's going on here. Make sure that your XMLs are called main1.xml and main2.xml. There is also a default layout file called main.xml. Make sure that you didn't edit this by accident. (sounds like you got this part covered to me, just to make absolutely sure)
The Android Developers TabWidget tutorial says the following:
"You can implement your tab content in one of two ways: use the tabs to swap Views within the same Activity, or use the tabs to change between entirely separate activities."
The tutorial goes on to demonstrate how you can use tabs with separate Activities. I have been unable to find an example of using tabs with different Views within the same Activity. I would rather not re-invent this particular wheel, so I am hoping someone here knows how this is done and can clue me in. Thanks!
I think in the .setContent method of each tab you pass in the view you wish to use:
TabHost.TabSpec spec1 = tabs.newTabSpec("tag1");
spec1.setContent(R.id.AnalogClock01);
spec1.setIndicator("Analog Clock");
Here's an example I found awhile back:
<?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"
>
<TabHost android:id="#+id/TabHost01" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TabWidget android:id="#android:id/tabs" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<FrameLayout android:id="#android:id/tabcontent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="65px">
<AnalogClock android:id="#+id/AnalogClock01" android:layout_width="wrap_content" android:layout_height="wrap_content"></AnalogClock>
<DigitalClock android:text="DigitalClock01" android:id="#+id/DigitalClock01" android:layout_width="wrap_content" android:layout_height="wrap_content"></DigitalClock>
</FrameLayout>
</TabHost>
</LinearLayout>
And the Java code for this example is as follows:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
public class tabexample extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabs = (TabHost)findViewById(R.id.TabHost01);
tabs.setup();
TabHost.TabSpec spec1 = tabs.newTabSpec("tag1");
spec1.setContent(R.id.AnalogClock01);
spec1.setIndicator("Analog Clock");
tabs.addTab(spec1);
TabHost.TabSpec spec2 = tabs.newTabSpec("tag2");
spec2.setContent(R.id.DigitalClock01);
spec2.setIndicator("Digital Clock");
tabs.addTab(spec2);
}
}
I used this one and it was fine for me
http://www.codeproject.com/Articles/107693/Tabbed-Applications-in-Android