Android including XML and their Java class - java

I am making an app where I have a few menus (about 7 of them) and a starting screen that is it's own menu.
To make things easier to read, I've decided I want to separate each menu and the starting screen from the main activity and include them when the app runs. But I'd like to include a java class per XML file.
What I have so far is a starting page called activity_start_page.xml it has it's own java class called StartPage.java. This all works fine and dandy.
Inside of this start page, I have included an xml layout called part_start_scroll.xml and I'd like to have it's java included as well, part_start_scroll.java.
On top of both of these, I'd like to do the same thing with each setting sub section. I'll have all of the subsections like so:
part_settings_camera.xml
part_settings_email.xml
part_settings_security.xml
etc...
I'd like to include each one of these as well as a java class that will accompany them.
My XML file for activity_start_page includes all of the layouts:
<ViewFlipper
android:layout_width="0dp"
android:layout_weight=".75"
android:layout_height="match_parent"
android:id="#+id/viewFlipper"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<include layout="#layout/part_settings_session" android:id="#+id/part_settings_session"/>
<include layout="#layout/part_settings_camera" android:id="#+id/part_settings_camera"/>
<include layout="#layout/part_settings_security" android:id="#+id/part_settings_security"/>
<include layout="#layout/part_settings_social" android:id="#+id/part_settings_social"/>
<include layout="#layout/part_settings_printer" android:id="#+id/part_settings_printer"/>
<include layout="#layout/part_settings_email" android:id="#+id/part_settings_email"/>
<include layout="#layout/part_settings_about" android:id="#+id/part_settings_about"/>
</ViewFlipper>
and the StartPage.java handles the flipper to show the specific view depending on what menu item was clicked:
//One of these for each menu button
menu_about.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
viewFlipper.setDisplayedChild(6);
...
}
});
All of this works, except for the Java that accompanies each layout.
In my StartPage.java, I tried to use startActivity() to include the java, but it only works on one java class and not multiples. I got this from another question I asked earlier:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(StartPage.this, part_start_scroll.class));
startActivity(new Intent(StartPage.this, part_settings_session.class));
finish();
}
}, 1000);
The above does not work, but if I were to do just a single class, it works just fine.
So my ultimate question is: How do I include all of these other java files so I can compartmentalize my code? Second to that, but should have no bearing on the answer is: Is this inefficient?

Related

Resources$NotFoundException on Android 4.4

I am using vector in ImageView in my activity, the app works just fine on android 7.0 but crashes on android 4.4. Logcat says, Resources$NotFoundException. I have tried solutions posted here on stackoverflow but none of them seems to be working.
These are the solutions I tried.
added this in my gradle file
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
Added this in OnCreate of activity
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
Replaced android:src with app:srcCompat in XML file.
This is my code in XML
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_arrow_back_white_24dp"
android:tint="#color/colorPrimary"
android:id="#+id/details_back"
android:layout_margin="15dp"
android:layout_alignParentStart="true"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/share"
android:tint="#color/colorPrimary"
android:id="#+id/details_share"
android:layout_margin="15dp"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
this is the code in JAVA file
ImageView detail_share;
detail_share = (ImageView) findViewById(R.id.details_share);
detail_share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// code for sharing item
}
});
Also, the error occurs in second ImageView, not the first one, despite of being exactly same. I am treating both images exactly same in java file too.
I had same issue a few days back. Android 4.4 does not support vectors with (v21) written in faded font after their names in android studio directory tree, these vector graphics are only supported by API 21+. Ideally Android studio should split the vector into PNG files with different sizes but for some unclear reason Android studio does not do it on it's own sometimes.
Looks like you are trying to use the vector graphic for a "Share" icon in the above described scenario, try importing the share icon from Material icons in Android Studio only, this way the vectors imported are supported by Android 4.4 too.

quickly change Android system language

I'm studying mandarin and want to change the language of my Android smartphone (Sony Xperia) between english/mandarin (or english/mandarin/portuguese/spanish) faster than using the default system settings. The way it is, I have to enter settings, scroll to the middle of it (which is slower than if it was on the beginning or end of the list), click Language & input, click language, have to scroll all the way down to 中文 (it should already be at the top, among the "recently used languages", but only my native language is always there), click 中文 and finally click ok.
I would like to reduce those 6+ clicks to a single button in the quick settings area (there is space for 4 more icons): when the phone is in a language, a tap on the icon would change to the next language, holding the icon would open a menu to add/remove languages/change order/etc.
I'm new to Android development, so I don't know if it's possible for an app to change the system language (need root privileges? I want it for myself, even if I won't be allowed to share it on Google Play, for instance). I've seen many answers on how to change an app language, that's not what I'm looking for. I also found many apps in Google Play, all of them promising to "quick toggle system language" etc, but none of them worked on my device. The closest I got was this, but looks like a dead thread.
So, is it possible? If so, where is the documentation?
I think this is what you might looking for!!!
If you would like to have your own application you can try this app i just created for you here!!!
Language Picker Widget
The Java Code
public class MainActivity extends Activity {
private Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.add_button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//in the line below it tells it to go to the language selection list
Intent intent = new Intent(Settings.ACTION_LOCALE_SETTINGS);
startActivity(intent);
MainActivity.this.finish();
}
});
}
}
The Layout File
<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=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change System Language"
android:id="#+id/add_button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
You can start a project in eclipse, use the codes above for the activity and the layout and then test it on the emulator

Android tab bar with different fragments and different action bar icons

Problem Description and Question
I'm using tab activity with action bar. In the HOME tab I have GridView which contains some items, like it is shown in the image below (Hot Lines, Taxi, Restaurants etc)
Wen user click on the items in the grid view I want my application to take following actions:
Change icon of application to grid view item image on which I pressed.
Change the test near the icon
Add standard back button near icon, which will go back to grid view screen.
Change the tab fragment to the one which I specify.
Like it is shown in the image below:
As I never deal with this kind of problems can you please give me example or share some link with me of how I can do this? and can I do this at all?
This might help:
Android studio - is possible to add tabs pointing to fragments from designer?
It is not exactly what you want, but a good start. If you are willing to put a bit of work in it, you should be able to get what you want. If you have a basic Frame to work with and more specific problems with this matter I will gladly help you out ^^
John
The first link you can check is THIS.
And you should read more about ActionBar.
The last thing is it's better if you google it first and try to write a code and when you got stuck somewhere share your code with us and ask for help.
You have to use actionbarsherlock library for it.
use android.support.v4.app.FragmentTabHost and TabWidget in the xml as shown below :
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
style="#style/yourstyle"/>
<FrameLayout
android:id="#+id/realtabcontent"
style="#style/realtabFrameContentStyle" />
<TabWidget
android:id="#android:id/tabs"
style="#style/yourtabstyle" />
</android.support.v4.app.FragmentTabHost>
Use SherlockFragmentActivity for showing the tabs.
In the activities code use following code (preferably in a function) to set the ctionbar icon and text :
activity.getSupportActionBar().setDisplayShowCustomEnabled(true);
activity.getSupportActionBar().setCustomView(R.layout.your_action_barlayout);
((TextView) (activity.getSupportActionBar().getCustomView().findViewById(R.id.action_bar_title))).setText("your title");
ImageView homeButton = ((ImageView) (activity.getSupportActionBar().getCustomView().findViewById(R.id.your_icon)));
homeButton.setVisibility(View.VISIBLE);
homeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, YourHOmeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(intent);
activity.finish();
}
});
ActionBar mActionBar = activity.getSupportActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
mActionBar.show();
then call this function with your icon and your text in your fragments onResume() method.

Soft keyboard covers an EditText in a PopupWindow

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.

How to modify Android app UI via java when I created it using main.xml file?

This is my first post here. Have done hundreds of posts, mostly answers, in linuxquestions.org, but nothing here yet.
I'm starting to develop using android SDK 1.2, and struggle to understand something.
I've used a basic source that uses xml to define a UI and gets it displayed using setContentView(R.layout.main);.
Now in my Java program I'd like to add some code to modify some of the objects I defined in my xml file.
I understand Java, the object concept, methods, etc ... but I don't know which exact syntax to use, and which id my object actually is.
My xml code includes:
<EditText android:layout_width="match_parent" android:id="#+id/editText1" android:layout_height="wrap_content" android:text="#string/saisie">
<requestFocus></requestFocus>
</EditText>
my .java includes:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
My question is: how do I modify the .java code to make the EditText object display "hello" instead of what is statically defined in my strings file?
I know there is not a big practical use in the example code I am doing, but it will help me do lots of other more interesting things.
In your onCreate() or another function, add:
EditText et = (EditText)findViewById(R.id.editText1);
et.setText("some text");
Change to this (delete the android:text=#string/..)
<EditText
android:layout_width="match_parent"
android:id="#+id/editText1"
android:layout_height="wrap_content"
<requestFocus></requestFocus>
</EditText>
And add the following in onCreate()
EditText text = (EditText)findViewById(R.id.editText1);
text.setText("some text");

Categories

Resources