Android Listview Hides Titlebar - android

I'm trying to make an app - it has 3 activities. The first 2 are listviews in linear layouts, and each of them prevents the title bar from showing up. By title bar I mean the section that is normally at the top of an activity which displays the activity's name as well as an option setting. My third activity is not a listview, and displays the title bar normally, which leads me to think it may be a problem with my listviews.
The xml for my first page is:
<LinearLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".SelectClass">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
My java class doesn't do much to the display, although it does set an array adapter and an onClick listener. If those are necessary to understand what's going on then let me know and I'll post them. I appreciate any help or clues. Thank you!
EDIT: first page's java:
package com.example.graeme.dnd5echaracterroller;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.view.View;
import android.widget.TextView;
public class SelectClass extends ListActivity {
private static String classString;
public static void setClassString(String classString) {
SelectClass.classString = classString;
}
public static String getClassString() {
return classString;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_class);
//Initialize the available class choices
final String[] classes = {"Barbarian","Bard","Cleric","Druid",
"Fighter","Monk","Paladin","Ranger","Rogue","Sorcerer",
"Warlock", "Wizard"};
ArrayAdapter<String> classAdapter = new ArrayAdapter<>(getListView().getContext(),
R.layout.classlayout, R.id.classname, classes);
getListView().setAdapter(classAdapter);
//Set on click listener to get selected class item
AdapterView.OnItemClickListener itemClickedHandler = new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View v, int position, long id){
//Start a new intent headed to selectRoll, fill it with the class string selected
Intent sendClassIntent = new Intent(SelectClass.this, SelectRoll.class);
//Each list item has an image, and text
//First grab the list item, then grab the text from it
LinearLayout ll = (LinearLayout)v;
TextView tv = (TextView)(ll).findViewById(R.id.classname);
setClassString((String)(tv.getText()));
startActivity(sendClassIntent);
}
};
getListView().setOnItemClickListener(itemClickedHandler);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_select_class, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

You should modify you first activity as this structure
public class SelectClass extends AppCompatActivity {
...
private ListView mListView;
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_class);
// Initialize the lisview
mListView = (ListView) findViewById(R.id.lisview_id); // lisview id
//Initialize the available class choices
final String[] classes = {"Barbarian","Bard","Cleric","Druid",
"Fighter","Monk","Paladin","Ranger","Rogue","Sorcerer",
"Warlock", "Wizard"};
ArrayAdapter<String> classAdapter = new ArrayAdapter<> (this,
R.layout.classlayout, R.id.classname, classes); // update
mListView.setAdapter(classAdapter);
...
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
....
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
.....
}
}
And in the ListView of first activity
<ListView
android:id="#+id/lisview_id" // use this id for initialize listview
android:layout_width="match_parent"
android:layout_height="match_parent">
Hope this help

I think that the problem is not the ListView, normally it is hided because of the style you have set in your manifest. This is what you have in the main activity inside manifest.xml:
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
the android:theme="#style/AppTheme" on the listView activity must be set as the Apptheme that shows the "Title bar".
Hope this helps

Related

How to create a drop-down list in Android Studio?

I am trying to create a drop-down list for an activity in Android Studio. I have tried using the Spinner.
Here is my xml code:
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/btn_dropdown"
android:spinnerMode="dropdown"/>
Here is my Java code:
Spinner dropdown = findViewById(R.id.spinner1);
String[] items = new String[]{"1", "2", "3"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
This is a screenshot of what it looks like:
As you can see, the drop-down list appears as a pop-up, however, I want the drop-down list to appear in a different way. See the image below as an example of what I want:
Does anyone have any ideas on how I could do this?
Actually it does look like there is a way with the Material design library called ExposedDropdownMenu
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/menu"
style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/label">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
/>
</com.google.android.material.textfield.TextInputLayout>
Obviously this is with TextInput and not a spinner so there will still be some custom logic to prevent typing if you dont want that
I personally have not tried it myself but it looks like what you want
Some ideas:
As #tyczj said, that is android's default layout that is showing, so there are some alternatives:
Create tour own custom layout
Use an external library like SearchableSpinner, it incorporates the search function and the item layout is similar from what you want.
Here I have found a useful example for a ComboBox/Spinner on Android:
https://abhiandroid.com/ui/spinner
Here is the code:
https://github.com/abhisheksaini4/SpinnerExamples/blob/master/SpinnerExamples.7z
The main codes include MainActivity.java:
package example.abhiandriod.spinnerexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
String[] bankNames={"BOI","SBI","HDFC","PNB","OBC"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) findViewById(R.id.simpleSpinner);
spin.setOnItemSelectedListener(this);
//Creating the ArrayAdapter instance having the bank name list
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,bankNames);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
}
//Performing action onItemSelected and onNothing selected
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
Toast.makeText(getApplicationContext(), bankNames[position], Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml:
<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=".MainActivity">
<Spinner
android:id="#+id/simpleSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp" />
</RelativeLayout>
menu_main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
And AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.abhiandriod.spinnerexample" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
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>
</manifest>

Can't make my Listview appear [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm working on an android application and cannot seem to make my listView appear when the app is run. I've been toggling with some of the xml but I'm still rather new and android updates quite often so tutorials are constantly becoming outdated.The actual java code seems fine to me I can't see much difference there. I could really use an extra pair of eyes on this , maybe there is something I'm missing.
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* A placeholder fragment containing a simple view.
*/
public class MainActivityFragment extends Fragment {
public MainActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String[] forecastArray = {
"Today, colder than bumblefuck",
"Friday, condsider moving",
"Saturday, not even gonna bother",
"Sunday, Partly Cloudy",
"Monday, Hell Froze over",
"Tuesday, Cloudy with a chance of meatballs",
"Wednesday,Light Showers"
};
List<String> weekForeCast = new ArrayList<String>(
Arrays.asList(forecastArray));
ArrayAdapter<String> bindIt= new ArrayAdapter<String>(getActivity(),R.layout.list_item_forecast,R.id.list_item_forecast_textview,weekForeCast);
ListView listView = (ListView) inflater.inflate(R.layout.fragment_main, container, false).findViewById(R.id.listview_forecast);
listView.setAdapter(bindIt);
return inflater.inflate(R.layout.fragment_main, container, false);
}
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivityFragment">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listview_forecast">
</ListView>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:id ="#+id/list_item_forecast_textview"
android:visibility="visible"
android:enabled="false">
</TextView>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/fragment"
android:name="com.alesterlewis.sunshine.app.MainActivityFragment"
tools:layout="#layout/fragment_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alesterlewis.sunshine.app" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
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>
</manifest>
The problem is that you are inflating a new view while returning from the onCreateView. Thus, the one in which you had set the adapter and for the listview is lost. This is the bug.
First inflate the layout for the fragment in the view
View view = inflater.inflate(R.layout.fragment_main, container, false);
Then initialise its components, here you can set the list view adapter, etc and if you would have had more view, initialised the same.
ArrayAdapter<String> bindIt= new ArrayAdapter<String>(getActivity(),R.layout.list_item_forecast,R.id.list_item_forecast_textview,weekForeCast);
ListView listView = (ListView) view.findViewById(R.id.listview_forecast);
listView.setAdapter(bindIt);
and in the end return the same view that was initially infalated and worked upon!
Did you set an adapter for your list? Maybe your adapter list is empty for some reason.
UPDATE
Try to anticipate the view creation:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
String[] forecastArray = {
"Today, colder than bumblefuck",
"Friday, condsider moving",
"Saturday, not even gonna bother",
"Sunday, Partly Cloudy",
"Monday, Hell Froze over",
"Tuesday, Cloudy with a chance of meatballs",
"Wednesday,Light Showers"
};
List<String> weekForeCast = new ArrayList<String>(
Arrays.asList(forecastArray));
ArrayAdapter<String> bindIt= new ArrayAdapter<String>(getActivity(),R.layout.list_item_forecast,R.id.list_item_forecast_textview,weekForeCast);
ListView listView = (ListView) view.findViewById(R.id.listview_forecast);
listView.setAdapter(bindIt);
return view;
}

Return text entered in textbox

I'm trying to work through the "Start Another Activity" tutorial but, having worked through the whole thing (twice), the app still refuses to return the text entered in the textbox - it always returns "Hello World".
I've tried to figure it out, and suspect it might be something to do with the fragment_display_message.xml, in which there is an EditText calling the Hello World string. But really I have no idea.
Any help would be appreciated. Here's the code:
MainActivity.java
package com.knargle.streetiteatit;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.knargle.streetiteatit.MESSAGE";
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
fragment_main.xml
<LinearLayout 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:orientation="horizontal"
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="com.knargle.streetiteatit.MainActivity$PlaceholderFragment" >
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
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" />
</LinearLayout>
DisplayMessageActivity.java
package com.knargle.streetiteatit;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
setContentView(R.layout.activity_display_message);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_display_message,
container, false);
return rootView;
}
}
}
fragment_display_message.xml
<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="com.knargle.streetiteatit.DisplayMessageActivity$PlaceholderFragment" >
<EditText android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/hello_world" />
</RelativeLayout>
In your DisplayMessageActivity.java file, you are calling setContentView twice which it-self is overwriting the first line of code. Currently your code looks like this:
setContentView(textView);
setContentView(R.layout.activity_display_message); // Delete this line. As this line of code appears in your java file, it is overwriting the 'text' you entered with the default 'hello world' message
It should be just like this:
setContentView(textView);
The setContentView is being called twice in DisplayMessageActivity. Remove the second one:
setContentView(R.layout.activity_display_message);
In your DisplayMessageActivity:
// Set the text view as the activity layout
setContentView(textView);
setContentView(R.layout.activity_display_message);
You call setContentView twice, which just overwrites what was already there. Remove the second setContentView.
You can follow the answers from both Andrew and Rajesh. But the fact that you have defined a layout fragment_display_message and I am sure you want to use it. In that case, all you need to do is get rid of the code to dynamically add the TextView and the correspodning setContentView and leave the second setContentView that uses your layout. Once done, you get the reference to your EditText and set it's text to what was sent in the Intent.
Remove these lines from DisplayMessageActivity :
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
You were attempting to set the message in a new text view which is not part of your layout file. And then setting that text View as the ContentView of your layout. That seems like a convoluted approach. Instead try the following :
After the setContentView(R.layout.activity_display_message); line, add these lines :
EditText editText = (EditText) findViewById(R.id.edit_message);
editText.setText(message);
This way, you use the existing Edit text element in your layout file to show the message. Your original code never replaced HelloWorld in this element. That is why it always shows you HelloWorld instead of your message.

Eclipse, Add libraries in Project. I'm stuck

I'm stuck when Add libraries in my project in Eclipse. I am following this link official android development website http://developer.android.com/tools/support-library/setup.html#libs-with-res and I do exactly what it says but I get errors for some reason.Here how it happends:
1.I download support libraries from SDK Manager.
2.I import them with existing Android Code into workspace and I press on the both .jar files Build Path>Add to Build Path.
3. Then on that project (made in step 2) I configure Build Path and I check both .jar files and uncheck the Android Dependencies and I click finish. Everything is okay for now.
But here comes the main problem=>
4. I press on my main project (myfirstapp) properties and click Add and select the libraries after that I press Apply and lots of errors raise up like R cannot be resolved to do variable and my R.id from /gen folder suddenly dissapear
I add now some screen shoots to make it better for you.
Sorry I have no reputations for posting images. Please copy links below
first image
second image
third image
fourth image
All my Codes: MainActivity.xml
package com.example.myfirstapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
public void sendMessage(View view) {
Intent intent = new Intent(this, Displaymessageactivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
fragment_main.xml
<LinearLayout 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:orientation="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
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" />
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">myfirstapp</string>
<string name="action_settings">Settings</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="title_activity_displaymessageactivity">Displaymessageactivity</string>
<string name="hello_world">Hello world!</string>
</resources>
Displaymessageactivity.xml
package com.example.myfirstapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Displaymessageactivity extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.displaymessageactivity, 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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.fragment_displaymessageactivity, container, false);
return rootView;
}
}
}
fragment_displaymessageactivity.xml
<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="com.example.myfirstapp.Displaymessageactivity$PlaceholderFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
Things I have tried so far:
1. Reinstall Eclipse and Java
2. Redownload libraries from SDK Manager
3. Clean the Project.
4. Make all my xml files starting with lowercase (but the MainActivity.xml and Displaymessageactivity.xml must start with uppercase)
I tried my best and I am stuck here forever. I am very frustated from Google :/
Every help is very appreciated and welcome!
Add the library to your application project:
In the Project Explorer, right-click your project and select Properties.
In the category panel on the left side of the dialog, select Android.
In the Library pane, click the Add button.
In step 3, notice that in the Library pane, you should see that the appcompat_v7 already be added (it's added automatically when you create your project follow MyFirstApp instruction, because you have already chosen minSdkVersion=8, not 11) (your second image).
So remove all the added libraries in the Library pane before adding your library (so in your third image it only has android-support-v7-appcompat in Library Pane), then the R.java will not disappear.

Android: Landscape layouts not being used

In my application I have one Main activity that sets up a tabbed ViewPager with three different fragments, each one with it's own layout and class. I've created landscape versions of each layout file and placed them all in res/layout-land. But when I run the app and switch orientations, the landscape layout isn't being used?
MainActivity:
package me.zaydbille.utilitywatch;
import android.content.Intent;
import android.content.res.Configuration;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
// Tab and ViewPager variables
Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"Coin Flip", "Counter", "Choice"};
int numTabs = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creating The Toolbar and setting it as the Toolbar for the activity
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, numTabs);
// Assigning ViewPager View and setting the adapter
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
// Assiging the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width
// Setting Custom Color for the Scroll bar indicator of the Tab View
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
#Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tabsScrollColor);
}
});
// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(pager);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent myIntent = new Intent(this, HelpActivity.class);
startActivity(myIntent);
return true;
}
return super.onOptionsItemSelected(item);
}
// Counter Fragment helper methods
public void addCount() {
Preferences.setIntPref(this, Preferences.getIntPref(this) + 1);
}
public void clearCount() { Preferences.setIntPref(this, 0); }
public int getCount() { return Preferences.getIntPref(this); }
// Choice Fragment helper methods
public void saveList(ArrayList<String> list){ Preferences.saveList(this, list); }
public ArrayList<String> getList() { return Preferences.getList(this); }
}
Fragment 1:
package me.zaydbille.utilitywatch;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import java.util.Random;
public class CoinFlip extends Fragment {
Button flipButton;
Random randomizer;
ImageView mSpinningCoin;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.coin_flip,container,false);
flipButton = (Button) v.findViewById(R.id.flipButton);
mSpinningCoin = (ImageView) v.findViewById(R.id.coin_spinning);
flipButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
randomizer = new Random();
int number = randomizer.nextInt(2);
if (number == 0) { // Heads
((AnimationDrawable) mSpinningCoin.getBackground()).stop();
mSpinningCoin.setBackgroundResource(R.drawable.coin_spin_heads);
((AnimationDrawable) mSpinningCoin.getBackground()).start();
} else if (number == 1) { // Tails
((AnimationDrawable) mSpinningCoin.getBackground()).stop();
mSpinningCoin.setBackgroundResource(R.drawable.coin_spin_tails);
((AnimationDrawable) mSpinningCoin.getBackground()).start();
}
}
});
return v;
}
}
Fragment 1's layout file:
<?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"
android:background="#color/ColorPrimaryLight">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="vertical"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false"
android:layout_alignParentBottom="false"
android:gravity="center_vertical|center_horizontal"
android:layout_marginTop="200dp">
<ImageView
android:id="#+id/coin_spinning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/coin_spin_heads"
android:contentDescription="#string/coinDescription" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/flipButtonText"
android:id="#+id/flipButton"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</RelativeLayout>
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.zaydbille.utilitywatch" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenLayout|screenSize"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".HelpActivity"
android:label="#string/title_activity_help"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="me.zaydbille.utilitywatch.MainActivity" />
</activity>
</application>
</manifest>
When you use android:configChanges="orientation|screenLayout|screenSize" you are saying that you do not want the system to do any of its default behavior when these configuration changes happen. This includes changing any layouts. Remove that line or actually handle the change yourself.
Just remove this piece of code from your AppManifest.xml:
android:configChanges="orientation|screenLayout|screenSize"
This disables your activity to change its orientation. Removing that will fix it.

Categories

Resources